bundle_test.go 5.1 KB

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