index.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* eslint no-console: 0 */
  2. /**
  3. * Test Runner
  4. *
  5. * For running the tests in the test register.
  6. *
  7. * @author tlwr [toby@toby.codes]
  8. * @author n1474335 [n1474335@gmail.com]
  9. * @copyright Crown Copyright 2017
  10. * @license Apache-2.0
  11. */
  12. // Define global environment functions
  13. global.ENVIRONMENT_IS_WORKER = function() {
  14. return typeof importScripts === "function";
  15. };
  16. global.ENVIRONMENT_IS_NODE = function() {
  17. return typeof process === "object" && typeof require === "function";
  18. };
  19. global.ENVIRONMENT_IS_WEB = function() {
  20. return typeof window === "object";
  21. };
  22. import TestRegister from "./TestRegister";
  23. import "./tests/BCD";
  24. import "./tests/BSON";
  25. import "./tests/Base58";
  26. import "./tests/Base64";
  27. import "./tests/Base62";
  28. import "./tests/BitwiseOp";
  29. import "./tests/ByteRepr";
  30. import "./tests/CartesianProduct";
  31. import "./tests/CharEnc";
  32. import "./tests/Checksum";
  33. import "./tests/Ciphers";
  34. import "./tests/Code";
  35. import "./tests/Comment";
  36. import "./tests/Compress";
  37. import "./tests/ConditionalJump";
  38. import "./tests/Crypt";
  39. import "./tests/CSV";
  40. import "./tests/DateTime";
  41. import "./tests/ExtractEmailAddresses";
  42. import "./tests/Fork";
  43. import "./tests/FromDecimal";
  44. import "./tests/Hash";
  45. import "./tests/HaversineDistance";
  46. import "./tests/Hexdump";
  47. import "./tests/Image";
  48. import "./tests/Jump";
  49. import "./tests/JSONBeautify";
  50. import "./tests/JSONMinify";
  51. import "./tests/JWTDecode";
  52. import "./tests/JWTSign";
  53. import "./tests/JWTVerify";
  54. import "./tests/MS";
  55. import "./tests/Magic";
  56. import "./tests/MorseCode";
  57. import "./tests/NetBIOS";
  58. import "./tests/OTP";
  59. import "./tests/PGP";
  60. import "./tests/PHP";
  61. import "./tests/ParseIPRange";
  62. import "./tests/ParseQRCode";
  63. import "./tests/PowerSet";
  64. import "./tests/Regex";
  65. import "./tests/Register";
  66. import "./tests/RemoveDiacritics";
  67. import "./tests/Rotate";
  68. import "./tests/SeqUtils";
  69. import "./tests/SetDifference";
  70. import "./tests/SetIntersection";
  71. import "./tests/SetUnion";
  72. import "./tests/StrUtils";
  73. import "./tests/SymmetricDifference";
  74. import "./tests/TextEncodingBruteForce";
  75. import "./tests/TranslateDateTimeFormat";
  76. import "./tests/Magic";
  77. import "./tests/ParseTLV";
  78. import "./tests/Media";
  79. import "./tests/ToFromInsensitiveRegex";
  80. import "./tests/YARA.mjs";
  81. import "./tests/ConvertCoordinateFormat";
  82. import "./tests/Enigma";
  83. import "./tests/Bombe";
  84. import "./tests/MultipleBombe";
  85. import "./tests/Typex";
  86. import "./tests/BLAKE2b";
  87. import "./tests/BLAKE2s";
  88. // Cannot test operations that use the File type yet
  89. //import "./tests/SplitColourChannels";
  90. let allTestsPassing = true;
  91. const testStatusCounts = {
  92. total: 0,
  93. };
  94. /**
  95. * Helper function to convert a status to an icon.
  96. *
  97. * @param {string} status
  98. * @returns {string}
  99. */
  100. function statusToIcon(status) {
  101. const icons = {
  102. erroring: "🔥",
  103. failing: "❌",
  104. passing: "✔️️",
  105. };
  106. return icons[status] || "?";
  107. }
  108. /**
  109. * Displays a given test result in the console.
  110. *
  111. * @param {Object} testResult
  112. */
  113. function handleTestResult(testResult) {
  114. allTestsPassing = allTestsPassing && testResult.status === "passing";
  115. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  116. testStatusCounts[testResult.status] = newCount;
  117. testStatusCounts.total += 1;
  118. console.log([
  119. statusToIcon(testResult.status),
  120. testResult.test.name
  121. ].join(" "));
  122. if (testResult.output) {
  123. console.log(
  124. testResult.output
  125. .trim()
  126. .replace(/^/, "\t")
  127. .replace(/\n/g, "\n\t")
  128. );
  129. }
  130. }
  131. /**
  132. * Fail if the process takes longer than 60 seconds.
  133. */
  134. setTimeout(function() {
  135. console.log("Tests took longer than 60 seconds to run, returning.");
  136. process.exit(1);
  137. }, 60 * 1000);
  138. TestRegister.runTests()
  139. .then(function(results) {
  140. results.forEach(handleTestResult);
  141. console.log("\n");
  142. for (const testStatus in testStatusCounts) {
  143. const count = testStatusCounts[testStatus];
  144. if (count > 0) {
  145. console.log(testStatus.toUpperCase(), count);
  146. }
  147. }
  148. if (!allTestsPassing) {
  149. console.log("\nFailing tests:\n");
  150. results.filter(r => r.status !== "passing").forEach(handleTestResult);
  151. }
  152. process.exit(allTestsPassing ? 0 : 1);
  153. });