@@ -25,6 +25,10 @@ import (
25
25
tmrand "github.com/tendermint/tendermint/libs/rand"
26
26
)
27
27
28
+ // Run go test -update from within this module
29
+ // to update the golden test vector file
30
+ var update = flag .Bool ("update" , false , "update .golden files" )
31
+
28
32
type kvstoreConn struct {
29
33
* io.PipeReader
30
34
* io.PipeWriter
@@ -39,60 +43,14 @@ func (drw kvstoreConn) Close() (err error) {
39
43
return err1
40
44
}
41
45
42
- // Each returned ReadWriteCloser is akin to a net.Connection
43
- func makeKVStoreConnPair () (fooConn , barConn kvstoreConn ) {
44
- barReader , fooWriter := io .Pipe ()
45
- fooReader , barWriter := io .Pipe ()
46
- return kvstoreConn {fooReader , fooWriter }, kvstoreConn {barReader , barWriter }
46
+ type privKeyWithNilPubKey struct {
47
+ orig crypto.PrivKey
47
48
}
48
49
49
- func makeSecretConnPair (tb testing.TB ) (fooSecConn , barSecConn * SecretConnection ) {
50
-
51
- var fooConn , barConn = makeKVStoreConnPair ()
52
- var fooPrvKey = ed25519 .GenPrivKey ()
53
- var fooPubKey = fooPrvKey .PubKey ()
54
- var barPrvKey = ed25519 .GenPrivKey ()
55
- var barPubKey = barPrvKey .PubKey ()
56
-
57
- // Make connections from both sides in parallel.
58
- var trs , ok = async .Parallel (
59
- func (_ int ) (val interface {}, abort bool , err error ) {
60
- fooSecConn , err = MakeSecretConnection (fooConn , fooPrvKey )
61
- if err != nil {
62
- tb .Errorf ("failed to establish SecretConnection for foo: %v" , err )
63
- return nil , true , err
64
- }
65
- remotePubBytes := fooSecConn .RemotePubKey ()
66
- if ! remotePubBytes .Equals (barPubKey ) {
67
- err = fmt .Errorf ("unexpected fooSecConn.RemotePubKey. Expected %v, got %v" ,
68
- barPubKey , fooSecConn .RemotePubKey ())
69
- tb .Error (err )
70
- return nil , false , err
71
- }
72
- return nil , false , nil
73
- },
74
- func (_ int ) (val interface {}, abort bool , err error ) {
75
- barSecConn , err = MakeSecretConnection (barConn , barPrvKey )
76
- if barSecConn == nil {
77
- tb .Errorf ("failed to establish SecretConnection for bar: %v" , err )
78
- return nil , true , err
79
- }
80
- remotePubBytes := barSecConn .RemotePubKey ()
81
- if ! remotePubBytes .Equals (fooPubKey ) {
82
- err = fmt .Errorf ("unexpected barSecConn.RemotePubKey. Expected %v, got %v" ,
83
- fooPubKey , barSecConn .RemotePubKey ())
84
- tb .Error (err )
85
- return nil , false , nil
86
- }
87
- return nil , false , nil
88
- },
89
- )
90
-
91
- require .Nil (tb , trs .FirstError ())
92
- require .True (tb , ok , "Unexpected task abortion" )
93
-
94
- return fooSecConn , barSecConn
95
- }
50
+ func (pk privKeyWithNilPubKey ) Bytes () []byte { return pk .orig .Bytes () }
51
+ func (pk privKeyWithNilPubKey ) Sign (msg []byte ) ([]byte , error ) { return pk .orig .Sign (msg ) }
52
+ func (pk privKeyWithNilPubKey ) PubKey () crypto.PubKey { return nil }
53
+ func (pk privKeyWithNilPubKey ) Equals (pk2 crypto.PrivKey ) bool { return pk .orig .Equals (pk2 ) }
96
54
97
55
func TestSecretConnectionHandshake (t * testing.T ) {
98
56
fooSecConn , barSecConn := makeSecretConnPair (t )
@@ -148,26 +106,6 @@ func TestConcurrentRead(t *testing.T) {
148
106
}
149
107
}
150
108
151
- func writeLots (t * testing.T , wg * sync.WaitGroup , conn io.Writer , txt string , n int ) {
152
- defer wg .Done ()
153
- for i := 0 ; i < n ; i ++ {
154
- _ , err := conn .Write ([]byte (txt ))
155
- if err != nil {
156
- t .Errorf ("failed to write to fooSecConn: %v" , err )
157
- return
158
- }
159
- }
160
- }
161
-
162
- func readLots (t * testing.T , wg * sync.WaitGroup , conn io.Reader , n int ) {
163
- readBuffer := make ([]byte , dataMaxSize )
164
- for i := 0 ; i < n ; i ++ {
165
- _ , err := conn .Read (readBuffer )
166
- assert .NoError (t , err )
167
- }
168
- wg .Done ()
169
- }
170
-
171
109
func TestSecretConnectionReadWrite (t * testing.T ) {
172
110
fooConn , barConn := makeKVStoreConnPair ()
173
111
fooWrites , barWrites := []string {}, []string {}
@@ -282,13 +220,8 @@ func TestSecretConnectionReadWrite(t *testing.T) {
282
220
283
221
compareWritesReads (fooWrites , barReads )
284
222
compareWritesReads (barWrites , fooReads )
285
-
286
223
}
287
224
288
- // Run go test -update from within this module
289
- // to update the golden test vector file
290
- var update = flag .Bool ("update" , false , "update .golden files" )
291
-
292
225
func TestDeriveSecretsAndChallengeGolden (t * testing.T ) {
293
226
goldenFilepath := filepath .Join ("testdata" , t .Name ()+ ".golden" )
294
227
if * update {
@@ -322,15 +255,6 @@ func TestDeriveSecretsAndChallengeGolden(t *testing.T) {
322
255
}
323
256
}
324
257
325
- type privKeyWithNilPubKey struct {
326
- orig crypto.PrivKey
327
- }
328
-
329
- func (pk privKeyWithNilPubKey ) Bytes () []byte { return pk .orig .Bytes () }
330
- func (pk privKeyWithNilPubKey ) Sign (msg []byte ) ([]byte , error ) { return pk .orig .Sign (msg ) }
331
- func (pk privKeyWithNilPubKey ) PubKey () crypto.PubKey { return nil }
332
- func (pk privKeyWithNilPubKey ) Equals (pk2 crypto.PrivKey ) bool { return pk .orig .Equals (pk2 ) }
333
-
334
258
func TestNilPubkey (t * testing.T ) {
335
259
var fooConn , barConn = makeKVStoreConnPair ()
336
260
var fooPrvKey = ed25519 .GenPrivKey ()
@@ -367,6 +291,26 @@ func TestNonEd25519Pubkey(t *testing.T) {
367
291
})
368
292
}
369
293
294
+ func writeLots (t * testing.T , wg * sync.WaitGroup , conn io.Writer , txt string , n int ) {
295
+ defer wg .Done ()
296
+ for i := 0 ; i < n ; i ++ {
297
+ _ , err := conn .Write ([]byte (txt ))
298
+ if err != nil {
299
+ t .Errorf ("failed to write to fooSecConn: %v" , err )
300
+ return
301
+ }
302
+ }
303
+ }
304
+
305
+ func readLots (t * testing.T , wg * sync.WaitGroup , conn io.Reader , n int ) {
306
+ readBuffer := make ([]byte , dataMaxSize )
307
+ for i := 0 ; i < n ; i ++ {
308
+ _ , err := conn .Read (readBuffer )
309
+ assert .NoError (t , err )
310
+ }
311
+ wg .Done ()
312
+ }
313
+
370
314
// Creates the data for a test vector file.
371
315
// The file format is:
372
316
// Hex(diffie_hellman_secret), loc_is_least, Hex(recvSecret), Hex(sendSecret), Hex(challenge)
@@ -386,6 +330,65 @@ func createGoldenTestVectors(t *testing.T) string {
386
330
return data
387
331
}
388
332
333
+ // Each returned ReadWriteCloser is akin to a net.Connection
334
+ func makeKVStoreConnPair () (fooConn , barConn kvstoreConn ) {
335
+ barReader , fooWriter := io .Pipe ()
336
+ fooReader , barWriter := io .Pipe ()
337
+ return kvstoreConn {fooReader , fooWriter }, kvstoreConn {barReader , barWriter }
338
+ }
339
+
340
+ func makeSecretConnPair (tb testing.TB ) (fooSecConn , barSecConn * SecretConnection ) {
341
+ var (
342
+ fooConn , barConn = makeKVStoreConnPair ()
343
+ fooPrvKey = ed25519 .GenPrivKey ()
344
+ fooPubKey = fooPrvKey .PubKey ()
345
+ barPrvKey = ed25519 .GenPrivKey ()
346
+ barPubKey = barPrvKey .PubKey ()
347
+ )
348
+
349
+ // Make connections from both sides in parallel.
350
+ var trs , ok = async .Parallel (
351
+ func (_ int ) (val interface {}, abort bool , err error ) {
352
+ fooSecConn , err = MakeSecretConnection (fooConn , fooPrvKey )
353
+ if err != nil {
354
+ tb .Errorf ("failed to establish SecretConnection for foo: %v" , err )
355
+ return nil , true , err
356
+ }
357
+ remotePubBytes := fooSecConn .RemotePubKey ()
358
+ if ! remotePubBytes .Equals (barPubKey ) {
359
+ err = fmt .Errorf ("unexpected fooSecConn.RemotePubKey. Expected %v, got %v" ,
360
+ barPubKey , fooSecConn .RemotePubKey ())
361
+ tb .Error (err )
362
+ return nil , true , err
363
+ }
364
+ return nil , false , nil
365
+ },
366
+ func (_ int ) (val interface {}, abort bool , err error ) {
367
+ barSecConn , err = MakeSecretConnection (barConn , barPrvKey )
368
+ if barSecConn == nil {
369
+ tb .Errorf ("failed to establish SecretConnection for bar: %v" , err )
370
+ return nil , true , err
371
+ }
372
+ remotePubBytes := barSecConn .RemotePubKey ()
373
+ if ! remotePubBytes .Equals (fooPubKey ) {
374
+ err = fmt .Errorf ("unexpected barSecConn.RemotePubKey. Expected %v, got %v" ,
375
+ fooPubKey , barSecConn .RemotePubKey ())
376
+ tb .Error (err )
377
+ return nil , true , err
378
+ }
379
+ return nil , false , nil
380
+ },
381
+ )
382
+
383
+ require .Nil (tb , trs .FirstError ())
384
+ require .True (tb , ok , "Unexpected task abortion" )
385
+
386
+ return fooSecConn , barSecConn
387
+ }
388
+
389
+ ///////////////////////////////////////////////////////////////////////////////
390
+ // Benchmarks
391
+
389
392
func BenchmarkWriteSecretConnection (b * testing.B ) {
390
393
b .StopTimer ()
391
394
b .ReportAllocs ()
0 commit comments