github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/kex_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssh 6 7 // Key exchange tests. 8 9 import ( 10 "crypto/rand" 11 "reflect" 12 "sync" 13 "testing" 14 ) 15 16 // Runs multiple key exchanges concurrent to detect potential data races with 17 // kex obtained from the global kexAlgoMap. 18 // This test needs to be executed using the race detector in order to detect 19 // race conditions. 20 func TestKexes(t *testing.T) { 21 type kexResultErr struct { 22 result *kexResult 23 err error 24 } 25 26 for name, kex := range kexAlgoMap { 27 t.Run(name, func(t *testing.T) { 28 wg := sync.WaitGroup{} 29 for i := 0; i < 3; i++ { 30 wg.Add(1) 31 go func() { 32 defer wg.Done() 33 a, b := memPipe() 34 35 s := make(chan kexResultErr, 1) 36 c := make(chan kexResultErr, 1) 37 var magics handshakeMagics 38 go func() { 39 r, e := kex.Client(a, rand.Reader, &magics) 40 a.Close() 41 c <- kexResultErr{r, e} 42 }() 43 go func() { 44 r, e := kex.Server(b, rand.Reader, &magics, testSigners["ecdsa"]) 45 b.Close() 46 s <- kexResultErr{r, e} 47 }() 48 49 clientRes := <-c 50 serverRes := <-s 51 if clientRes.err != nil { 52 t.Errorf("client: %v", clientRes.err) 53 } 54 if serverRes.err != nil { 55 t.Errorf("server: %v", serverRes.err) 56 } 57 if !reflect.DeepEqual(clientRes.result, serverRes.result) { 58 t.Errorf("kex %q: mismatch %#v, %#v", name, clientRes.result, serverRes.result) 59 } 60 }() 61 } 62 wg.Wait() 63 }) 64 } 65 }