1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
//go:build ignore
// +build ignore
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package h2_test
import (
"context"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net/url"
"sync"
"testing"
"github.com/google/martian/v3/h2"
mgrpc "github.com/google/martian/v3/h2/grpc"
ht "github.com/google/martian/v3/h2/testing"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/protobuf/proto"
tspb "github.com/google/martian/v3/h2/testservice"
)
type requestProcessor struct {
dest mgrpc.Processor
requests *[]*tspb.EchoRequest
}
func (p *requestProcessor) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
return p.dest.Header(headers, streamEnded, priority)
}
func (p *requestProcessor) Message(data []byte, streamEnded bool) error {
msg := &tspb.EchoRequest{}
if err := proto.Unmarshal(data, msg); err != nil {
return fmt.Errorf("unmarshalling request: %w", err)
}
*p.requests = append(*p.requests, msg)
return p.dest.Message(data, streamEnded)
}
type responseProcessor struct {
dest mgrpc.Processor
responses *[]*tspb.EchoResponse
}
func (p *responseProcessor) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
return p.dest.Header(headers, streamEnded, priority)
}
func (p *responseProcessor) Message(data []byte, streamEnded bool) error {
msg := &tspb.EchoResponse{}
if err := proto.Unmarshal(data, msg); err != nil {
return fmt.Errorf("unmarshalling response: %w", err)
}
*p.responses = append(*p.responses, msg)
return p.dest.Message(data, streamEnded)
}
func TestEcho(t *testing.T) {
// This is a basic smoke test. It verifies that the end-to-end flow works and that gRPC messages
// are observed as expected in processors.
var requests []*tspb.EchoRequest
var responses []*tspb.EchoResponse
fixture, err := ht.New([]h2.StreamProcessorFactory{
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &requestProcessor{server, &requests}, &responseProcessor{client, &responses}
}),
})
if err != nil {
t.Fatalf("ht.New(...) = %v, want nil", err)
}
defer func() {
if err := fixture.Close(); err != nil {
t.Fatalf("f.Close() = %v, want nil", err)
}
}()
ctx := context.Background()
req := &tspb.EchoRequest{
Payload: "Hello",
}
resp, err := fixture.Echo(ctx, req)
if err != nil {
t.Fatalf("fixture.Echo(...) = _, %v, want _, nil", err)
}
if got, want := resp.GetPayload(), req.GetPayload(); got != want {
t.Errorf("resp.GetPayload() = %s, want = %s", got, want)
}
// Verifies the captured requests and responses.
if got := len(requests); got != 1 {
t.Fatalf("len(requests) = %d, want 1", got)
}
if got, want := requests[0].GetPayload(), req.GetPayload(); got != want {
t.Errorf("requests[0].GetPayload() = %s, want = %s", got, want)
}
if got := len(responses); got != 1 {
t.Fatalf("len(requests) = %d, want 1", got)
}
if got, want := responses[0].GetPayload(), req.GetPayload(); got != want {
t.Errorf("responses[0].GetPayload() = %s, want = %s", got, want)
}
}
type requestEditor struct {
dest mgrpc.Processor
}
func (p *requestEditor) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
return p.dest.Header(headers, streamEnded, priority)
}
func (p *requestEditor) Message(_ []byte, streamEnded bool) error {
msg := &tspb.EchoRequest{
Payload: "Goodbye",
}
data, err := proto.Marshal(msg)
if err != nil {
return fmt.Errorf("marshalling request: %w", err)
}
return p.dest.Message(data, streamEnded)
}
func TestRequestEditor(t *testing.T) {
// This test inserts a request modifier that changes the payload from "Hello" to "Goodbye".
fixture, err := ht.New([]h2.StreamProcessorFactory{
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &requestEditor{server}, nil
}),
})
if err != nil {
t.Fatalf("ht.New(...) = %v, want nil", err)
}
defer func() {
if err := fixture.Close(); err != nil {
t.Fatalf("f.Close() = %v, want nil", err)
}
}()
ctx := context.Background()
req := &tspb.EchoRequest{
Payload: "Hello",
}
resp, err := fixture.Echo(ctx, req)
if err != nil {
t.Fatalf("fixture.Echo(...) = _, %v, want _, nil", err)
}
if got, want := resp.GetPayload(), "Goodbye"; got != want {
t.Errorf("resp.GetPayload() = %s, want = %s", got, want)
}
}
type plusOne struct {
dest mgrpc.Processor
}
func (p *plusOne) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
return p.dest.Header(headers, streamEnded, priority)
}
func (p *plusOne) Message(data []byte, streamEnded bool) error {
msg := &tspb.SumRequest{}
if err := proto.Unmarshal(data, msg); err != nil {
return fmt.Errorf("unmarshalling request: %w", err)
}
msg.Values = append(msg.Values, 1)
data, err := proto.Marshal(msg)
if err != nil {
return fmt.Errorf("marshalling request: %w", err)
}
return p.dest.Message(data, streamEnded)
}
func TestProcessorChaining(t *testing.T) {
// This test constructs a chain of processors and checks that the effects are correctly applied
// at the result.
fixture, err := ht.New([]h2.StreamProcessorFactory{
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &plusOne{server}, nil
}),
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &plusOne{server}, nil
}),
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &plusOne{server}, nil
}),
})
if err != nil {
t.Fatalf("ht.New(...) = %v, want nil", err)
}
defer func() {
if err := fixture.Close(); err != nil {
t.Fatalf("f.Close() = %v, want nil", err)
}
}()
ctx := context.Background()
req := &tspb.SumRequest{
Values: []int32{5},
}
resp, err := fixture.Sum(ctx, req)
if err != nil {
t.Fatalf("fixture.Sum(...) = _, %v, want _, nil", err)
}
if got, want := resp.GetValue(), int32(8); got != want {
t.Errorf("resp.GetValue() = %d, want = %d", got, want)
}
}
type headerCapture struct {
dest mgrpc.Processor
headers *[][]hpack.HeaderField
}
func (h *headerCapture) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
c := make([]hpack.HeaderField, len(headers))
copy(c, headers)
*h.headers = append(*h.headers, c)
return h.dest.Header(headers, streamEnded, priority)
}
func (h *headerCapture) Message(data []byte, streamEnded bool) error {
return h.dest.Message(data, streamEnded)
}
func TestLargeEcho(t *testing.T) {
// Sends a >128KB payload through the proxy. Since the standard gRPC frame size is only 16KB,
// this exercises frame merging, splitting and flow control code.
payload := make([]byte, 128*1024)
rand.Read(payload)
req := &tspb.EchoRequest{
Payload: base64.StdEncoding.EncodeToString(payload),
}
// This test also covers using gzip compression. Ideally, we would test more compression types
// but the golang gRPC implementation only provides a gzip compressor.
tests := []struct {
name string
useCompression bool
}{
{"RawData", false},
{"Gzip", true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var cToSHeaders, sToCHeaders [][]hpack.HeaderField
fixture, err := ht.New([]h2.StreamProcessorFactory{
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &headerCapture{server, &cToSHeaders}, &headerCapture{client, &sToCHeaders}
}),
})
if err != nil {
t.Fatalf("ht.New(...) = %v, want nil", err)
}
defer func() {
if err := fixture.Close(); err != nil {
t.Fatalf("f.Close() = %v, want nil", err)
}
}()
ctx := context.Background()
var resp *tspb.EchoResponse
if tc.useCompression {
resp, err = fixture.Echo(ctx, req, grpc.UseCompressor(gzip.Name))
} else {
resp, err = fixture.Echo(ctx, req)
}
if err != nil {
t.Fatalf("fixture.Echo(...) = _, %v, want _, nil", err)
}
if got, want := resp.GetPayload(), req.GetPayload(); got != want {
t.Errorf("resp.GetPayload() = %s, want = %s", got, want)
}
// Verifies that grpc-encoding=gzip is present in the first headers on the stream when
// compression is active.
for _, headers := range [][]hpack.HeaderField{cToSHeaders[0], sToCHeaders[0]} {
foundGRPCEncoding := false
for _, h := range headers {
if h.Name == "grpc-encoding" {
foundGRPCEncoding = true
if got, want := h.Value, "gzip"; got != want {
t.Errorf("h.Value = %s, want %s", got, want)
}
}
}
if got, want := foundGRPCEncoding, tc.useCompression; got != want {
t.Errorf("foundGRPCEncoding = %t, want %t", got, want)
}
}
})
}
}
type noopProcessor struct {
sink mgrpc.Processor
}
func (p *noopProcessor) Header(
headers []hpack.HeaderField,
streamEnded bool,
priority http2.PriorityParam,
) error {
return p.sink.Header(headers, streamEnded, priority)
}
func (p *noopProcessor) Message(data []byte, streamEnded bool) error {
return p.sink.Message(data, streamEnded)
}
func TestStream(t *testing.T) {
tests := []struct {
name string
factory h2.StreamProcessorFactory
}{
{
"NilH2Processor",
func(_ *url.URL, _ *h2.Processors) (h2.Processor, h2.Processor) {
return nil, nil
},
},
{
// This differs from NilH2Processor only in how mgrpc.AsStreamProcessorFactory handles nil
// grpc.Processor values. It should end up processing exactly the same as
// h2.StreamProcessorFactory afterwards.
"NilGRPCProcessor",
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, _, _ mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return nil, nil
}),
},
{
// This differs from NilGRPCProcessor in that NilGRPCProcessor ends up behaving like
// NilH2Processor and no gRPC processing takes place. NoopProcessor causes the frames to
// be processed as gRPC.
"NoopGRPCProcessor",
mgrpc.AsStreamProcessorFactory(
func(_ *url.URL, server, client mgrpc.Processor) (mgrpc.Processor, mgrpc.Processor) {
return &noopProcessor{server}, &noopProcessor{client}
}),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fixture, err := ht.New([]h2.StreamProcessorFactory{tc.factory})
if err != nil {
t.Fatalf("ht.New(...) = %v, want nil", err)
}
defer func() {
if err := fixture.Close(); err != nil {
t.Fatalf("f.Close() = %v, want nil", err)
}
}()
ctx := context.Background()
stream, err := fixture.DoubleEcho(ctx)
if err != nil {
t.Fatalf("fixture.DoubleEcho(ctx) = _, %v, want _, nil", err)
}
var received []*tspb.EchoResponse
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
resp, err := stream.Recv()
if err == io.EOF {
return
}
if err != nil {
t.Errorf("stream.Recv() = %v, want nil", err)
return
}
received = append(received, resp)
}
}()
var sent []*tspb.EchoRequest
for i := 0; i < 5; i++ {
payload := make([]byte, 20*1024)
rand.Read(payload)
req := &tspb.EchoRequest{
Payload: base64.StdEncoding.EncodeToString(payload),
}
if err := stream.Send(req); err != nil {
t.Fatalf("stream.Send(req) = %v, want nil", err)
}
sent = append(sent, req)
}
if err := stream.CloseSend(); err != nil {
t.Fatalf("stream.CloseSend() = %v, want nil", err)
}
wg.Wait()
for i, req := range sent {
want := req.GetPayload()
if got := received[2*i].GetPayload(); got != want {
t.Errorf("received[2*i].GetPayload() = %s, want %s", got, want)
}
if got := received[2*i+1].GetPayload(); got != want {
t.Errorf("received[2*i+1].GetPayload() = %s, want %s", got, want)
}
}
})
}
}
|