github.com/hashicorp/vault/sdk@v0.11.0/helper/compressutil/compress_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package compressutil 5 6 import ( 7 "bytes" 8 "compress/gzip" 9 "testing" 10 ) 11 12 func TestCompressUtil_CompressDecompress(t *testing.T) { 13 t.Parallel() 14 15 tests := []struct { 16 compressionType string 17 compressionConfig CompressionConfig 18 canary byte 19 }{ 20 { 21 "GZIP default implicit", 22 CompressionConfig{Type: CompressionTypeGzip}, 23 CompressionCanaryGzip, 24 }, 25 { 26 "GZIP default explicit", 27 CompressionConfig{Type: CompressionTypeGzip, GzipCompressionLevel: gzip.DefaultCompression}, 28 CompressionCanaryGzip, 29 }, 30 { 31 "GZIP best speed", 32 CompressionConfig{Type: CompressionTypeGzip, GzipCompressionLevel: gzip.BestSpeed}, 33 CompressionCanaryGzip, 34 }, 35 { 36 "GZIP best compression", 37 CompressionConfig{Type: CompressionTypeGzip, GzipCompressionLevel: gzip.BestCompression}, 38 CompressionCanaryGzip, 39 }, 40 { 41 "Snappy", 42 CompressionConfig{Type: CompressionTypeSnappy}, 43 CompressionCanarySnappy, 44 }, 45 { 46 "LZ4", 47 CompressionConfig{Type: CompressionTypeLZ4}, 48 CompressionCanaryLZ4, 49 }, 50 { 51 "LZW", 52 CompressionConfig{Type: CompressionTypeLZW}, 53 CompressionCanaryLZW, 54 }, 55 } 56 57 inputJSONBytes := []byte(`{"sample":"data","verification":"process"}`) 58 59 for _, test := range tests { 60 // Compress the input 61 compressedJSONBytes, err := Compress(inputJSONBytes, &test.compressionConfig) 62 if err != nil { 63 t.Fatalf("compress error (%s): %s", test.compressionType, err) 64 } 65 if len(compressedJSONBytes) == 0 { 66 t.Fatalf("failed to compress data in %s format", test.compressionType) 67 } 68 69 // Check the presence of the canary 70 if compressedJSONBytes[0] != test.canary { 71 t.Fatalf("bad (%s): compression canary: expected: %d actual: %d", test.compressionType, test.canary, compressedJSONBytes[0]) 72 } 73 74 decompressedJSONBytes, wasNotCompressed, err := Decompress(compressedJSONBytes) 75 if err != nil { 76 t.Fatalf("decompress error (%s): %s", test.compressionType, err) 77 } 78 79 // Check if the input for decompress was not compressed in the first place 80 if wasNotCompressed { 81 t.Fatalf("bad (%s): expected compressed bytes", test.compressionType) 82 } 83 84 if len(decompressedJSONBytes) == 0 { 85 t.Fatalf("bad (%s): expected decompressed bytes", test.compressionType) 86 } 87 88 // Compare the value after decompression 89 if !bytes.Equal(inputJSONBytes, decompressedJSONBytes) { 90 t.Fatalf("bad (%s): decompressed value;\nexpected: %q\nactual: %q", test.compressionType, string(inputJSONBytes), string(decompressedJSONBytes)) 91 } 92 93 decompressedJSONBytes, compressionType, wasNotCompressed, err := DecompressWithCanary(compressedJSONBytes) 94 if err != nil { 95 t.Fatalf("decompress error (%s): %s", test.compressionType, err) 96 } 97 98 if compressionType != test.compressionConfig.Type { 99 t.Fatalf("bad compressionType value;\nexpected: %q\naction: %q", test.compressionConfig.Type, compressionType) 100 } 101 } 102 } 103 104 func TestCompressUtil_InvalidConfigurations(t *testing.T) { 105 t.Parallel() 106 107 inputJSONBytes := []byte(`{"sample":"data","verification":"process"}`) 108 109 // Test nil configuration 110 if _, err := Compress(inputJSONBytes, nil); err == nil { 111 t.Fatal("expected an error") 112 } 113 114 // Test invalid configuration 115 if _, err := Compress(inputJSONBytes, &CompressionConfig{}); err == nil { 116 t.Fatal("expected an error") 117 } 118 }