root_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "net/http"
  5. "reflect"
  6. "testing"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/assert"
  9. "github.com/smallstep/cli/crypto/pemutil"
  10. )
  11. func TestRoot(t *testing.T) {
  12. a := testAuthority(t)
  13. a.certificates.Store("invaliddata", "a string") // invalid cert for testing
  14. tests := map[string]struct {
  15. sum string
  16. err *apiError
  17. }{
  18. "not-found": {"foo", &apiError{errors.New("certificate with fingerprint foo was not found"), http.StatusNotFound, apiCtx{}}},
  19. "invalid-stored-certificate": {"invaliddata", &apiError{errors.New("stored value is not a *x509.Certificate"), http.StatusInternalServerError, apiCtx{}}},
  20. "success": {"189f573cfa159251e445530847ef80b1b62a3a380ee670dcb49e33ed34da0616", nil},
  21. }
  22. for name, tc := range tests {
  23. t.Run(name, func(t *testing.T) {
  24. crt, err := a.Root(tc.sum)
  25. if err != nil {
  26. if assert.NotNil(t, tc.err) {
  27. switch v := err.(type) {
  28. case *apiError:
  29. assert.HasPrefix(t, v.err.Error(), tc.err.Error())
  30. assert.Equals(t, v.code, tc.err.code)
  31. assert.Equals(t, v.context, tc.err.context)
  32. default:
  33. t.Errorf("unexpected error type: %T", v)
  34. }
  35. }
  36. } else {
  37. if assert.Nil(t, tc.err) {
  38. assert.Equals(t, crt, a.rootX509Certs[0])
  39. }
  40. }
  41. })
  42. }
  43. }
  44. func TestAuthority_GetRootCertificate(t *testing.T) {
  45. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. tests := []struct {
  50. name string
  51. want *x509.Certificate
  52. }{
  53. {"ok", cert},
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. a := testAuthority(t)
  58. if got := a.GetRootCertificate(); !reflect.DeepEqual(got, tt.want) {
  59. t.Errorf("Authority.GetRootCertificate() = %v, want %v", got, tt.want)
  60. }
  61. })
  62. }
  63. }
  64. func TestAuthority_GetRootCertificates(t *testing.T) {
  65. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. tests := []struct {
  70. name string
  71. want []*x509.Certificate
  72. }{
  73. {"ok", []*x509.Certificate{cert}},
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. a := testAuthority(t)
  78. if got := a.GetRootCertificates(); !reflect.DeepEqual(got, tt.want) {
  79. t.Errorf("Authority.GetRootCertificates() = %v, want %v", got, tt.want)
  80. }
  81. })
  82. }
  83. }
  84. func TestAuthority_GetRoots(t *testing.T) {
  85. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. tests := []struct {
  90. name string
  91. want []*x509.Certificate
  92. wantErr bool
  93. }{
  94. {"ok", []*x509.Certificate{cert}, false},
  95. }
  96. for _, tt := range tests {
  97. a := testAuthority(t)
  98. t.Run(tt.name, func(t *testing.T) {
  99. got, err := a.GetRoots()
  100. if (err != nil) != tt.wantErr {
  101. t.Errorf("Authority.GetRoots() error = %v, wantErr %v", err, tt.wantErr)
  102. return
  103. }
  104. if !reflect.DeepEqual(got, tt.want) {
  105. t.Errorf("Authority.GetRoots() = %v, want %v", got, tt.want)
  106. }
  107. })
  108. }
  109. }
  110. func TestAuthority_GetFederation(t *testing.T) {
  111. cert, err := pemutil.ReadCertificate("testdata/certs/root_ca.crt")
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. tests := []struct {
  116. name string
  117. wantFederation []*x509.Certificate
  118. wantErr bool
  119. fn func(a *Authority)
  120. }{
  121. {"ok", []*x509.Certificate{cert}, false, nil},
  122. {"fail", nil, true, func(a *Authority) {
  123. a.certificates.Store("foo", "bar")
  124. }},
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. a := testAuthority(t)
  129. if tt.fn != nil {
  130. tt.fn(a)
  131. }
  132. gotFederation, err := a.GetFederation()
  133. if (err != nil) != tt.wantErr {
  134. t.Errorf("Authority.GetFederation() error = %v, wantErr %v", err, tt.wantErr)
  135. return
  136. }
  137. if !reflect.DeepEqual(gotFederation, tt.wantFederation) {
  138. t.Errorf("Authority.GetFederation() = %v, want %v", gotFederation, tt.wantFederation)
  139. }
  140. })
  141. }
  142. }