index.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import "babel-polyfill";
  13. // Define global environment functions
  14. global.ENVIRONMENT_IS_WORKER = function() {
  15. return typeof importScripts === "function";
  16. };
  17. global.ENVIRONMENT_IS_NODE = function() {
  18. return typeof process === "object" && typeof require === "function";
  19. };
  20. global.ENVIRONMENT_IS_WEB = function() {
  21. return typeof window === "object";
  22. };
  23. import TestRegister from "./TestRegister";
  24. import "./tests/BCD";
  25. import "./tests/BSON";
  26. import "./tests/Base58";
  27. import "./tests/Base64";
  28. import "./tests/Base62";
  29. import "./tests/BitwiseOp";
  30. import "./tests/ByteRepr";
  31. import "./tests/CartesianProduct";
  32. import "./tests/CharEnc";
  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/FromGeohash";
  46. import "./tests/Hash";
  47. import "./tests/HaversineDistance";
  48. import "./tests/Hexdump";
  49. import "./tests/Image";
  50. import "./tests/Jump";
  51. import "./tests/JSONBeautify";
  52. import "./tests/JSONMinify";
  53. import "./tests/JWTDecode";
  54. import "./tests/JWTSign";
  55. import "./tests/JWTVerify";
  56. import "./tests/MS";
  57. import "./tests/Magic";
  58. import "./tests/MorseCode";
  59. import "./tests/NetBIOS";
  60. import "./tests/OTP";
  61. import "./tests/PGP";
  62. import "./tests/PHP";
  63. import "./tests/ParseIPRange";
  64. import "./tests/ParseQRCode";
  65. import "./tests/PowerSet";
  66. import "./tests/Regex";
  67. import "./tests/Register";
  68. import "./tests/RemoveDiacritics";
  69. import "./tests/Rotate";
  70. import "./tests/SeqUtils";
  71. import "./tests/SetDifference";
  72. import "./tests/SetIntersection";
  73. import "./tests/SetUnion";
  74. import "./tests/StrUtils";
  75. import "./tests/SymmetricDifference";
  76. import "./tests/TextEncodingBruteForce";
  77. import "./tests/ToGeohash";
  78. import "./tests/TranslateDateTimeFormat";
  79. import "./tests/Magic";
  80. import "./tests/ParseTLV";
  81. import "./tests/Media";
  82. import "./tests/ToFromInsensitiveRegex";
  83. import "./tests/YARA.mjs";
  84. // Cannot test operations that use the File type yet
  85. //import "./tests/SplitColourChannels";
  86. let allTestsPassing = true;
  87. const testStatusCounts = {
  88. total: 0,
  89. };
  90. /**
  91. * Helper function to convert a status to an icon.
  92. *
  93. * @param {string} status
  94. * @returns {string}
  95. */
  96. function statusToIcon(status) {
  97. const icons = {
  98. erroring: "🔥",
  99. failing: "❌",
  100. passing: "✔️️",
  101. };
  102. return icons[status] || "?";
  103. }
  104. /**
  105. * Displays a given test result in the console.
  106. *
  107. * @param {Object} testResult
  108. */
  109. function handleTestResult(testResult) {
  110. allTestsPassing = allTestsPassing && testResult.status === "passing";
  111. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  112. testStatusCounts[testResult.status] = newCount;
  113. testStatusCounts.total += 1;
  114. console.log([
  115. statusToIcon(testResult.status),
  116. testResult.test.name
  117. ].join(" "));
  118. if (testResult.output) {
  119. console.log(
  120. testResult.output
  121. .trim()
  122. .replace(/^/, "\t")
  123. .replace(/\n/g, "\n\t")
  124. );
  125. }
  126. }
  127. /**
  128. * Fail if the process takes longer than 60 seconds.
  129. */
  130. setTimeout(function() {
  131. console.log("Tests took longer than 60 seconds to run, returning.");
  132. process.exit(1);
  133. }, 60 * 1000);
  134. TestRegister.runTests()
  135. .then(function(results) {
  136. results.forEach(handleTestResult);
  137. console.log("\n");
  138. for (const testStatus in testStatusCounts) {
  139. const count = testStatusCounts[testStatus];
  140. if (count > 0) {
  141. console.log(testStatus.toUpperCase(), count);
  142. }
  143. }
  144. if (!allTestsPassing) {
  145. console.log("\nFailing tests:\n");
  146. results.filter(r => r.status !== "passing").forEach(handleTestResult);
  147. }
  148. process.exit(allTestsPassing ? 0 : 1);
  149. });