1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
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' )
8
5
9
6
const PeerId = require ( 'peer-id' )
10
7
const duplexPair = require ( 'it-pair/duplex' )
@@ -22,6 +19,7 @@ const {
22
19
const { createBoxStream, createUnboxStream } = require ( '../src/etm' )
23
20
const State = require ( '../src/state' )
24
21
const { Propose } = require ( '../src/handshake/secio.proto' )
22
+ const uint8ArrayConcat = require ( 'uint8arrays/concat' )
25
23
26
24
describe ( 'secio' , ( ) => {
27
25
let remotePeer
@@ -43,13 +41,16 @@ describe('secio', () => {
43
41
const proposal = createProposal ( state )
44
42
45
43
// 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 ] ) )
49
49
50
50
// Read their proposal
51
51
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 )
53
54
theirProposalRaw = theirProposalRaw . slice ( 4 , dataLength + 4 )
54
55
const theirProposal = Propose . decode ( theirProposalRaw )
55
56
expect ( theirProposal . rand ) . to . have . length ( 16 )
@@ -68,13 +69,16 @@ describe('secio', () => {
68
69
const exchange = await createExchange ( state )
69
70
70
71
// 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 ] ) )
74
77
75
78
// Read their exchange
76
79
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 )
78
82
theirExchangeRaw = theirExchangeRaw . slice ( 4 , dataLength + 4 )
79
83
await verify ( state , theirExchangeRaw )
80
84
@@ -88,13 +92,18 @@ describe('secio', () => {
88
92
// Send back their nonce over the crypto stream
89
93
const { value : nonce } = await box ( [ state . proposal . in . rand ] ) . next ( )
90
94
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 ( ) ] ) )
94
101
95
102
// Read our nonce from the crypto stream
96
103
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 )
98
107
ourNonceRaw = ourNonceRaw . shallowSlice ( 4 , dataLength + 4 ) // Unbox expects a BufferList, so shallow slice here
99
108
expect ( ourNonceRaw . slice ( ) ) . to . not . eql ( state . proposal . out . rand ) // The nonce should be encrypted
100
109
const { value : ourNonce } = await unbox ( [ ourNonceRaw ] ) . next ( )
@@ -121,13 +130,16 @@ describe('secio', () => {
121
130
const proposal = createProposal ( state )
122
131
123
132
// 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 ] ) )
127
138
128
139
// Read their proposal
129
140
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 )
131
143
theirProposalRaw = theirProposalRaw . slice ( 4 , dataLength + 4 )
132
144
const theirProposal = Propose . decode ( theirProposalRaw )
133
145
expect ( theirProposal . rand ) . to . have . length ( 16 )
@@ -146,13 +158,16 @@ describe('secio', () => {
146
158
const exchange = await createExchange ( state )
147
159
148
160
// 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 ] ) )
152
166
153
167
// Read their exchange
154
168
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 )
156
171
theirExchangeRaw = theirExchangeRaw . slice ( 4 , dataLength + 4 )
157
172
await verify ( state , theirExchangeRaw )
158
173
@@ -166,13 +181,18 @@ describe('secio', () => {
166
181
// Send back their nonce over the crypto stream
167
182
const { value : nonce } = await box ( [ state . proposal . in . rand ] ) . next ( )
168
183
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 ( ) ] ) )
172
190
173
191
// Read our nonce from the crypto stream
174
192
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 )
176
196
ourNonceRaw = ourNonceRaw . shallowSlice ( 4 , dataLength + 4 ) // Unbox expects a BufferList, so shallow slice here
177
197
expect ( ourNonceRaw . slice ( ) ) . to . not . eql ( state . proposal . out . rand ) // The nonce should be encrypted
178
198
const { value : ourNonce } = await unbox ( [ ourNonceRaw ] ) . next ( )
0 commit comments