testdata_test.go 2.2 KB

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