Debian Codegraph @

tools/http2_interop/http2interop_test.go

raw
1 // Copyright 2019 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package http2interop
16
17 import (
18 "crypto/tls"
19 "crypto/x509"
20 "encoding/json"
21 "flag"
22 "fmt"
23 "io/ioutil"
24 "os"
25 "strconv"
26 "strings"
27 "testing"
28 )
29
30 var (
31 serverHost = flag.String("server_host", "", "The host to test")
32 serverPort = flag.Int("server_port", 443, "The port to test")
33 useTls = flag.Bool("use_tls", true, "Should TLS tests be run")
34 testCase = flag.String("test_case", "", "What test cases to run (tls, framing)")
35
36 // The rest of these are unused, but present to fulfill the client interface
37 serverHostOverride = flag.String("server_host_override", "", "Unused")
38 useTestCa = flag.Bool("use_test_ca", false, "Unused")
39 defaultServiceAccount = flag.String("default_service_account", "", "Unused")
40 oauthScope = flag.String("oauth_scope", "", "Unused")
41 serviceAccountKeyFile = flag.String("service_account_key_file", "", "Unused")
42 )
43
44 func InteropCtx(t *testing.T) *HTTP2InteropCtx {
45 ctx := &HTTP2InteropCtx{
46 ServerHost: *serverHost,
47 ServerPort: *serverPort,
48 ServerHostnameOverride: *serverHostOverride,
49 UseTLS: *useTls,
50 UseTestCa: *useTestCa,
51 T: t,
52 }
53
54 ctx.serverSpec = ctx.ServerHost
55 if ctx.ServerPort != -1 {
56 ctx.serverSpec += ":" + strconv.Itoa(ctx.ServerPort)
57 }
58 if ctx.ServerHostnameOverride == "" {
59 ctx.authority = ctx.ServerHost
60 } else {
61 ctx.authority = ctx.ServerHostnameOverride
62 }
63
64 if ctx.UseTestCa {
65 // It would be odd if useTestCa was true, but not useTls. meh
66 certData, err := ioutil.ReadFile("src/core/tsi/test_creds/ca.pem")
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 ctx.rootCAs = x509.NewCertPool()
72 if !ctx.rootCAs.AppendCertsFromPEM(certData) {
73 t.Fatal(fmt.Errorf("Unable to parse pem data"))
74 }
75 }
76
77 return ctx
78 }
79
80 func (ctx *HTTP2InteropCtx) Close() error {
81 // currently a noop
82 return nil
83 }
84
85 func TestSoonClientShortSettings(t *testing.T) {
86 defer Report(t)
87 if *testCase != "framing" {
88 t.SkipNow()
89 }
90 ctx := InteropCtx(t)
91 for i := 1; i <= 5; i++ {
92 err := testClientShortSettings(ctx, i)
93 matchError(t, err, "EOF")
94 }
95 }
96
97 func TestSoonShortPreface(t *testing.T) {
98 defer Report(t)
99 if *testCase != "framing" {
100 t.SkipNow()
101 }
102 ctx := InteropCtx(t)
103 for i := 0; i < len(Preface)-1; i++ {
104 err := testShortPreface(ctx, Preface[:i]+"X")
105 matchError(t, err, "EOF")
106 }
107 }
108
109 func TestSoonUnknownFrameType(t *testing.T) {
110 defer Report(t)
111 if *testCase != "framing" {
112 t.SkipNow()
113 }
114 ctx := InteropCtx(t)
115 if err := testUnknownFrameType(ctx); err != nil {
116 t.Fatal(err)
117 }
118 }
119
120 func TestSoonClientPrefaceWithStreamId(t *testing.T) {
121 defer Report(t)
122 if *testCase != "framing" {
123 t.SkipNow()
124 }
125 ctx := InteropCtx(t)
126 err := testClientPrefaceWithStreamId(ctx)
127 matchError(t, err, "EOF")
128 }
129
130 func TestSoonTLSApplicationProtocol(t *testing.T) {
131 defer Report(t)
132 if *testCase != "tls" {
133 t.SkipNow()
134 }
135 ctx := InteropCtx(t)
136 err := testTLSApplicationProtocol(ctx)
137 matchError(t, err, "EOF", "broken pipe")
138 }
139
140 func TestSoonTLSMaxVersion(t *testing.T) {
141 defer Report(t)
142 if *testCase != "tls" {
143 t.SkipNow()
144 }
145 ctx := InteropCtx(t)
146 err := testTLSMaxVersion(ctx, tls.VersionTLS11)
147 // TODO(carl-mastrangelo): maybe this should be some other error. If the server picks
148 // the wrong protocol version, that's bad too.
149 matchError(t, err, "EOF", "server selected unsupported protocol")
150 }
151
152 func TestSoonTLSBadCipherSuites(t *testing.T) {
153 defer Report(t)
154 if *testCase != "tls" {
155 t.SkipNow()
156 }
157 ctx := InteropCtx(t)
158 err := testTLSBadCipherSuites(ctx)
159 matchError(t, err, "EOF", "Got goaway frame")
160 }
161
162 func matchError(t *testing.T, err error, matches ...string) {
163 if err == nil {
164 t.Fatal("Expected an error")
165 }
166 for _, s := range matches {
167 if strings.Contains(err.Error(), s) {
168 return
169 }
170 }
171 t.Fatalf("Error %v not in %+v", err, matches)
172 }
173
174 func TestMain(m *testing.M) {
175 flag.Parse()
176 m.Run()
177 var fatal bool
178 var any bool
179 for _, ci := range allCaseInfos.Cases {
180 if ci.Skipped {
181 continue
182 }
183 any = true
184 if !ci.Passed && ci.Fatal {
185 fatal = true
186 }
187 }
188
189 if err := json.NewEncoder(os.Stderr).Encode(&allCaseInfos); err != nil {
190 fmt.Println("Failed to encode", err)
191 }
192 var code int
193 if !any || fatal {
194 code = 1
195 }
196 os.Exit(code)
197 }