blob: b35d08556944e6ff60654878eaade07c361a0216 [file] [log] [blame]
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07001package http2interop
2
3import (
4 "crypto/tls"
Carl Mastrangelode449102015-10-28 11:05:49 -07005 "crypto/x509"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07006 "flag"
Carl Mastrangelode449102015-10-28 11:05:49 -07007 "fmt"
Carl Mastrangelode449102015-10-28 11:05:49 -07008 "io/ioutil"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07009 "os"
Carl Mastrangelode449102015-10-28 11:05:49 -070010 "strconv"
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -080011 "strings"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070012 "testing"
13)
14
15var (
Carl Mastrangelode449102015-10-28 11:05:49 -070016 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 Mastrangelo2dd55db2015-11-19 10:51:48 -080019 testCase = flag.String("test_case", "", "What test cases to run (tls, framing)")
Carl Mastrangelode449102015-10-28 11:05:49 -070020
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 Mastrangelo4aca7962015-10-05 16:17:47 -070027)
28
Carl Mastrangelode449102015-10-28 11:05:49 -070029func 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
65func (ctx *HTTP2InteropCtx) Close() error {
66 // currently a noop
67 return nil
68}
69
Carl Mastrangelo945836e2015-11-20 17:43:15 -080070func 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 Mastrangelo4aca7962015-10-05 16:17:47 -070081func TestShortPreface(t *testing.T) {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080082 if *testCase != "framing" {
83 t.SkipNow()
84 }
Carl Mastrangelode449102015-10-28 11:05:49 -070085 ctx := InteropCtx(t)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070086 for i := 0; i < len(Preface)-1; i++ {
Carl Mastrangelo945836e2015-11-20 17:43:15 -080087 err := testShortPreface(ctx, Preface[:i]+"X")
88 matchError(t, err, "EOF")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070089 }
90}
91
92func TestUnknownFrameType(t *testing.T) {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080093 if *testCase != "framing" {
94 t.SkipNow()
95 }
Carl Mastrangelode449102015-10-28 11:05:49 -070096 ctx := InteropCtx(t)
97 if err := testUnknownFrameType(ctx); err != nil {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070098 t.Fatal(err)
99 }
100}
101
Carl Mastrangelo945836e2015-11-20 17:43:15 -0800102func 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 Mastrangelo4aca7962015-10-05 16:17:47 -0700111func TestTLSApplicationProtocol(t *testing.T) {
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800112 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800113 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800114 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700115 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800116 err := testTLSApplicationProtocol(ctx)
Carl Mastrangelo945836e2015-11-20 17:43:15 -0800117 matchError(t, err, "EOF", "broken pipe")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700118}
119
120func TestTLSMaxVersion(t *testing.T) {
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800121 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800122 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800123 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700124 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800125 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 Mastrangelode449102015-10-28 11:05:49 -0700128 matchError(t, err, "EOF", "server selected unsupported protocol")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700129}
130
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800131func TestTLSBadCipherSuites(t *testing.T) {
132 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800133 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800134 }
135 ctx := InteropCtx(t)
136 err := testTLSBadCipherSuites(ctx)
137 matchError(t, err, "EOF", "Got goaway frame")
138}
139
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800140func 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 Mastrangelo4aca7962015-10-05 16:17:47 -0700150}
151
152func TestMain(m *testing.M) {
153 flag.Parse()
154 os.Exit(m.Run())
155}