test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var assert = require("assert");
  2. var fs = require("fs");
  3. var networking = require("../modules/networking");
  4. var helpers = require("../modules/helpers");
  5. var logging = require("../modules/logging");
  6. var config = require("../modules/config");
  7. var skins = require("../modules/skins");
  8. var cache = require("../modules/cache");
  9. // we don't want tests to fail because of slow internet
  10. config.http_timeout *= 3;
  11. // no spam
  12. logging.log = function(){};
  13. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  14. var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  15. // Get a random UUID + name in order to prevent rate limiting
  16. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  17. var name = names[Math.round(Math.random() * (names.length - 1))];
  18. var ids = [
  19. uuid.toLowerCase(),
  20. uuid.toUpperCase(),
  21. name.toLowerCase(),
  22. name.toUpperCase()
  23. ];
  24. describe("Crafatar", function() {
  25. // we might have to make 2 HTTP requests
  26. this.timeout(config.http_timeout * 2 + 50);
  27. before(function() {
  28. cache.get_redis().flushall();
  29. });
  30. describe("UUID/username", function() {
  31. it("non-hex uuid is invalid", function(done) {
  32. assert.strictEqual(helpers.uuid_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  33. done();
  34. });
  35. it("empty id is invalid", function(done) {
  36. assert.strictEqual(helpers.uuid_valid(""), false);
  37. done();
  38. });
  39. it("non-alphanumeric username is invalid", function(done) {
  40. assert.strictEqual(helpers.uuid_valid("usernäme"), false);
  41. done();
  42. });
  43. it("dashed username is invalid", function(done) {
  44. assert.strictEqual(helpers.uuid_valid("user-name"), false);
  45. done();
  46. });
  47. it(">16 length username is invalid", function(done) {
  48. assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
  49. done();
  50. });
  51. it("lowercase uuid is valid", function(done) {
  52. assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  53. done();
  54. });
  55. it("uppercase uuid is valid", function(done) {
  56. assert.strictEqual(helpers.uuid_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  57. done();
  58. });
  59. it("dashed uuid is valid", function(done) {
  60. assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  61. done();
  62. });
  63. it("16 chars, underscored, capital, numbered username is valid", function(done) {
  64. assert.strictEqual(helpers.uuid_valid("__niceUs3rname__"), true);
  65. done();
  66. });
  67. it("1 char username is valid", function(done) {
  68. assert.strictEqual(helpers.uuid_valid("a"), true);
  69. done();
  70. });
  71. it("should not exist (uuid)", function(done) {
  72. networking.get_skin_url("00000000000000000000000000000000", function(err, profile) {
  73. assert.strictEqual(err, null);
  74. done();
  75. });
  76. });
  77. it("should not exist (username)", function(done) {
  78. networking.get_skin_url("Steve", function(err, profile) {
  79. assert.strictEqual(err, null);
  80. done();
  81. });
  82. });
  83. });
  84. describe("Avatar", function() {
  85. // profile "Alex" - hoping it'll never have a skin
  86. var alex_uuid = "ec561538f3fd461daff5086b22154bce";
  87. // profile "Steven" (Steve doesn't exist) - hoping it'll never have a skin
  88. var steven_uuid = "b8ffc3d37dbf48278f69475f6690aabd";
  89. it("uuid's account should exist, but skin should not", function(done) {
  90. helpers.get_avatar(alex_uuid, false, 160, function(err, status, image) {
  91. assert.strictEqual(status, 2);
  92. done();
  93. });
  94. });
  95. it("odd UUID should default to Alex", function(done) {
  96. assert.strictEqual(skins.default_skin(alex_uuid), "alex");
  97. done();
  98. });
  99. it("even UUID should default to Steve", function(done) {
  100. assert.strictEqual(skins.default_skin(steven_uuid), "steve");
  101. done();
  102. });
  103. });
  104. describe("Errors", function() {
  105. it("should time out on uuid info download", function(done) {
  106. var original_timeout = config.http_timeout;
  107. config.http_timeout = 1;
  108. networking.get_skin_url("069a79f444e94726a5befca90e38aaf5", function(err, skin_url) {
  109. assert.strictEqual(err.code, "ETIMEDOUT");
  110. config.http_timeout = original_timeout;
  111. done();
  112. });
  113. });
  114. it("should time out on username info download", function(done) {
  115. var original_timeout = config.http_timeout;
  116. config.http_timeout = 1;
  117. networking.get_skin_url("redstone_sheep", function(err, skin_url) {
  118. assert.strictEqual(err.code, "ETIMEDOUT");
  119. config.http_timeout = original_timeout;
  120. done();
  121. });
  122. });
  123. it("should time out on skin download", function(done) {
  124. var original_timeout = config.http_timeout;
  125. config.http_timeout = 1;
  126. networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) {
  127. assert.strictEqual(err.code, "ETIMEDOUT");
  128. config.http_timeout = original_timeout;
  129. done();
  130. });
  131. });
  132. it("should not find the skin", function(done) {
  133. assert.doesNotThrow(function() {
  134. networking.get_skin("http://textures.minecraft.net/texture/this-does-not-exist", function(err, img) {
  135. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  136. done();
  137. });
  138. });
  139. });
  140. it("should ignore file updates on invalid files", function(done) {
  141. assert.doesNotThrow(function() {
  142. cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png");
  143. });
  144. done();
  145. });
  146. });
  147. // DRY with uuid and username tests
  148. for (var i in ids) {
  149. var id = ids[i];
  150. var id_type = id.length > 16 ? "uuid" : "name";
  151. // needs an anonymous function because id and id_type aren't constant
  152. (function(id, id_type) {
  153. describe("Networking: Avatar", function() {
  154. before(function() {
  155. cache.get_redis().flushall();
  156. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  157. });
  158. it("should be downloaded", function(done) {
  159. helpers.get_avatar(id, false, 160, function(err, status, image) {
  160. assert.strictEqual(status, 2);
  161. done();
  162. });
  163. });
  164. it("should be cached", function(done) {
  165. helpers.get_avatar(id, false, 160, function(err, status, image) {
  166. assert.strictEqual(status === 0 || status === 1, true);
  167. done();
  168. });
  169. });
  170. if (id.length > 16) {
  171. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  172. } else {
  173. it("should be checked", function(done) {
  174. var original_cache_time = config.local_cache_time;
  175. config.local_cache_time = 0;
  176. helpers.get_avatar(id, false, 160, function(err, status, image) {
  177. assert.strictEqual(status, 3);
  178. config.local_cache_time = original_cache_time;
  179. done();
  180. });
  181. });
  182. }
  183. });
  184. describe("Networking: Skin", function() {
  185. it("should not fail (uuid)", function(done) {
  186. helpers.get_skin(id, function(err, hash, img) {
  187. assert.strictEqual(err, null);
  188. done();
  189. });
  190. });
  191. });
  192. describe("Errors", function() {
  193. before(function() {
  194. cache.get_redis().flushall();
  195. });
  196. if (id_type == "uuid") {
  197. it("uuid should be rate limited", function(done) {
  198. helpers.get_avatar(id, false, 160, function(err, status, image) {
  199. assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
  200. done();
  201. });
  202. });
  203. } else {
  204. it("username should NOT be rate limited (username)", function(done) {
  205. helpers.get_avatar(id, false, 160, function(err, status, image) {
  206. assert.strictEqual(err, null);
  207. done();
  208. });
  209. });
  210. }
  211. });
  212. })(id, id_type);
  213. }
  214. });