github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/vault/sdk/helper/compressutil/compress_test.go (about)

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