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