blob: dc2960048f13e58e3d2a5d79a88d39b2c3834cc1 [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"
6 "strings"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07007 "flag"
Carl Mastrangelode449102015-10-28 11:05:49 -07008 "fmt"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -07009 "io"
Carl Mastrangelode449102015-10-28 11:05:49 -070010 "io/ioutil"
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070011 "os"
Carl Mastrangelode449102015-10-28 11:05:49 -070012 "strconv"
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")
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 Mastrangelo4aca7962015-10-05 16:17:47 -070029)
30
Carl Mastrangelode449102015-10-28 11:05:49 -070031func 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
67func (ctx *HTTP2InteropCtx) Close() error {
68 // currently a noop
69 return nil
70}
71
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070072func TestShortPreface(t *testing.T) {
Carl Mastrangelode449102015-10-28 11:05:49 -070073 ctx := InteropCtx(t)
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070074 for i := 0; i < len(Preface)-1; i++ {
Carl Mastrangelode449102015-10-28 11:05:49 -070075 if err := testShortPreface(ctx, Preface[:i]+"X"); err != io.EOF {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070076 t.Error("Expected an EOF but was", err)
77 }
78 }
79}
80
81func TestUnknownFrameType(t *testing.T) {
Carl Mastrangelode449102015-10-28 11:05:49 -070082 ctx := InteropCtx(t)
83 if err := testUnknownFrameType(ctx); err != nil {
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070084 t.Fatal(err)
85 }
86}
87
88func TestTLSApplicationProtocol(t *testing.T) {
Carl Mastrangelode449102015-10-28 11:05:49 -070089 ctx := InteropCtx(t)
90 err := testTLSApplicationProtocol(ctx);
91 matchError(t, err, "EOF")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070092}
93
94func TestTLSMaxVersion(t *testing.T) {
Carl Mastrangelode449102015-10-28 11:05:49 -070095 ctx := InteropCtx(t)
96 err := testTLSMaxVersion(ctx, tls.VersionTLS11);
97 matchError(t, err, "EOF", "server selected unsupported protocol")
Carl Mastrangelo4aca7962015-10-05 16:17:47 -070098}
99
100func TestClientPrefaceWithStreamId(t *testing.T) {
Carl Mastrangelode449102015-10-28 11:05:49 -0700101 ctx := InteropCtx(t)
102 err := testClientPrefaceWithStreamId(ctx)
103 matchError(t, err, "EOF")
104}
105
106func 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 Mastrangelo4aca7962015-10-05 16:17:47 -0700116}
117
118func TestMain(m *testing.M) {
119 flag.Parse()
120 os.Exit(m.Run())
121}