info_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package info
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "github.com/cloudflare/cfssl/api"
  10. "github.com/cloudflare/cfssl/signer"
  11. "github.com/cloudflare/cfssl/signer/local"
  12. )
  13. const (
  14. testCaFile = "../testdata/ca.pem"
  15. testCaKeyFile = "../testdata/ca_key.pem"
  16. // second test CA for multiroot
  17. testCaFile2 = "../testdata/ca2.pem"
  18. testCaKeyFile2 = "../testdata/ca2-key.pem"
  19. )
  20. // Generally, the single root function and its multiroot analogue will
  21. // be presented together.
  22. func newTestHandler(t *testing.T) (h http.Handler) {
  23. signer, err := local.NewSignerFromFile(testCaFile, testCaKeyFile, nil)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. h, err = NewHandler(signer)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. return
  32. }
  33. func newTestMultiHandler(t *testing.T) (h http.Handler) {
  34. signer1, err := local.NewSignerFromFile(testCaFile, testCaKeyFile, nil)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. signer2, err := local.NewSignerFromFile(testCaFile2, testCaKeyFile2, nil)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. signers := map[string]signer.Signer{
  43. "test1": signer1,
  44. "test2": signer2,
  45. }
  46. h, err = NewMultiHandler(signers, "test1")
  47. if err != nil {
  48. t.Fatalf("%v", err)
  49. }
  50. return
  51. }
  52. func TestNewHandler(t *testing.T) {
  53. newTestHandler(t)
  54. }
  55. func TestNewMultiHandler(t *testing.T) {
  56. newTestMultiHandler(t)
  57. }
  58. func newInfoServer(t *testing.T) *httptest.Server {
  59. ts := httptest.NewServer(newTestHandler(t))
  60. return ts
  61. }
  62. func newMultiInfoServer(t *testing.T) *httptest.Server {
  63. return httptest.NewServer(newTestMultiHandler(t))
  64. }
  65. func testInfoFile(t *testing.T, req map[string]interface{}) (resp *http.Response, body []byte) {
  66. ts := newInfoServer(t)
  67. defer ts.Close()
  68. blob, err := json.Marshal(req)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. resp, err = http.Post(ts.URL, "application/json", bytes.NewReader(blob))
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. body, err = ioutil.ReadAll(resp.Body)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. return
  81. }
  82. func testMultiInfoFile(t *testing.T, req map[string]interface{}) (resp *http.Response, body []byte) {
  83. ts := newMultiInfoServer(t)
  84. defer ts.Close()
  85. blob, err := json.Marshal(req)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. resp, err = http.Post(ts.URL, "application/json", bytes.NewReader(blob))
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. body, err = ioutil.ReadAll(resp.Body)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. return
  98. }
  99. type infoTest struct {
  100. RequestObject map[string]interface{}
  101. ExpectedHTTPStatus int
  102. ExpectedSuccess bool
  103. ExpectedErrorCode int
  104. }
  105. var infoTests = []infoTest{
  106. {
  107. map[string]interface{}{
  108. "label": "",
  109. "profile": "",
  110. },
  111. http.StatusOK,
  112. true,
  113. 0,
  114. },
  115. {
  116. map[string]interface{}{
  117. "label": 123,
  118. },
  119. http.StatusBadRequest,
  120. false,
  121. http.StatusBadRequest,
  122. },
  123. }
  124. var multiInfoTests = []infoTest{
  125. {
  126. map[string]interface{}{
  127. "label": "",
  128. "profile": "",
  129. },
  130. http.StatusOK,
  131. true,
  132. 0,
  133. },
  134. {
  135. map[string]interface{}{
  136. "label": "test1",
  137. "profile": "",
  138. },
  139. http.StatusOK,
  140. true,
  141. 0,
  142. },
  143. {
  144. map[string]interface{}{
  145. "label": "test2",
  146. "profile": "",
  147. },
  148. http.StatusOK,
  149. true,
  150. 0,
  151. },
  152. {
  153. map[string]interface{}{
  154. "label": "badlabel",
  155. "profile": "",
  156. },
  157. http.StatusBadRequest,
  158. false,
  159. http.StatusBadRequest,
  160. },
  161. {
  162. map[string]interface{}{
  163. "label": 123,
  164. },
  165. http.StatusBadRequest,
  166. false,
  167. http.StatusBadRequest,
  168. },
  169. }
  170. func TestInfo(t *testing.T) {
  171. for i, test := range infoTests {
  172. resp, body := testInfoFile(t, test.RequestObject)
  173. if resp.StatusCode != test.ExpectedHTTPStatus {
  174. t.Fatalf("Test %d: expected: %d, have %d", i, test.ExpectedHTTPStatus, resp.StatusCode)
  175. t.Fatal(resp.Status, test.ExpectedHTTPStatus, string(body))
  176. }
  177. message := new(api.Response)
  178. err := json.Unmarshal(body, message)
  179. if err != nil {
  180. t.Fatalf("failed to read response body: %v", err)
  181. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  182. }
  183. if test.ExpectedSuccess != message.Success {
  184. t.Fatalf("Test %d: expected: %v, have %v", i, test.ExpectedSuccess, message.Success)
  185. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  186. }
  187. if test.ExpectedSuccess == true {
  188. continue
  189. }
  190. if test.ExpectedErrorCode != message.Errors[0].Code {
  191. t.Fatalf("Test %d: expected: %v, have %v", i, test.ExpectedErrorCode, message.Errors[0].Code)
  192. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  193. }
  194. }
  195. }
  196. func TestMultiInfo(t *testing.T) {
  197. for i, test := range multiInfoTests {
  198. resp, body := testMultiInfoFile(t, test.RequestObject)
  199. if resp.StatusCode != test.ExpectedHTTPStatus {
  200. t.Fatalf("Test %d: expected: %d, have %d", i, test.ExpectedHTTPStatus, resp.StatusCode)
  201. t.Fatal(resp.Status, test.ExpectedHTTPStatus, string(body))
  202. }
  203. message := new(api.Response)
  204. err := json.Unmarshal(body, message)
  205. if err != nil {
  206. t.Fatalf("failed to read response body: %v", err)
  207. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  208. }
  209. if test.ExpectedSuccess != message.Success {
  210. t.Fatalf("Test %d: expected: %v, have %v", i, test.ExpectedSuccess, message.Success)
  211. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  212. }
  213. if test.ExpectedSuccess == true {
  214. continue
  215. }
  216. if test.ExpectedErrorCode != message.Errors[0].Code {
  217. t.Fatalf("Test %d: expected: %v, have %v", i, test.ExpectedErrorCode, message.Errors[0].Code)
  218. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  219. }
  220. }
  221. }