@@ -3,11 +3,12 @@ var tape = require('tape')
33var pull = require ( 'pull-stream' )
44var ssbKeys = require ( 'ssb-keys' )
55var box1 = require ( 'ssb-private1/box1' )
6+ const { promisify } = require ( 'util' )
67
78var createSSB = require ( './create-ssb' )
89var { originalValue } = require ( '../util' )
910
10- module . exports = function ( opts ) {
11+ module . exports = function ( ) {
1112 var alice = ssbKeys . generate ( )
1213 var bob = ssbKeys . generate ( )
1314 var charles = ssbKeys . generate ( )
@@ -34,13 +35,12 @@ module.exports = function (opts) {
3435 tape ( 'error when trying to encrypt without boxer' , ( t ) => {
3536 t . plan ( 2 ) ;
3637 const darlene = ssbKeys . generate ( )
37- const darleneSSB = createSSB ( 'test-ssb-darlene' , { keys : darlene } )
3838 const darleneFeed = ssb . createFeed ( darlene )
3939 darleneFeed . add (
4040 { type : "error" , recps : [ alice , darlene ] } ,
4141 ( err , msg ) => {
42- t . ok ( err ) ;
43- t . notOk ( msg ) ;
42+ t . ok ( err ) ;
43+ t . notOk ( msg ) ;
4444 t . end ( )
4545 } )
4646 } )
@@ -52,7 +52,7 @@ module.exports = function (opts) {
5252 var postObserved
5353 var listener = ssb . post ( msg => { postObserved = msg } )
5454
55- feed . add ( boxed , function ( err , msg ) {
55+ feed . add ( boxed , function ( err ) {
5656 if ( err ) throw err
5757 t . notOk ( err )
5858
@@ -145,7 +145,7 @@ module.exports = function (opts) {
145145 var listener = ssb . post ( msg => { postObserved = msg } )
146146
147147 // secret message sent to self
148- feed . add ( { type : 'secret2' , secret : "it's a secret!" , recps : feed . id } , function ( err , msg ) {
148+ feed . add ( { type : 'secret2' , secret : "it's a secret!" , recps : feed . id } , function ( err ) {
149149 if ( err ) throw err
150150 t . notOk ( err )
151151
@@ -165,7 +165,7 @@ module.exports = function (opts) {
165165 )
166166
167167 listener ( )
168- t . true ( typeof postObserved . value . content === 'string' , 'post obs messages should not be decrypted' )
168+ t . true ( typeof postObserved . value . content === 'string' , 'db. post obs messages should not be decrypted' )
169169
170170 t . end ( )
171171 } )
@@ -264,7 +264,7 @@ module.exports = function (opts) {
264264 } )
265265
266266 tape ( 'addUnboxer (simple)' , function ( t ) {
267- const unboxer = function ( ciphertext , value ) {
267+ const unboxer = function ( ciphertext ) {
268268 if ( ! ciphertext . endsWith ( '.box.hah' ) ) return
269269
270270 const base64 = ciphertext . replace ( '.box.hah' , '' )
@@ -301,12 +301,12 @@ module.exports = function (opts) {
301301 done ( )
302302 } , 500 )
303303 } ,
304- key : function ( ciphertext , value ) {
304+ key : function ( ciphertext ) {
305305 if ( ! ciphertext . endsWith ( '.box.hah' ) ) return
306306
307307 return '"the msgKey"'
308308 } ,
309- value : function ( ciphertext , msgKey ) {
309+ value : function ( ciphertext ) {
310310 const base64 = ciphertext . replace ( '.box.hah' , '' )
311311 return JSON . parse (
312312 Buffer . from ( base64 , 'base64' ) . toString ( 'utf8' )
@@ -319,19 +319,87 @@ module.exports = function (opts) {
319319 const content = {
320320 type : 'poke' ,
321321 reason : 'why not' ,
322- recps : [ '!test' ]
322+ recps : [ '!test' ] ,
323+ myFriend : alice . id // Necessary to test links()
323324 }
324325 const ciphertext = Buffer . from ( JSON . stringify ( content ) ) . toString ( 'base64' ) + '.box.hah'
325326
326327 feed . publish ( ciphertext , ( _ , msg ) => {
327328 t . true ( initDone , 'unboxer completed initialisation before publish' )
328329
329- ssb . get ( { id : msg . key , private : true , meta : true } , ( err , msg ) => {
330- if ( err ) throw err
330+ ssb . get ( { id : msg . key , private : true , meta : true } , async ( err , msg ) => {
331+ t . error ( err )
331332
332333 t . true ( initDone , 'unboxer completed initialisation before get' )
333334 t . deepEqual ( msg . value . content , content , 'auto unboxing works' )
335+
336+ const assertBoxed = ( methodName , message ) => {
337+ if ( typeof message . key === 'string' ) {
338+ t . equal ( message . key , msg . key , `${ methodName } () returned correct message` )
339+ t . equal ( typeof message . value . content , 'string' , `${ methodName } () does not unbox by default` )
340+ } else {
341+ t . equal ( typeof message . content , 'string' , `${ methodName } () does not unbox by default` )
342+ }
343+ }
344+
345+ const assertBoxedAsync = async ( methodName , options ) => {
346+ assertBoxed ( methodName , await promisify ( ssb [ methodName ] ) ( options ) )
347+ if ( typeof options === 'object' && Array . isArray ( options ) === false ) {
348+ assertBoxed ( methodName , await promisify ( ssb [ methodName ] ) ( { ...options , private : false } ) )
349+ }
350+ }
351+
352+ // This tests the default behavior of `ssb.get()`, which should never
353+ // decrypt messages by default. This is **very important**.
354+ await assertBoxedAsync ( 'get' , msg . key )
355+ await assertBoxedAsync ( 'get' , { id : msg . key } )
356+ await assertBoxedAsync ( 'get' , { id : msg . key , meta : true } )
357+ await assertBoxedAsync ( 'getAtSequence' , [ msg . value . author , msg . value . sequence ] )
358+ await assertBoxedAsync ( 'getLatest' , msg . value . author )
359+
360+ const assertBoxedSourceOnce = ( methodName , options ) => new Promise ( ( resolve ) => {
361+ pull (
362+ ssb [ methodName ] ( options ) ,
363+ pull . collect ( ( err , val ) => {
364+ t . error ( err , `${ methodName } () does not error` )
365+ switch ( methodName ) {
366+ case 'createRawLogStream' :
367+ assertBoxed ( methodName , val [ 0 ] . value )
368+ break ;
369+ case 'createFeedStream' :
370+ case 'createUserStream' :
371+ case 'messagesByType' :
372+ // Apparently some methods take `{ private: false }` to mean
373+ // "don't return any private messages". :/
374+ if ( options . private === undefined ) {
375+ assertBoxed ( methodName , val [ 0 ] . value )
376+ }
377+ break
378+ default :
379+ assertBoxed ( methodName , val [ 0 ] )
380+ }
381+ resolve ( )
382+ } )
383+ )
384+ } )
385+
386+ // Test the default **and** `{ private: false }`.
387+ const assertBoxedSource = async ( methodName , options ) => {
388+ await assertBoxedSourceOnce ( methodName , options )
389+ await assertBoxedSourceOnce ( methodName , { ...options , private : false } )
390+ }
391+
392+ await assertBoxedSource ( 'createLogStream' , { limit : 1 , reverse : true } )
393+ await assertBoxedSource ( 'createHistoryStream' , { id : msg . value . author , seq : msg . value . sequence , reverse : true } )
394+ await assertBoxedSource ( 'messagesByType' , { type : 'poke' , limit : 1 , reverse : true } )
395+ await assertBoxedSource ( 'createFeedStream' , { id : msg . value . author , seq : msg . value . sequence , reverse : true } )
396+ await assertBoxedSource ( 'createUserStream' , { id : msg . value . author , seq : msg . value . sequence , reverse : true } )
397+ await assertBoxedSource ( 'links' , { source : msg . value . author , limit : 1 , values : true } )
398+ await assertBoxedSource ( 'createRawLogStream' , { source : msg . value . author , limit : 1 , reverse : true , values : true } )
399+ // createRawLogStream currently not exported as a method
400+
334401 t . end ( )
402+
335403 } )
336404 } )
337405 } )
0 commit comments