|
1 | 1 | var signalstream = require('..') |
2 | | -var helpers = require('./helpers') |
| 2 | +var bobAliceSessionCiphers = require('./helpers') |
3 | 3 | var signal = require('signal-protocol') |
4 | 4 | var read = require('fs').createReadStream |
5 | 5 | var through = require('through2') |
| 6 | +var test = require('tape') |
6 | 7 |
|
7 | 8 | var l = require('../src/helpers') |
8 | 9 |
|
9 | | -var ALICE_ADDRESS = new signal.SignalProtocolAddress("+14151111111", 1); |
10 | | -var BOB_ADDRESS = new signal.SignalProtocolAddress("+14152222222", 1); |
11 | | - |
12 | | -var aliceStore = new helpers.SignalStore(); |
13 | | -var bobStore = new helpers.SignalStore(); |
14 | | - |
15 | | -var bobPreKeyId = 1337; |
16 | | -var bobSignedKeyId = 1; |
17 | | - |
18 | | -Promise.all([ |
19 | | - helpers.generateIdentity(aliceStore), |
20 | | - helpers.generateIdentity(bobStore), |
21 | | -]).then(function() { |
22 | | - return helpers.generatePreKeyBundle(bobStore, bobPreKeyId, bobSignedKeyId); |
23 | | -}).then(function(preKeyBundle) { |
24 | | - var builder = new signal.SessionBuilder(aliceStore, BOB_ADDRESS); |
25 | | - return builder.processPreKey(preKeyBundle) |
26 | | -}).then(function () { |
27 | | - var aliceSessionCipher = new signal.SessionCipher(aliceStore, BOB_ADDRESS); |
28 | | - var bobSessionCipher = new signal.SessionCipher(bobStore, ALICE_ADDRESS); |
29 | | - return [ aliceSessionCipher, bobSessionCipher ] |
30 | | -}).then(echo, log) |
31 | | - |
32 | 10 | function log (note) { |
33 | 11 | return function (x) { |
34 | 12 | console.log(note, x) |
35 | 13 | return x |
36 | 14 | } |
37 | 15 | } |
38 | 16 |
|
39 | | -// // // the world's slowest impl of `echo` |
40 | | -function echo ([aliceCipher, bobCipher]) { |
| 17 | +function roundTripEcho (path, cb) { |
41 | 18 |
|
42 | | - let alice = require('..')(aliceCipher) |
43 | | - let bob = require('..')(bobCipher) |
| 19 | + bobAliceSessionCiphers() |
| 20 | + .then(echo, log('ERR')) |
44 | 21 |
|
45 | | - console.log('starting') |
| 22 | + // // // the world's slowest impl of `echo` |
| 23 | + function echo ([aliceCipher, bobCipher]) { |
| 24 | + let alice = require('..')(aliceCipher) |
| 25 | + let bob = require('..')(bobCipher) |
| 26 | + let concat = require('concat-stream') |
| 27 | + read(path) |
| 28 | + .pipe(through(function (buf, enc, next) { |
| 29 | + let amt = 239 |
| 30 | + for (let i=0; i<buf.length; i+=amt) |
| 31 | + this.push(buf.slice(i, i+amt)) |
| 32 | + next() |
| 33 | + })) |
| 34 | + .pipe(alice.encrypt) |
| 35 | + .pipe(bob.decrypt) |
| 36 | + .pipe(bob.encrypt) |
| 37 | + .pipe(alice.decrypt) |
| 38 | + .pipe(through.obj((buff, enc, next) => { |
| 39 | + let b = new Buffer(buff, enc) |
| 40 | + next(null, b) |
| 41 | + })) |
| 42 | + .pipe(concat(cb)) |
| 43 | + // .on('data', d => console.log(d)) |
| 44 | + .on('error', err => console.log('err', err)) |
| 45 | + } |
| 46 | +} |
46 | 47 |
|
47 | | - read(__dirname + '/story.txt')//, 'utf-8') |
48 | | - .pipe(through(function (buf, enc, next) { |
49 | | - let amt = 200 |
50 | | - for (let i=0; i<buf.length; i+=amt) |
51 | | - this.push(buf.slice(i, i+amt)) |
52 | | - next() |
53 | | - })) |
54 | | - .pipe(alice.encrypt) |
55 | | - .pipe(bob.decrypt) |
56 | | - .pipe(bob.encrypt) |
57 | | - .pipe(alice.decrypt) |
58 | | - .on('data', d => { |
59 | | - let pt = new Buffer.from(d).toString('utf-8') |
60 | | - console.log(pt) |
61 | | - }) |
| 48 | +// test('can echo a short textfile from alice to bob to alice again', t => { |
| 49 | +// let filepath = __dirname + '/story.txt' |
| 50 | +// roundTripEcho(filepath, buff => { |
| 51 | +// let trueBuff = require('fs').readFileSync(filepath) |
| 52 | +// t.deepEqual(buff, trueBuff, |
| 53 | +// 'textfile buffer after round trip encrypt/decrypt/encrypt/decrypt identical to original buffer') |
| 54 | +// t.end() |
| 55 | +// }) |
| 56 | +// }) |
62 | 57 |
|
63 | | - .on('error', err => console.log('err', err)) |
64 | | -} |
| 58 | +// test('can echo long short textfile from alice to bob to alice again', t => { |
| 59 | +// let filepath = __dirname + '/story2.txt' |
| 60 | +// roundTripEcho(filepath, buff => { |
| 61 | +// let trueBuff = require('fs').readFileSync(filepath) |
| 62 | +// t.deepEqual(buff, trueBuff, |
| 63 | +// 'long textfile buffer perserved after roundtrip') |
| 64 | +// t.end() |
| 65 | +// }) |
| 66 | +// }) |
| 67 | + |
| 68 | +test('can echo an image file', t => { |
| 69 | + let filepath = __dirname + '/oakland-bridge.jpg' |
| 70 | + roundTripEcho(filepath, buff => { |
| 71 | + let trueBuff = require('fs').readFileSync(filepath) |
| 72 | + t.deepEqual(buff, trueBuff, |
| 73 | + 'long textfile buffer perserved after roundtrip') |
| 74 | + t.end() |
| 75 | + }) |
| 76 | +}) |
0 commit comments