bundle_from_remote_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package bundler
  2. // This test file contains tests on checking the correctness of BundleFromRemote
  3. import (
  4. "net"
  5. "strings"
  6. "testing"
  7. "github.com/cloudflare/cfssl/ubiquity"
  8. )
  9. // remoteTest defines a test case for BundleFromRemote. Hostname and ip are the test inputs.
  10. // bundlerConstructor points the bundler ctor and errorCallback handles the error checking.
  11. type remoteTest struct {
  12. hostname string
  13. ip string
  14. bundlerConstructor func(*testing.T) (b *Bundler)
  15. errorCallback func(*testing.T, *remoteTest, error)
  16. bundleCallback func(*testing.T, *remoteTest, *Bundle)
  17. }
  18. const (
  19. RSACertSite = "rsa2048.badssl.com"
  20. SelfSignedSSLSite = "self-signed.badssl.com"
  21. MismatchedHostnameSite = "wrong.host.badssl.com"
  22. ECCCertSite = "ecc256.badssl.com"
  23. InvalidSite = "cloudflare1337.com"
  24. ValidSNI = "badssl.com"
  25. ValidSNIWildcard = "badssl.com"
  26. SNISANWildcard = "*.badssl.com"
  27. InvalidIP = "300.300.300.300"
  28. )
  29. func getBundleHostnameChecker(hostname string) func(*testing.T, *remoteTest, *Bundle) {
  30. return func(t *testing.T, test *remoteTest, bundle *Bundle) {
  31. if bundle == nil {
  32. t.Fatalf("Nil bundle returned hostname=%q ip=%q", test.hostname, test.ip)
  33. }
  34. var found = false
  35. for _, h := range bundle.Hostnames {
  36. if h == hostname {
  37. found = true
  38. }
  39. }
  40. if !found {
  41. t.Errorf("hostname expected but not found: %s hostname=%q ip=%q found=%v", hostname, test.hostname, test.ip, bundle.Hostnames)
  42. }
  43. }
  44. }
  45. func expectErrorMessages(expectedContents []string) func(*testing.T, *remoteTest, error) {
  46. return func(t *testing.T, test *remoteTest, err error) {
  47. if err == nil {
  48. t.Fatalf("Expected error has %s. Got nothing. hostname=%q ip=%q", expectedContents, test.hostname, test.ip)
  49. } else {
  50. for _, expected := range expectedContents {
  51. if !strings.Contains(err.Error(), expected) {
  52. t.Fatalf("Expected error has %s. Got %s. hostname=%q ip=%q", expected, err.Error(), test.hostname, test.ip)
  53. }
  54. }
  55. }
  56. }
  57. }
  58. // test cases of BundleFromRemote
  59. var remoteTests = []remoteTest{
  60. {
  61. hostname: RSACertSite,
  62. bundlerConstructor: newBundler,
  63. errorCallback: nil,
  64. },
  65. {
  66. hostname: ECCCertSite,
  67. bundlerConstructor: newBundler,
  68. errorCallback: nil,
  69. },
  70. {
  71. hostname: SelfSignedSSLSite,
  72. bundlerConstructor: newBundler,
  73. errorCallback: expectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
  74. },
  75. {
  76. hostname: MismatchedHostnameSite,
  77. bundlerConstructor: newBundler,
  78. errorCallback: expectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
  79. },
  80. {
  81. hostname: InvalidSite,
  82. bundlerConstructor: newBundler,
  83. errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup cloudflare1337.com"}),
  84. },
  85. {
  86. hostname: InvalidIP,
  87. bundlerConstructor: newBundler,
  88. errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
  89. },
  90. {
  91. ip: InvalidIP,
  92. bundlerConstructor: newBundler,
  93. errorCallback: expectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
  94. },
  95. }
  96. // TestBundleFromRemote goes through the test cases defined in remoteTests and run them through. See above for test case definitions.
  97. func TestBundleFromRemote(t *testing.T) {
  98. t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
  99. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  100. for _, test := range remoteTests {
  101. b := test.bundlerConstructor(t)
  102. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  103. if test.errorCallback != nil {
  104. test.errorCallback(t, &test, err)
  105. } else {
  106. if err != nil {
  107. t.Fatalf("expected no error. but an error occurred hostname=%q ip=%q errpr=%q", test.hostname, test.ip, err.Error())
  108. }
  109. if test.bundleCallback != nil {
  110. test.bundleCallback(t, &test, bundle)
  111. }
  112. }
  113. }
  114. }
  115. }
  116. func resolveHostIP(host string) string {
  117. addrs, err := net.LookupHost(host)
  118. if err != nil {
  119. panic(err)
  120. }
  121. if len(addrs) == 0 {
  122. panic("failed to resolve " + host)
  123. }
  124. return addrs[0]
  125. }
  126. var remoteSNITests = []remoteTest{
  127. {
  128. hostname: ValidSNI,
  129. bundlerConstructor: newBundler,
  130. errorCallback: nil,
  131. bundleCallback: getBundleHostnameChecker(ValidSNI),
  132. },
  133. {
  134. hostname: ValidSNIWildcard,
  135. bundlerConstructor: newBundler,
  136. errorCallback: nil,
  137. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  138. },
  139. {
  140. hostname: ValidSNI,
  141. ip: resolveHostIP(ValidSNI),
  142. bundlerConstructor: newBundler,
  143. errorCallback: nil,
  144. bundleCallback: getBundleHostnameChecker(ValidSNI),
  145. },
  146. {
  147. hostname: ValidSNIWildcard,
  148. ip: resolveHostIP(ValidSNIWildcard),
  149. bundlerConstructor: newBundler,
  150. errorCallback: nil,
  151. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  152. },
  153. }
  154. // TestBundleFromRemoteSNI goes through the test cases defined in remoteSNITests and run them through. See above for test case definitions.
  155. func TestBundleFromRemoteSNI(t *testing.T) {
  156. t.Skip("expired cert https://github.com/cloudflare/cfssl/issues/1237")
  157. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  158. for _, test := range remoteSNITests {
  159. b := test.bundlerConstructor(t)
  160. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  161. if test.errorCallback != nil {
  162. test.errorCallback(t, &test, err)
  163. } else {
  164. if err != nil {
  165. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  166. }
  167. if test.bundleCallback != nil {
  168. test.bundleCallback(t, &test, bundle)
  169. }
  170. }
  171. }
  172. }
  173. }
  174. func TestBundleFromRemoteFlavor(t *testing.T) {
  175. // This test was crafted for the specific cert bundle that benflare.us was
  176. // serving. The majority of the functionality is validated via the other
  177. // bundle tests.
  178. t.Skip("skipped; need new example site for test")
  179. b := newBundler(t)
  180. ubiquity.Platforms = nil
  181. ubiquity.LoadPlatforms(testMetadata)
  182. bundle, err := b.BundleFromRemote(ECCCertSite, "", Ubiquitous)
  183. if err != nil {
  184. t.Fatalf("expected no error. but an error occurred: %s", err.Error())
  185. }
  186. if len(bundle.Chain) != 3 {
  187. t.Error("expected 3-cert bundle. Got ", len(bundle.Chain))
  188. }
  189. if len(bundle.Status.Untrusted) != 0 {
  190. t.Error("expected no untrusted platforms. Got ", bundle.Status.Untrusted)
  191. }
  192. bundle, err = b.BundleFromRemote(ECCCertSite, "", Optimal)
  193. if err != nil {
  194. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  195. }
  196. if len(bundle.Chain) != 2 {
  197. t.Error("expected 2-cert bundle. Got ", len(bundle.Chain))
  198. }
  199. }