ops.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* eslint no-console: 0 */
  2. /**
  3. * nodeApi.js
  4. *
  5. * Test node api operations
  6. *
  7. * Aim of these tests is to ensure each arg type is
  8. * handled correctly by the wrapper.
  9. *
  10. * @author d98762625 [d98762625@gmail.com]
  11. * @copyright Crown Copyright 2018
  12. * @license Apache-2.0
  13. */
  14. import assert from "assert";
  15. import it from "../assertionHandler";
  16. import {
  17. ADD,
  18. addLineNumbers,
  19. adler32Checksum,
  20. AESDecrypt,
  21. affineCipherDecode,
  22. affineCipherEncode,
  23. bifidCipherEncode,
  24. bitShiftRight,
  25. cartesianProduct,
  26. CSSMinify,
  27. toBase64,
  28. toHex,
  29. } from "../../../src/node/index";
  30. import TestRegister from "../../TestRegister";
  31. TestRegister.addApiTests([
  32. it("ADD: toggleString argument", () => {
  33. const result = ADD("sample input", {
  34. key: {
  35. string: "some key",
  36. option: "Hex"
  37. }
  38. });
  39. assert.equal(result.toString(), "aO[^ZS\u000eW\\^cb");
  40. }),
  41. it("addLineNumbers: No arguments", () => {
  42. const result = addLineNumbers("sample input");
  43. assert.equal(result.toString(), "1 sample input");
  44. }),
  45. it("adler32Checksum: No args", () => {
  46. const result = adler32Checksum("sample input");
  47. assert.equal(result.toString(), "1f2304d3");
  48. }),
  49. it("AES decrypt: toggleString and option", () => {
  50. const result = AESDecrypt("812c34ae6af353244a63c6ce23b7c34286b60be28ea4645523d4494700e7", {
  51. key: {
  52. string: "some longer key1",
  53. option: "utf8",
  54. },
  55. iv: {
  56. string: "some iv",
  57. option: "utf8",
  58. },
  59. mode: "OFB",
  60. });
  61. assert.equal(result.toString(), "a slightly longer sampleinput?");
  62. }),
  63. it("AffineCipherDecode: number input", () => {
  64. const result = affineCipherDecode("some input", {
  65. a: 7,
  66. b: 4
  67. });
  68. assert.strictEqual(result.toString(), "cuqa ifjgr");
  69. }),
  70. it("affineCipherEncode: number input", () => {
  71. const result = affineCipherEncode("some input", {
  72. a: 11,
  73. b: 6
  74. });
  75. assert.strictEqual(result.toString(), "weiy qtpsh");
  76. }),
  77. it("bifid cipher encode: string option", () => {
  78. const result = bifidCipherEncode("some input", {
  79. keyword: "mykeyword",
  80. });
  81. assert.strictEqual(result.toString(), "nmhs zmsdo");
  82. }),
  83. it("bitShiftRight: number and option", () => {
  84. const result = bitShiftRight("some bits to shift", {
  85. type: "Arithmetic shift",
  86. amount: 1,
  87. });
  88. assert.strictEqual(result.toString(), "9762\u001014:9\u0010:7\u00109443:");
  89. }),
  90. it("cartesianProduct: binary string", () => {
  91. const result = cartesianProduct("1:2\\n\\n3:4", {
  92. itemDelimiter: ":",
  93. });
  94. assert.strictEqual(result.toString(), "(1,3):(1,4):(2,3):(2,4)");
  95. }),
  96. it("CSS minify: boolean", () => {
  97. const input = `header {
  98. // comment
  99. width: 100%;
  100. color: white;
  101. }`;
  102. const result = CSSMinify(input, {
  103. preserveComments: true,
  104. });
  105. assert.strictEqual(result.toString(), "header {// comment width: 100%;color: white;}");
  106. }),
  107. it("toBase64: editableOption", () => {
  108. const result = toBase64("some input", {
  109. alphabet: {
  110. value: "0-9A-W"
  111. },
  112. });
  113. assert.strictEqual(result.toString(), "SPI1R1T0");
  114. }),
  115. it("toBase64: editableOptions key is value", () => {
  116. const result = toBase64("some input", {
  117. alphabet: "0-9A-W",
  118. });
  119. assert.strictEqual(result.toString(), "SPI1R1T0");
  120. }),
  121. it("toBase64: editableOptions default", () => {
  122. const result = toBase64("some input");
  123. assert.strictEqual(result.toString(), "c29tZSBpbnB1dA==");
  124. }),
  125. it("toHex: accepts args", () => {
  126. const result = toHex("some input", {
  127. delimiter: "Colon",
  128. });
  129. assert.strictEqual(result.toString(), "73:6f:6d:65:20:69:6e:70:75:74");
  130. })
  131. ]);