streaming-tests.js 3.2 KB

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