root_test.go 3.8 KB

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