Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 1 | package http2interop |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 5 | "crypto/x509" |
Carl Mastrangelo | 3c78624 | 2015-12-02 14:46:50 -0800 | [diff] [blame] | 6 | "encoding/json" |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 7 | "flag" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 8 | "fmt" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 9 | "io/ioutil" |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 10 | "os" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 11 | "strconv" |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 12 | "strings" |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | var ( |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 17 | serverHost = flag.String("server_host", "", "The host to test") |
| 18 | serverPort = flag.Int("server_port", 443, "The port to test") |
| 19 | useTls = flag.Bool("use_tls", true, "Should TLS tests be run") |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 20 | testCase = flag.String("test_case", "", "What test cases to run (tls, framing)") |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 21 | |
| 22 | // The rest of these are unused, but present to fulfill the client interface |
| 23 | serverHostOverride = flag.String("server_host_override", "", "Unused") |
| 24 | useTestCa = flag.Bool("use_test_ca", false, "Unused") |
| 25 | defaultServiceAccount = flag.String("default_service_account", "", "Unused") |
| 26 | oauthScope = flag.String("oauth_scope", "", "Unused") |
| 27 | serviceAccountKeyFile = flag.String("service_account_key_file", "", "Unused") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 30 | func InteropCtx(t *testing.T) *HTTP2InteropCtx { |
| 31 | ctx := &HTTP2InteropCtx{ |
| 32 | ServerHost: *serverHost, |
| 33 | ServerPort: *serverPort, |
| 34 | ServerHostnameOverride: *serverHostOverride, |
| 35 | UseTLS: *useTls, |
| 36 | UseTestCa: *useTestCa, |
| 37 | T: t, |
| 38 | } |
| 39 | |
| 40 | ctx.serverSpec = ctx.ServerHost |
| 41 | if ctx.ServerPort != -1 { |
| 42 | ctx.serverSpec += ":" + strconv.Itoa(ctx.ServerPort) |
| 43 | } |
| 44 | if ctx.ServerHostnameOverride == "" { |
| 45 | ctx.authority = ctx.ServerHost |
| 46 | } else { |
| 47 | ctx.authority = ctx.ServerHostnameOverride |
| 48 | } |
| 49 | |
| 50 | if ctx.UseTestCa { |
| 51 | // It would be odd if useTestCa was true, but not useTls. meh |
Craig Tiller | 1796f87 | 2016-03-25 19:42:49 -0700 | [diff] [blame^] | 52 | certData, err := ioutil.ReadFile("src/core/lib/tsi/test_creds/ca.pem") |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | |
| 57 | ctx.rootCAs = x509.NewCertPool() |
| 58 | if !ctx.rootCAs.AppendCertsFromPEM(certData) { |
| 59 | t.Fatal(fmt.Errorf("Unable to parse pem data")) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return ctx |
| 64 | } |
| 65 | |
| 66 | func (ctx *HTTP2InteropCtx) Close() error { |
| 67 | // currently a noop |
| 68 | return nil |
| 69 | } |
| 70 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 71 | func TestSoonClientShortSettings(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 72 | defer Report(t) |
Carl Mastrangelo | 945836e | 2015-11-20 17:43:15 -0800 | [diff] [blame] | 73 | if *testCase != "framing" { |
| 74 | t.SkipNow() |
| 75 | } |
| 76 | ctx := InteropCtx(t) |
| 77 | for i := 1; i <= 5; i++ { |
| 78 | err := testClientShortSettings(ctx, i) |
| 79 | matchError(t, err, "EOF") |
| 80 | } |
| 81 | } |
| 82 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 83 | func TestSoonShortPreface(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 84 | defer Report(t) |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 85 | if *testCase != "framing" { |
| 86 | t.SkipNow() |
| 87 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 88 | ctx := InteropCtx(t) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 89 | for i := 0; i < len(Preface)-1; i++ { |
Carl Mastrangelo | 945836e | 2015-11-20 17:43:15 -0800 | [diff] [blame] | 90 | err := testShortPreface(ctx, Preface[:i]+"X") |
| 91 | matchError(t, err, "EOF") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 95 | func TestSoonUnknownFrameType(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 96 | defer Report(t) |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 97 | if *testCase != "framing" { |
| 98 | t.SkipNow() |
| 99 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 100 | ctx := InteropCtx(t) |
| 101 | if err := testUnknownFrameType(ctx); err != nil { |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 102 | t.Fatal(err) |
| 103 | } |
| 104 | } |
| 105 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 106 | func TestSoonClientPrefaceWithStreamId(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 107 | defer Report(t) |
Carl Mastrangelo | 945836e | 2015-11-20 17:43:15 -0800 | [diff] [blame] | 108 | if *testCase != "framing" { |
| 109 | t.SkipNow() |
| 110 | } |
| 111 | ctx := InteropCtx(t) |
| 112 | err := testClientPrefaceWithStreamId(ctx) |
| 113 | matchError(t, err, "EOF") |
| 114 | } |
| 115 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 116 | func TestSoonTLSApplicationProtocol(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 117 | defer Report(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 118 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 119 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 120 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 121 | ctx := InteropCtx(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 122 | err := testTLSApplicationProtocol(ctx) |
Carl Mastrangelo | 945836e | 2015-11-20 17:43:15 -0800 | [diff] [blame] | 123 | matchError(t, err, "EOF", "broken pipe") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 126 | func TestSoonTLSMaxVersion(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 127 | defer Report(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 128 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 129 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 130 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 131 | ctx := InteropCtx(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 132 | err := testTLSMaxVersion(ctx, tls.VersionTLS11) |
| 133 | // TODO(carl-mastrangelo): maybe this should be some other error. If the server picks |
| 134 | // the wrong protocol version, thats bad too. |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 135 | matchError(t, err, "EOF", "server selected unsupported protocol") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Carl Mastrangelo | 77a32f3 | 2015-12-07 11:50:46 -0800 | [diff] [blame] | 138 | func TestSoonTLSBadCipherSuites(t *testing.T) { |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 139 | defer Report(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 140 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame] | 141 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 142 | } |
| 143 | ctx := InteropCtx(t) |
| 144 | err := testTLSBadCipherSuites(ctx) |
| 145 | matchError(t, err, "EOF", "Got goaway frame") |
| 146 | } |
| 147 | |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 148 | func matchError(t *testing.T, err error, matches ...string) { |
| 149 | if err == nil { |
| 150 | t.Fatal("Expected an error") |
| 151 | } |
| 152 | for _, s := range matches { |
| 153 | if strings.Contains(err.Error(), s) { |
| 154 | return |
| 155 | } |
| 156 | } |
| 157 | t.Fatalf("Error %v not in %+v", err, matches) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func TestMain(m *testing.M) { |
| 161 | flag.Parse() |
Carl Mastrangelo | 3c78624 | 2015-12-02 14:46:50 -0800 | [diff] [blame] | 162 | m.Run() |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 163 | var fatal bool |
| 164 | var any bool |
| 165 | for _, ci := range allCaseInfos.Cases { |
| 166 | if ci.Skipped { |
| 167 | continue |
| 168 | } |
| 169 | any = true |
| 170 | if !ci.Passed && ci.Fatal { |
| 171 | fatal = true |
| 172 | } |
| 173 | } |
| 174 | |
Carl Mastrangelo | 3c78624 | 2015-12-02 14:46:50 -0800 | [diff] [blame] | 175 | if err := json.NewEncoder(os.Stderr).Encode(&allCaseInfos); err != nil { |
| 176 | fmt.Println("Failed to encode", err) |
| 177 | } |
Carl Mastrangelo | 3e21ba4 | 2015-12-04 16:49:33 -0800 | [diff] [blame] | 178 | var code int |
| 179 | if !any || fatal { |
| 180 | code = 1 |
| 181 | } |
| 182 | os.Exit(code) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 183 | } |