blob: 305125f0c1127138c17d32226add4170b834c296 [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 Mastrangelo3c786242015-12-02 14:46:50 -08006 "encoding/json"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07007 "flag"
Carl Mastrangelode449102015-10-28 11:05:49 -07008 "fmt"
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
Craig Tiller1796f872016-03-25 19:42:49 -070052 certData, err := ioutil.ReadFile("src/core/lib/tsi/test_creds/ca.pem")
Carl Mastrangelode449102015-10-28 11:05:49 -070053 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 Mastrangelo77a32f32015-12-07 11:50:46 -080071func TestSoonClientShortSettings(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -080072 defer Report(t)
Carl Mastrangelo945836e2015-11-20 17:43:15 -080073 if *testCase != "framing" {
74 t.SkipNow()
75 }
76 ctx := InteropCtx(t)
77 for i := 1; i <= 5; i++ {
78 err := testClientShortSettings(ctx, i)
79 matchError(t, err, "EOF")
80 }
81}
82
Carl Mastrangelo77a32f32015-12-07 11:50:46 -080083func TestSoonShortPreface(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -080084 defer Report(t)
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080085 if *testCase != "framing" {
86 t.SkipNow()
87 }
Carl Mastrangelode449102015-10-28 11:05:49 -070088 ctx := InteropCtx(t)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070089 for i := 0; i < len(Preface)-1; i++ {
Carl Mastrangelo945836e2015-11-20 17:43:15 -080090 err := testShortPreface(ctx, Preface[:i]+"X")
91 matchError(t, err, "EOF")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070092 }
93}
94
Carl Mastrangelo77a32f32015-12-07 11:50:46 -080095func TestSoonUnknownFrameType(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -080096 defer Report(t)
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -080097 if *testCase != "framing" {
98 t.SkipNow()
99 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700100 ctx := InteropCtx(t)
101 if err := testUnknownFrameType(ctx); err != nil {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700102 t.Fatal(err)
103 }
104}
105
Carl Mastrangelo77a32f32015-12-07 11:50:46 -0800106func TestSoonClientPrefaceWithStreamId(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800107 defer Report(t)
Carl Mastrangelo945836e2015-11-20 17:43:15 -0800108 if *testCase != "framing" {
109 t.SkipNow()
110 }
111 ctx := InteropCtx(t)
112 err := testClientPrefaceWithStreamId(ctx)
113 matchError(t, err, "EOF")
114}
115
Carl Mastrangelo77a32f32015-12-07 11:50:46 -0800116func TestSoonTLSApplicationProtocol(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800117 defer Report(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800118 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800119 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800120 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700121 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800122 err := testTLSApplicationProtocol(ctx)
Carl Mastrangelo945836e2015-11-20 17:43:15 -0800123 matchError(t, err, "EOF", "broken pipe")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700124}
125
Carl Mastrangelo77a32f32015-12-07 11:50:46 -0800126func TestSoonTLSMaxVersion(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800127 defer Report(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800128 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800129 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800130 }
Carl Mastrangelode449102015-10-28 11:05:49 -0700131 ctx := InteropCtx(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800132 err := testTLSMaxVersion(ctx, tls.VersionTLS11)
133 // TODO(carl-mastrangelo): maybe this should be some other error. If the server picks
134 // the wrong protocol version, thats bad too.
Carl Mastrangelode449102015-10-28 11:05:49 -0700135 matchError(t, err, "EOF", "server selected unsupported protocol")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700136}
137
Carl Mastrangelo77a32f32015-12-07 11:50:46 -0800138func TestSoonTLSBadCipherSuites(t *testing.T) {
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800139 defer Report(t)
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800140 if *testCase != "tls" {
Carl Mastrangelo2dd55db2015-11-19 10:51:48 -0800141 t.SkipNow()
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800142 }
143 ctx := InteropCtx(t)
144 err := testTLSBadCipherSuites(ctx)
145 matchError(t, err, "EOF", "Got goaway frame")
146}
147
Carl Mastrangelo8a1cdec2015-11-18 16:48:57 -0800148func matchError(t *testing.T, err error, matches ...string) {
149 if err == nil {
150 t.Fatal("Expected an error")
151 }
152 for _, s := range matches {
153 if strings.Contains(err.Error(), s) {
154 return
155 }
156 }
157 t.Fatalf("Error %v not in %+v", err, matches)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700158}
159
160func TestMain(m *testing.M) {
161 flag.Parse()
Carl Mastrangelo3c786242015-12-02 14:46:50 -0800162 m.Run()
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800163 var fatal bool
164 var any bool
165 for _, ci := range allCaseInfos.Cases {
166 if ci.Skipped {
167 continue
168 }
169 any = true
170 if !ci.Passed && ci.Fatal {
171 fatal = true
172 }
173 }
174
Carl Mastrangelo3c786242015-12-02 14:46:50 -0800175 if err := json.NewEncoder(os.Stderr).Encode(&allCaseInfos); err != nil {
176 fmt.Println("Failed to encode", err)
177 }
Carl Mastrangelo3e21ba42015-12-04 16:49:33 -0800178 var code int
179 if !any || fatal {
180 code = 1
181 }
182 os.Exit(code)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -0700183}