CryptUtilsStringArray.spec.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  4. import { ICryptUtilsStringArray } from '../../../src/interfaces/utils/ICryptUtilsStringArray';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  7. import { swapLettersCase } from '../../helpers/swapLettersCase';
  8. describe('CryptUtilsStringArray', () => {
  9. let cryptUtilsStringArray: ICryptUtilsStringArray;
  10. before(() => {
  11. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  12. inversifyContainerFacade.load('', '', {});
  13. cryptUtilsStringArray = inversifyContainerFacade
  14. .get<ICryptUtilsStringArray>(ServiceIdentifiers.ICryptUtilsStringArray);
  15. });
  16. describe('btoa', () => {
  17. describe('Variant #1: basic', () => {
  18. const expectedString: string = swapLettersCase('c3RyaW5n');
  19. let string: string;
  20. before(() => {
  21. string = cryptUtilsStringArray.btoa('string');
  22. });
  23. it('should create a base-64 encoded string with swapped alphabet from a given string', () => {
  24. assert.equal(string, expectedString);
  25. });
  26. });
  27. describe('Variant #2: no padding characters', () => {
  28. const expectedString: string = swapLettersCase('c3RyaQ');
  29. let string: string;
  30. before(() => {
  31. string = cryptUtilsStringArray.btoa('stri');
  32. });
  33. it('should create a base-64 encoded string from a given string without padding characters', () => {
  34. assert.equal(string, expectedString);
  35. });
  36. });
  37. });
  38. describe('rc4', () => {
  39. const string: string = 'test';
  40. const key: string = 'key';
  41. let encodedString: string,
  42. decodedString: string;
  43. before(() => {
  44. encodedString = cryptUtilsStringArray.rc4(string, key);
  45. decodedString = cryptUtilsStringArray.rc4(encodedString, key);
  46. });
  47. it('should encode string using the rc4 algorithm', () => {
  48. assert.notEqual(encodedString, string);
  49. });
  50. it('should encode and successfully decode string using the rc4 algorithm', () => {
  51. assert.equal(decodedString, string);
  52. });
  53. });
  54. });