github.com/glycerine/xcryptossh@v7.0.4+incompatible/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  	"context"
    11  	"crypto/rand"
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  func TestKexes(t *testing.T) {
    17  	defer xtestend(xtestbegin(t))
    18  
    19  	type kexResultErr struct {
    20  		result *kexResult
    21  		err    error
    22  	}
    23  
    24  	for name, kex := range kexAlgoMap {
    25  		a, b := memPipe()
    26  
    27  		s := make(chan kexResultErr, 1)
    28  		c := make(chan kexResultErr, 1)
    29  		var magics handshakeMagics
    30  		ctx := context.Background()
    31  
    32  		go func() {
    33  			r, e := kex.Client(ctx, a, rand.Reader, &magics)
    34  			a.Close()
    35  			c <- kexResultErr{r, e}
    36  		}()
    37  		go func() {
    38  			r, e := kex.Server(ctx, b, rand.Reader, &magics, testSigners["ecdsa"])
    39  			b.Close()
    40  			s <- kexResultErr{r, e}
    41  		}()
    42  
    43  		clientRes := <-c
    44  		serverRes := <-s
    45  		if clientRes.err != nil {
    46  			t.Errorf("client: %v", clientRes.err)
    47  		}
    48  		if serverRes.err != nil {
    49  			t.Errorf("server: %v", serverRes.err)
    50  		}
    51  		if !reflect.DeepEqual(clientRes.result, serverRes.result) {
    52  			t.Errorf("kex %q: mismatch %#v, %#v", name, clientRes.result, serverRes.result)
    53  		}
    54  	}
    55  }