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 | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 8 | "io" |
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 |
| 52 | certData, err := ioutil.ReadFile("src/core/tsi/test_creds/ca.pem") |
| 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 | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 71 | func TestShortPreface(t *testing.T) { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 72 | if *testCase != "framing" { |
| 73 | t.SkipNow() |
| 74 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 75 | ctx := InteropCtx(t) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 76 | for i := 0; i < len(Preface)-1; i++ { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 77 | if err := testShortPreface(ctx, Preface[:i]+"X"); err != io.EOF { |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 78 | t.Error("Expected an EOF but was", err) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestUnknownFrameType(t *testing.T) { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 84 | if *testCase != "framing" { |
| 85 | t.SkipNow() |
| 86 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 87 | ctx := InteropCtx(t) |
| 88 | if err := testUnknownFrameType(ctx); err != nil { |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 89 | t.Fatal(err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestTLSApplicationProtocol(t *testing.T) { |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 94 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 95 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 96 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 97 | ctx := InteropCtx(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 98 | err := testTLSApplicationProtocol(ctx) |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 99 | matchError(t, err, "EOF") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func TestTLSMaxVersion(t *testing.T) { |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 103 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 104 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 105 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 106 | ctx := InteropCtx(t) |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 107 | err := testTLSMaxVersion(ctx, tls.VersionTLS11) |
| 108 | // TODO(carl-mastrangelo): maybe this should be some other error. If the server picks |
| 109 | // the wrong protocol version, thats bad too. |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 110 | matchError(t, err, "EOF", "server selected unsupported protocol") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 113 | func TestTLSBadCipherSuites(t *testing.T) { |
| 114 | if *testCase != "tls" { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 115 | t.SkipNow() |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 116 | } |
| 117 | ctx := InteropCtx(t) |
| 118 | err := testTLSBadCipherSuites(ctx) |
| 119 | matchError(t, err, "EOF", "Got goaway frame") |
| 120 | } |
| 121 | |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 122 | func TestClientPrefaceWithStreamId(t *testing.T) { |
Carl Mastrangelo | 2dd55db | 2015-11-19 10:51:48 -0800 | [diff] [blame^] | 123 | if *testCase != "framing" { |
| 124 | t.SkipNow() |
| 125 | } |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame] | 126 | ctx := InteropCtx(t) |
| 127 | err := testClientPrefaceWithStreamId(ctx) |
| 128 | matchError(t, err, "EOF") |
| 129 | } |
| 130 | |
Carl Mastrangelo | 8a1cdec | 2015-11-18 16:48:57 -0800 | [diff] [blame] | 131 | func matchError(t *testing.T, err error, matches ...string) { |
| 132 | if err == nil { |
| 133 | t.Fatal("Expected an error") |
| 134 | } |
| 135 | for _, s := range matches { |
| 136 | if strings.Contains(err.Error(), s) { |
| 137 | return |
| 138 | } |
| 139 | } |
| 140 | t.Fatalf("Error %v not in %+v", err, matches) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func TestMain(m *testing.M) { |
| 144 | flag.Parse() |
| 145 | os.Exit(m.Run()) |
| 146 | } |