11/* eslint-env mocha */
22'use strict'
33
4- const chai = require ( 'chai' )
5- const dirtyChai = require ( 'dirty-chai' )
6- const expect = chai . expect
7- chai . use ( dirtyChai )
4+ const { expect } = require ( 'aegir/utils/chai' )
85
96const PeerId = require ( 'peer-id' )
107const duplexPair = require ( 'it-pair/duplex' )
@@ -22,6 +19,7 @@ const {
2219const { createBoxStream, createUnboxStream } = require ( '../src/etm' )
2320const State = require ( '../src/state' )
2421const { Propose } = require ( '../src/handshake/secio.proto' )
22+ const uint8ArrayConcat = require ( 'uint8arrays/concat' )
2523
2624describe ( 'secio' , ( ) => {
2725 let remotePeer
@@ -43,13 +41,16 @@ describe('secio', () => {
4341 const proposal = createProposal ( state )
4442
4543 // Send our proposal
46- const proposalLength = Buffer . allocUnsafe ( 4 )
47- proposalLength . writeInt32BE ( proposal . length , 0 )
48- wrap . write ( Buffer . concat ( [ proposalLength , proposal ] ) )
44+ const proposalBuffer = new ArrayBuffer ( 4 )
45+ const proposalLengthView = new DataView ( proposalBuffer )
46+ proposalLengthView . setInt32 ( 0 , proposal . length )
47+ const proposalLength = new Uint8Array ( proposalBuffer )
48+ wrap . write ( uint8ArrayConcat ( [ proposalLength , proposal ] ) )
4949
5050 // Read their proposal
5151 let theirProposalRaw = ( await wrap . read ( ) ) . slice ( )
52- let dataLength = theirProposalRaw . readInt32BE ( 0 )
52+ const theirProposalRawView = new DataView ( theirProposalRaw . buffer , theirProposalRaw . byteOffset , theirProposalRaw . byteLength )
53+ let dataLength = theirProposalRawView . getInt32 ( 0 )
5354 theirProposalRaw = theirProposalRaw . slice ( 4 , dataLength + 4 )
5455 const theirProposal = Propose . decode ( theirProposalRaw )
5556 expect ( theirProposal . rand ) . to . have . length ( 16 )
@@ -68,13 +69,16 @@ describe('secio', () => {
6869 const exchange = await createExchange ( state )
6970
7071 // Send our exchange
71- const exchangeLength = Buffer . allocUnsafe ( 4 )
72- exchangeLength . writeInt32BE ( exchange . length , 0 )
73- wrap . write ( Buffer . concat ( [ exchangeLength , exchange ] ) )
72+ const exchangeBuffer = new ArrayBuffer ( 4 )
73+ const exchangeLengthView = new DataView ( exchangeBuffer )
74+ exchangeLengthView . setInt32 ( 0 , exchange . length )
75+ const exchangeLength = new Uint8Array ( exchangeBuffer )
76+ wrap . write ( uint8ArrayConcat ( [ exchangeLength , exchange ] ) )
7477
7578 // Read their exchange
7679 let theirExchangeRaw = ( await wrap . read ( ) ) . slice ( )
77- dataLength = theirExchangeRaw . readInt32BE ( 0 )
80+ const theirExchangeRawView = new DataView ( theirExchangeRaw . buffer , theirExchangeRaw . byteOffset , theirExchangeRaw . byteLength )
81+ dataLength = theirExchangeRawView . getInt32 ( 0 )
7882 theirExchangeRaw = theirExchangeRaw . slice ( 4 , dataLength + 4 )
7983 await verify ( state , theirExchangeRaw )
8084
@@ -88,13 +92,18 @@ describe('secio', () => {
8892 // Send back their nonce over the crypto stream
8993 const { value : nonce } = await box ( [ state . proposal . in . rand ] ) . next ( )
9094 expect ( nonce . slice ( ) ) . to . not . eql ( state . proposal . in . rand ) // The nonce should be encrypted
91- const nonceLength = Buffer . allocUnsafe ( 4 )
92- nonceLength . writeInt32BE ( nonce . length , 0 )
93- wrap . write ( Buffer . concat ( [ nonceLength , nonce . slice ( ) ] ) )
95+
96+ const nonceBuffer = new ArrayBuffer ( 4 )
97+ const nonceView = new DataView ( nonceBuffer )
98+ nonceView . setInt32 ( 0 , nonce . length )
99+ const nonceLength = new Uint8Array ( nonceBuffer )
100+ wrap . write ( uint8ArrayConcat ( [ nonceLength , nonce . slice ( ) ] ) )
94101
95102 // Read our nonce from the crypto stream
96103 let ourNonceRaw = ( await wrap . read ( ) )
97- dataLength = ourNonceRaw . readInt32BE ( 0 )
104+ const ourNonceRawBuffer = ourNonceRaw . slice ( )
105+ const ourNonceRawView = new DataView ( ourNonceRawBuffer . buffer , ourNonceRawBuffer . byteOffset , ourNonceRawBuffer . byteLength )
106+ dataLength = ourNonceRawView . getInt32 ( 0 )
98107 ourNonceRaw = ourNonceRaw . shallowSlice ( 4 , dataLength + 4 ) // Unbox expects a BufferList, so shallow slice here
99108 expect ( ourNonceRaw . slice ( ) ) . to . not . eql ( state . proposal . out . rand ) // The nonce should be encrypted
100109 const { value : ourNonce } = await unbox ( [ ourNonceRaw ] ) . next ( )
@@ -121,13 +130,16 @@ describe('secio', () => {
121130 const proposal = createProposal ( state )
122131
123132 // Send our proposal
124- const proposalLength = Buffer . allocUnsafe ( 4 )
125- proposalLength . writeInt32BE ( proposal . length , 0 )
126- wrap . write ( Buffer . concat ( [ proposalLength , proposal ] ) )
133+ const proposalBuffer = new ArrayBuffer ( 4 )
134+ const proposalLengthView = new DataView ( proposalBuffer )
135+ proposalLengthView . setInt32 ( 0 , proposal . length )
136+ const proposalLength = new Uint8Array ( proposalBuffer )
137+ wrap . write ( uint8ArrayConcat ( [ proposalLength , proposal ] ) )
127138
128139 // Read their proposal
129140 let theirProposalRaw = ( await wrap . read ( ) ) . slice ( )
130- let dataLength = theirProposalRaw . readInt32BE ( 0 )
141+ const theirProposalRawView = new DataView ( theirProposalRaw . buffer , theirProposalRaw . byteOffset , theirProposalRaw . byteLength )
142+ let dataLength = theirProposalRawView . getInt32 ( 0 )
131143 theirProposalRaw = theirProposalRaw . slice ( 4 , dataLength + 4 )
132144 const theirProposal = Propose . decode ( theirProposalRaw )
133145 expect ( theirProposal . rand ) . to . have . length ( 16 )
@@ -146,13 +158,16 @@ describe('secio', () => {
146158 const exchange = await createExchange ( state )
147159
148160 // Send our exchange
149- const exchangeLength = Buffer . allocUnsafe ( 4 )
150- exchangeLength . writeInt32BE ( exchange . length , 0 )
151- wrap . write ( Buffer . concat ( [ exchangeLength , exchange ] ) )
161+ const exchangeBuffer = new ArrayBuffer ( 4 )
162+ const exchangeLengthView = new DataView ( exchangeBuffer )
163+ exchangeLengthView . setInt32 ( 0 , exchange . length )
164+ const exchangeLength = new Uint8Array ( exchangeBuffer )
165+ wrap . write ( uint8ArrayConcat ( [ exchangeLength , exchange ] ) )
152166
153167 // Read their exchange
154168 let theirExchangeRaw = ( await wrap . read ( ) ) . slice ( )
155- dataLength = theirExchangeRaw . readInt32BE ( 0 )
169+ const theirExchangeRawView = new DataView ( theirExchangeRaw . buffer , theirExchangeRaw . byteOffset , theirExchangeRaw . byteLength )
170+ dataLength = theirExchangeRawView . getInt32 ( 0 )
156171 theirExchangeRaw = theirExchangeRaw . slice ( 4 , dataLength + 4 )
157172 await verify ( state , theirExchangeRaw )
158173
@@ -166,13 +181,18 @@ describe('secio', () => {
166181 // Send back their nonce over the crypto stream
167182 const { value : nonce } = await box ( [ state . proposal . in . rand ] ) . next ( )
168183 expect ( nonce . slice ( ) ) . to . not . eql ( state . proposal . in . rand ) // The nonce should be encrypted
169- const nonceLength = Buffer . allocUnsafe ( 4 )
170- nonceLength . writeInt32BE ( nonce . length , 0 )
171- wrap . write ( Buffer . concat ( [ nonceLength , nonce . slice ( ) ] ) )
184+
185+ const nonceBuffer = new ArrayBuffer ( 4 )
186+ const nonceView = new DataView ( nonceBuffer )
187+ nonceView . setInt32 ( 0 , nonce . length )
188+ const nonceLength = new Uint8Array ( nonceBuffer )
189+ wrap . write ( uint8ArrayConcat ( [ nonceLength , nonce . slice ( ) ] ) )
172190
173191 // Read our nonce from the crypto stream
174192 let ourNonceRaw = ( await wrap . read ( ) )
175- dataLength = ourNonceRaw . readInt32BE ( 0 )
193+ const ourNonceRawBuffer = ourNonceRaw . slice ( )
194+ const ourNonceRawView = new DataView ( ourNonceRawBuffer . buffer , ourNonceRawBuffer . byteOffset , ourNonceRawBuffer . byteLength )
195+ dataLength = ourNonceRawView . getInt32 ( 0 )
176196 ourNonceRaw = ourNonceRaw . shallowSlice ( 4 , dataLength + 4 ) // Unbox expects a BufferList, so shallow slice here
177197 expect ( ourNonceRaw . slice ( ) ) . to . not . eql ( state . proposal . out . rand ) // The nonce should be encrypted
178198 const { value : ourNonce } = await unbox ( [ ourNonceRaw ] ) . next ( )
0 commit comments