bundle_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package bundle
  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. )
  11. const (
  12. testCaBundleFile = "../testdata/ca-bundle.pem"
  13. testIntBundleFile = "../testdata/int-bundle.pem"
  14. testLeafCertFile = "../testdata/leaf.pem"
  15. testLeafKeyFile = "../testdata/leaf.key"
  16. testLeafWrongKeyFile = "../testdata/leaf.badkey"
  17. testBrokenCertFile = "../testdata/broken.pem"
  18. )
  19. func newTestHandler(t *testing.T) (h http.Handler) {
  20. h, err := NewHandler(testCaBundleFile, testIntBundleFile)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. return
  25. }
  26. func newBundleServer(t *testing.T) *httptest.Server {
  27. ts := httptest.NewServer(newTestHandler(t))
  28. return ts
  29. }
  30. func testBundleFile(t *testing.T, domain, ip, certFile, keyFile, flavor string) (resp *http.Response, body []byte) {
  31. ts := newBundleServer(t)
  32. defer ts.Close()
  33. var certPEM, keyPEM []byte
  34. if certFile != "" {
  35. var err error
  36. certPEM, err = ioutil.ReadFile(certFile)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. }
  41. if keyFile != "" {
  42. var err error
  43. keyPEM, err = ioutil.ReadFile(keyFile)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. }
  48. obj := map[string]string{"flavor": flavor}
  49. if len(domain) > 0 {
  50. obj["domain"] = domain
  51. }
  52. if len(ip) > 0 {
  53. obj["ip"] = ip
  54. }
  55. if len(certPEM) > 0 {
  56. obj["certificate"] = string(certPEM)
  57. }
  58. if len(keyPEM) > 0 {
  59. obj["private_key"] = string(keyPEM)
  60. }
  61. blob, err := json.Marshal(obj)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. resp, err = http.Post(ts.URL, "application/json", bytes.NewReader(blob))
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. body, err = ioutil.ReadAll(resp.Body)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. return
  74. }
  75. func TestNewHandler(t *testing.T) {
  76. newTestHandler(t)
  77. }
  78. type bundleTest struct {
  79. Domain string
  80. IP string
  81. CertFile string
  82. KeyFile string
  83. Flavor string
  84. ExpectedHTTPStatus int
  85. ExpectedSuccess bool
  86. ExpectedErrorCode int
  87. }
  88. var bundleTests = []bundleTest{
  89. // Test bundling with certificate
  90. {
  91. CertFile: testLeafCertFile,
  92. ExpectedHTTPStatus: http.StatusOK,
  93. ExpectedSuccess: true,
  94. ExpectedErrorCode: 0,
  95. },
  96. {
  97. CertFile: testLeafCertFile,
  98. Flavor: "ubiquitous",
  99. ExpectedHTTPStatus: http.StatusOK,
  100. ExpectedSuccess: true,
  101. ExpectedErrorCode: 0,
  102. },
  103. {
  104. CertFile: testLeafCertFile,
  105. Flavor: "optimal",
  106. ExpectedHTTPStatus: http.StatusOK,
  107. ExpectedSuccess: true,
  108. ExpectedErrorCode: 0,
  109. },
  110. {
  111. CertFile: testLeafCertFile,
  112. KeyFile: testLeafKeyFile,
  113. ExpectedHTTPStatus: http.StatusOK,
  114. ExpectedSuccess: true,
  115. ExpectedErrorCode: 0,
  116. },
  117. {
  118. CertFile: testLeafCertFile,
  119. Domain: "cfssl-leaf.com",
  120. ExpectedHTTPStatus: http.StatusOK,
  121. ExpectedSuccess: true,
  122. ExpectedErrorCode: 0,
  123. },
  124. // Test bundling with remote domain
  125. {
  126. Domain: "google.com",
  127. ExpectedHTTPStatus: http.StatusBadRequest,
  128. ExpectedSuccess: false,
  129. },
  130. // Error testing.
  131. {
  132. CertFile: testLeafCertFile,
  133. KeyFile: testLeafWrongKeyFile,
  134. ExpectedHTTPStatus: http.StatusBadRequest,
  135. ExpectedSuccess: false,
  136. ExpectedErrorCode: 2300,
  137. },
  138. {
  139. // no input parameter is specified
  140. ExpectedHTTPStatus: http.StatusBadRequest,
  141. ExpectedSuccess: false,
  142. ExpectedErrorCode: http.StatusBadRequest,
  143. },
  144. {
  145. CertFile: testBrokenCertFile,
  146. ExpectedHTTPStatus: http.StatusBadRequest,
  147. ExpectedSuccess: false,
  148. ExpectedErrorCode: 1003,
  149. },
  150. {
  151. CertFile: testLeafKeyFile,
  152. KeyFile: testLeafKeyFile,
  153. ExpectedHTTPStatus: http.StatusBadRequest,
  154. ExpectedSuccess: false,
  155. ExpectedErrorCode: 1003,
  156. },
  157. {
  158. CertFile: testLeafCertFile,
  159. KeyFile: testLeafCertFile,
  160. ExpectedHTTPStatus: http.StatusBadRequest,
  161. ExpectedSuccess: false,
  162. ExpectedErrorCode: 2003,
  163. },
  164. {
  165. CertFile: testLeafCertFile,
  166. Domain: "cloudflare-leaf.com",
  167. ExpectedHTTPStatus: http.StatusBadRequest,
  168. ExpectedSuccess: false,
  169. ExpectedErrorCode: 1200,
  170. },
  171. }
  172. func TestBundle(t *testing.T) {
  173. for i, test := range bundleTests {
  174. resp, body := testBundleFile(t, test.Domain, test.IP, test.CertFile, test.KeyFile, test.Flavor)
  175. if resp.StatusCode != test.ExpectedHTTPStatus {
  176. t.Errorf("Test %d: expected: %d, have %d", i, test.ExpectedHTTPStatus, resp.StatusCode)
  177. t.Fatal(resp.Status, test.ExpectedHTTPStatus, string(body))
  178. }
  179. message := new(api.Response)
  180. err := json.Unmarshal(body, message)
  181. if err != nil {
  182. t.Errorf("failed to read response body: %v", err)
  183. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  184. }
  185. if test.ExpectedSuccess != message.Success {
  186. t.Errorf("Test %d: expected: %v, have %v", i, test.ExpectedSuccess, message.Success)
  187. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  188. }
  189. if test.ExpectedSuccess == true {
  190. continue
  191. }
  192. if test.ExpectedErrorCode != 0 && test.ExpectedErrorCode != message.Errors[0].Code {
  193. t.Errorf("Test %d: expected: %v, have %v", i, test.ExpectedErrorCode, message.Errors[0].Code)
  194. t.Fatal(resp.Status, test.ExpectedHTTPStatus, message)
  195. }
  196. }
  197. }