index.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true, jasmine:true */
  9. 'use strict';
  10. var codec = require('../lib/index.js');
  11. var forge = require('node-forge');
  12. var BigInteger = forge.jsbn.BigInteger;
  13. describe('The codec ...', function() {
  14. var testString = 'scytl';
  15. var testStringBytes = new Uint8Array([115, 99, 121, 116, 108]);
  16. var testStringNonAscii = '你好';
  17. var testStringNonAsciiBytes = new Uint8Array([228, 189, 160, 229, 165, 189]);
  18. var testStringBase64 = 'c2N5dGw=';
  19. var testNumberString = '1234';
  20. var testNumberOneBase64 = 'AQ==';
  21. var testNumberBase64 = 'BNI=';
  22. var testStringHex = '736379746c';
  23. var testNumberOneHex = '01';
  24. var testNumberHex = '04d2';
  25. var testNumberBytes = new Uint8Array([4, 210]);
  26. describe('should be able to ..', function() {
  27. it('UTF-8 encode a string', function() {
  28. expect(codec.utf8Encode(testString)).toEqual(testStringBytes);
  29. expect(codec.utf8Encode(testStringNonAscii))
  30. .toEqual(testStringNonAsciiBytes);
  31. });
  32. it('UTF-8 decode an encoded string', function() {
  33. expect(codec.utf8Decode(testStringBytes)).toEqual(testString);
  34. expect(codec.utf8Decode(testStringNonAsciiBytes))
  35. .toBe(testStringNonAscii);
  36. });
  37. it('Convert some bytes to a BigInteger object', function() {
  38. expect(codec.bytesToBigInteger(testNumberBytes))
  39. .toEqual(new BigInteger(testNumberString));
  40. });
  41. it('Convert a BigInteger object to bytes', function() {
  42. expect(codec.bigIntegerToBytes(new BigInteger(testNumberString)))
  43. .toEqual(testNumberBytes);
  44. });
  45. it('Base64 encode some bytes', function() {
  46. expect(codec.base64Encode(testStringBytes)).toEqual(testStringBase64);
  47. });
  48. it('Base64 encode a string', function() {
  49. expect(codec.base64Encode(testString)).toBe(testStringBase64);
  50. });
  51. it('Base64 encode a BigInteger object', function() {
  52. expect(codec.base64Encode(BigInteger.ONE)).toBe(testNumberOneBase64);
  53. expect(codec.base64Encode(new BigInteger(testNumberString)))
  54. .toBe(testNumberBase64);
  55. });
  56. it('Base64 encode some bytes, using a specified output line length',
  57. function() {
  58. var base64 = codec.base64Encode(testStringBytes, {lineLength: 2});
  59. expect(base64.indexOf('\n')).toBe(3);
  60. });
  61. it('Base64 encode a string, using a specified output line length',
  62. function() {
  63. var base64 = codec.base64Encode(testString, {lineLength: 2});
  64. expect(base64.indexOf('\n')).toBe(3);
  65. });
  66. it('Base64 encode a BigInteger object, using a specified output line length',
  67. function() {
  68. var base64 = codec.base64Encode(
  69. new BigInteger(testNumberString), {lineLength: 2});
  70. expect(base64.indexOf('\n')).toBe(3);
  71. });
  72. it('Base64 decode some encoded bytes', function() {
  73. expect(codec.base64Decode(testStringBase64)).toEqual(testStringBytes);
  74. });
  75. it('Base64 decode a string', function() {
  76. expect(codec.utf8Decode(codec.base64Decode(testStringBase64)))
  77. .toBe(testString);
  78. });
  79. it('Base64 decode an encoded BigInteger object', function() {
  80. expect(codec.bytesToBigInteger(codec.base64Decode(testNumberOneBase64)))
  81. .toEqual(BigInteger.ONE);
  82. expect(codec.bytesToBigInteger(codec.base64Decode(testNumberBase64)))
  83. .toEqual(new BigInteger(testNumberString));
  84. });
  85. it('Hexadecimally encode some bytes', function() {
  86. expect(codec.hexEncode(testStringBytes)).toBe(testStringHex);
  87. });
  88. it('Hexadecimally encode a string', function() {
  89. expect(codec.hexEncode(testString)).toBe(testStringHex);
  90. });
  91. it('Hexadecimally encode a BigInteger object', function() {
  92. expect(codec.hexEncode(BigInteger.ONE)).toBe(testNumberOneHex);
  93. expect(codec.hexEncode(new BigInteger(testNumberString)))
  94. .toBe(testNumberHex);
  95. });
  96. it('Hexadecimally decode some encoded bytes', function() {
  97. expect(codec.hexDecode(testStringHex)).toEqual(testStringBytes);
  98. });
  99. it('Hexadecimally decode an encoded string', function() {
  100. expect(codec.utf8Decode(codec.hexDecode(testStringHex))).toBe(testString);
  101. });
  102. it('Hexadecimally decode an encoded BigInteger object', function() {
  103. expect(codec.bytesToBigInteger(codec.hexDecode(testNumberOneHex)))
  104. .toEqual(BigInteger.ONE);
  105. expect(codec.bytesToBigInteger(codec.hexDecode(testNumberHex)))
  106. .toEqual(new BigInteger(testNumberString));
  107. });
  108. it('Binary encode some bytes', function() {
  109. expect(codec.binaryEncode(testStringBytes)).toBe(testString);
  110. });
  111. it('Binary decode some encoded bytes', function() {
  112. expect(codec.binaryDecode(testString)).toEqual(testStringBytes);
  113. });
  114. it('Base64 encode some binary decoded bytes', function() {
  115. expect(codec.base64Encode(codec.binaryDecode(testString)))
  116. .toBe(testStringBase64);
  117. });
  118. it('Base64 encode some binary decoded bytes, using a specified output line length',
  119. function() {
  120. var base64 = codec.base64Encode(
  121. codec.binaryDecode(testString), {lineLength: 2});
  122. expect(base64.indexOf('\n')).toBe(3);
  123. });
  124. it('Base64 decode some binary encoded bytes', function() {
  125. expect(codec.binaryEncode(codec.base64Decode(testStringBase64)))
  126. .toBe(testString);
  127. });
  128. it('Hexadecimally encode some binary decoded bytes', function() {
  129. expect(codec.hexEncode(codec.binaryDecode(testString)))
  130. .toBe(testStringHex);
  131. });
  132. it('Hexadecimally decode some binary decoded bytes', function() {
  133. expect(codec.binaryEncode(codec.hexDecode(testStringHex)))
  134. .toBe(testString);
  135. });
  136. });
  137. });