bundle_from_remote_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package bundler
  2. // This test file contains tests on checking the correctness of BundleFromRemote
  3. import (
  4. "flag"
  5. "testing"
  6. "github.com/cloudflare/cfssl/ubiquity"
  7. )
  8. var shouldTestSNI bool
  9. func init() {
  10. flag.BoolVar(&shouldTestSNI, "test-sni", false, "run the SNI tests")
  11. flag.Parse()
  12. }
  13. // remoteTest defines a test case for BundleFromRemote. Hostname and ip are the test inputs.
  14. // bundlerConstructor points the bundler ctor and errorCallback handles the error checking.
  15. type remoteTest struct {
  16. hostname string
  17. ip string
  18. bundlerConstructor func(*testing.T) (b *Bundler)
  19. errorCallback func(*testing.T, error)
  20. bundleCallback func(*testing.T, *Bundle)
  21. }
  22. const (
  23. ValidSSLSite = "google.com"
  24. SelfSignedSSLSite = "cacert.org"
  25. MismatchedHostnameSite = "www.capitol.state.tx.us"
  26. ECCCertSite = "benflare.us"
  27. InvalidSite = "cloudflare1337.com"
  28. ValidSNI = "alice.sni.velox.ch"
  29. ValidSNIWildcard = "cloudflare.sni.velox.ch"
  30. SNISANWildcard = "*.sni.velox.ch"
  31. ValidSNIIP = "85.25.46.13"
  32. InvalidIP = "300.300.300.300"
  33. )
  34. func getBundleHostnameChecker(hostname string) func(*testing.T, *Bundle) {
  35. return func(t *testing.T, bundle *Bundle) {
  36. if bundle == nil {
  37. t.Fatalf("Nil bundle returned")
  38. }
  39. var found = false
  40. for _, h := range bundle.Hostnames {
  41. if h == hostname {
  42. found = true
  43. }
  44. }
  45. if !found {
  46. t.Errorf("hostname expected but not found: %s", hostname)
  47. }
  48. }
  49. }
  50. // test cases of BundleFromRemote
  51. var remoteTests = []remoteTest{
  52. {
  53. hostname: ValidSSLSite,
  54. bundlerConstructor: newBundler,
  55. errorCallback: nil,
  56. },
  57. {
  58. hostname: SelfSignedSSLSite,
  59. bundlerConstructor: newBundler,
  60. errorCallback: ExpectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
  61. },
  62. {
  63. hostname: MismatchedHostnameSite,
  64. bundlerConstructor: newBundler,
  65. errorCallback: ExpectErrorMessages([]string{`"code":12`}), // only check it is a 12xx error
  66. },
  67. {
  68. hostname: InvalidSite,
  69. bundlerConstructor: newBundler,
  70. errorCallback: ExpectErrorMessages([]string{`"code":6000`, "dial tcp: lookup cloudflare1337.com"}),
  71. },
  72. {
  73. hostname: InvalidIP,
  74. bundlerConstructor: newBundler,
  75. errorCallback: ExpectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
  76. },
  77. {
  78. ip: InvalidIP,
  79. bundlerConstructor: newBundler,
  80. errorCallback: ExpectErrorMessages([]string{`"code":6000`, "dial tcp: lookup 300.300.300.300"}),
  81. },
  82. }
  83. // TestBundleFromRemote goes through the test cases defined in remoteTests and run them through. See above for test case definitions.
  84. func TestBundleFromRemote(t *testing.T) {
  85. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  86. for _, test := range remoteTests {
  87. b := test.bundlerConstructor(t)
  88. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  89. if test.errorCallback != nil {
  90. test.errorCallback(t, err)
  91. } else {
  92. if err != nil {
  93. t.Fatal("expected no error. but an error occurred", err.Error())
  94. }
  95. if test.bundleCallback != nil {
  96. test.bundleCallback(t, bundle)
  97. }
  98. }
  99. }
  100. }
  101. }
  102. var remoteSNITests = []remoteTest{
  103. {
  104. hostname: ValidSNI,
  105. bundlerConstructor: newBundler,
  106. errorCallback: nil,
  107. bundleCallback: getBundleHostnameChecker(ValidSNI),
  108. },
  109. {
  110. hostname: ValidSNIWildcard,
  111. bundlerConstructor: newBundler,
  112. errorCallback: nil,
  113. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  114. },
  115. {
  116. hostname: ValidSNI,
  117. ip: ValidSNIIP,
  118. bundlerConstructor: newBundler,
  119. errorCallback: nil,
  120. bundleCallback: getBundleHostnameChecker(ValidSNI),
  121. },
  122. {
  123. hostname: ValidSNIWildcard,
  124. ip: ValidSNIIP,
  125. bundlerConstructor: newBundler,
  126. errorCallback: nil,
  127. bundleCallback: getBundleHostnameChecker(SNISANWildcard),
  128. },
  129. }
  130. // TestBundleFromRemoteSNI goes through the test cases defined in remoteSNITests and run them through. See above for test case definitions.
  131. func TestBundleFromRemoteSNI(t *testing.T) {
  132. if !shouldTestSNI {
  133. t.Skip()
  134. }
  135. for _, bf := range []BundleFlavor{Ubiquitous, Optimal} {
  136. for _, test := range remoteSNITests {
  137. b := test.bundlerConstructor(t)
  138. bundle, err := b.BundleFromRemote(test.hostname, test.ip, bf)
  139. if test.errorCallback != nil {
  140. test.errorCallback(t, err)
  141. } else {
  142. if err != nil {
  143. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  144. }
  145. if test.bundleCallback != nil {
  146. test.bundleCallback(t, bundle)
  147. }
  148. }
  149. }
  150. }
  151. }
  152. func TestBundleFromRemoteFlavor(t *testing.T) {
  153. b := newBundler(t)
  154. ubiquity.Platforms = nil
  155. ubiquity.LoadPlatforms(testMetadata)
  156. bundle, err := b.BundleFromRemote(ECCCertSite, "", Ubiquitous)
  157. if err != nil {
  158. t.Fatalf("expected no error. but an error occurred: %s", err.Error())
  159. }
  160. if len(bundle.Chain) != 3 {
  161. t.Error("expected 3-cert bundle. Got ", len(bundle.Chain))
  162. }
  163. if len(bundle.Status.Untrusted) != 0 {
  164. t.Error("expected no untrusted platforms. Got ", bundle.Status.Untrusted)
  165. }
  166. bundle, err = b.BundleFromRemote(ECCCertSite, "", Optimal)
  167. if err != nil {
  168. t.Errorf("expected no error. but an error occurred: %s", err.Error())
  169. }
  170. if len(bundle.Chain) != 2 {
  171. t.Error("expected 2-cert bundle. Got ", len(bundle.Chain))
  172. }
  173. }