http2interop_test.go 4.7 KB

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