test.js 8.5 KB

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