@@ -785,3 +785,62 @@ func (s) TestGzipBadChecksum(t *testing.T) {
785
785
t .Errorf ("ss.Client.UnaryCall(_) = _, %v\n \t want: _, status(codes.Internal, contains %q)" , err , gzip .ErrChecksum )
786
786
}
787
787
}
788
+
789
+ // fakeCompressor returns a messages of a configured size, irrespective of the
790
+ // input.
791
+ type fakeCompressor struct {
792
+ decompressedMessageSize int
793
+ }
794
+
795
+ func (f * fakeCompressor ) Compress (w io.Writer ) (io.WriteCloser , error ) {
796
+ return nopWriteCloser {w }, nil
797
+ }
798
+
799
+ func (f * fakeCompressor ) Decompress (io.Reader ) (io.Reader , error ) {
800
+ return bytes .NewReader (make ([]byte , f .decompressedMessageSize )), nil
801
+ }
802
+
803
+ func (f * fakeCompressor ) Name () string {
804
+ // Use the name of an existing compressor to avoid interactions with other
805
+ // tests since compressors can't be un-registered.
806
+ return "gzip"
807
+ }
808
+
809
+ type nopWriteCloser struct {
810
+ io.Writer
811
+ }
812
+
813
+ func (nopWriteCloser ) Close () error {
814
+ return nil
815
+ }
816
+
817
+ // TestDecompressionExceedsMaxMessageSize uses a fake compressor that produces
818
+ // messages of size 100 bytes on decompression. A server is started with the
819
+ // max receive message size restricted to 99 bytes. The test verifies that the
820
+ // client receives a ResourceExhausted response from the server.
821
+ func (s ) TestDecompressionExceedsMaxMessageSize (t * testing.T ) {
822
+ oldC := encoding .GetCompressor ("gzip" )
823
+ defer func () {
824
+ encoding .RegisterCompressor (oldC )
825
+ }()
826
+ const messageLen = 100
827
+ encoding .RegisterCompressor (& fakeCompressor {decompressedMessageSize : messageLen })
828
+ ss := & stubserver.StubServer {
829
+ UnaryCallF : func (context.Context , * testpb.SimpleRequest ) (* testpb.SimpleResponse , error ) {
830
+ return & testpb.SimpleResponse {}, nil
831
+ },
832
+ }
833
+ if err := ss .Start ([]grpc.ServerOption {grpc .MaxRecvMsgSize (messageLen - 1 )}); err != nil {
834
+ t .Fatalf ("Error starting endpoint server: %v" , err )
835
+ }
836
+ defer ss .Stop ()
837
+
838
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
839
+ defer cancel ()
840
+
841
+ req := & testpb.SimpleRequest {Payload : & testpb.Payload {}}
842
+ _ , err := ss .Client .UnaryCall (ctx , req , grpc .UseCompressor ("gzip" ))
843
+ if got , want := status .Code (err ), codes .ResourceExhausted ; got != want {
844
+ t .Errorf ("Client.UnaryCall(%+v) returned status %v, want %v" , req , got , want )
845
+ }
846
+ }
0 commit comments