commons.utils.spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. describe('Utils', function() {
  9. 'use strict';
  10. it('should exist as a module', function() {
  11. cryptolib('commons.utils', function(box) {
  12. expect(box.commons.utils).toBeDefined();
  13. });
  14. });
  15. it('should contain a Converters object', function() {
  16. cryptolib('commons.utils', function(box) {
  17. var converters = new box.commons.utils.Converters();
  18. expect(converters).toBeDefined();
  19. });
  20. });
  21. describe('Converters', function() {
  22. it('should encode a string to base64', function() {
  23. cryptolib('commons.utils', function(box) {
  24. var converters = new box.commons.utils.Converters();
  25. expect(converters.base64Encode('scytl')).toBe('c2N5dGw=');
  26. });
  27. });
  28. it('should decode a string in base64 encoded format to a string',
  29. function() {
  30. cryptolib('commons.utils', function(box) {
  31. var converters = new box.commons.utils.Converters();
  32. expect(converters.base64Decode('c2N5dGw=')).toBe('scytl');
  33. });
  34. });
  35. it('should convert a string to in its byte array presentation', function() {
  36. cryptolib('commons.utils', function(box) {
  37. var converters = new box.commons.utils.Converters();
  38. expect(converters.bytesFromString('scytl')).toEqual([
  39. 115, 99, 121, 116, 108
  40. ]);
  41. });
  42. });
  43. it('should convert a byte array in its string representation', function() {
  44. cryptolib('commons.utils', function(box) {
  45. var converters = new box.commons.utils.Converters();
  46. expect(converters.bytesToString([
  47. 115, 99, 121, 116, 108
  48. ])).toEqual('scytl');
  49. });
  50. });
  51. it('should convert a string to in its hexadecimal representation',
  52. function() {
  53. cryptolib('commons.utils', function(box) {
  54. var converters = new box.commons.utils.Converters();
  55. expect(converters.hexFromString('scytl')).toEqual('736379746c');
  56. });
  57. });
  58. it('should convert an hexadecimal in its string representation',
  59. function() {
  60. cryptolib('commons.utils', function(box) {
  61. var converters = new box.commons.utils.Converters();
  62. expect(converters.hexToString('736379746c')).toEqual('scytl');
  63. });
  64. });
  65. it('should be able to encode and decode BigIntegers as Base64', function() {
  66. cryptolib('commons.utils', function(box) {
  67. var converters = new box.commons.utils.Converters();
  68. var number =
  69. new forge.jsbn.BigInteger('123456789012345678901234567890');
  70. var base64Number = converters.base64FromBigInteger(number);
  71. expect(base64Number).toBeTruthy();
  72. expect(converters.base64ToBigInteger(base64Number)).toEqual(number);
  73. });
  74. });
  75. it('should correctly encode a big integer to Base64', function() {
  76. cryptolib('commons.utils', function(box) {
  77. var converters = new box.commons.utils.Converters();
  78. expect(converters.base64ToBigInteger('AQ=='))
  79. .toEqual(forge.jsbn.BigInteger.ONE);
  80. expect(converters.base64ToBigInteger('BNI='))
  81. .toEqual(new forge.jsbn.BigInteger('1234'));
  82. });
  83. });
  84. it('should correctly decode a big integer from its Base64 representation',
  85. function() {
  86. cryptolib('commons.utils', function(box) {
  87. var converters = new box.commons.utils.Converters();
  88. expect(converters.base64FromBigInteger(forge.jsbn.BigInteger.ONE))
  89. .toEqual('AQ==');
  90. expect(converters.base64FromBigInteger(
  91. new forge.jsbn.BigInteger('1234')))
  92. .toEqual('BNI=');
  93. });
  94. });
  95. });
  96. it('should contain a BitOperators object', function() {
  97. cryptolib('commons.utils', function(box) {
  98. var bitOperators = new box.commons.utils.BitOperators();
  99. expect(bitOperators).toBeDefined();
  100. });
  101. });
  102. describe('BitOperators', function() {
  103. it('should make a bit-wise concat of two given strings', function() {
  104. cryptolib('commons.utils', function(box) {
  105. var bitOperators = new box.commons.utils.BitOperators();
  106. var string1 = '101';
  107. var string2 = '010';
  108. expect(bitOperators.concatenate(string1, string2)).toBe('101010');
  109. });
  110. });
  111. it('should extract the latest part of a given string', function() {
  112. cryptolib('commons.utils', function(box) {
  113. var bitOperators = new box.commons.utils.BitOperators();
  114. var string = 'ScytlSecureElectronicVoting';
  115. expect(bitOperators.extract(string, 5, string.length))
  116. .toBe('SecureElectronicVoting');
  117. });
  118. });
  119. });
  120. describe('XPath', function() {
  121. var inStream = [
  122. '<?xml version="1.0" encoding="UTF-8"?>', '<root>',
  123. ' <name attr=\'attr value\'>node value</name>', '</root>'
  124. ].join('\n');
  125. it('should find the proper node and show the contents', function() {
  126. cryptolib('commons.utils', function(box) {
  127. var parsers = new box.commons.utils.Parsers();
  128. var xml = parsers.xmlFromString(inStream);
  129. var node = new box.commons.utils.XPath(xml, '/root/name');
  130. expect(node.getValue()).toBe('node value');
  131. expect(node.getAttribute('attr')).toBe('attr value');
  132. });
  133. });
  134. it('should preserve the original xml object', function() {
  135. cryptolib('commons.utils', function(box) {
  136. var parsers = new box.commons.utils.Parsers();
  137. var xml = parsers.xmlFromString(inStream);
  138. var node2 = new box.commons.utils.XPath(xml, '/root/name');
  139. expect(node2.getValue()).toBe('node value');
  140. expect(node2.getAttribute('attr')).toBe('attr value');
  141. });
  142. });
  143. });
  144. describe('StringUtils', function() {
  145. it('should find the substring in the string', function() {
  146. cryptolib('commons.utils', function(box) {
  147. var stringUtils = new box.commons.utils.StringUtils();
  148. expect(stringUtils.containsSubString(
  149. 'main text with a lot of words', 'text with'))
  150. .toBeTruthy();
  151. });
  152. });
  153. it('should not find the substring in the string', function() {
  154. cryptolib('commons.utils', function(box) {
  155. var stringUtils = new box.commons.utils.StringUtils();
  156. expect(stringUtils.containsSubString(
  157. 'main text with a lot of words', 'text a'))
  158. .toBeFalsy();
  159. });
  160. });
  161. it('should find the substring in the string with sensitive case',
  162. function() {
  163. cryptolib('commons.utils', function(box) {
  164. var stringUtils = new box.commons.utils.StringUtils();
  165. expect(stringUtils.containsSubString(
  166. 'main text with a lot of words', 'text with', true))
  167. .toBeTruthy();
  168. });
  169. });
  170. it('should find the substring in the string with sensitive case and capital letters',
  171. function() {
  172. cryptolib('commons.utils', function(box) {
  173. var stringUtils = new box.commons.utils.StringUtils();
  174. expect(stringUtils.containsSubString(
  175. 'main tExT with a lot of words', 'tExT with', true))
  176. .toBeTruthy();
  177. });
  178. });
  179. it('should find the substring in the string without sensitive case and capital letters',
  180. function() {
  181. cryptolib('commons.utils', function(box) {
  182. var stringUtils = new box.commons.utils.StringUtils();
  183. expect(stringUtils.containsSubString(
  184. 'main tExT with a lot of words', 'text with'))
  185. .toBeTruthy();
  186. });
  187. });
  188. it('should not find the substring in the string with sensitive case and capital letters',
  189. function() {
  190. cryptolib('commons.utils', function(box) {
  191. var stringUtils = new box.commons.utils.StringUtils();
  192. expect(stringUtils.containsSubString(
  193. 'main tExT with a lot of words', 'text with', true))
  194. .toBeFalsy();
  195. });
  196. });
  197. });
  198. describe('ProgressMeter ..', function() {
  199. var _progressMeter;
  200. var _percentMeasuredSum;
  201. var _percentMeasuredLatest;
  202. beforeEach(function() {
  203. var progressMax = 100;
  204. var progressCallback = function(progressPercent) {
  205. _percentMeasuredSum += progressPercent;
  206. _percentMeasuredLatest = progressPercent;
  207. };
  208. var progressPercentMinCheckInterval = 10;
  209. cryptolib('commons.utils', function(box) {
  210. _progressMeter = new box.commons.utils.ProgressMeter(
  211. progressMax, progressCallback, progressPercentMinCheckInterval);
  212. });
  213. _percentMeasuredSum = 0;
  214. _percentMeasuredLatest = 0;
  215. });
  216. it('should measure progress for variable increases in progress',
  217. function() {
  218. cryptolib('commons.utils', function() {
  219. _progressMeter.update(9);
  220. expect(_percentMeasuredSum).toBe(0);
  221. expect(_percentMeasuredLatest).toBe(0);
  222. _progressMeter.update(10);
  223. expect(_percentMeasuredSum).toBe(10);
  224. expect(_percentMeasuredLatest).toBe(10);
  225. _progressMeter.update(11);
  226. expect(_percentMeasuredSum).toBe(10);
  227. expect(_percentMeasuredLatest).toBe(10);
  228. _progressMeter.update(39);
  229. expect(_percentMeasuredSum).toBe(49);
  230. expect(_percentMeasuredLatest).toBe(39);
  231. _progressMeter.update(75);
  232. expect(_percentMeasuredSum).toBe(124);
  233. expect(_percentMeasuredLatest).toBe(75);
  234. _progressMeter.update(100);
  235. expect(_percentMeasuredSum).toBe(224);
  236. expect(_percentMeasuredLatest).toBe(100);
  237. });
  238. });
  239. it('should measure 100 percent if progress provided as input exceeds maximum',
  240. function() {
  241. cryptolib('commons.utils', function() {
  242. _progressMeter.update(15);
  243. expect(_percentMeasuredSum).toBe(15);
  244. expect(_percentMeasuredLatest).toBe(15);
  245. _progressMeter.update(100);
  246. expect(_percentMeasuredSum).toBe(115);
  247. expect(_percentMeasuredLatest).toBe(100);
  248. _progressMeter.update(110);
  249. expect(_percentMeasuredSum).toBe(115);
  250. expect(_percentMeasuredLatest).toBe(100);
  251. });
  252. });
  253. });
  254. });