test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 cleaner = require("../modules/cleaner");
  11. // we don't want tests to fail because of slow internet
  12. config.http_timeout *= 3;
  13. // no spam
  14. logging.log = function() {};
  15. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  16. var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  17. // Get a random UUID + name in order to prevent rate limiting
  18. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  19. var name = names[Math.round(Math.random() * (names.length - 1))];
  20. var rid = "TestReqID: ";
  21. function getRandomInt(min, max) {
  22. return Math.floor(Math.random() * (max - min + 1)) + min;
  23. }
  24. var ids = [
  25. uuid.toLowerCase(),
  26. name.toLowerCase(),
  27. uuid.toUpperCase(),
  28. name.toUpperCase()
  29. ];
  30. describe("Crafatar", function() {
  31. // we might have to make 2 HTTP requests
  32. this.timeout(config.http_timeout * 2 + 50);
  33. before(function() {
  34. cache.get_redis().flushall();
  35. cleaner.run();
  36. });
  37. describe("UUID/username", function() {
  38. it("non-hex uuid is invalid", function(done) {
  39. assert.strictEqual(helpers.id_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  40. done();
  41. });
  42. it("empty id is invalid", function(done) {
  43. assert.strictEqual(helpers.id_valid(""), false);
  44. done();
  45. });
  46. it("non-alphanumeric username is invalid", function(done) {
  47. assert.strictEqual(helpers.id_valid("usernäme"), false);
  48. done();
  49. });
  50. it("dashed username is invalid", function(done) {
  51. assert.strictEqual(helpers.id_valid("user-name"), false);
  52. done();
  53. });
  54. it(">16 length username is invalid", function(done) {
  55. assert.strictEqual(helpers.id_valid("ThisNameIsTooLong"), false);
  56. done();
  57. });
  58. it("lowercase uuid is valid", function(done) {
  59. assert.strictEqual(helpers.id_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  60. done();
  61. });
  62. it("uppercase uuid is valid", function(done) {
  63. assert.strictEqual(helpers.id_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  64. done();
  65. });
  66. it("dashed uuid is valid", function(done) {
  67. assert.strictEqual(helpers.id_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  68. done();
  69. });
  70. it("16 chars, underscored, capital, numbered username is valid", function(done) {
  71. assert.strictEqual(helpers.id_valid("__niceUs3rname__"), true);
  72. done();
  73. });
  74. it("1 char username is valid", function(done) {
  75. assert.strictEqual(helpers.id_valid("a"), true);
  76. done();
  77. });
  78. it("should not exist (uuid)", function(done) {
  79. var number = getRandomInt(0, 9).toString();
  80. networking.get_profile(rid, Array(33).join(number), function(err, profile) {
  81. assert.strictEqual(profile, null);
  82. done();
  83. });
  84. });
  85. it("should not exist (username)", function(done) {
  86. networking.get_username_url(rid, "Steve", 0, function(err, profile) {
  87. assert.strictEqual(err, null);
  88. done();
  89. });
  90. });
  91. });
  92. describe("Avatar", function() {
  93. // profile "Alex" - hoping it'll never have a skin
  94. var alex_uuid = "ec561538f3fd461daff5086b22154bce";
  95. // profile "Steven" (Steve doesn't exist) - hoping it'll never have a skin
  96. var steven_uuid = "b8ffc3d37dbf48278f69475f6690aabd";
  97. it("uuid's account should exist, but skin should not", function(done) {
  98. networking.get_profile(rid, alex_uuid, function(err, profile) {
  99. assert.notStrictEqual(profile, null);
  100. networking.get_uuid_url(profile, 1, function(url) {
  101. assert.strictEqual(url, null);
  102. done();
  103. });
  104. });
  105. });
  106. it("odd UUID should default to Alex", function(done) {
  107. assert.strictEqual(skins.default_skin(alex_uuid), "alex");
  108. done();
  109. });
  110. it("even UUID should default to Steve", function(done) {
  111. assert.strictEqual(skins.default_skin(steven_uuid), "steve");
  112. done();
  113. });
  114. });
  115. describe("Errors", function() {
  116. it("should time out on uuid info download", function(done) {
  117. var original_timeout = config.http_timeout;
  118. config.http_timeout = 1;
  119. networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  120. assert.strictEqual(err.code, "ETIMEDOUT");
  121. config.http_timeout = original_timeout;
  122. done();
  123. });
  124. });
  125. it("should time out on username info download", function(done) {
  126. var original_timeout = config.http_timeout;
  127. config.http_timeout = 1;
  128. networking.get_username_url(rid, "jomo", 0, function(err, url) {
  129. assert.strictEqual(err.code, "ETIMEDOUT");
  130. config.http_timeout = original_timeout;
  131. done();
  132. });
  133. });
  134. it("should time out on skin download", function(done) {
  135. var original_timeout = config.http_timeout;
  136. config.http_timeout = 1;
  137. networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) {
  138. assert.strictEqual(error.code, "ETIMEDOUT");
  139. config.http_timeout = original_timeout;
  140. done();
  141. });
  142. });
  143. it("should not find the skin", function(done) {
  144. assert.doesNotThrow(function() {
  145. networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) {
  146. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  147. done();
  148. });
  149. });
  150. });
  151. it("should ignore file updates on invalid files", function(done) {
  152. assert.doesNotThrow(function() {
  153. cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", function(err) {
  154. done();
  155. });
  156. });
  157. });
  158. it("should not find the file", function(done) {
  159. skins.open_skin(rid, 'non/existant/path', function(err, img) {
  160. assert.notStrictEqual(err, null);
  161. done();
  162. });
  163. });
  164. });
  165. // we have to make sure that we test both a 32x64 and 64x64 skin
  166. describe("Networking: Render", function() {
  167. it("should not fail (username, 32x64 skin)", function(done) {
  168. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  169. assert.strictEqual(err, null);
  170. done();
  171. });
  172. });
  173. it("should not fail (username, 64x64 skin)", function(done) {
  174. helpers.get_render(rid, "Jake0oo0", 6, true, true, function(err, hash, img) {
  175. assert.strictEqual(err, null);
  176. done();
  177. });
  178. });
  179. });
  180. describe("Networking: Cape", function() {
  181. it("should not fail (guaranteed cape)", function(done) {
  182. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  183. assert.strictEqual(err, null);
  184. done();
  185. });
  186. });
  187. it("should already exist", function(done) {
  188. before(function() {
  189. cache.get_redis().flushall();
  190. });
  191. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  192. assert.strictEqual(err, null);
  193. done();
  194. });
  195. });
  196. it("should not be found", function(done) {
  197. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  198. assert.strictEqual(img, null);
  199. done();
  200. });
  201. });
  202. });
  203. describe("Networking: Skin", function() {
  204. it("should not fail", function(done) {
  205. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  206. assert.strictEqual(err, null);
  207. done();
  208. });
  209. });
  210. it("should already exist", function(done) {
  211. before(function() {
  212. cache.get_redis().flushall();
  213. });
  214. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  215. assert.strictEqual(err, null);
  216. done();
  217. });
  218. });
  219. });
  220. // DRY with uuid and username tests
  221. for (var i in ids) {
  222. var id = ids[i];
  223. var id_type = id.length > 16 ? "uuid" : "name";
  224. // needs an anonymous function because id and id_type aren't constant
  225. (function(id, id_type) {
  226. describe("Networking: Avatar", function() {
  227. before(function() {
  228. cache.get_redis().flushall();
  229. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  230. });
  231. it("should be downloaded", function(done) {
  232. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  233. assert.strictEqual(status, 2);
  234. done();
  235. });
  236. });
  237. it("should be cached", function(done) {
  238. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  239. assert.strictEqual(status === 0 || status === 1, true);
  240. done();
  241. });
  242. });
  243. if (id.length > 16) {
  244. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  245. } else {
  246. it("should be checked", function(done) {
  247. var original_cache_time = config.local_cache_time;
  248. config.local_cache_time = 0;
  249. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  250. assert.strictEqual(status, 3);
  251. config.local_cache_time = original_cache_time;
  252. done();
  253. });
  254. });
  255. }
  256. });
  257. describe("Networking: Skin", function() {
  258. it("should not fail (uuid)", function(done) {
  259. helpers.get_skin(rid, id, function(err, hash, img) {
  260. assert.strictEqual(err, null);
  261. done();
  262. });
  263. });
  264. });
  265. describe("Networking: Render", function() {
  266. it("should not fail (full body)", function(done) {
  267. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  268. assert.strictEqual(err, null);
  269. done();
  270. });
  271. });
  272. it("should not fail (only head)", function(done) {
  273. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  274. assert.strictEqual(err, null);
  275. done();
  276. });
  277. });
  278. });
  279. describe("Networking: Cape", function() {
  280. it("should not fail (possible cape)", function(done) {
  281. helpers.get_cape(rid, id, function(err, hash, img) {
  282. assert.strictEqual(err, null);
  283. done();
  284. });
  285. });
  286. });
  287. describe("Errors", function() {
  288. before(function() {
  289. cache.get_redis().flushall();
  290. });
  291. if (id_type == "uuid") {
  292. it("uuid should be rate limited", function(done) {
  293. networking.get_profile(rid, id, function(err, profile) {
  294. assert.strictEqual(profile.error, "TooManyRequestsException");
  295. done();
  296. });
  297. });
  298. } else {
  299. it("username should NOT be rate limited (username)", function(done) {
  300. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  301. assert.strictEqual(err, null);
  302. done();
  303. });
  304. });
  305. }
  306. });
  307. })(id, id_type);
  308. }
  309. });