-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
421 lines (414 loc) · 14.4 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
var assert = require('assert')
, fs = require('fs')
, path = require('path')
, crypto = require('crypto')
, rimraf = require('rimraf')
, child_process = require('child_process')
, suppose = require('suppose')
, tmpDir = path.join(require('os').tmpdir(), Math.random().toString(36).slice(2))
, BIN = path.join(__dirname, '..', 'bin', 'salty')
, request = require('micro-request')
, libSalty = require('../')
describe('tests', function () {
var p = path.join(tmpDir, 'alice.jpg')
before(function () {
fs.mkdirSync(tmpDir)
if (!process.env.DEBUG) {
process.once('exit', function () {
rimraf.sync(tmpDir)
})
}
else console.log('tmpDir', tmpDir)
})
it('stream fixture', function (done) {
request('https://raw.githubusercontent.com/carlos8f/node-buffet/master/test/files/folder/Alice-white-rabbit.jpg', {stream: true}, function (err, resp, body) {
assert.ifError(err)
body.pipe(fs.createWriteStream(p))
.once('finish', done)
})
})
it('verify stream fixture', function (done) {
fs.createReadStream(p)
.pipe(crypto.createHash('sha1'))
.on('data', function (data) {
assert.equal(data.toString('hex'), '2bce2ffc40e0d90afe577a76db5db4290c48ddf4')
done()
})
})
it('set up alice', function (done) {
suppose(BIN, ['init', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Create a passphrase: ').respond('disney sucks\n')
.when('Verify passphrase: ').respond('disney sucks\n')
.end(function (code) {
assert(!code)
done()
})
//.stderr.pipe(process.stderr)
})
it('set up bob', function (done) {
suppose(BIN, ['init', '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Create a passphrase: ').respond('i am bob\n')
.when('Verify passphrase: ').respond('i am bob\n')
.end(function (code) {
assert(!code)
done()
})
})
var alice_pubkey
it('alice pubkey', function (done) {
var chunks = []
var proc = suppose(BIN, ['pubkey', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.end(function (code) {
assert(!code)
})
proc.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(libSalty.pubkey.regex)
assert(match)
alice_pubkey = match[0]
done()
})
//proc.stderr.pipe(process.stderr)
})
it('alice change password', function (done) {
suppose(BIN, ['init', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet exists. Update it? (y/n): ').respond('y\n')
.when('Wallet is encrypted.\nEnter passphrase: ').respond('disney sucks\n')
.when('Create a passphrase: ').respond('not a blonde\n')
.when('Verify passphrase: ').respond('not a blonde\n')
.end(function (code) {
assert(!code)
done()
})
})
var bob_pubkey
it('bob pubkey', function (done) {
var chunks = []
suppose(BIN, ['pubkey', '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.end(function (code) {
assert(!code)
})
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(libSalty.pubkey.regex)
assert(match)
bob_pubkey = match[0]
done()
})
})
it('alice import bob', function (done) {
var chunks = []
var proc = suppose(BIN, ['import', '--wallet', 'alice', bob_pubkey], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Enter name: ').respond('Bob\n')
.when('Enter email: ').respond('[email protected]\n')
.end(function (code) {
assert(!code)
done()
})
})
it('alice ls', function (done) {
var chunks = []
suppose(BIN, ['ls', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.end(function (code) {
assert(!code)
})
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(libSalty.pubkey.regex)
assert.equal(match.length, 4)
assert.equal(match[0], bob_pubkey.trim() + ' "Bob" <[email protected]>')
done()
})
})
it('alice save', function (done) {
var chunks = []
var proc = suppose(BIN, ['save', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Create a passphrase: ').respond('blarg\n')
.when('Verify passphrase: ').respond('blarg\n')
.end(function (code) {
assert(!code)
done()
})
//.stderr.pipe(process.stderr)
})
it('alice destroy', function () {
rimraf.sync(path.join(tmpDir, 'alice'))
})
it('alice restore', function (done) {
var chunks = []
var proc = suppose(BIN, ['restore', 'salty.pem', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Enter passphrase: ').respond('blarg\n')
.end(function (code) {
assert(!code)
/*
require('child_process').exec('ls -la ' + path.join(tmpDir, 'alice'), function (err, stdout, stderr) {
if (err) throw err
console.error('alice after restore', stdout)
})
*/
done()
})
//.stderr.pipe(process.stderr)
})
var outFile
it('alice encrypt for bob (no sign)', function (done) {
var chunks = []
var proc = suppose(BIN, ['encrypt', '--to', '[email protected]', 'alice.jpg', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.end(function (code) {
assert(!code)
})
proc
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(/Encrypted to (.*)/)
assert(match)
outFile = match[1]
done()
})
//proc.stderr.pipe(process.stderr)
})
it('bob decrypt', function (done) {
var proc = suppose(BIN, ['decrypt', outFile, '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('i am bob\n')
.end(function (code) {
assert(!code)
done()
})
})
it('verify decrypt', function (done) {
/*
require('child_process').exec('ls -la ' + outFile.replace('.salty', ''), function (err, stdout, stderr) {
if (err) throw err
console.error('file after decrypt', stdout)
})
*/
fs.createReadStream(outFile.replace('.salty', ''))
.pipe(crypto.createHash('sha1'))
.on('data', function (data) {
assert.equal(data.toString('hex'), '2bce2ffc40e0d90afe577a76db5db4290c48ddf4')
done()
})
})
it('alice encrypt for bob (sign)', function (done) {
var chunks = []
var proc = suppose(BIN, ['encrypt', '--to', '[email protected]', 'alice.jpg', '--sign', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('not a blonde\n')
.end(function (code) {
assert(!code)
})
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(/Encrypted to (.*)/)
assert(match)
assert(match[1] !== outFile)
outFile = match[1]
done()
})
})
it('bob decrypt', function (done) {
var proc = suppose(BIN, ['decrypt', outFile, '--sig', '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('i am bob\n')
.end(function (code) {
assert(!code)
done()
})
//.stderr.pipe(process.stderr)
})
it('verify decrypt', function (done) {
fs.createReadStream(outFile.replace('.salty', ''))
.pipe(crypto.createHash('sha1'))
.on('data', function (data) {
assert.equal(data.toString('hex'), '2bce2ffc40e0d90afe577a76db5db4290c48ddf4')
done()
})
})
it('stream fixture', function (done) {
request('https://gist.githubusercontent.com/carlos8f/a3fd03a48341e36bd2d1/raw/bc01eeaf1b664f79bf4de9c917ac87f94a291a76/jabberwocky.txt', {stream: true}, function (err, resp, body) {
assert.ifError(err)
body.pipe(fs.createWriteStream(path.join(tmpDir, 'jabberwocky.txt')))
.once('finish', done)
})
})
it('verify stream fixture', function (done) {
fs.createReadStream(path.join(tmpDir, 'jabberwocky.txt'))
.pipe(crypto.createHash('sha1'))
.on('data', function (data) {
assert.equal(data.toString('hex'), '24a80c902db33368958664babde4b019cdaa65f0')
done()
})
})
var pem
it('alice encrypt for bob (armor)', function (done) {
var chunks = []
var proc = suppose(BIN, ['encrypt', '--to', '[email protected]', 'jabberwocky.txt', '--sign', '--armor', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('not a blonde\n')
.end(function (code) {
assert(!code)
})
proc.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
//console.error('stdout', stdout)
assert(stdout.match(/BEGIN SALTY MESSAGE/))
assert(stdout.match(/END SALTY MESSAGE/))
pem = stdout
done()
})
//proc.stderr.pipe(process.stderr)
})
it('write pem to file', function (done) {
fs.writeFile(path.join(tmpDir, 'ctxt.pem'), pem, done)
})
var stdout
it('bob decrypt', function (done) {
var chunks = [], valid = false
var proc = suppose(BIN, ['decrypt', 'ctxt.pem', '--sig', '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('i am bob\n')
.end(function (code) {
assert(!code)
assert(stdout)
assert(valid)
done()
})
proc
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
stdout = Buffer.concat(chunks).toString('utf8')
var beginMatch = stdout.match(/^'Twas brillig, and the slithy toves/)
assert(beginMatch, stdout)
var endMatch = stdout.match(/And the mome raths outgrabe\.$/)
assert(endMatch, stdout)
valid = true
})
//proc.stderr.pipe(process.stderr)
})
var msg
it('alice encrypt for bob (compose)', function (done) {
var chunks = [], valid = false
msg = 'Hi,\n\nThis is my message...\n\nRegards,\Alice\n'
var proc = suppose(BIN, ['encrypt', '--to', '[email protected]', '--sign', '--message', '--wallet', 'alice'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('not a blonde\n')
.when('\nCompose message: (CTL-D when done)\n\n> ').respond(msg + '\4')
.end(function (code) {
assert(!code)
assert(valid)
done()
})
proc
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
assert(stdout.match(/BEGIN SALTY MESSAGE/))
assert(stdout.match(/END SALTY MESSAGE/))
pem = stdout
valid = true
})
//proc.stderr.pipe(process.stderr)
})
it('write pem to file', function (done) {
fs.writeFile(path.join(tmpDir, 'ctxt.pem'), pem, done)
})
it('bob decrypt', function (done) {
var chunks = [], valid = false
var proc = suppose(BIN, ['decrypt', 'ctxt.pem', '--sig', '--wallet', 'bob'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('i am bob\n')
.end(function (code) {
assert(!code)
assert(stdout)
assert(valid)
done()
})
proc
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
stdout = Buffer.concat(chunks).toString('utf8')
assert.deepEqual(stdout, msg)
valid = true
})
//proc.stderr.pipe(process.stderr)
})
it('alice sign', function (done) {
var chunks = [], valid = false
msg = 'Hi,\n\nThis is my message...\n\nRegards,\Alice\n'
var proc = suppose(BIN, ['sign', '--wallet', 'alice', 'alice.jpg'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.when('Wallet is encrypted.\nEnter passphrase: ').respond('not a blonde\n')
.end(function (code) {
assert(!code)
assert(valid)
done()
})
.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function () {
var stdout = Buffer.concat(chunks).toString('utf8')
var match = stdout.match(/Wrote signature to alice\.jpg\.salty-sig/)
assert(match)
valid = true
})
})
var stderr
it('bob verify', function (done) {
var chunks = [], valid = false
var proc = suppose(BIN, ['verify', '--wallet', 'bob', 'alice.jpg'], {cwd: tmpDir, debug: fs.createWriteStream('/tmp/debug.txt')})
.end(function (code) {
assert(!code)
assert(stdout)
assert(valid)
done()
})
.stderr.on('data', function (chunk) {
chunks.push(chunk)
})
.once('end', function (code) {
stderr = Buffer.concat(chunks).toString('utf8')
var match = stderr.match(/\(verified\)/)
assert(match)
valid = true
})
})
/* errors
- init over existing dir
- import error
- restore bad pw
- open wallet bad pw
- encrypt noent, encrypt bad recip
- decrypt noent
- decrypt fail (bad hash, bad sig, box open)
- verify fail
edge cases
- init in home/specified dir
- init without email
- init without name
- weird chars in id?
- import from url/file
- encrypt for self
- force flag
- armor flag
- delete flag
- sign/verify path detection
*/
})