4
0

api_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "strings"
  12. "testing"
  13. "testing/iotest"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func Test_decodeRequest(t *testing.T) {
  18. randomB64 := "wx/1mQ49TpdLRfvVjQhXNSe8RB3hjZEarqYp5XVIxpSbvOhQSs8hP2TgucID1IputbA8JC6CbsUpcVae3+8hRNqs5pTsSHP2aNxsw8AHGSX9dZVymSclkUV8irk+ztfEfs7aLA=="
  19. expectedRandom, err := base64.StdEncoding.DecodeString(randomB64)
  20. require.NoError(t, err)
  21. weirdMacOSCase := "wx/1mQ49TpdLRfvVjQhXNSe8RB3hjZEarqYp5XVIxpSbvOhQSs8hP2TgucID1IputbA8JC6CbsUpcVae3+8hRNqs5pTsSHP2aNxsw8AHGSX9dZVymSclkUV8irk+ztfEfs7aLA%3D%3D"
  22. expectedWeirdMacOSCase, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(weirdMacOSCase, "%3D", "="))
  23. require.NoError(t, err)
  24. type args struct {
  25. r *http.Request
  26. }
  27. tests := []struct {
  28. name string
  29. args args
  30. want request
  31. wantErr bool
  32. }{
  33. {
  34. name: "fail/invalid-query",
  35. args: args{
  36. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=bla;message=invalid-separator", http.NoBody),
  37. },
  38. want: request{},
  39. wantErr: true,
  40. },
  41. {
  42. name: "fail/empty-operation",
  43. args: args{
  44. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=", http.NoBody),
  45. },
  46. want: request{},
  47. wantErr: true,
  48. },
  49. {
  50. name: "fail/unsupported-method",
  51. args: args{
  52. r: httptest.NewRequest(http.MethodPatch, "http://scep:8080/?operation=AnUnsupportOperation", http.NoBody),
  53. },
  54. want: request{},
  55. wantErr: true,
  56. },
  57. {
  58. name: "fail/get-unsupported-operation",
  59. args: args{
  60. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=AnUnsupportOperation", http.NoBody),
  61. },
  62. want: request{},
  63. wantErr: true,
  64. },
  65. {
  66. name: "fail/get-PKIOperation-empty-message",
  67. args: args{
  68. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=", http.NoBody),
  69. },
  70. want: request{},
  71. wantErr: true,
  72. },
  73. {
  74. name: "fail/get-PKIOperation",
  75. args: args{
  76. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message='somewronginput'", http.NoBody),
  77. },
  78. want: request{},
  79. wantErr: true,
  80. },
  81. {
  82. name: "fail/post-PKIOperation",
  83. args: args{
  84. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", iotest.ErrReader(errors.New("a read error"))),
  85. },
  86. want: request{},
  87. wantErr: true,
  88. },
  89. {
  90. name: "ok/get-GetCACert",
  91. args: args{
  92. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACert", http.NoBody),
  93. },
  94. want: request{
  95. Operation: "GetCACert",
  96. Message: []byte{},
  97. },
  98. wantErr: false,
  99. },
  100. {
  101. name: "ok/get-GetCACaps",
  102. args: args{
  103. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=GetCACaps", http.NoBody),
  104. },
  105. want: request{
  106. Operation: "GetCACaps",
  107. Message: []byte{},
  108. },
  109. wantErr: false,
  110. },
  111. {
  112. name: "ok/get-PKIOperation",
  113. args: args{
  114. r: httptest.NewRequest(http.MethodGet, "http://scep:8080/?operation=PKIOperation&message=MTIzNA==", http.NoBody),
  115. },
  116. want: request{
  117. Operation: "PKIOperation",
  118. Message: []byte("1234"),
  119. },
  120. wantErr: false,
  121. },
  122. {
  123. name: "ok/get-PKIOperation-escaped",
  124. args: args{
  125. r: httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://scep:8080/?operation=PKIOperation&message=%s", url.QueryEscape(randomB64)), http.NoBody),
  126. },
  127. want: request{
  128. Operation: "PKIOperation",
  129. Message: expectedRandom,
  130. },
  131. wantErr: false,
  132. },
  133. {
  134. name: "ok/get-PKIOperation-not-escaped", // bit of a special case, but this is supported because of the macOS case now
  135. args: args{
  136. r: httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://scep:8080/?operation=PKIOperation&message=%s", randomB64), http.NoBody),
  137. },
  138. want: request{
  139. Operation: "PKIOperation",
  140. Message: expectedRandom,
  141. },
  142. wantErr: false,
  143. },
  144. {
  145. name: "ok/get-PKIOperation-weird-macos-case", // a special case for macOS, which seems to result in the message not arriving fully percent-encoded
  146. args: args{
  147. r: httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://scep:8080/?operation=PKIOperation&message=%s", weirdMacOSCase), http.NoBody),
  148. },
  149. want: request{
  150. Operation: "PKIOperation",
  151. Message: expectedWeirdMacOSCase,
  152. },
  153. wantErr: false,
  154. },
  155. {
  156. name: "ok/post-PKIOperation",
  157. args: args{
  158. r: httptest.NewRequest(http.MethodPost, "http://scep:8080/?operation=PKIOperation", bytes.NewBufferString("1234")),
  159. },
  160. want: request{
  161. Operation: "PKIOperation",
  162. Message: []byte("1234"),
  163. },
  164. wantErr: false,
  165. },
  166. }
  167. for _, tt := range tests {
  168. t.Run(tt.name, func(t *testing.T) {
  169. got, err := decodeRequest(tt.args.r)
  170. if tt.wantErr {
  171. assert.Error(t, err)
  172. assert.Equal(t, tt.want, got)
  173. return
  174. }
  175. assert.NoError(t, err)
  176. assert.Equal(t, tt.want, got)
  177. })
  178. }
  179. }