| 10 | ) |
| 11 | |
| 12 | func TestDecompress(t *testing.T) { |
| 13 | codecs := []CompressionCodec{ |
| 14 | CompressionGZIP, |
| 15 | CompressionSnappy, |
| 16 | CompressionLZ4, |
| 17 | CompressionZSTD, |
| 18 | } |
| 19 | |
| 20 | const limit = 64 * 1024 |
| 21 | |
| 22 | withLimit := func(t *testing.T, v int32) { |
| 23 | t.Helper() |
| 24 | old := MaxDecompressedBatchSize |
| 25 | MaxDecompressedBatchSize = v |
| 26 | t.Cleanup(func() { MaxDecompressedBatchSize = old }) |
| 27 | } |
| 28 | |
| 29 | t.Run("rejects a compression bomb under the wire limit", func(t *testing.T) { |
| 30 | for _, cc := range codecs { |
| 31 | t.Run(cc.String(), func(t *testing.T) { |
| 32 | withLimit(t, limit) |
| 33 | |
| 34 | // highly compressible input that decompresses well past the cap |
| 35 | payload := make([]byte, limit*8) |
| 36 | compressed, err := compress(cc, CompressionLevelDefault, payload) |
| 37 | require.NoError(t, err) |
| 38 | require.Less(t, len(compressed), int(MaxResponseSize)) |
| 39 | |
| 40 | out, err := decompress(cc, compressed) |
| 41 | require.ErrorIs(t, err, ErrDecompressedBatchTooLarge) |
| 42 | require.Empty(t, out) |
| 43 | }) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | t.Run("decodes a batch well under the cap", func(t *testing.T) { |
| 48 | for _, cc := range codecs { |
| 49 | t.Run(cc.String(), func(t *testing.T) { |
| 50 | withLimit(t, limit) |
| 51 | |
| 52 | payload := bytes.Repeat([]byte("sarama"), 1000) |
| 53 | require.Less(t, len(payload), limit) |
| 54 | |
| 55 | compressed, err := compress(cc, CompressionLevelDefault, payload) |
| 56 | require.NoError(t, err) |
| 57 | |
| 58 | out, err := decompress(cc, compressed) |
| 59 | require.NoError(t, err) |
| 60 | require.Equal(t, payload, out) |
| 61 | }) |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | t.Run("accepts exactly the cap and rejects one byte over", func(t *testing.T) { |
| 66 | for _, cc := range codecs { |
| 67 | t.Run(cc.String(), func(t *testing.T) { |
| 68 | withLimit(t, limit) |
| 69 | |