CryptUtils.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. describe('Variant #1: basic', () => {
  16. const expectedString: string = '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('Variant #2: padding characters', () => {
  26. const expectedString: string = 'c3RyaQ==';
  27. let string: string;
  28. before(() => {
  29. string = cryptUtils.btoa('stri');
  30. });
  31. it('should create a base-64 encoded string from a given string with padding characters', () => {
  32. assert.equal(string, expectedString);
  33. });
  34. });
  35. });
  36. describe('hideString', () => {
  37. const originalString: string = 'example.com';
  38. const hiddenStringLength: number = 30;
  39. let hiddenString: string,
  40. diffString: string;
  41. before(() => {
  42. [hiddenString, diffString] = cryptUtils.hideString(originalString, hiddenStringLength);
  43. });
  44. describe('hidden string length check', () => {
  45. let originalStringActualLength: number,
  46. hiddenStringActualLength: number;
  47. before(() => {
  48. originalStringActualLength = originalString.length;
  49. hiddenStringActualLength = hiddenString.length;
  50. });
  51. it('should create hidden string with length equal or bigger than given length', () => {
  52. assert.isTrue(hiddenStringActualLength > originalStringActualLength);
  53. });
  54. });
  55. describe('hidden string content', () => {
  56. let hiddenStringWithoutDiff: string;
  57. before(() => {
  58. const regExp: RegExp = new RegExp(`[${diffString}]`, 'g');
  59. hiddenStringWithoutDiff = hiddenString.replace(regExp, '');
  60. });
  61. it('should return a hidden string with the original string within', () => {
  62. assert.equal(hiddenStringWithoutDiff, originalString);
  63. });
  64. });
  65. });
  66. describe('rc4', () => {
  67. const string: string = 'test';
  68. const key: string = 'key';
  69. let encodedString: string,
  70. decodedString: string;
  71. before(() => {
  72. encodedString = cryptUtils.rc4(string, key);
  73. decodedString = cryptUtils.rc4(encodedString, key);
  74. });
  75. it('should encode string using the rc4 algorithm', () => {
  76. assert.notEqual(encodedString, string);
  77. });
  78. it('should encode and successfully decode string using the rc4 algorithm', () => {
  79. assert.equal(decodedString, string);
  80. });
  81. });
  82. });