github.com/hashicorp/vault/sdk@v0.11.0/helper/jsonutil/json_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package jsonutil
     5  
     6  import (
     7  	"bytes"
     8  	"compress/gzip"
     9  	"fmt"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/hashicorp/vault/sdk/helper/compressutil"
    15  )
    16  
    17  func TestJSONUtil_CompressDecompressJSON(t *testing.T) {
    18  	expected := map[string]interface{}{
    19  		"test":       "data",
    20  		"validation": "process",
    21  	}
    22  
    23  	// Compress an object
    24  	compressedBytes, err := EncodeJSONAndCompress(expected, nil)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if len(compressedBytes) == 0 {
    29  		t.Fatal("expected compressed data")
    30  	}
    31  
    32  	// Check if canary is present in the compressed data
    33  	if compressedBytes[0] != compressutil.CompressionCanaryGzip {
    34  		t.Fatalf("canary missing in compressed data")
    35  	}
    36  
    37  	// Decompress and decode the compressed information and verify the functional
    38  	// behavior
    39  	var actual map[string]interface{}
    40  	if err = DecodeJSON(compressedBytes, &actual); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if !reflect.DeepEqual(expected, actual) {
    44  		t.Fatalf("bad: expected: %#v\nactual: %#v", expected, actual)
    45  	}
    46  	for key := range actual {
    47  		delete(actual, key)
    48  	}
    49  
    50  	// Test invalid data
    51  	if err = DecodeJSON([]byte{}, &actual); err == nil {
    52  		t.Fatalf("expected a failure")
    53  	}
    54  
    55  	// Test invalid data after the canary byte
    56  	var buf bytes.Buffer
    57  	buf.Write([]byte{compressutil.CompressionCanaryGzip})
    58  	if err = DecodeJSON(buf.Bytes(), &actual); err == nil {
    59  		t.Fatalf("expected a failure")
    60  	}
    61  
    62  	// Compress an object
    63  	compressedBytes, err = EncodeJSONAndCompress(expected, &compressutil.CompressionConfig{
    64  		Type:                 compressutil.CompressionTypeGzip,
    65  		GzipCompressionLevel: gzip.BestSpeed,
    66  	})
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	if len(compressedBytes) == 0 {
    71  		t.Fatal("expected compressed data")
    72  	}
    73  
    74  	// Check if canary is present in the compressed data
    75  	if compressedBytes[0] != compressutil.CompressionCanaryGzip {
    76  		t.Fatalf("canary missing in compressed data")
    77  	}
    78  
    79  	// Decompress and decode the compressed information and verify the functional
    80  	// behavior
    81  	if err = DecodeJSON(compressedBytes, &actual); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if !reflect.DeepEqual(expected, actual) {
    85  		t.Fatalf("bad: expected: %#v\nactual: %#v", expected, actual)
    86  	}
    87  }
    88  
    89  func TestJSONUtil_EncodeJSON(t *testing.T) {
    90  	input := map[string]interface{}{
    91  		"test":       "data",
    92  		"validation": "process",
    93  	}
    94  
    95  	actualBytes, err := EncodeJSON(input)
    96  	if err != nil {
    97  		t.Fatalf("failed to encode JSON: %v", err)
    98  	}
    99  
   100  	actual := strings.TrimSpace(string(actualBytes))
   101  	expected := `{"test":"data","validation":"process"}`
   102  
   103  	if actual != expected {
   104  		t.Fatalf("bad: encoded JSON: expected:%s\nactual:%s\n", expected, string(actualBytes))
   105  	}
   106  }
   107  
   108  func TestJSONUtil_DecodeJSON(t *testing.T) {
   109  	input := `{"test":"data","validation":"process"}`
   110  
   111  	var actual map[string]interface{}
   112  
   113  	err := DecodeJSON([]byte(input), &actual)
   114  	if err != nil {
   115  		fmt.Printf("decoding err: %v\n", err)
   116  	}
   117  
   118  	expected := map[string]interface{}{
   119  		"test":       "data",
   120  		"validation": "process",
   121  	}
   122  	if !reflect.DeepEqual(actual, expected) {
   123  		t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
   124  	}
   125  }
   126  
   127  func TestJSONUtil_DecodeJSONFromReader(t *testing.T) {
   128  	input := `{"test":"data","validation":"process"}`
   129  
   130  	var actual map[string]interface{}
   131  
   132  	err := DecodeJSONFromReader(bytes.NewReader([]byte(input)), &actual)
   133  	if err != nil {
   134  		fmt.Printf("decoding err: %v\n", err)
   135  	}
   136  
   137  	expected := map[string]interface{}{
   138  		"test":       "data",
   139  		"validation": "process",
   140  	}
   141  	if !reflect.DeepEqual(actual, expected) {
   142  		t.Fatalf("bad: expected:%#v\nactual:%#v", expected, actual)
   143  	}
   144  }