test.js 15 KB

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