test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. var assert = require("assert");
  2. var fs = require("fs");
  3. var networking = require("../lib/networking");
  4. var helpers = require("../lib/helpers");
  5. var logging = require("../lib/logging");
  6. var config = require("../lib/config");
  7. var skins = require("../lib/skins");
  8. var cache = require("../lib/cache");
  9. var server = require("../lib/server");
  10. var cleaner = require("../lib/cleaner");
  11. var request = require("request");
  12. // we don't want tests to fail because of slow internet
  13. config.http_timeout *= 3;
  14. // no spam
  15. if (process.env.VERBOSE_TEST !== "true") {
  16. logging.log = function() {};
  17. }
  18. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  19. var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  20. // Get a random UUID + name in order to prevent rate limiting
  21. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  22. var name = names[Math.round(Math.random() * (names.length - 1))];
  23. // Let's hope these will never be assigned
  24. var steve_ids = [
  25. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff0",
  26. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff1",
  27. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff1",
  28. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff0",
  29. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff1",
  30. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff0",
  31. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff0",
  32. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff1",
  33. ];
  34. // Let's hope these will never be assigned
  35. var alex_ids = [
  36. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff1",
  37. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff0",
  38. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff0",
  39. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff1",
  40. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff0",
  41. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff1",
  42. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff1",
  43. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff0",
  44. ];
  45. var rid = "TestReqID: ";
  46. function getRandomInt(min, max) {
  47. return Math.floor(Math.random() * (max - min + 1)) + min;
  48. }
  49. var ids = [
  50. uuid.toLowerCase(),
  51. name.toLowerCase(),
  52. name.toUpperCase(),
  53. uuid.toUpperCase(),
  54. ];
  55. describe("Crafatar", function() {
  56. // we might have to make 2 HTTP requests
  57. this.timeout(config.http_timeout * 2 + 50);
  58. before(function() {
  59. cache.get_redis().flushall();
  60. // cause I don't know how big hard drives are these days
  61. config.cleaning_disk_limit = Infinity;
  62. config.cleaning_redis_limit = Infinity;
  63. cleaner.run();
  64. });
  65. describe("UUID/username", function() {
  66. it("non-hex uuid is invalid", function(done) {
  67. assert.strictEqual(helpers.id_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  68. done();
  69. });
  70. it("empty id is invalid", function(done) {
  71. assert.strictEqual(helpers.id_valid(""), false);
  72. done();
  73. });
  74. it("non-alphanumeric username is invalid", function(done) {
  75. assert.strictEqual(helpers.id_valid("usernäme"), false);
  76. done();
  77. });
  78. it("dashed username is invalid", function(done) {
  79. assert.strictEqual(helpers.id_valid("user-name"), false);
  80. done();
  81. });
  82. it(">16 length username is invalid", function(done) {
  83. assert.strictEqual(helpers.id_valid("ThisNameIsTooLong"), false);
  84. done();
  85. });
  86. it("lowercase uuid is valid", function(done) {
  87. assert.strictEqual(helpers.id_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  88. done();
  89. });
  90. it("uppercase uuid is valid", function(done) {
  91. assert.strictEqual(helpers.id_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  92. done();
  93. });
  94. it("dashed uuid is valid", function(done) {
  95. assert.strictEqual(helpers.id_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  96. done();
  97. });
  98. it("16 chars, underscored, capital, numbered username is valid", function(done) {
  99. assert.strictEqual(helpers.id_valid("__niceUs3rname__"), true);
  100. done();
  101. });
  102. it("1 char username is valid", function(done) {
  103. assert.strictEqual(helpers.id_valid("a"), true);
  104. done();
  105. });
  106. it("should not exist (uuid)", function(done) {
  107. var number = getRandomInt(0, 9).toString();
  108. networking.get_profile(rid, Array(33).join(number), function(err, profile) {
  109. assert.strictEqual(profile, null);
  110. done();
  111. });
  112. });
  113. it("should not exist (username)", function(done) {
  114. networking.get_username_url(rid, "Steve", 0, function(err, profile) {
  115. assert.strictEqual(err, null);
  116. done();
  117. });
  118. });
  119. });
  120. describe("Avatar", function() {
  121. it("uuid's account should exist, but skin should not", function(done) {
  122. // profile "Alex" - hoping it'll never have a skin
  123. networking.get_profile(rid, "ec561538f3fd461daff5086b22154bce", function(err, profile) {
  124. assert.notStrictEqual(profile, null);
  125. networking.get_uuid_url(profile, 1, function(url) {
  126. assert.strictEqual(url, null);
  127. done();
  128. });
  129. });
  130. });
  131. it("Username should default to Steve", function(done) {
  132. assert.strictEqual(skins.default_skin("TestUser"), "steve");
  133. done();
  134. });
  135. for (var a in alex_ids) {
  136. var alex_id = alex_ids[a];
  137. (function(alex_id) {
  138. it("UUID " + alex_id + " should default to Alex", function(done) {
  139. assert.strictEqual(skins.default_skin(alex_id), "alex");
  140. done();
  141. });
  142. })(alex_id);
  143. }
  144. for (var s in steve_ids) {
  145. var steve_id = steve_ids[s];
  146. (function(steve_id) {
  147. it("UUID " + steve_id + " should default to Steve", function(done) {
  148. assert.strictEqual(skins.default_skin(steve_id), "steve");
  149. done();
  150. });
  151. })(steve_id);
  152. }
  153. });
  154. describe("Errors", function() {
  155. it("should time out on uuid info download", function(done) {
  156. var original_timeout = config.http_timeout;
  157. config.http_timeout = 1;
  158. networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  159. assert.strictEqual(err.code, "ETIMEDOUT");
  160. config.http_timeout = original_timeout;
  161. done();
  162. });
  163. });
  164. it("should time out on username info download", function(done) {
  165. var original_timeout = config.http_timeout;
  166. config.http_timeout = 1;
  167. networking.get_username_url(rid, "jomo", 0, function(err, url) {
  168. assert.strictEqual(err.code, "ETIMEDOUT");
  169. config.http_timeout = original_timeout;
  170. done();
  171. });
  172. });
  173. it("should time out on skin download", function(done) {
  174. var original_timeout = config.http_timeout;
  175. config.http_timeout = 1;
  176. networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) {
  177. assert.strictEqual(error.code, "ETIMEDOUT");
  178. config.http_timeout = original_timeout;
  179. done();
  180. });
  181. });
  182. it("should not find the skin", function(done) {
  183. assert.doesNotThrow(function() {
  184. networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) {
  185. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  186. done();
  187. });
  188. });
  189. });
  190. it("should ignore file updates on invalid files", function(done) {
  191. assert.doesNotThrow(function() {
  192. cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", false, function(err) {
  193. done();
  194. });
  195. });
  196. });
  197. it("should not find the file", function(done) {
  198. skins.open_skin(rid, "non/existent/path", function(err, img) {
  199. assert.notStrictEqual(err, null);
  200. done();
  201. });
  202. });
  203. });
  204. describe("Server", function() {
  205. before(function(done) {
  206. server.boot(function() {
  207. done();
  208. });
  209. });
  210. // Test the home page
  211. it("should return a 200 (home page)", function(done) {
  212. request.get("http://localhost:3000", function(error, res, body) {
  213. assert.equal(200, res.statusCode);
  214. done();
  215. });
  216. });
  217. it("should return a 200 (asset request)", function(done) {
  218. request.get("http://localhost:3000/stylesheets/style.css", function(error, res, body) {
  219. assert.equal(200, res.statusCode);
  220. done();
  221. });
  222. });
  223. // invalid method, we only allow GET and HEAD requests
  224. it("should return a 405 (invalid method)", function(done) {
  225. request.post("http://localhost:3000", function(error, res, body) {
  226. assert.equal(405, res.statusCode);
  227. done();
  228. });
  229. });
  230. it("should return a 422 (invalid size)", function(done) {
  231. var size = config.max_size + 1;
  232. request.get("http://localhost:3000/avatars/Jake_0?size=" + size, function(error, res, body) {
  233. assert.equal(422, res.statusCode);
  234. done();
  235. });
  236. });
  237. it("should return a 422 (invalid scale)", function(done) {
  238. var scale = config.max_scale + 1;
  239. request.get("http://localhost:3000/renders/head/Jake_0?scale=" + scale, function(error, res, body) {
  240. assert.equal(422, res.statusCode);
  241. done();
  242. });
  243. });
  244. // no default images for capes, should 404
  245. it("should return a 404 (no cape)", function(done) {
  246. request.get("http://localhost:3000/capes/Jake_0", function(error, res, body) {
  247. assert.equal(404, res.statusCode);
  248. done();
  249. });
  250. });
  251. it("should return a 422 (invalid render type)", function(done) {
  252. request.get("http://localhost:3000/renders/side/Jake_0", function(error, res, body) {
  253. assert.equal(422, res.statusCode);
  254. done();
  255. });
  256. });
  257. // testing all paths for valid inputs
  258. var locations = ["avatars", "skins", "renders/head"];
  259. for (var l in locations) {
  260. var location = locations[l];
  261. (function(location) {
  262. it("should return a 200 (valid input " + location + ")", function(done) {
  263. request.get("http://localhost:3000/" + location + "/Jake_0", function(error, res, body) {
  264. assert.equal(200, res.statusCode);
  265. done();
  266. });
  267. });
  268. it("should return a 422 (invalid id " + location + ")", function(done) {
  269. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  270. assert.equal(422, res.statusCode);
  271. done();
  272. });
  273. });
  274. })(location);
  275. }
  276. // testing all paths for invalid id formats
  277. locations = ["avatars", "capes", "skins", "renders/head"];
  278. for (l in locations) {
  279. var location = locations[l];
  280. (function(location) {
  281. it("should return a 422 (invalid id " + location + ")", function(done) {
  282. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  283. assert.equal(422, res.statusCode);
  284. done();
  285. });
  286. });
  287. })(location);
  288. }
  289. //testing all paths for default images
  290. locations = ["avatars", "skins", "renders/head"];
  291. for (l in locations) {
  292. var location = locations[l];
  293. (function(location) {
  294. it("should return a 200 (default steve image " + location + ")", function(done) {
  295. request.get("http://localhost:3000/" + location + "/invalidjsvns?default=steve", function(error, res, body) {
  296. assert.equal(200, res.statusCode);
  297. done();
  298. });
  299. });
  300. it("should return a 200 (default external image " + location + ")", function(done) {
  301. request.get("http://localhost:3000/" + location + "/invalidjsvns?default=https%3A%2F%2Fi.imgur.com%2FocJVWAc.png", function(error, res, body) {
  302. assert.equal(200, res.statusCode);
  303. done();
  304. });
  305. });
  306. })(location);
  307. }
  308. after(function(done) {
  309. server.close(function() {
  310. done();
  311. });
  312. });
  313. });
  314. // we have to make sure that we test both a 32x64 and 64x64 skin
  315. describe("Networking: Render", function() {
  316. it("should not fail (username, 32x64 skin)", function(done) {
  317. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  318. assert.strictEqual(err, null);
  319. done();
  320. });
  321. });
  322. it("should not fail (username, 64x64 skin)", function(done) {
  323. helpers.get_render(rid, "Jake_0", 6, true, true, function(err, hash, img) {
  324. assert.strictEqual(err, null);
  325. done();
  326. });
  327. });
  328. });
  329. describe("Networking: Cape", function() {
  330. it("should not fail (guaranteed cape)", function(done) {
  331. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  332. assert.strictEqual(err, null);
  333. done();
  334. });
  335. });
  336. it("should already exist", function(done) {
  337. before(function() {
  338. cache.get_redis().flushall();
  339. });
  340. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  341. assert.strictEqual(err, null);
  342. done();
  343. });
  344. });
  345. it("should not be found", function(done) {
  346. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  347. assert.strictEqual(img, null);
  348. done();
  349. });
  350. });
  351. });
  352. describe("Networking: Skin", function() {
  353. it("should not fail", function(done) {
  354. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  355. assert.strictEqual(err, null);
  356. done();
  357. });
  358. });
  359. it("should already exist", function(done) {
  360. before(function() {
  361. cache.get_redis().flushall();
  362. });
  363. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  364. assert.strictEqual(err, null);
  365. done();
  366. });
  367. });
  368. });
  369. // DRY with uuid and username tests
  370. for (var i in ids) {
  371. var id = ids[i];
  372. var id_type = id.length > 16 ? "uuid" : "name";
  373. // needs an anonymous function because id and id_type aren't constant
  374. (function(id, id_type) {
  375. describe("Networking: Avatar", function() {
  376. before(function() {
  377. cache.get_redis().flushall();
  378. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  379. });
  380. it("should be downloaded", function(done) {
  381. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  382. assert.strictEqual(status, 2);
  383. done();
  384. });
  385. });
  386. it("should be cached", function(done) {
  387. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  388. assert.strictEqual(status === 0 || status === 1, true);
  389. done();
  390. });
  391. });
  392. if (id.length > 16) {
  393. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  394. } else {
  395. it("should be checked", function(done) {
  396. var original_cache_time = config.local_cache_time;
  397. config.local_cache_time = 0;
  398. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  399. assert.strictEqual(status, 3);
  400. config.local_cache_time = original_cache_time;
  401. done();
  402. });
  403. });
  404. }
  405. });
  406. describe("Networking: Skin", function() {
  407. it("should not fail (uuid)", function(done) {
  408. helpers.get_skin(rid, id, function(err, hash, status, img) {
  409. assert.strictEqual(err, null);
  410. done();
  411. });
  412. });
  413. });
  414. describe("Networking: Render", function() {
  415. it("should not fail (full body)", function(done) {
  416. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  417. assert.strictEqual(err, null);
  418. done();
  419. });
  420. });
  421. it("should not fail (only head)", function(done) {
  422. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  423. assert.strictEqual(err, null);
  424. done();
  425. });
  426. });
  427. });
  428. describe("Networking: Cape", function() {
  429. it("should not fail (possible cape)", function(done) {
  430. helpers.get_cape(rid, id, function(err, hash, status, img) {
  431. assert.strictEqual(err, null);
  432. done();
  433. });
  434. });
  435. });
  436. describe("Errors", function() {
  437. before(function() {
  438. cache.get_redis().flushall();
  439. });
  440. if (id_type == "uuid") {
  441. it("uuid should be rate limited", function(done) {
  442. networking.get_profile(rid, id, function(err, profile) {
  443. assert.strictEqual(profile.error, "TooManyRequestsException");
  444. done();
  445. });
  446. });
  447. } else {
  448. it("username should NOT be rate limited (username)", function(done) {
  449. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  450. assert.strictEqual(err, null);
  451. done();
  452. });
  453. });
  454. }
  455. });
  456. })(id, id_type);
  457. }
  458. });