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" |
| 6 | "strings" |
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 | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 9 | "io" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 10 | "io/ioutil" |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 11 | "os" |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 12 | "strconv" |
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") |
| 20 | // TODO: implement |
| 21 | testCase = flag.String("test_case", "", "What test cases to run") |
| 22 | |
| 23 | // The rest of these are unused, but present to fulfill the client interface |
| 24 | serverHostOverride = flag.String("server_host_override", "", "Unused") |
| 25 | useTestCa = flag.Bool("use_test_ca", false, "Unused") |
| 26 | defaultServiceAccount = flag.String("default_service_account", "", "Unused") |
| 27 | oauthScope = flag.String("oauth_scope", "", "Unused") |
| 28 | serviceAccountKeyFile = flag.String("service_account_key_file", "", "Unused") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 31 | func InteropCtx(t *testing.T) *HTTP2InteropCtx { |
| 32 | ctx := &HTTP2InteropCtx{ |
| 33 | ServerHost: *serverHost, |
| 34 | ServerPort: *serverPort, |
| 35 | ServerHostnameOverride: *serverHostOverride, |
| 36 | UseTLS: *useTls, |
| 37 | UseTestCa: *useTestCa, |
| 38 | T: t, |
| 39 | } |
| 40 | |
| 41 | ctx.serverSpec = ctx.ServerHost |
| 42 | if ctx.ServerPort != -1 { |
| 43 | ctx.serverSpec += ":" + strconv.Itoa(ctx.ServerPort) |
| 44 | } |
| 45 | if ctx.ServerHostnameOverride == "" { |
| 46 | ctx.authority = ctx.ServerHost |
| 47 | } else { |
| 48 | ctx.authority = ctx.ServerHostnameOverride |
| 49 | } |
| 50 | |
| 51 | if ctx.UseTestCa { |
| 52 | // It would be odd if useTestCa was true, but not useTls. meh |
| 53 | certData, err := ioutil.ReadFile("src/core/tsi/test_creds/ca.pem") |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | |
| 58 | ctx.rootCAs = x509.NewCertPool() |
| 59 | if !ctx.rootCAs.AppendCertsFromPEM(certData) { |
| 60 | t.Fatal(fmt.Errorf("Unable to parse pem data")) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return ctx |
| 65 | } |
| 66 | |
| 67 | func (ctx *HTTP2InteropCtx) Close() error { |
| 68 | // currently a noop |
| 69 | return nil |
| 70 | } |
| 71 | |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 72 | func TestShortPreface(t *testing.T) { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 73 | ctx := InteropCtx(t) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 74 | for i := 0; i < len(Preface)-1; i++ { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 75 | if err := testShortPreface(ctx, Preface[:i]+"X"); err != io.EOF { |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 76 | t.Error("Expected an EOF but was", err) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestUnknownFrameType(t *testing.T) { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 82 | ctx := InteropCtx(t) |
| 83 | if err := testUnknownFrameType(ctx); err != nil { |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 84 | t.Fatal(err) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestTLSApplicationProtocol(t *testing.T) { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 89 | ctx := InteropCtx(t) |
| 90 | err := testTLSApplicationProtocol(ctx); |
| 91 | matchError(t, err, "EOF") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func TestTLSMaxVersion(t *testing.T) { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 95 | ctx := InteropCtx(t) |
| 96 | err := testTLSMaxVersion(ctx, tls.VersionTLS11); |
| 97 | matchError(t, err, "EOF", "server selected unsupported protocol") |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func TestClientPrefaceWithStreamId(t *testing.T) { |
Carl Mastrangelo | de44910 | 2015-10-28 11:05:49 -0700 | [diff] [blame^] | 101 | ctx := InteropCtx(t) |
| 102 | err := testClientPrefaceWithStreamId(ctx) |
| 103 | matchError(t, err, "EOF") |
| 104 | } |
| 105 | |
| 106 | func matchError(t *testing.T, err error, matches ... string) { |
| 107 | if err == nil { |
| 108 | t.Fatal("Expected an error") |
| 109 | } |
| 110 | for _, s := range matches { |
| 111 | if strings.Contains(err.Error(), s) { |
| 112 | return |
| 113 | } |
| 114 | } |
| 115 | t.Fatalf("Error %v not in %+v", err, matches) |
Carl Mastrangelo | 4aca796 | 2015-10-05 16:17:47 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func TestMain(m *testing.M) { |
| 119 | flag.Parse() |
| 120 | os.Exit(m.Run()) |
| 121 | } |