diff --git a/helper/compressutil/compress.go b/helper/compressutil/compress.go index 3a7538810..7da854338 100644 --- a/helper/compressutil/compress.go +++ b/helper/compressutil/compress.go @@ -6,6 +6,7 @@ import ( "compress/lzw" "fmt" "io" + "log" ) const ( @@ -155,5 +156,6 @@ func Decompress(data []byte, config *CompressionConfig) ([]byte, bool, error) { return nil, false, err } + log.Printf("decompressedBytes: %s\n", string(jsonBuf.Bytes())) return jsonBuf.Bytes(), false, nil } diff --git a/helper/jsonutil/json_test.go b/helper/jsonutil/json_test.go index d7e85d2b5..7366b12e3 100644 --- a/helper/jsonutil/json_test.go +++ b/helper/jsonutil/json_test.go @@ -6,9 +6,57 @@ import ( "reflect" "strings" "testing" + + "github.com/hashicorp/vault/helper/compressutil" ) -func Test_EncodeJSON(t *testing.T) { +func TestJSONUtil_CompressDecompressJSON(t *testing.T) { + expected := map[string]interface{}{ + "test": "data", + "validation": "process", + } + + // Compress an object + compressedBytes, err := EncodeJSONAndCompress(expected) + if err != nil { + t.Fatal(err) + } + if len(compressedBytes) == 0 { + t.Fatal("expected compressed data") + } + + // Check if canary is present in the compressed data + if compressedBytes[0] != compressutil.CompressionCanary { + t.Fatalf("canary missing in compressed data") + } + + // Decompress and decode the compressed information and verify the functional + // behavior + var actual map[string]interface{} + if err = DecompressAndDecodeJSON(compressedBytes, &actual); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(expected, actual) { + t.Fatalf("bad: expected: %#v\nactual: %#v", expected, actual) + } + for key, _ := range actual { + delete(actual, key) + } + + // Test invalid data + if err = DecompressAndDecodeJSON([]byte{}, &actual); err == nil { + t.Fatalf("expected a failure") + } + + // Test invalid data after the canary byte + var buf bytes.Buffer + buf.Write([]byte{compressutil.CompressionCanary}) + if err = DecompressAndDecodeJSON(buf.Bytes(), &actual); err == nil { + t.Fatalf("expected a failure") + } +} + +func TestJSONUtil_EncodeJSON(t *testing.T) { input := map[string]interface{}{ "test": "data", "validation": "process", @@ -27,7 +75,7 @@ func Test_EncodeJSON(t *testing.T) { } } -func Test_DecodeJSON(t *testing.T) { +func TestJSONUtil_DecodeJSON(t *testing.T) { input := `{"test":"data","validation":"process"}` var actual map[string]interface{} @@ -46,7 +94,7 @@ func Test_DecodeJSON(t *testing.T) { } } -func Test_DecodeJSONFromReader(t *testing.T) { +func TestJSONUtil_DecodeJSONFromReader(t *testing.T) { input := `{"test":"data","validation":"process"}` var actual map[string]interface{}