test.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /* globals describe, it, before, after */
  2. /* eslint no-loop-func:0 guard-for-in:0 */
  3. var networking = require("../lib/networking");
  4. var helpers = require("../lib/helpers");
  5. var logging = require("../lib/logging");
  6. var cleaner = require("../lib/cleaner");
  7. var request = require("request");
  8. var config = require("../config");
  9. var server = require("../lib/server");
  10. var assert = require("assert");
  11. var skins = require("../lib/skins");
  12. var cache = require("../lib/cache");
  13. var crc = require("crc").crc32;
  14. var fs = require("fs");
  15. // we don't want tests to fail because of slow internet
  16. config.server.http_timeout *= 3;
  17. // no spam
  18. if (process.env.VERBOSE_TEST !== "true") {
  19. logging.log = logging.debug = logging.warn = logging.error = function() {};
  20. }
  21. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  22. var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  23. // Get a random UUID + name in order to prevent rate limiting
  24. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  25. var name = names[Math.round(Math.random() * (names.length - 1))];
  26. // Let's hope these will never be assigned
  27. var steve_ids = [
  28. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff0",
  29. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff1",
  30. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff1",
  31. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff0",
  32. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff1",
  33. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff0",
  34. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff0",
  35. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff1",
  36. ];
  37. // Let's hope these will never be assigned
  38. var alex_ids = [
  39. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff1",
  40. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff0",
  41. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff0",
  42. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff1",
  43. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff0",
  44. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff1",
  45. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff1",
  46. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff0",
  47. ];
  48. var rid = "TestReqID: ";
  49. function getRandomInt(min, max) {
  50. return Math.floor(Math.random() * (max - min + 1)) + min;
  51. }
  52. var ids = [
  53. uuid.toLowerCase(),
  54. name.toLowerCase(),
  55. name.toUpperCase(),
  56. uuid.toUpperCase(),
  57. ];
  58. describe("Crafatar", function() {
  59. // we might have to make 2 HTTP requests
  60. this.timeout(config.server.http_timeout * 2 + 50);
  61. before(function() {
  62. cache.get_redis().flushall();
  63. // cause I don't know how big hard drives are these days
  64. config.cleaner.disk_limit = Infinity;
  65. config.cleaner.redis_limit = Infinity;
  66. cleaner.run();
  67. });
  68. describe("UUID/username", function() {
  69. it("non-hex uuid is invalid", function(done) {
  70. assert.strictEqual(helpers.id_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  71. done();
  72. });
  73. it("empty id is invalid", function(done) {
  74. assert.strictEqual(helpers.id_valid(""), false);
  75. done();
  76. });
  77. it("non-alphanumeric username is invalid", function(done) {
  78. assert.strictEqual(helpers.id_valid("usernäme"), false);
  79. done();
  80. });
  81. it("dashed username is invalid", function(done) {
  82. assert.strictEqual(helpers.id_valid("user-name"), false);
  83. done();
  84. });
  85. it(">16 length username is invalid", function(done) {
  86. assert.strictEqual(helpers.id_valid("ThisNameIsTooLong"), false);
  87. done();
  88. });
  89. it("lowercase uuid is valid", function(done) {
  90. assert.strictEqual(helpers.id_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  91. done();
  92. });
  93. it("uppercase uuid is valid", function(done) {
  94. assert.strictEqual(helpers.id_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  95. done();
  96. });
  97. it("dashed uuid is valid", function(done) {
  98. assert.strictEqual(helpers.id_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  99. done();
  100. });
  101. it("16 chars, underscored, capital, numbered username is valid", function(done) {
  102. assert.strictEqual(helpers.id_valid("__niceUs3rname__"), true);
  103. done();
  104. });
  105. it("1 char username is valid", function(done) {
  106. assert.strictEqual(helpers.id_valid("a"), true);
  107. done();
  108. });
  109. it("should not exist (uuid)", function(done) {
  110. var number = getRandomInt(0, 9).toString();
  111. networking.get_profile(rid, Array(33).join(number), function(err, profile) {
  112. assert.ifError(err);
  113. assert.strictEqual(profile, null);
  114. done();
  115. });
  116. });
  117. it("should not exist (username)", function(done) {
  118. networking.get_username_url(rid, "Steve", 0, function(err, profile) {
  119. assert.ifError(err);
  120. done();
  121. });
  122. });
  123. });
  124. describe("Avatar", function() {
  125. it("uuid's account should exist, but skin should not", function(done) {
  126. // profile "Alex" - hoping it'll never have a skin
  127. networking.get_profile(rid, "ec561538f3fd461daff5086b22154bce", function(err, profile) {
  128. assert.ifError(err);
  129. assert.notStrictEqual(profile, null);
  130. networking.get_uuid_url(profile, 1, function(url) {
  131. assert.strictEqual(url, null);
  132. done();
  133. });
  134. });
  135. });
  136. it("Username should default to MHF_Steve", function(done) {
  137. assert.strictEqual(skins.default_skin("TestUser"), "mhf_steve");
  138. done();
  139. });
  140. for (var a in alex_ids) {
  141. var alexid = alex_ids[a];
  142. (function(alex_id) {
  143. it("UUID " + alex_id + " should default to MHF_Alex", function(done) {
  144. assert.strictEqual(skins.default_skin(alex_id), "mhf_alex");
  145. done();
  146. });
  147. }(alexid));
  148. }
  149. for (var s in steve_ids) {
  150. var steveid = steve_ids[s];
  151. (function(steve_id) {
  152. it("UUID " + steve_id + " should default to MHF_Steve", function(done) {
  153. assert.strictEqual(skins.default_skin(steve_id), "mhf_steve");
  154. done();
  155. });
  156. }(steveid));
  157. }
  158. });
  159. describe("Errors", function() {
  160. it("should time out on uuid info download", function(done) {
  161. var original_timeout = config.server.http_timeout;
  162. config.server.http_timeout = 1;
  163. networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  164. assert.strictEqual(err.code, "ETIMEDOUT");
  165. config.server.http_timeout = original_timeout;
  166. done();
  167. });
  168. });
  169. it("should time out on username info download", function(done) {
  170. var original_timeout = config.server.http_timeout;
  171. config.server.http_timeout = 1;
  172. networking.get_username_url(rid, "jomo", 0, function(err, url) {
  173. assert.strictEqual(err.code, "ETIMEDOUT");
  174. config.server.http_timeout = original_timeout;
  175. done();
  176. });
  177. });
  178. it("should time out on skin download", function(done) {
  179. var original_timeout = config.http_timeout;
  180. config.server.http_timeout = 1;
  181. networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) {
  182. assert.strictEqual(error.code, "ETIMEDOUT");
  183. config.server.http_timeout = original_timeout;
  184. done();
  185. });
  186. });
  187. it("should not find the skin", function(done) {
  188. assert.doesNotThrow(function() {
  189. networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) {
  190. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  191. done();
  192. });
  193. });
  194. });
  195. it("should not find the file", function(done) {
  196. skins.open_skin(rid, "non/existent/path", function(err, img) {
  197. assert(err);
  198. done();
  199. });
  200. });
  201. });
  202. describe("Server", function() {
  203. // throws Exception when default headers are not in res.headers
  204. function assert_headers(res) {
  205. assert(res.headers["content-type"]);
  206. assert("" + res.headers["response-time"]);
  207. assert(res.headers["x-request-id"]);
  208. assert.equal(res.headers["access-control-allow-origin"], "*");
  209. assert.equal(res.headers["cache-control"], "max-age=" + config.caching.browser + ", public");
  210. }
  211. // throws Exception when +url+ is requested with +etag+
  212. // and it does not return 304 without a body
  213. function assert_cache(url, etag, callback) {
  214. request.get(url, {
  215. headers: {
  216. "If-None-Match": etag
  217. }
  218. }, function(error, res, body) {
  219. assert.ifError(error);
  220. assert.ifError(body);
  221. assert.equal(res.statusCode, 304);
  222. assert(res.headers.etag);
  223. assert_headers(res);
  224. callback();
  225. });
  226. }
  227. before(function(done) {
  228. server.boot(function() {
  229. done();
  230. });
  231. });
  232. it("should return 405 Method Not Allowed for POST", function(done) {
  233. request.post("http://localhost:3000", function(error, res, body) {
  234. assert.ifError(error);
  235. assert.strictEqual(res.statusCode, 405);
  236. done();
  237. });
  238. });
  239. it("should return correct HTTP response for home page", function(done) {
  240. var url = "http://localhost:3000";
  241. request.get(url, function(error, res, body) {
  242. assert.ifError(error);
  243. assert.strictEqual(res.statusCode, 200);
  244. assert_headers(res);
  245. assert(res.headers.etag);
  246. assert.strictEqual(res.headers["content-type"], "text/html; charset=utf-8");
  247. assert.strictEqual(res.headers.etag, '"' + crc(body) + '"');
  248. assert(body);
  249. assert_cache(url, res.headers.etag, function() {
  250. done();
  251. });
  252. });
  253. });
  254. it("should return correct HTTP response for assets", function(done) {
  255. var url = "http://localhost:3000/stylesheets/style.css";
  256. request.get(url, function(error, res, body) {
  257. assert.ifError(error);
  258. assert.strictEqual(res.statusCode, 200);
  259. assert_headers(res);
  260. assert(res.headers.etag);
  261. assert.strictEqual(res.headers["content-type"], "text/css");
  262. assert.strictEqual(res.headers.etag, '"' + crc(body) + '"');
  263. assert(body);
  264. assert_cache(url, res.headers.etag, function() {
  265. done();
  266. });
  267. });
  268. });
  269. it("should return correct HTTP response for URL encoded URLs", function(done) {
  270. var url = "http://localhost:3000/%61%76%61%74%61%72%73/%6a%6f%6d%6f"; // avatars/jomo
  271. request.get(url, function(error, res, body) {
  272. assert.ifError(error);
  273. assert.strictEqual(res.statusCode, 200);
  274. assert_headers(res);
  275. assert(res.headers.etag);
  276. assert.strictEqual(res.headers["content-type"], "image/png");
  277. assert(body);
  278. done();
  279. });
  280. });
  281. it("should not fail on simultaneous requests", function(done) {
  282. // do not change "constructor" !
  283. // it's a reserved property name, we're testing for that
  284. var sids = ["696a82ce41f44b51aa31b8709b8686f0", "constructor"];
  285. for (var j in sids) {
  286. var id = sids[j];
  287. var url = "http://localhost:3000/avatars/" + id;
  288. // 10 requests at once
  289. var requests = 10;
  290. var finished = 0;
  291. function partDone() {
  292. finished++;
  293. if (requests === finished) {
  294. done();
  295. }
  296. }
  297. function req() {
  298. request.get(url, function(error, res, body) {
  299. assert.ifError(error);
  300. assert.strictEqual(res.statusCode, 200);
  301. assert_headers(res);
  302. assert(res.headers.etag);
  303. assert.strictEqual(res.headers["content-type"], "image/png");
  304. assert(body);
  305. partDone();
  306. });
  307. }
  308. // make simultanous requests
  309. for (var k = 0; k < requests; k++) {
  310. req(k);
  311. }
  312. }
  313. });
  314. var server_tests = {
  315. "avatar with existing username": {
  316. url: "http://localhost:3000/avatars/jeb_?size=16",
  317. etag: '"a846b82963"',
  318. crc32: 1623808067
  319. },
  320. "avatar with non-existent username": {
  321. url: "http://localhost:3000/avatars/0?size=16",
  322. etag: '"mhf_steve"',
  323. crc32: [2416827277, 1243826040]
  324. },
  325. "avatar with non-existent username defaulting to alex": {
  326. url: "http://localhost:3000/avatars/0?size=16&default=mhf_alex",
  327. etag: '"mhf_alex"',
  328. crc32: [862751081, 809395677]
  329. },
  330. "avatar with non-existent username defaulting to username": {
  331. url: "http://localhost:3000/avatars/0?size=16&default=jeb_",
  332. crc32: 0,
  333. redirect: "/avatars/jeb_?size=16"
  334. },
  335. "avatar with non-existent username defaulting to uuid": {
  336. url: "http://localhost:3000/avatars/0?size=16&default=853c80ef3c3749fdaa49938b674adae6",
  337. crc32: 0,
  338. redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
  339. },
  340. "avatar with non-existent username defaulting to url": {
  341. url: "http://localhost:3000/avatars/0?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  342. crc32: 0,
  343. redirect: "http://example.com/CaseSensitive"
  344. },
  345. "helm avatar with existing username": {
  346. url: "http://localhost:3000/avatars/jeb_?size=16&helm",
  347. etag: '"a846b82963"',
  348. crc32: 646871998
  349. },
  350. "helm avatar with non-existent username": {
  351. url: "http://localhost:3000/avatars/0?size=16&helm",
  352. etag: '"mhf_steve"',
  353. crc32: [2416827277, 1243826040]
  354. },
  355. "helm avatar with non-existent username defaulting to alex": {
  356. url: "http://localhost:3000/avatars/0?size=16&helm&default=mhf_alex",
  357. etag: '"mhf_alex"',
  358. crc32: [862751081, 809395677]
  359. },
  360. "helm avatar with non-existent username defaulting to username": {
  361. url: "http://localhost:3000/avatars/0?size=16&helm&default=jeb_",
  362. crc32: 0,
  363. redirect: "/avatars/jeb_?size=16&helm="
  364. },
  365. "helm avatar with non-existent username defaulting to uuid": {
  366. url: "http://localhost:3000/avatars/0?size=16&helm&default=853c80ef3c3749fdaa49938b674adae6",
  367. crc32: 0,
  368. redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&helm="
  369. },
  370. "helm avatar with non-existent username defaulting to url": {
  371. url: "http://localhost:3000/avatars/0?size=16&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  372. crc32: 0,
  373. redirect: "http://example.com/CaseSensitive"
  374. },
  375. "avatar with existing uuid": {
  376. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16",
  377. etag: '"a846b82963"',
  378. crc32: 1623808067
  379. },
  380. "avatar with non-existent uuid": {
  381. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16",
  382. etag: '"mhf_steve"',
  383. crc32: [2416827277, 1243826040]
  384. },
  385. "avatar with non-existent uuid defaulting to alex": {
  386. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=mhf_alex",
  387. etag: '"mhf_alex"',
  388. crc32: [862751081, 809395677]
  389. },
  390. "avatar with non-existent uuid defaulting to username": {
  391. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_",
  392. crc32: 0,
  393. redirect: "/avatars/jeb_?size=16"
  394. },
  395. "avatar with non-existent uuid defaulting to uuid": {
  396. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
  397. crc32: 0,
  398. redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
  399. },
  400. "avatar with non-existent uuid defaulting to url": {
  401. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  402. crc32: 0,
  403. redirect: "http://example.com/CaseSensitive"
  404. },
  405. "helm avatar with existing uuid": {
  406. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&helm",
  407. etag: '"a846b82963"',
  408. crc32: 646871998
  409. },
  410. "helm avatar with non-existent uuid": {
  411. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm",
  412. etag: '"mhf_steve"',
  413. crc32: [2416827277, 1243826040]
  414. },
  415. "helm avatar with non-existent uuid defaulting to alex": {
  416. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=mhf_alex",
  417. etag: '"mhf_alex"',
  418. crc32: [862751081, 809395677]
  419. },
  420. "helm avatar with non-existent uuid defaulting to username": {
  421. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=jeb_",
  422. crc32: 0,
  423. redirect: "/avatars/jeb_?size=16"
  424. },
  425. "helm avatar with non-existent uuid defaulting to uuid": {
  426. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
  427. crc32: 0,
  428. redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?size=16"
  429. },
  430. "helm avatar with non-existent uuid defaulting to url": {
  431. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  432. crc32: 0,
  433. redirect: "http://example.com/CaseSensitive"
  434. },
  435. "cape with existing username": {
  436. url: "http://localhost:3000/capes/jeb_",
  437. etag: '"3f688e0e69"',
  438. crc32: [989800403, 1901140141]
  439. },
  440. "cape with non-existent username": {
  441. url: "http://localhost:3000/capes/0",
  442. crc32: 0
  443. },
  444. "cape with non-existent username defaulting to url": {
  445. url: "http://localhost:3000/capes/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  446. crc32: 0,
  447. redirect: "http://example.com/CaseSensitive"
  448. },
  449. "cape with existing uuid": {
  450. url: "http://localhost:3000/capes/853c80ef3c3749fdaa49938b674adae6",
  451. etag: '"3f688e0e69"',
  452. crc32: [989800403, 1901140141]
  453. },
  454. "cape with non-existent uuid": {
  455. url: "http://localhost:3000/capes/00000000000000000000000000000000",
  456. crc32: 0
  457. },
  458. "cape with non-existent uuid defaulting to url": {
  459. url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  460. crc32: 0,
  461. redirect: "http://example.com/CaseSensitive"
  462. },
  463. "skin with existing username": {
  464. url: "http://localhost:3000/skins/jeb_",
  465. etag: '"a846b82963"',
  466. crc32: 26500336
  467. },
  468. "skin with non-existent username": {
  469. url: "http://localhost:3000/skins/0",
  470. etag: '"mhf_steve"',
  471. crc32: 981937087
  472. },
  473. "skin with non-existent username defaulting to alex": {
  474. url: "http://localhost:3000/skins/0?default=mhf_alex",
  475. etag: '"mhf_alex"',
  476. crc32: 2298915739
  477. },
  478. "skin with non-existent username defaulting to username": {
  479. url: "http://localhost:3000/skins/0?size=16&default=jeb_",
  480. crc32: 0,
  481. redirect: "/skins/jeb_?size=16"
  482. },
  483. "skin with non-existent username defaulting to uuid": {
  484. url: "http://localhost:3000/skins/0?size=16&default=853c80ef3c3749fdaa49938b674adae6",
  485. crc32: 0,
  486. redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16"
  487. },
  488. "skin with non-existent username defaulting to url": {
  489. url: "http://localhost:3000/skins/0?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  490. crc32: 0,
  491. redirect: "http://example.com/CaseSensitive"
  492. },
  493. "skin with existing uuid": {
  494. url: "http://localhost:3000/skins/853c80ef3c3749fdaa49938b674adae6",
  495. etag: '"a846b82963"',
  496. crc32: 26500336
  497. },
  498. "skin with non-existent uuid": {
  499. url: "http://localhost:3000/skins/00000000000000000000000000000000",
  500. etag: '"mhf_steve"',
  501. crc32: 981937087
  502. },
  503. "skin with non-existent uuid defaulting to alex": {
  504. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=mhf_alex",
  505. etag: '"mhf_alex"',
  506. crc32: 2298915739
  507. },
  508. "skin with non-existent uuid defaulting to username": {
  509. url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=jeb_",
  510. crc32: 0,
  511. redirect: "/skins/jeb_?size=16"
  512. },
  513. "skin with non-existent uuid defaulting to uuid": {
  514. url: "http://localhost:3000/skins/00000000000000000000000000000000?size=16&default=853c80ef3c3749fdaa49938b674adae6",
  515. crc32: 0,
  516. redirect: "/skins/853c80ef3c3749fdaa49938b674adae6?size=16"
  517. },
  518. "skin with non-existent uuid defaulting to url": {
  519. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  520. crc32: 0,
  521. redirect: "http://example.com/CaseSensitive"
  522. },
  523. "head render with existing username": {
  524. url: "http://localhost:3000/renders/head/jeb_?scale=2",
  525. etag: '"a846b82963"',
  526. crc32: [1743362302, 208074514]
  527. },
  528. "head render with non-existent username": {
  529. url: "http://localhost:3000/renders/head/0?scale=2",
  530. etag: '"mhf_steve"',
  531. crc32: [897270661, 1026982335]
  532. },
  533. "head render with non-existent username defaulting to alex": {
  534. url: "http://localhost:3000/renders/head/0?scale=2&default=mhf_alex",
  535. etag: '"mhf_alex"',
  536. crc32: [2357619670, 3172866498]
  537. },
  538. "head render with non-existent username defaulting to username": {
  539. url: "http://localhost:3000/avatars/0?scale=2&default=jeb_",
  540. crc32: 0,
  541. redirect: "/avatars/jeb_?scale=2"
  542. },
  543. "head render with non-existent username defaulting to uuid": {
  544. url: "http://localhost:3000/avatars/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
  545. crc32: 0,
  546. redirect: "/avatars/853c80ef3c3749fdaa49938b674adae6?scale=2"
  547. },
  548. "head render with non-existent username defaulting to url": {
  549. url: "http://localhost:3000/renders/head/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  550. crc32: 0,
  551. redirect: "http://example.com/CaseSensitive"
  552. },
  553. "helm head render with existing username": {
  554. url: "http://localhost:3000/renders/head/jeb_?scale=2&helm",
  555. etag: '"a846b82963"',
  556. crc32: [4178514320, 2340078566]
  557. },
  558. "helm head render with non-existent username": {
  559. url: "http://localhost:3000/renders/head/0?scale=2&helm",
  560. etag: '"mhf_steve"',
  561. crc32: [507497693, 3868868707]
  562. },
  563. "helm head render with non-existent username defaulting to alex": {
  564. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=mhf_alex",
  565. etag: '"mhf_alex"',
  566. crc32: [891113664, 1785326216]
  567. },
  568. "helm head render with non-existent username defaulting to username": {
  569. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=jeb_",
  570. crc32: 0,
  571. redirect: "/renders/head/jeb_?scale=2&helm="
  572. },
  573. "helm head render with non-existent username defaulting to uuid": {
  574. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=853c80ef3c3749fdaa49938b674adae6",
  575. crc32: 0,
  576. redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&helm="
  577. },
  578. "helm head render with non-existent username defaulting to url": {
  579. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  580. crc32: 0,
  581. redirect: "http://example.com/CaseSensitive"
  582. },
  583. "head render with existing uuid": {
  584. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2",
  585. etag: '"a846b82963"',
  586. crc32: [1743362302, 208074514]
  587. },
  588. "head render with non-existent uuid": {
  589. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2",
  590. etag: '"mhf_steve"',
  591. crc32: [897270661, 1026982335]
  592. },
  593. "head render with non-existent uuid defaulting to alex": {
  594. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=mhf_alex",
  595. etag: '"mhf_alex"',
  596. crc32: [2357619670, 3172866498]
  597. },
  598. "head render with non-existent uuid defaulting to username": {
  599. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=jeb_",
  600. crc32: 0,
  601. redirect: "/renders/head/jeb_?scale=2"
  602. },
  603. "head render with non-existent uuid defaulting to uuid": {
  604. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
  605. crc32: 0,
  606. redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2"
  607. },
  608. "head render with non-existent uuid defaulting to url": {
  609. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  610. crc32: 0,
  611. redirect: "http://example.com/CaseSensitive"
  612. },
  613. "helm head render with existing uuid": {
  614. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  615. etag: '"a846b82963"',
  616. crc32: [4178514320, 2340078566]
  617. },
  618. "helm head render with non-existent uuid": {
  619. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm",
  620. etag: '"mhf_steve"',
  621. crc32: [507497693, 3868868707]
  622. },
  623. "helm head render with non-existent uuid defaulting to alex": {
  624. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=mhf_alex",
  625. etag: '"mhf_alex"',
  626. crc32: [891113664, 1785326216]
  627. },
  628. "helm head with non-existent uuid defaulting to username": {
  629. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=jeb_",
  630. crc32: 0,
  631. redirect: "/renders/head/jeb_?scale=2&helm="
  632. },
  633. "helm head with non-existent uuid defaulting to uuid": {
  634. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=853c80ef3c3749fdaa49938b674adae6",
  635. crc32: 0,
  636. redirect: "/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&helm="
  637. },
  638. "helm head render with non-existent uuid defaulting to url": {
  639. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  640. crc32: 0,
  641. redirect: "http://example.com/CaseSensitive"
  642. },
  643. "body render with existing username": {
  644. url: "http://localhost:3000/renders/body/jeb_?scale=2",
  645. etag: '"a846b82963"',
  646. crc32: [1023392610, 4127764743]
  647. },
  648. "body render with non-existent username": {
  649. url: "http://localhost:3000/renders/body/0?scale=2",
  650. etag: '"mhf_steve"',
  651. crc32: [3559591930, 3663447404]
  652. },
  653. "body render with non-existent username defaulting to alex": {
  654. url: "http://localhost:3000/renders/body/0?scale=2&default=mhf_alex",
  655. etag: '"mhf_alex"',
  656. crc32: [470529151, 1823026927]
  657. },
  658. "body render with non-existent username defaulting to username": {
  659. url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_",
  660. crc32: 0,
  661. redirect: "/renders/body/jeb_?scale=2"
  662. },
  663. "body render with non-existent username defaulting to uuid": {
  664. url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
  665. crc32: 0,
  666. redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2"
  667. },
  668. "body render with non-existent username defaulting to url": {
  669. url: "http://localhost:3000/renders/body/0?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  670. crc32: 0,
  671. redirect: "http://example.com/CaseSensitive"
  672. },
  673. "helm body render with existing username": {
  674. url: "http://localhost:3000/renders/body/jeb_?scale=2&helm",
  675. etag: '"a846b82963"',
  676. crc32: [3476579592, 97705180]
  677. },
  678. "helm body render with non-existent username": {
  679. url: "http://localhost:3000/renders/body/0?scale=2&helm",
  680. etag: '"mhf_steve"',
  681. crc32: [3992841063, 1025743887]
  682. },
  683. "helm body render with non-existent username defaulting to alex": {
  684. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=mhf_alex",
  685. etag: '"mhf_alex"',
  686. crc32: [3317518715, 3621585514]
  687. },
  688. "helm body render with non-existent username defaulting to username": {
  689. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=jeb_",
  690. crc32: 0,
  691. redirect: "/renders/body/jeb_?scale=2&helm="
  692. },
  693. "helm body render with non-existent username defaulting to uuid": {
  694. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=853c80ef3c3749fdaa49938b674adae6",
  695. crc32: 0,
  696. redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&helm="
  697. },
  698. "helm body render with non-existent username defaulting to url": {
  699. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  700. crc32: 0,
  701. redirect: "http://example.com/CaseSensitive"
  702. },
  703. "body render with existing uuid": {
  704. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2",
  705. etag: '"a846b82963"',
  706. crc32: [1023392610, 4127764743]
  707. },
  708. "body render with non-existent uuid": {
  709. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2",
  710. etag: '"mhf_steve"',
  711. crc32: [3559591930, 3663447404]
  712. },
  713. "body render with non-existent uuid defaulting to alex": {
  714. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=mhf_alex",
  715. etag: '"mhf_alex"',
  716. crc32: [470529151, 1823026927]
  717. },
  718. "body render with non-existent uuid defaulting to username": {
  719. url: "http://localhost:3000/renders/body/0?scale=2&default=jeb_",
  720. crc32: 0,
  721. redirect: "/renders/body/jeb_?scale=2"
  722. },
  723. "body render with non-existent uuid defaulting to uuid": {
  724. url: "http://localhost:3000/renders/body/0?scale=2&default=853c80ef3c3749fdaa49938b674adae6",
  725. crc32: 0,
  726. redirect: "/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2"
  727. },
  728. "body render with non-existent uuid defaulting to url": {
  729. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  730. crc32: 0,
  731. redirect: "http://example.com/CaseSensitive"
  732. },
  733. "helm body render with existing uuid": {
  734. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  735. etag: '"a846b82963"',
  736. crc32: [3476579592, 97705180]
  737. },
  738. "helm body render with non-existent uuid": {
  739. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm",
  740. etag: '"mhf_steve"',
  741. crc32: [3992841063, 1025743887]
  742. },
  743. "helm body render with non-existent uuid defaulting to alex": {
  744. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=mhf_alex",
  745. etag: '"mhf_alex"',
  746. crc32: [3317518715, 3621585514]
  747. },
  748. "helm body render with non-existent uuid defaulting to url": {
  749. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=http%3A%2F%2Fexample.com%2FCaseSensitive",
  750. crc32: 0,
  751. redirect: "http://example.com/CaseSensitive"
  752. },
  753. };
  754. for (var description in server_tests) {
  755. var loc = server_tests[description];
  756. (function(location) {
  757. it("should return correct HTTP response for " + description, function(done) {
  758. request.get(location.url, {followRedirect: false, encoding: null}, function(error, res, body) {
  759. assert.ifError(error);
  760. assert_headers(res);
  761. assert(res.headers["x-storage-type"]);
  762. assert.strictEqual(res.headers.etag, location.etag);
  763. var matches = false;
  764. if (location.crc32 instanceof Array) {
  765. for (var i = 0; i < location.crc32.length; i++) {
  766. if (location.crc32[i] === crc(body)) {
  767. matches = true;
  768. break;
  769. }
  770. }
  771. } else {
  772. matches = (location.crc32 === crc(body));
  773. }
  774. try {
  775. assert.ok(matches);
  776. } catch(e) {
  777. throw new Error(crc(body) + " != " + location.crc32);
  778. }
  779. assert.strictEqual(res.headers.location, location.redirect);
  780. if (location.etag === undefined) {
  781. assert.strictEqual(res.statusCode, location.redirect ? 307 : 404);
  782. assert.strictEqual(res.headers["content-type"], "text/plain");
  783. done();
  784. } else {
  785. assert(res.headers.etag);
  786. assert.strictEqual(res.headers["content-type"], "image/png");
  787. assert.strictEqual(res.statusCode, 200);
  788. assert_cache(location.url, res.headers.etag, function() {
  789. done();
  790. });
  791. }
  792. });
  793. });
  794. }(loc));
  795. }
  796. it("should return a 422 (invalid size)", function(done) {
  797. var size = config.avatars.max_size + 1;
  798. request.get("http://localhost:3000/avatars/Jake_0?size=" + size, function(error, res, body) {
  799. assert.ifError(error);
  800. assert.strictEqual(res.statusCode, 422);
  801. done();
  802. });
  803. });
  804. it("should return a 422 (invalid scale)", function(done) {
  805. var scale = config.renders.max_scale + 1;
  806. request.get("http://localhost:3000/renders/head/Jake_0?scale=" + scale, function(error, res, body) {
  807. assert.ifError(error);
  808. assert.strictEqual(res.statusCode, 422);
  809. done();
  810. });
  811. });
  812. it("should return a 422 (invalid render type)", function(done) {
  813. request.get("http://localhost:3000/renders/invalid/Jake_0", function(error, res, body) {
  814. assert.ifError(error);
  815. assert.strictEqual(res.statusCode, 422);
  816. done();
  817. });
  818. });
  819. // testing all paths for Invalid UserID
  820. var locations = ["avatars", "skins", "capes", "renders/body", "renders/head"];
  821. for (var l in locations) {
  822. loc = locations[l];
  823. (function(location) {
  824. it("should return a 422 (invalid id " + location + ")", function(done) {
  825. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  826. assert.ifError(error);
  827. assert.strictEqual(res.statusCode, 422);
  828. done();
  829. });
  830. });
  831. it("should return a 404 (invalid path " + location + ")", function(done) {
  832. request.get("http://localhost:3000/" + location + "/853c80ef3c3749fdaa49938b674adae6/invalid", function(error, res, body) {
  833. assert.ifError(error);
  834. assert.strictEqual(res.statusCode, 404);
  835. done();
  836. });
  837. });
  838. }(loc));
  839. }
  840. after(function(done) {
  841. server.close(function() {
  842. done();
  843. });
  844. });
  845. });
  846. // we have to make sure that we test both a 32x64 and 64x64 skin
  847. describe("Networking: Render", function() {
  848. it("should not fail (username, 32x64 skin)", function(done) {
  849. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  850. assert.strictEqual(err, null);
  851. done();
  852. });
  853. });
  854. it("should not fail (username, 64x64 skin)", function(done) {
  855. helpers.get_render(rid, "Jake_0", 6, true, true, function(err, hash, img) {
  856. assert.strictEqual(err, null);
  857. done();
  858. });
  859. });
  860. });
  861. describe("Networking: Cape", function() {
  862. it("should not fail (guaranteed cape)", function(done) {
  863. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  864. assert.strictEqual(err, null);
  865. done();
  866. });
  867. });
  868. it("should already exist", function(done) {
  869. before(function() {
  870. cache.get_redis().flushall();
  871. });
  872. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  873. assert.strictEqual(err, null);
  874. done();
  875. });
  876. });
  877. it("should not be found", function(done) {
  878. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  879. assert.ifError(err);
  880. assert.strictEqual(img, null);
  881. done();
  882. });
  883. });
  884. });
  885. describe("Networking: Skin", function() {
  886. it("should not fail", function(done) {
  887. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  888. assert.strictEqual(err, null);
  889. done();
  890. });
  891. });
  892. it("should already exist", function(done) {
  893. before(function() {
  894. cache.get_redis().flushall();
  895. });
  896. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  897. assert.strictEqual(err, null);
  898. done();
  899. });
  900. });
  901. });
  902. // DRY with uuid and username tests
  903. for (var i in ids) {
  904. var iid = ids[i];
  905. var iid_type = iid.length > 16 ? "uuid" : "name";
  906. // needs an anonymous function because id and id_type aren't constant
  907. (function(id, id_type) {
  908. describe("Networking: Avatar", function() {
  909. before(function() {
  910. cache.get_redis().flushall();
  911. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  912. });
  913. it("should be downloaded", function(done) {
  914. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  915. assert.ifError(err);
  916. assert.strictEqual(status, 2);
  917. done();
  918. });
  919. });
  920. it("should be cached", function(done) {
  921. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  922. assert.ifError(err);
  923. assert.strictEqual(status === 0 || status === 1, true);
  924. done();
  925. });
  926. });
  927. if (id.length > 16) {
  928. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  929. } else {
  930. it("should be checked", function(done) {
  931. var original_cache_time = config.caching.local;
  932. config.caching.local = 0;
  933. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  934. assert.ifError(err);
  935. assert.strictEqual(status, 3);
  936. config.caching.local = original_cache_time;
  937. done();
  938. });
  939. });
  940. }
  941. });
  942. describe("Networking: Skin", function() {
  943. it("should not fail (uuid)", function(done) {
  944. helpers.get_skin(rid, id, function(err, hash, status, img) {
  945. assert.strictEqual(err, null);
  946. done();
  947. });
  948. });
  949. });
  950. describe("Networking: Render", function() {
  951. it("should not fail (full body)", function(done) {
  952. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  953. assert.ifError(err);
  954. done();
  955. });
  956. });
  957. it("should not fail (only head)", function(done) {
  958. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  959. assert.ifError(err);
  960. done();
  961. });
  962. });
  963. });
  964. describe("Networking: Cape", function() {
  965. it("should not fail (possible cape)", function(done) {
  966. helpers.get_cape(rid, id, function(err, hash, status, img) {
  967. assert.ifError(err);
  968. done();
  969. });
  970. });
  971. });
  972. describe("Errors", function() {
  973. before(function() {
  974. cache.get_redis().flushall();
  975. });
  976. if (id_type === "uuid") {
  977. it("uuid should be rate limited", function(done) {
  978. networking.get_profile(rid, id, function() {
  979. networking.get_profile(rid, id, function(err, profile) {
  980. assert.ifError(err);
  981. assert.strictEqual(profile, null);
  982. done();
  983. });
  984. });
  985. });
  986. } else {
  987. it("username should NOT be rate limited (username)", function(done) {
  988. helpers.get_avatar(rid, id, false, 160, function() {
  989. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  990. assert.strictEqual(err, null);
  991. done();
  992. });
  993. });
  994. });
  995. }
  996. });
  997. }(iid, iid_type));
  998. }
  999. });