api_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Package api implements a SCEP HTTP server.
  2. package api
  3. import (
  4. "bytes"
  5. "encoding/base64"
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "net/url"
  11. "testing"
  12. "testing/iotest"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func Test_decodeRequest(t *testing.T) {
  17. randomB64 := "wx/1mQ49TpdLRfvVjQhXNSe8RB3hjZEarqYp5XVIxpSbvOhQSs8hP2TgucID1IputbA8JC6CbsUpcVae3+8hRNqs5pTsSHP2aNxsw8AHGSX9dZVymSclkUV8irk+ztfEfs7aLA=="
  18. expectedRandom, err := base64.StdEncoding.DecodeString(randomB64)
  19. require.NoError(t, err)
  20. type args struct {
  21. r *http.Request
  22. }
  23. tests := []struct {
  24. name string
  25. args args
  26. want request
  27. wantErr bool
  28. }{
  29. {
  30. name: "fail/invalid-query",
  31. args: args{
  32. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=bla;message=invalid-separator", http.NoBody),
  33. },
  34. want: request{},
  35. wantErr: true,
  36. },
  37. {
  38. name: "fail/empty-operation",
  39. args: args{
  40. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=", http.NoBody),
  41. },
  42. want: request{},
  43. wantErr: true,
  44. },
  45. {
  46. name: "fail/unsupported-method",
  47. args: args{
  48. r: httptest.NewRequest(http.MethodPatch, "http://scep:8080/?operation=AnUnsupportOperation", http.NoBody),
  49. },
  50. want: request{},
  51. wantErr: true,
  52. },
  53. {
  54. name: "fail/get-unsupported-operation",
  55. args: args{
  56. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=AnUnsupportOperation", http.NoBody),
  57. },
  58. want: request{},
  59. wantErr: true,
  60. },
  61. {
  62. name: "fail/get-PKIOperation-empty-message",
  63. args: args{
  64. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=", http.NoBody),
  65. },
  66. want: request{},
  67. wantErr: true,
  68. },
  69. {
  70. name: "fail/get-PKIOperation",
  71. args: args{
  72. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message='somewronginput'", http.NoBody),
  73. },
  74. want: request{},
  75. wantErr: true,
  76. },
  77. {
  78. name: "fail/get-PKIOperation-not-escaped",
  79. args: args{
  80. r: httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://scep:8080/?operation=PKIOperation&message=%s", randomB64), http.NoBody),
  81. },
  82. want: request{},
  83. wantErr: true,
  84. },
  85. {
  86. name: "fail/post-PKIOperation",
  87. args: args{
  88. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", iotest.ErrReader(errors.New("a read error"))),
  89. },
  90. want: request{},
  91. wantErr: true,
  92. },
  93. {
  94. name: "ok/get-GetCACert",
  95. args: args{
  96. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACert", http.NoBody),
  97. },
  98. want: request{
  99. Operation: "GetCACert",
  100. Message: []byte{},
  101. },
  102. wantErr: false,
  103. },
  104. {
  105. name: "ok/get-GetCACaps",
  106. args: args{
  107. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACaps", http.NoBody),
  108. },
  109. want: request{
  110. Operation: "GetCACaps",
  111. Message: []byte{},
  112. },
  113. wantErr: false,
  114. },
  115. {
  116. name: "ok/get-PKIOperation",
  117. args: args{
  118. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=MTIzNA==", http.NoBody),
  119. },
  120. want: request{
  121. Operation: "PKIOperation",
  122. Message: []byte("1234"),
  123. },
  124. wantErr: false,
  125. },
  126. {
  127. name: "ok/get-PKIOperation-escaped",
  128. args: args{
  129. r: httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://scep:8080/?operation=PKIOperation&message=%s", url.QueryEscape(randomB64)), http.NoBody),
  130. },
  131. want: request{
  132. Operation: "PKIOperation",
  133. Message: expectedRandom,
  134. },
  135. wantErr: false,
  136. },
  137. {
  138. name: "ok/post-PKIOperation",
  139. args: args{
  140. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", bytes.NewBufferString("1234")),
  141. },
  142. want: request{
  143. Operation: "PKIOperation",
  144. Message: []byte("1234"),
  145. },
  146. wantErr: false,
  147. },
  148. }
  149. for _, tt := range tests {
  150. t.Run(tt.name, func(t *testing.T) {
  151. got, err := decodeRequest(tt.args.r)
  152. if tt.wantErr {
  153. assert.Error(t, err)
  154. assert.Equal(t, tt.want, got)
  155. return
  156. }
  157. assert.NoError(t, err)
  158. assert.Equal(t, tt.want, got)
  159. })
  160. }
  161. }