|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2023 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "reflect" |
| 25 | + "sync/atomic" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/codes" |
| 30 | + "google.golang.org/grpc/encoding" |
| 31 | + "google.golang.org/grpc/internal/stubserver" |
| 32 | + testgrpc "google.golang.org/grpc/interop/grpc_testing" |
| 33 | + testpb "google.golang.org/grpc/interop/grpc_testing" |
| 34 | + "google.golang.org/grpc/metadata" |
| 35 | + "google.golang.org/grpc/status" |
| 36 | + "google.golang.org/protobuf/proto" |
| 37 | +) |
| 38 | + |
| 39 | +type errCodec struct { |
| 40 | + noError bool |
| 41 | +} |
| 42 | + |
| 43 | +func (c *errCodec) Marshal(v any) ([]byte, error) { |
| 44 | + if c.noError { |
| 45 | + return []byte{}, nil |
| 46 | + } |
| 47 | + return nil, fmt.Errorf("3987^12 + 4365^12 = 4472^12") |
| 48 | +} |
| 49 | + |
| 50 | +func (c *errCodec) Unmarshal(data []byte, v any) error { |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (c *errCodec) Name() string { |
| 55 | + return "Fermat's near-miss." |
| 56 | +} |
| 57 | + |
| 58 | +func (s) TestEncodeDoesntPanic(t *testing.T) { |
| 59 | + for _, e := range listTestEnv() { |
| 60 | + testEncodeDoesntPanic(t, e) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func testEncodeDoesntPanic(t *testing.T, e env) { |
| 65 | + te := newTest(t, e) |
| 66 | + erc := &errCodec{} |
| 67 | + te.customCodec = erc |
| 68 | + te.startServer(&testServer{security: e.security}) |
| 69 | + defer te.tearDown() |
| 70 | + te.customCodec = nil |
| 71 | + tc := testgrpc.NewTestServiceClient(te.clientConn()) |
| 72 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 73 | + defer cancel() |
| 74 | + // Failure case, should not panic. |
| 75 | + tc.EmptyCall(ctx, &testpb.Empty{}) |
| 76 | + erc.noError = true |
| 77 | + // Passing case. |
| 78 | + if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil { |
| 79 | + t.Fatalf("EmptyCall(_, _) = _, %v, want _, <nil>", err) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +type countingProtoCodec struct { |
| 84 | + marshalCount int32 |
| 85 | + unmarshalCount int32 |
| 86 | +} |
| 87 | + |
| 88 | +func (p *countingProtoCodec) Marshal(v any) ([]byte, error) { |
| 89 | + atomic.AddInt32(&p.marshalCount, 1) |
| 90 | + vv, ok := v.(proto.Message) |
| 91 | + if !ok { |
| 92 | + return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v) |
| 93 | + } |
| 94 | + return proto.Marshal(vv) |
| 95 | +} |
| 96 | + |
| 97 | +func (p *countingProtoCodec) Unmarshal(data []byte, v any) error { |
| 98 | + atomic.AddInt32(&p.unmarshalCount, 1) |
| 99 | + vv, ok := v.(proto.Message) |
| 100 | + if !ok { |
| 101 | + return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v) |
| 102 | + } |
| 103 | + return proto.Unmarshal(data, vv) |
| 104 | +} |
| 105 | + |
| 106 | +func (*countingProtoCodec) Name() string { |
| 107 | + return "proto" |
| 108 | +} |
| 109 | + |
| 110 | +func (s) TestForceServerCodec(t *testing.T) { |
| 111 | + ss := &stubserver.StubServer{ |
| 112 | + EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) { |
| 113 | + return &testpb.Empty{}, nil |
| 114 | + }, |
| 115 | + } |
| 116 | + codec := &countingProtoCodec{} |
| 117 | + if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(codec)}); err != nil { |
| 118 | + t.Fatalf("Error starting endpoint server: %v", err) |
| 119 | + } |
| 120 | + defer ss.Stop() |
| 121 | + |
| 122 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 123 | + defer cancel() |
| 124 | + |
| 125 | + if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil { |
| 126 | + t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err) |
| 127 | + } |
| 128 | + |
| 129 | + unmarshalCount := atomic.LoadInt32(&codec.unmarshalCount) |
| 130 | + const wantUnmarshalCount = 1 |
| 131 | + if unmarshalCount != wantUnmarshalCount { |
| 132 | + t.Fatalf("protoCodec.unmarshalCount = %d; want %d", unmarshalCount, wantUnmarshalCount) |
| 133 | + } |
| 134 | + marshalCount := atomic.LoadInt32(&codec.marshalCount) |
| 135 | + const wantMarshalCount = 1 |
| 136 | + if marshalCount != wantMarshalCount { |
| 137 | + t.Fatalf("protoCodec.marshalCount = %d; want %d", marshalCount, wantMarshalCount) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// renameProtoCodec is an encoding.Codec wrapper that allows customizing the |
| 142 | +// Name() of another codec. |
| 143 | +type renameProtoCodec struct { |
| 144 | + encoding.Codec |
| 145 | + name string |
| 146 | +} |
| 147 | + |
| 148 | +func (r *renameProtoCodec) Name() string { return r.name } |
| 149 | + |
| 150 | +// TestForceCodecName confirms that the ForceCodec call option sets the subtype |
| 151 | +// in the content-type header according to the Name() of the codec provided. |
| 152 | +func (s) TestForceCodecName(t *testing.T) { |
| 153 | + wantContentTypeCh := make(chan []string, 1) |
| 154 | + defer close(wantContentTypeCh) |
| 155 | + |
| 156 | + ss := &stubserver.StubServer{ |
| 157 | + EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) { |
| 158 | + md, ok := metadata.FromIncomingContext(ctx) |
| 159 | + if !ok { |
| 160 | + return nil, status.Errorf(codes.Internal, "no metadata in context") |
| 161 | + } |
| 162 | + if got, want := md["content-type"], <-wantContentTypeCh; !reflect.DeepEqual(got, want) { |
| 163 | + return nil, status.Errorf(codes.Internal, "got content-type=%q; want [%q]", got, want) |
| 164 | + } |
| 165 | + return &testpb.Empty{}, nil |
| 166 | + }, |
| 167 | + } |
| 168 | + if err := ss.Start([]grpc.ServerOption{grpc.ForceServerCodec(encoding.GetCodec("proto"))}); err != nil { |
| 169 | + t.Fatalf("Error starting endpoint server: %v", err) |
| 170 | + } |
| 171 | + defer ss.Stop() |
| 172 | + |
| 173 | + ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 174 | + defer cancel() |
| 175 | + |
| 176 | + codec := &renameProtoCodec{Codec: encoding.GetCodec("proto"), name: "some-test-name"} |
| 177 | + wantContentTypeCh <- []string{"application/grpc+some-test-name"} |
| 178 | + if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil { |
| 179 | + t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err) |
| 180 | + } |
| 181 | + |
| 182 | + // Confirm the name is converted to lowercase before transmitting. |
| 183 | + codec.name = "aNoTHeRNaME" |
| 184 | + wantContentTypeCh <- []string{"application/grpc+anothername"} |
| 185 | + if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodec(codec)); err != nil { |
| 186 | + t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err) |
| 187 | + } |
| 188 | +} |
0 commit comments