test.js 9.8 KB

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