CryptUtils.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  4. import { ICryptUtils } from '../../../src/interfaces/utils/ICryptUtils';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  7. import { cryptUtilsAtob } from '../../helpers/cryptUtilsAtob';
  8. describe('CryptUtils', () => {
  9. let cryptUtils: ICryptUtils;
  10. before(() => {
  11. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  12. inversifyContainerFacade.load('', '', {});
  13. cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
  14. });
  15. describe('btoa', () => {
  16. describe('Variant #1: basic', () => {
  17. const expectedEncodedString: string = 'c3RyaW5n';
  18. const expectedDecodedString: string = 'string';
  19. let encodedString: string,
  20. decodedString: string;
  21. before(() => {
  22. encodedString = cryptUtils.btoa('string');
  23. decodedString = cryptUtilsAtob(encodedString);
  24. });
  25. it('should create a base-64 encoded string from a given string', () => {
  26. assert.equal(encodedString, expectedEncodedString);
  27. });
  28. it('should create encoded string that can be successfully decoded', () => {
  29. assert.equal(decodedString, expectedDecodedString);
  30. });
  31. });
  32. describe('Variant #2: padding characters', () => {
  33. const expectedEncodedString: string = 'c3RyaQ==';
  34. const expectedDecodedString: string = 'stri';
  35. let encodedString: string,
  36. decodedString: string;
  37. before(() => {
  38. encodedString = cryptUtils.btoa('stri');
  39. decodedString = cryptUtilsAtob(encodedString);
  40. });
  41. it('should create a base-64 encoded string from a given string with padding characters', () => {
  42. assert.equal(encodedString, expectedEncodedString);
  43. });
  44. it('should create encoded string that can be successfully decoded', () => {
  45. assert.equal(decodedString, expectedDecodedString);
  46. });
  47. });
  48. describe('Variant #3: cyrillic string', () => {
  49. const expectedEncodedString: string = '0YLQtdGB0YI=';
  50. const expectedDecodedString: string = 'тест';
  51. let encodedString: string,
  52. decodedString: string;
  53. before(() => {
  54. encodedString = cryptUtils.btoa('тест');
  55. decodedString = cryptUtilsAtob(encodedString);
  56. });
  57. it('should create a base-64 encoded string from a given string', () => {
  58. assert.equal(encodedString, expectedEncodedString);
  59. });
  60. it('should create encoded string with a cyrillic characters that can be successfully decoded', () => {
  61. assert.equal(decodedString, expectedDecodedString);
  62. });
  63. });
  64. });
  65. describe('hideString', () => {
  66. const originalString: string = 'example.com';
  67. const hiddenStringLength: number = 30;
  68. let hiddenString: string,
  69. diffString: string;
  70. before(() => {
  71. [hiddenString, diffString] = cryptUtils.hideString(originalString, hiddenStringLength);
  72. });
  73. describe('hidden string length check', () => {
  74. let originalStringActualLength: number,
  75. hiddenStringActualLength: number;
  76. before(() => {
  77. originalStringActualLength = originalString.length;
  78. hiddenStringActualLength = hiddenString.length;
  79. });
  80. it('should create hidden string with length equal or bigger than given length', () => {
  81. assert.isTrue(hiddenStringActualLength > originalStringActualLength);
  82. });
  83. });
  84. describe('hidden string content', () => {
  85. let hiddenStringWithoutDiff: string;
  86. before(() => {
  87. const regExp: RegExp = new RegExp(`[${diffString}]`, 'g');
  88. hiddenStringWithoutDiff = hiddenString.replace(regExp, '');
  89. });
  90. it('should return a hidden string with the original string within', () => {
  91. assert.equal(hiddenStringWithoutDiff, originalString);
  92. });
  93. });
  94. });
  95. describe('rc4', () => {
  96. const string: string = 'test';
  97. const key: string = 'key';
  98. let encodedString: string,
  99. decodedString: string;
  100. before(() => {
  101. encodedString = cryptUtils.rc4(string, key);
  102. decodedString = cryptUtils.rc4(encodedString, key);
  103. });
  104. it('should encode string using the rc4 algorithm', () => {
  105. assert.notEqual(encodedString, string);
  106. });
  107. it('should encode and successfully decode string using the rc4 algorithm', () => {
  108. assert.equal(decodedString, string);
  109. });
  110. });
  111. });