test.js 16 KB

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