index.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/Charts";
  33. import "./tests/Checksum";
  34. import "./tests/Ciphers";
  35. import "./tests/Code";
  36. import "./tests/Comment";
  37. import "./tests/Compress";
  38. import "./tests/ConditionalJump";
  39. import "./tests/Crypt";
  40. import "./tests/CSV";
  41. import "./tests/DateTime";
  42. import "./tests/ExtractEmailAddresses";
  43. import "./tests/Fork";
  44. import "./tests/FromDecimal";
  45. import "./tests/Hash";
  46. import "./tests/HaversineDistance";
  47. import "./tests/Hexdump";
  48. import "./tests/Image";
  49. import "./tests/Jump";
  50. import "./tests/JSONBeautify";
  51. import "./tests/JSONMinify";
  52. import "./tests/JWTDecode";
  53. import "./tests/JWTSign";
  54. import "./tests/JWTVerify";
  55. import "./tests/MS";
  56. import "./tests/Magic";
  57. import "./tests/MorseCode";
  58. import "./tests/NetBIOS";
  59. import "./tests/OTP";
  60. import "./tests/PGP";
  61. import "./tests/PHP";
  62. import "./tests/ParseIPRange";
  63. import "./tests/ParseQRCode";
  64. import "./tests/PowerSet";
  65. import "./tests/Regex";
  66. import "./tests/Register";
  67. import "./tests/RemoveDiacritics";
  68. import "./tests/Rotate";
  69. import "./tests/SeqUtils";
  70. import "./tests/SetDifference";
  71. import "./tests/SetIntersection";
  72. import "./tests/SetUnion";
  73. import "./tests/StrUtils";
  74. import "./tests/SymmetricDifference";
  75. import "./tests/TextEncodingBruteForce";
  76. import "./tests/TranslateDateTimeFormat";
  77. import "./tests/Magic";
  78. import "./tests/ParseTLV";
  79. import "./tests/Media";
  80. import "./tests/ToFromInsensitiveRegex";
  81. import "./tests/YARA.mjs";
  82. import "./tests/ConvertCoordinateFormat";
  83. import "./tests/Enigma";
  84. import "./tests/Bombe";
  85. import "./tests/MultipleBombe";
  86. import "./tests/Typex";
  87. import "./tests/BLAKE2b";
  88. import "./tests/BLAKE2s";
  89. import "./tests/Protobuf";
  90. // Cannot test operations that use the File type yet
  91. //import "./tests/SplitColourChannels";
  92. let allTestsPassing = true;
  93. const testStatusCounts = {
  94. total: 0,
  95. };
  96. /**
  97. * Helper function to convert a status to an icon.
  98. *
  99. * @param {string} status
  100. * @returns {string}
  101. */
  102. function statusToIcon(status) {
  103. const icons = {
  104. erroring: "🔥",
  105. failing: "❌",
  106. passing: "✔️️",
  107. };
  108. return icons[status] || "?";
  109. }
  110. /**
  111. * Displays a given test result in the console.
  112. *
  113. * @param {Object} testResult
  114. */
  115. function handleTestResult(testResult) {
  116. allTestsPassing = allTestsPassing && testResult.status === "passing";
  117. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  118. testStatusCounts[testResult.status] = newCount;
  119. testStatusCounts.total += 1;
  120. console.log([
  121. statusToIcon(testResult.status),
  122. testResult.test.name
  123. ].join(" "));
  124. if (testResult.output) {
  125. console.log(
  126. testResult.output
  127. .trim()
  128. .replace(/^/, "\t")
  129. .replace(/\n/g, "\n\t")
  130. );
  131. }
  132. }
  133. /**
  134. * Fail if the process takes longer than 60 seconds.
  135. */
  136. setTimeout(function() {
  137. console.log("Tests took longer than 60 seconds to run, returning.");
  138. process.exit(1);
  139. }, 60 * 1000);
  140. TestRegister.runTests()
  141. .then(function(results) {
  142. results.forEach(handleTestResult);
  143. console.log("\n");
  144. for (const testStatus in testStatusCounts) {
  145. const count = testStatusCounts[testStatus];
  146. if (count > 0) {
  147. console.log(testStatus.toUpperCase(), count);
  148. }
  149. }
  150. if (!allTestsPassing) {
  151. console.log("\nFailing tests:\n");
  152. results.filter(r => r.status !== "passing").forEach(handleTestResult);
  153. }
  154. process.exit(allTestsPassing ? 0 : 1);
  155. });