common_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2011 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. package ssh
  5. import (
  6. "crypto/dsa"
  7. "crypto/ecdsa"
  8. "crypto/elliptic"
  9. "crypto/rsa"
  10. "errors"
  11. "testing"
  12. )
  13. func TestSafeString(t *testing.T) {
  14. strings := map[string]string{
  15. "\x20\x0d\x0a": "\x20\x0d\x0a",
  16. "flibble": "flibble",
  17. "new\x20line": "new\x20line",
  18. "123456\x07789": "123456 789",
  19. "\t\t\x10\r\n": "\t\t \r\n",
  20. }
  21. for s, expected := range strings {
  22. actual := safeString(s)
  23. if expected != actual {
  24. t.Errorf("expected: %v, actual: %v", []byte(expected), []byte(actual))
  25. }
  26. }
  27. }
  28. func TestAlgoNameSupported(t *testing.T) {
  29. supported := map[string]interface{}{
  30. KeyAlgoRSA: new(rsa.PublicKey),
  31. KeyAlgoDSA: new(dsa.PublicKey),
  32. KeyAlgoECDSA256: &ecdsa.PublicKey{Curve: elliptic.P256()},
  33. KeyAlgoECDSA384: &ecdsa.PublicKey{Curve: elliptic.P384()},
  34. KeyAlgoECDSA521: &ecdsa.PublicKey{Curve: elliptic.P521()},
  35. CertAlgoRSAv01: &OpenSSHCertV01{Key: new(rsa.PublicKey)},
  36. CertAlgoDSAv01: &OpenSSHCertV01{Key: new(dsa.PublicKey)},
  37. CertAlgoECDSA256v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P256()}},
  38. CertAlgoECDSA384v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P384()}},
  39. CertAlgoECDSA521v01: &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P521()}},
  40. }
  41. for expected, key := range supported {
  42. actual := algoName(key)
  43. if expected != actual {
  44. t.Errorf("expected: %s, actual: %s", expected, actual)
  45. }
  46. }
  47. }
  48. func TestAlgoNameNotSupported(t *testing.T) {
  49. notSupported := []interface{}{
  50. &ecdsa.PublicKey{Curve: elliptic.P224()},
  51. &OpenSSHCertV01{Key: &ecdsa.PublicKey{Curve: elliptic.P224()}},
  52. }
  53. panicTest := func(key interface{}) (algo string, err error) {
  54. defer func() {
  55. if r := recover(); r != nil {
  56. err = errors.New(r.(string))
  57. }
  58. }()
  59. algo = algoName(key)
  60. return
  61. }
  62. for _, unsupportedKey := range notSupported {
  63. if algo, err := panicTest(unsupportedKey); err == nil {
  64. t.Errorf("Expected a panic, Got: %s (for type %T)", algo, unsupportedKey)
  65. }
  66. }
  67. }