CryptUtils.spec.ts 3.0 KB

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