streaming-tests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const ece = require('http_ece');
  2. require('buffer');
  3. import assert from 'assert';
  4. import Archive from '../../../app/archive';
  5. import { b64ToArray } from '../../../app/utils';
  6. import { blobStream, concatStream } from '../../../app/streams';
  7. import { decryptStream, encryptStream } from '../../../app/ece.js';
  8. import { encryptedSize } from '../../../app/utils';
  9. const rs = 36;
  10. const str = 'You are the dancing queen, young and sweet, only seventeen.';
  11. const testSalt = 'I1BsxtFttlv3u_Oo94xnmw';
  12. const keystr = 'yqdlZ-tYemfogSmv7Ws5PQ';
  13. const buffer = Buffer.from(str);
  14. const params = {
  15. version: 'aes128gcm',
  16. rs: rs,
  17. salt: testSalt,
  18. keyid: '',
  19. key: keystr
  20. };
  21. const encrypted = ece.encrypt(buffer, params);
  22. const decrypted = ece.decrypt(encrypted, params);
  23. describe('Streaming', function() {
  24. describe('blobStream', function() {
  25. it('reads the entire blob', async function() {
  26. const len = 12345;
  27. const chunkSize = 1024;
  28. const blob = new Blob([new Uint8Array(len)]);
  29. const stream = blobStream(blob, chunkSize);
  30. const reader = stream.getReader();
  31. let bytes = 0;
  32. let data = await reader.read();
  33. while (!data.done) {
  34. bytes += data.value.byteLength;
  35. assert.ok(data.value.byteLength <= chunkSize, 'chunk too big');
  36. data = await reader.read();
  37. }
  38. assert.equal(bytes, len);
  39. });
  40. });
  41. describe('concatStream', function() {
  42. it('reads all the streams', async function() {
  43. const count = 5;
  44. const len = 12345;
  45. const streams = Array.from({ length: count }, () =>
  46. blobStream(new Blob([new Uint8Array(len)]))
  47. );
  48. const concat = concatStream(streams);
  49. const reader = concat.getReader();
  50. let bytes = 0;
  51. let data = await reader.read();
  52. while (!data.done) {
  53. bytes += data.value.byteLength;
  54. data = await reader.read();
  55. }
  56. assert.equal(bytes, len * count);
  57. });
  58. });
  59. //testing against http_ece's implementation
  60. describe('ECE', function() {
  61. const key = b64ToArray(keystr);
  62. const salt = b64ToArray(testSalt).buffer;
  63. it('can encrypt', async function() {
  64. const stream = new Archive([new Blob([str], { type: 'text/plain' })])
  65. .stream;
  66. const encStream = encryptStream(stream, key, rs, salt);
  67. const reader = encStream.getReader();
  68. let result = Buffer.from([]);
  69. let state = await reader.read();
  70. while (!state.done) {
  71. result = Buffer.concat([result, state.value]);
  72. state = await reader.read();
  73. }
  74. assert.deepEqual(result, encrypted);
  75. });
  76. it('can decrypt', async function() {
  77. const stream = new Archive([new Blob([encrypted])]).stream;
  78. const decStream = decryptStream(stream, key, rs);
  79. const reader = decStream.getReader();
  80. let result = Buffer.from([]);
  81. let state = await reader.read();
  82. while (!state.done) {
  83. result = Buffer.concat([result, state.value]);
  84. state = await reader.read();
  85. }
  86. assert.deepEqual(result, decrypted);
  87. });
  88. });
  89. describe('encryptedSize', function() {
  90. it('matches the size of an encrypted buffer', function() {
  91. assert.equal(encryptedSize(buffer.length, rs), encrypted.length);
  92. });
  93. });
  94. });