blob: e3d366f2f2752d8b7248990bfd432c914d958ed2 [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 Mastrangelo4aca7962015-10-05 16:17:47 -07008 "io"
Carl Mastrangelode449102015-10-28 11:05:49 -07009 "io/ioutil"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070010 "os"
Carl Mastrangelode449102015-10-28 11:05:49 -070011 "strconv"
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -080012 "strings"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070013 "testing"
14)
15
16var (
Carl Mastrangelode449102015-10-28 11:05:49 -070017 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 Mastrangelo2dd55db2015-11-19 10:51:48 -080020 testCase = flag.String("test_case", "", "What test cases to run (tls, framing)")
Carl Mastrangelode449102015-10-28 11:05:49 -070021
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 Mastrangelo4aca7962015-10-05 16:17:47 -070028)
29
Carl Mastrangelode449102015-10-28 11:05:49 -070030func 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
66func (ctx *HTTP2InteropCtx) Close() error {
67 // currently a noop
68 return nil
69}
70
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070071func TestShortPreface(t *testing.T) {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080072 if *testCase != "framing" {
73 t.SkipNow()
74 }
Carl Mastrangelode449102015-10-28 11:05:49 -070075 ctx := InteropCtx(t)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070076 for i := 0; i < len(Preface)-1; i++ {
Carl Mastrangelode449102015-10-28 11:05:49 -070077 if err := testShortPreface(ctx, Preface[:i]+"X"); err != io.EOF {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070078 t.Error("Expected an EOF but was", err)
79 }
80 }
81}
82
83func TestUnknownFrameType(t *testing.T) {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080084 if *testCase != "framing" {
85 t.SkipNow()
86 }
Carl Mastrangelode449102015-10-28 11:05:49 -070087 ctx := InteropCtx(t)
88 if err := testUnknownFrameType(ctx); err != nil {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070089 t.Fatal(err)
90 }
91}
92
93func TestTLSApplicationProtocol(t *testing.T) {
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -080094 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080095 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -080096 }
Carl Mastrangelode449102015-10-28 11:05:49 -070097 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -080098 err := testTLSApplicationProtocol(ctx)
Carl Mastrangelode449102015-10-28 11:05:49 -070099 matchError(t, err, "EOF")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700100}
101
102func TestTLSMaxVersion(t *testing.T) {
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800103 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800104 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800105 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700106 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800107 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 Mastrangelode449102015-10-28 11:05:49 -0700110 matchError(t, err, "EOF", "server selected unsupported protocol")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700111}
112
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800113func TestTLSBadCipherSuites(t *testing.T) {
114 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800115 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800116 }
117 ctx := InteropCtx(t)
118 err := testTLSBadCipherSuites(ctx)
119 matchError(t, err, "EOF", "Got goaway frame")
120}
121
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700122func TestClientPrefaceWithStreamId(t *testing.T) {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800123 if *testCase != "framing" {
124 t.SkipNow()
125 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700126 ctx := InteropCtx(t)
127 err := testClientPrefaceWithStreamId(ctx)
128 matchError(t, err, "EOF")
129}
130
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800131func 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 Mastrangelo4aca7962015-10-05 16:17:47 -0700141}
142
143func TestMain(m *testing.M) {
144 flag.Parse()
145 os.Exit(m.Run())
146}