api_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Package api implements a SCEP HTTP server.
  2. package api
  3. import (
  4. "bytes"
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "reflect"
  9. "testing"
  10. "testing/iotest"
  11. "github.com/smallstep/certificates/authority/config"
  12. "github.com/smallstep/certificates/authority/provisioner"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "go.step.sm/linkedca"
  16. )
  17. func Test_decodeRequest(t *testing.T) {
  18. type args struct {
  19. r *http.Request
  20. }
  21. tests := []struct {
  22. name string
  23. args args
  24. want request
  25. wantErr bool
  26. }{
  27. {
  28. name: "fail/unsupported-method",
  29. args: args{
  30. r: httptest.NewRequest(http.MethodPatch, "http://scep:8080/?operation=AnUnsupportOperation", nil),
  31. },
  32. want: request{},
  33. wantErr: true,
  34. },
  35. {
  36. name: "fail/get-unsupported-operation",
  37. args: args{
  38. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=AnUnsupportOperation", nil),
  39. },
  40. want: request{},
  41. wantErr: true,
  42. },
  43. {
  44. name: "fail/get-PKIOperation",
  45. args: args{
  46. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message='somewronginput'", nil),
  47. },
  48. want: request{},
  49. wantErr: true,
  50. },
  51. {
  52. name: "fail/post-PKIOperation",
  53. args: args{
  54. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", iotest.ErrReader(errors.New("a read error"))),
  55. },
  56. want: request{},
  57. wantErr: true,
  58. },
  59. {
  60. name: "ok/get-GetCACert",
  61. args: args{
  62. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACert", nil),
  63. },
  64. want: request{
  65. Operation: "GetCACert",
  66. Message: []byte{},
  67. },
  68. wantErr: false,
  69. },
  70. {
  71. name: "ok/get-GetCACaps",
  72. args: args{
  73. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACaps", nil),
  74. },
  75. want: request{
  76. Operation: "GetCACaps",
  77. Message: []byte{},
  78. },
  79. wantErr: false,
  80. },
  81. {
  82. name: "ok/get-PKIOperation",
  83. args: args{
  84. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=MTIzNA==", nil),
  85. },
  86. want: request{
  87. Operation: "PKIOperation",
  88. Message: []byte("1234"),
  89. },
  90. wantErr: false,
  91. },
  92. {
  93. name: "ok/post-PKIOperation",
  94. args: args{
  95. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", bytes.NewBufferString("1234")),
  96. },
  97. want: request{
  98. Operation: "PKIOperation",
  99. Message: []byte("1234"),
  100. },
  101. wantErr: false,
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. got, err := decodeRequest(tt.args.r)
  107. if (err != nil) != tt.wantErr {
  108. t.Errorf("decodeRequest() error = %v, wantErr %v", err, tt.wantErr)
  109. return
  110. }
  111. if !reflect.DeepEqual(got, tt.want) {
  112. t.Errorf("decodeRequest() = %v, want %v", got, tt.want)
  113. }
  114. })
  115. }
  116. }
  117. func Test_selectValidationMethod(t *testing.T) {
  118. tests := []struct {
  119. name string
  120. p *provisioner.SCEP
  121. want validationMethod
  122. }{
  123. {"webhooks", &provisioner.SCEP{
  124. Name: "SCEP",
  125. Type: "SCEP",
  126. Options: &provisioner.Options{
  127. Webhooks: []*provisioner.Webhook{
  128. {
  129. Kind: linkedca.Webhook_SCEPCHALLENGE.String(),
  130. },
  131. },
  132. },
  133. Claims: &provisioner.Claims{},
  134. }, "webhook"},
  135. {"challenge", &provisioner.SCEP{
  136. Name: "SCEP",
  137. Type: "SCEP",
  138. ChallengePassword: "pass",
  139. Options: &provisioner.Options{},
  140. Claims: &provisioner.Claims{},
  141. }, "static"},
  142. {"none", &provisioner.SCEP{
  143. Name: "SCEP",
  144. Type: "SCEP",
  145. Options: &provisioner.Options{},
  146. Claims: &provisioner.Claims{},
  147. }, "none"},
  148. }
  149. for _, tt := range tests {
  150. t.Run(tt.name, func(t *testing.T) {
  151. err := tt.p.Init(provisioner.Config{
  152. Claims: config.GlobalProvisionerClaims,
  153. })
  154. require.NoError(t, err)
  155. got := selectValidationMethod(tt.p)
  156. assert.Equal(t, tt.want, got)
  157. })
  158. }
  159. }