testdata_test.go 2.1 KB

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