bundle_from_remote_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  99. for _, test := range remoteTests {
  100. b := test.bundlerConstructor(t)
  101. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  102. if test.errorCallback != nil {
  103. test.errorCallback(t, &test, err)
  104. } else {
  105. if err != nil {
  106. t.Fatalf("expected no error. but an error occurred hostname=%q ip=%q errpr=%q", test.hostname, test.ip, err.Error())
  107. }
  108. if test.bundleCallback != nil {
  109. test.bundleCallback(t, &test, bundle)
  110. }
  111. }
  112. }
  113. }
  114. }
  115. func resolveHostIP(host string) string {
  116. addrs, err := net.LookupHost(host)
  117. if err != nil {
  118. panic(err)
  119. }
  120. if len(addrs) == 0 {
  121. panic("failed to resolve " + host)
  122. }
  123. return addrs[0]
  124. }
  125. var remoteSNITests = []remoteTest{
  126. {
  127. hostname: ValidSNI,
  128. bundlerConstructor: newBundler,
  129. errorCallback: nil,
  130. bundleCallback: getBundleHostnameChecker(ValidSNI),
  131. },
  132. {
  133. hostname: ValidSNIWildcard,
  134. bundlerConstructor: newBundler,
  135. errorCallback: nil,
  136. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  137. },
  138. {
  139. hostname: ValidSNI,
  140. ip: resolveHostIP(ValidSNI),
  141. bundlerConstructor: newBundler,
  142. errorCallback: nil,
  143. bundleCallback: getBundleHostnameChecker(ValidSNI),
  144. },
  145. {
  146. hostname: ValidSNIWildcard,
  147. ip: resolveHostIP(ValidSNIWildcard),
  148. bundlerConstructor: newBundler,
  149. errorCallback: nil,
  150. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  151. },
  152. }
  153. // TestBundleFromRemoteSNI goes through the test cases defined in remoteSNITests and run them through. See above for test case definitions.
  154. func TestBundleFromRemoteSNI(t *testing.T) {
  155. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  156. for _, test := range remoteSNITests {
  157. b := test.bundlerConstructor(t)
  158. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  159. if test.errorCallback != nil {
  160. test.errorCallback(t, &test, err)
  161. } else {
  162. if err != nil {
  163. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  164. }
  165. if test.bundleCallback != nil {
  166. test.bundleCallback(t, &test, bundle)
  167. }
  168. }
  169. }
  170. }
  171. }
  172. func TestBundleFromRemoteFlavor(t *testing.T) {
  173. // This test was crafted for the specific cert bundle that benflare.us was
  174. // serving. The majority of the functionality is validated via the other
  175. // bundle tests.
  176. t.Skip("skipped; need new example site for test")
  177. b := newBundler(t)
  178. ubiquity.Platforms = nil
  179. ubiquity.LoadPlatforms(testMetadata)
  180. bundle, err := b.BundleFromRemote(ECCCertSite, "", Ubiquitous)
  181. if err != nil {
  182. t.Fatalf("expected no error. but an error occurred: %s", err.Error())
  183. }
  184. if len(bundle.Chain) != 3 {
  185. t.Error("expected 3-cert bundle. Got ", len(bundle.Chain))
  186. }
  187. if len(bundle.Status.Untrusted) != 0 {
  188. t.Error("expected no untrusted platforms. Got ", bundle.Status.Untrusted)
  189. }
  190. bundle, err = b.BundleFromRemote(ECCCertSite, "", Optimal)
  191. if err != nil {
  192. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  193. }
  194. if len(bundle.Chain) != 2 {
  195. t.Error("expected 2-cert bundle. Got ", len(bundle.Chain))
  196. }
  197. }