test.js 15 KB

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