github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/test/testdata_test.go (about)

     1  // Copyright 2014 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  // IMPLEMENTATION NOTE: To avoid a package loop, this file is in three places:
     6  // ssh/, ssh/agent, and ssh/test/. It should be kept in sync across all three
     7  // instances.
     8  
     9  package test
    10  
    11  import (
    12  	"crypto/rand"
    13  	"fmt"
    14  
    15  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
    16  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh/testdata"
    17  )
    18  
    19  var (
    20  	testPrivateKeys map[string]interface{}
    21  	testSigners     map[string]ssh.Signer
    22  	testPublicKeys  map[string]ssh.PublicKey
    23  )
    24  
    25  func init() {
    26  	var err error
    27  
    28  	n := len(testdata.PEMBytes)
    29  	testPrivateKeys = make(map[string]interface{}, n)
    30  	testSigners = make(map[string]ssh.Signer, n)
    31  	testPublicKeys = make(map[string]ssh.PublicKey, n)
    32  	for t, k := range testdata.PEMBytes {
    33  		testPrivateKeys[t], err = ssh.ParseRawPrivateKey(k)
    34  		if err != nil {
    35  			panic(fmt.Sprintf("Unable to parse test key %s: %v", t, err))
    36  		}
    37  		testSigners[t], err = ssh.NewSignerFromKey(testPrivateKeys[t])
    38  		if err != nil {
    39  			panic(fmt.Sprintf("Unable to create signer for test key %s: %v", t, err))
    40  		}
    41  		testPublicKeys[t] = testSigners[t].PublicKey()
    42  	}
    43  
    44  	// Create a cert and sign it for use in tests.
    45  	testCert := &ssh.Certificate{
    46  		Nonce:           []byte{},                       // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
    47  		ValidPrincipals: []string{"gopher1", "gopher2"}, // increases test coverage
    48  		ValidAfter:      0,                              // unix epoch
    49  		ValidBefore:     ssh.CertTimeInfinity,           // The end of currently representable time.
    50  		Reserved:        []byte{},                       // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
    51  		Key:             testPublicKeys["ecdsa"],
    52  		SignatureKey:    testPublicKeys["rsa"],
    53  		Permissions: ssh.Permissions{
    54  			CriticalOptions: map[string]string{},
    55  			Extensions:      map[string]string{},
    56  		},
    57  	}
    58  	testCert.SignCert(rand.Reader, testSigners["rsa"])
    59  	testPrivateKeys["cert"] = testPrivateKeys["ecdsa"]
    60  	testSigners["cert"], err = ssh.NewCertSigner(testCert, testSigners["ecdsa"])
    61  	if err != nil {
    62  		panic(fmt.Sprintf("Unable to create certificate signer: %v", err))
    63  	}
    64  }