CryptUtils.spec.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { assert } from 'chai';
  2. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  3. import { ICryptUtils } from '../../../src/interfaces/utils/ICryptUtils';
  4. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  5. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  6. describe('CryptUtils', () => {
  7. let cryptUtils: ICryptUtils;
  8. before(() => {
  9. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  10. inversifyContainerFacade.load({});
  11. cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
  12. });
  13. describe('btoa (string: string): string', () => {
  14. const expectedString: string = 'c3RyaW5n';
  15. let string: string;
  16. before(() => {
  17. string = cryptUtils.btoa('string');
  18. });
  19. it('should create a base-64 encoded string from a given string', () => {
  20. assert.equal(string, expectedString);
  21. });
  22. });
  23. describe('hideString (str: string, length: number): [string, string]', () => {
  24. const originalString: string = 'example.com';
  25. const hiddenStringLength: number = 30;
  26. let hiddenString: string,
  27. diffString: string;
  28. before(() => {
  29. [hiddenString, diffString] = cryptUtils.hideString(originalString, hiddenStringLength);
  30. });
  31. describe('hidden string length check', () => {
  32. let originalStringActualLength: number,
  33. hiddenStringActualLength: number;
  34. before(() => {
  35. originalStringActualLength = originalString.length;
  36. hiddenStringActualLength = hiddenString.length;
  37. });
  38. it('should create hidden string with length equal or bigger than given length', () => {
  39. assert.isTrue(hiddenStringActualLength > originalStringActualLength);
  40. });
  41. });
  42. describe('hidden string content', () => {
  43. let hiddenStringWithoutDiff: string;
  44. before(() => {
  45. const regExp: RegExp = new RegExp(`[${diffString}]`, 'g');
  46. hiddenStringWithoutDiff = hiddenString.replace(regExp, '');
  47. });
  48. it('should return a hidden string with the original string within', () => {
  49. assert.equal(hiddenStringWithoutDiff, originalString);
  50. });
  51. });
  52. });
  53. describe('rc4 (string: string, key: string): string', () => {
  54. const string: string = 'test';
  55. const key: string = 'key';
  56. let encodedString: string,
  57. decodedString: string;
  58. before(() => {
  59. encodedString = cryptUtils.rc4(string, key);
  60. decodedString = cryptUtils.rc4(encodedString, key);
  61. });
  62. it('should encode string using the rc4 algorithm', () => {
  63. assert.notEqual(encodedString, string);
  64. });
  65. it('should encode and successfully decode string using the rc4 algorithm', () => {
  66. assert.equal(decodedString, string);
  67. });
  68. });
  69. });