test.js 15 KB

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