test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, "redstone_sheep", 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");
  154. });
  155. done();
  156. });
  157. it("should not find the file", function(done) {
  158. skins.open_skin(rid, 'non/existant/path', function(err, img) {
  159. assert.notStrictEqual(err, null);
  160. done();
  161. });
  162. });
  163. });
  164. // we have to make sure that we test both a 32x64 and 64x64 skin
  165. describe("Networking: Render", function() {
  166. it("should not fail (username, 32x64 skin)", function(done) {
  167. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  168. assert.strictEqual(err, null);
  169. done();
  170. });
  171. });
  172. it("should not fail (username, 64x64 skin)", function(done) {
  173. helpers.get_render(rid, "Jake0oo0", 6, true, true, function(err, hash, img) {
  174. assert.strictEqual(err, null);
  175. done();
  176. });
  177. });
  178. });
  179. describe("Networking: Cape", function() {
  180. it("should not fail (guaranteed cape)", function(done) {
  181. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  182. assert.strictEqual(err, null);
  183. done();
  184. });
  185. });
  186. it("should already exist", function(done) {
  187. before(function() {
  188. cache.get_redis().flushall();
  189. });
  190. helpers.get_cape(rid, "Dinnerbone", function(err, hash, img) {
  191. assert.strictEqual(err, null);
  192. done();
  193. });
  194. });
  195. it("should not be found", function(done) {
  196. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  197. assert.strictEqual(img, null);
  198. done();
  199. });
  200. });
  201. });
  202. describe("Networking: Skin", function() {
  203. it("should not fail", function(done) {
  204. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  205. assert.strictEqual(err, null);
  206. done();
  207. });
  208. });
  209. it("should already exist", function(done) {
  210. before(function() {
  211. cache.get_redis().flushall();
  212. });
  213. helpers.get_cape(rid, "Jake0oo0", function(err, hash, img) {
  214. assert.strictEqual(err, null);
  215. done();
  216. });
  217. });
  218. });
  219. // DRY with uuid and username tests
  220. for (var i in ids) {
  221. var id = ids[i];
  222. var id_type = id.length > 16 ? "uuid" : "name";
  223. // needs an anonymous function because id and id_type aren't constant
  224. (function(id, id_type) {
  225. describe("Networking: Avatar", function() {
  226. before(function() {
  227. cache.get_redis().flushall();
  228. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  229. });
  230. it("should be downloaded", function(done) {
  231. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  232. assert.strictEqual(status, 2);
  233. done();
  234. });
  235. });
  236. it("should be cached", function(done) {
  237. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  238. assert.strictEqual(status === 0 || status === 1, true);
  239. done();
  240. });
  241. });
  242. if (id.length > 16) {
  243. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  244. } else {
  245. it("should be checked", function(done) {
  246. var original_cache_time = config.local_cache_time;
  247. config.local_cache_time = 0;
  248. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  249. assert.strictEqual(status, 3);
  250. config.local_cache_time = original_cache_time;
  251. done();
  252. });
  253. });
  254. }
  255. });
  256. describe("Networking: Skin", function() {
  257. it("should not fail (uuid)", function(done) {
  258. helpers.get_skin(rid, id, function(err, hash, img) {
  259. assert.strictEqual(err, null);
  260. done();
  261. });
  262. });
  263. });
  264. describe("Networking: Render", function() {
  265. it("should not fail (full body)", function(done) {
  266. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  267. assert.strictEqual(err, null);
  268. done();
  269. });
  270. });
  271. it("should not fail (only head)", function(done) {
  272. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  273. assert.strictEqual(err, null);
  274. done();
  275. });
  276. });
  277. });
  278. describe("Networking: Cape", function() {
  279. it("should not fail (possible cape)", function(done) {
  280. helpers.get_cape(rid, id, function(err, hash, img) {
  281. assert.strictEqual(err, null);
  282. done();
  283. });
  284. });
  285. });
  286. describe("Errors", function() {
  287. before(function() {
  288. cache.get_redis().flushall();
  289. });
  290. if (id_type == "uuid") {
  291. it("uuid should be rate limited", function(done) {
  292. networking.get_profile(rid, id, function(err, profile) {
  293. assert.strictEqual(profile.error, "TooManyRequestsException");
  294. done();
  295. });
  296. });
  297. } else {
  298. it("username should NOT be rate limited (username)", function(done) {
  299. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  300. assert.strictEqual(err, null);
  301. done();
  302. });
  303. });
  304. }
  305. });
  306. })(id, id_type);
  307. }
  308. });