CryptUtils.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 { swapLettersCase } from '../../helpers/swapLettersCase';
  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. const expectedString: string = swapLettersCase('c3RyaW5n');
  17. let string: string;
  18. before(() => {
  19. string = cryptUtils.btoa('string');
  20. });
  21. it('should create a base-64 encoded string from a given string', () => {
  22. assert.equal(string, expectedString);
  23. });
  24. });
  25. describe('hideString', () => {
  26. const originalString: string = 'example.com';
  27. const hiddenStringLength: number = 30;
  28. let hiddenString: string,
  29. diffString: string;
  30. before(() => {
  31. [hiddenString, diffString] = cryptUtils.hideString(originalString, hiddenStringLength);
  32. });
  33. describe('hidden string length check', () => {
  34. let originalStringActualLength: number,
  35. hiddenStringActualLength: number;
  36. before(() => {
  37. originalStringActualLength = originalString.length;
  38. hiddenStringActualLength = hiddenString.length;
  39. });
  40. it('should create hidden string with length equal or bigger than given length', () => {
  41. assert.isTrue(hiddenStringActualLength > originalStringActualLength);
  42. });
  43. });
  44. describe('hidden string content', () => {
  45. let hiddenStringWithoutDiff: string;
  46. before(() => {
  47. const regExp: RegExp = new RegExp(`[${diffString}]`, 'g');
  48. hiddenStringWithoutDiff = hiddenString.replace(regExp, '');
  49. });
  50. it('should return a hidden string with the original string within', () => {
  51. assert.equal(hiddenStringWithoutDiff, originalString);
  52. });
  53. });
  54. });
  55. describe('rc4', () => {
  56. const string: string = 'test';
  57. const key: string = 'key';
  58. let encodedString: string,
  59. decodedString: string;
  60. before(() => {
  61. encodedString = cryptUtils.rc4(string, key);
  62. decodedString = cryptUtils.rc4(encodedString, key);
  63. });
  64. it('should encode string using the rc4 algorithm', () => {
  65. assert.notEqual(encodedString, string);
  66. });
  67. it('should encode and successfully decode string using the rc4 algorithm', () => {
  68. assert.equal(decodedString, string);
  69. });
  70. });
  71. });