test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. for (var a in alex_ids) {
  132. var alex_id = alex_ids[a];
  133. (function(alex_id) {
  134. it("UUID " + alex_id + " should default to Alex", function(done) {
  135. assert.strictEqual(skins.default_skin(alex_id), "alex");
  136. done();
  137. });
  138. })(alex_id);
  139. }
  140. for (var s in steve_ids) {
  141. var steve_id = steve_ids[s];
  142. (function(steve_id) {
  143. it("UUID " + steve_id + " should default to Steve", function(done) {
  144. assert.strictEqual(skins.default_skin(steve_id), "steve");
  145. done();
  146. });
  147. })(steve_id);
  148. }
  149. });
  150. describe("Errors", function() {
  151. it("should time out on uuid info download", function(done) {
  152. var original_timeout = config.http_timeout;
  153. config.http_timeout = 1;
  154. networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  155. assert.strictEqual(err.code, "ETIMEDOUT");
  156. config.http_timeout = original_timeout;
  157. done();
  158. });
  159. });
  160. it("should time out on username info download", function(done) {
  161. var original_timeout = config.http_timeout;
  162. config.http_timeout = 1;
  163. networking.get_username_url(rid, "jomo", 0, function(err, url) {
  164. assert.strictEqual(err.code, "ETIMEDOUT");
  165. config.http_timeout = original_timeout;
  166. done();
  167. });
  168. });
  169. it("should time out on skin download", function(done) {
  170. var original_timeout = config.http_timeout;
  171. config.http_timeout = 1;
  172. networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) {
  173. assert.strictEqual(error.code, "ETIMEDOUT");
  174. config.http_timeout = original_timeout;
  175. done();
  176. });
  177. });
  178. it("should not find the skin", function(done) {
  179. assert.doesNotThrow(function() {
  180. networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) {
  181. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  182. done();
  183. });
  184. });
  185. });
  186. it("should ignore file updates on invalid files", function(done) {
  187. assert.doesNotThrow(function() {
  188. cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", false, function(err) {
  189. done();
  190. });
  191. });
  192. });
  193. it("should not find the file", function(done) {
  194. skins.open_skin(rid, "non/existent/path", function(err, img) {
  195. assert.notStrictEqual(err, null);
  196. done();
  197. });
  198. });
  199. });
  200. describe("Server", function() {
  201. before(function(done) {
  202. server.boot(function() {
  203. done();
  204. });
  205. });
  206. // Test the home page
  207. it("should return a 200 (home page)", function(done) {
  208. request.get("http://localhost:3000", function(error, res, body) {
  209. assert.equal(200, res.statusCode);
  210. done();
  211. });
  212. });
  213. it("should return a 200 (asset request)", function(done) {
  214. request.get("http://localhost:3000/stylesheets/style.css", function(error, res, body) {
  215. assert.equal(200, res.statusCode);
  216. done();
  217. });
  218. });
  219. // invalid method, we only allow GET and HEAD requests
  220. it("should return a 405 (invalid method)", function(done) {
  221. request.post("http://localhost:3000", function(error, res, body) {
  222. assert.equal(405, res.statusCode);
  223. done();
  224. });
  225. });
  226. it("should return a 422 (invalid size)", function(done) {
  227. var size = config.max_size + 1;
  228. request.get("http://localhost:3000/avatars/Jake_0?size=" + size, function(error, res, body) {
  229. assert.equal(422, res.statusCode);
  230. done();
  231. });
  232. });
  233. it("should return a 422 (invalid scale)", function(done) {
  234. var scale = config.max_scale + 1;
  235. request.get("http://localhost:3000/renders/head/Jake_0?scale=" + scale, function(error, res, body) {
  236. assert.equal(422, res.statusCode);
  237. done();
  238. });
  239. });
  240. // no default images for capes, should 404
  241. it("should return a 404 (no cape)", function(done) {
  242. request.get("http://localhost:3000/capes/Jake_0", function(error, res, body) {
  243. assert.equal(404, res.statusCode);
  244. done();
  245. });
  246. });
  247. it("should return a 422 (invalid render type)", function(done) {
  248. request.get("http://localhost:3000/renders/side/Jake_0", function(error, res, body) {
  249. assert.equal(422, res.statusCode);
  250. done();
  251. });
  252. });
  253. // testing all paths for valid inputs
  254. var locations = ["avatars", "skins", "renders/head"];
  255. for (var l in locations) {
  256. var location = locations[l];
  257. (function(location) {
  258. it("should return a 200 (valid input " + location + ")", function(done) {
  259. request.get("http://localhost:3000/" + location + "/Jake_0", function(error, res, body) {
  260. assert.equal(200, res.statusCode);
  261. done();
  262. });
  263. });
  264. it("should return a 422 (invalid id " + location + ")", function(done) {
  265. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  266. assert.equal(422, res.statusCode);
  267. done();
  268. });
  269. });
  270. })(location);
  271. }
  272. // testing all paths for invalid id formats
  273. locations = ["avatars", "capes", "skins", "renders/head"];
  274. for (l in locations) {
  275. var location = locations[l];
  276. (function(location) {
  277. it("should return a 422 (invalid id " + location + ")", function(done) {
  278. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  279. assert.equal(422, res.statusCode);
  280. done();
  281. });
  282. });
  283. })(location);
  284. }
  285. //testing all paths for default images
  286. locations = ["avatars", "skins", "renders/head"];
  287. for (l in locations) {
  288. var location = locations[l];
  289. (function(location) {
  290. it("should return a 200 (default steve image " + location + ")", function(done) {
  291. request.get("http://localhost:3000/" + location + "/invalidjsvns?default=steve", function(error, res, body) {
  292. assert.equal(200, res.statusCode);
  293. done();
  294. });
  295. });
  296. it("should return a 200 (default external image " + location + ")", function(done) {
  297. request.get("http://localhost:3000/" + location + "/invalidjsvns?default=https%3A%2F%2Fi.imgur.com%2FocJVWAc.png", function(error, res, body) {
  298. assert.equal(200, res.statusCode);
  299. done();
  300. });
  301. });
  302. })(location);
  303. }
  304. after(function(done) {
  305. server.close(function() {
  306. done();
  307. });
  308. });
  309. });
  310. // we have to make sure that we test both a 32x64 and 64x64 skin
  311. describe("Networking: Render", function() {
  312. it("should not fail (username, 32x64 skin)", function(done) {
  313. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  314. assert.strictEqual(err, null);
  315. done();
  316. });
  317. });
  318. it("should not fail (username, 64x64 skin)", function(done) {
  319. helpers.get_render(rid, "Jake_0", 6, true, true, function(err, hash, img) {
  320. assert.strictEqual(err, null);
  321. done();
  322. });
  323. });
  324. });
  325. describe("Networking: Cape", function() {
  326. it("should not fail (guaranteed cape)", function(done) {
  327. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  328. assert.strictEqual(err, null);
  329. done();
  330. });
  331. });
  332. it("should already exist", function(done) {
  333. before(function() {
  334. cache.get_redis().flushall();
  335. });
  336. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  337. assert.strictEqual(err, null);
  338. done();
  339. });
  340. });
  341. it("should not be found", function(done) {
  342. helpers.get_cape(rid, "Jake_0", function(err, hash, img) {
  343. assert.strictEqual(img, null);
  344. done();
  345. });
  346. });
  347. });
  348. describe("Networking: Skin", function() {
  349. it("should not fail", function(done) {
  350. helpers.get_cape(rid, "Jake_0", function(err, hash, img) {
  351. assert.strictEqual(err, null);
  352. done();
  353. });
  354. });
  355. it("should already exist", function(done) {
  356. before(function() {
  357. cache.get_redis().flushall();
  358. });
  359. helpers.get_cape(rid, "Jake_0", function(err, hash, img) {
  360. assert.strictEqual(err, null);
  361. done();
  362. });
  363. });
  364. });
  365. // DRY with uuid and username tests
  366. for (var i in ids) {
  367. var id = ids[i];
  368. var id_type = id.length > 16 ? "uuid" : "name";
  369. // needs an anonymous function because id and id_type aren't constant
  370. (function(id, id_type) {
  371. describe("Networking: Avatar", function() {
  372. before(function() {
  373. cache.get_redis().flushall();
  374. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  375. });
  376. it("should be downloaded", function(done) {
  377. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  378. assert.strictEqual(status, 2);
  379. done();
  380. });
  381. });
  382. it("should be cached", function(done) {
  383. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  384. assert.strictEqual(status === 0 || status === 1, true);
  385. done();
  386. });
  387. });
  388. if (id.length > 16) {
  389. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  390. } else {
  391. it("should be checked", function(done) {
  392. var original_cache_time = config.local_cache_time;
  393. config.local_cache_time = 0;
  394. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  395. assert.strictEqual(status, 3);
  396. config.local_cache_time = original_cache_time;
  397. done();
  398. });
  399. });
  400. }
  401. });
  402. describe("Networking: Skin", function() {
  403. it("should not fail (uuid)", function(done) {
  404. helpers.get_skin(rid, id, function(err, hash, img) {
  405. assert.strictEqual(err, null);
  406. done();
  407. });
  408. });
  409. });
  410. describe("Networking: Render", function() {
  411. it("should not fail (full body)", function(done) {
  412. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  413. assert.strictEqual(err, null);
  414. done();
  415. });
  416. });
  417. it("should not fail (only head)", function(done) {
  418. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  419. assert.strictEqual(err, null);
  420. done();
  421. });
  422. });
  423. });
  424. describe("Networking: Cape", function() {
  425. it("should not fail (possible cape)", function(done) {
  426. helpers.get_cape(rid, id, function(err, hash, img) {
  427. assert.strictEqual(err, null);
  428. done();
  429. });
  430. });
  431. });
  432. describe("Errors", function() {
  433. before(function() {
  434. cache.get_redis().flushall();
  435. });
  436. if (id_type == "uuid") {
  437. it("uuid should be rate limited", function(done) {
  438. networking.get_profile(rid, id, function(err, profile) {
  439. assert.strictEqual(profile.error, "TooManyRequestsException");
  440. done();
  441. });
  442. });
  443. } else {
  444. it("username should NOT be rate limited (username)", function(done) {
  445. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  446. assert.strictEqual(err, null);
  447. done();
  448. });
  449. });
  450. }
  451. });
  452. })(id, id_type);
  453. }
  454. });