open-vault/helper/jsonutil/json_test.go

142 lines
3.4 KiB
Go
Raw Normal View History

2016-07-07 16:12:51 +00:00
package jsonutil
import (
"bytes"
2016-08-09 14:33:41 +00:00
"compress/gzip"
2016-07-07 16:12:51 +00:00
"fmt"
"reflect"
"strings"
"testing"
2016-08-08 19:28:54 +00:00
"github.com/hashicorp/vault/helper/compressutil"
2016-07-07 16:12:51 +00:00
)
2016-08-08 19:28:54 +00:00
func TestJSONUtil_CompressDecompressJSON(t *testing.T) {
expected := map[string]interface{}{
"test": "data",
"validation": "process",
}
// Compress an object
2016-08-09 14:33:41 +00:00
compressedBytes, err := EncodeJSONAndCompress(expected, nil)
2016-08-08 19:28:54 +00:00
if err != nil {
t.Fatal(err)
}
if len(compressedBytes) == 0 {
t.Fatal("expected compressed data")
}
// Check if canary is present in the compressed data
2016-08-09 14:33:41 +00:00
if compressedBytes[0] != compressutil.CompressionCanaryGzip {
2016-08-08 19:28:54 +00:00
t.Fatalf("canary missing in compressed data")
}
// Decompress and decode the compressed information and verify the functional
// behavior
var actual map[string]interface{}
2016-08-09 14:33:41 +00:00
if err = DecodeJSON(compressedBytes, &actual); err != nil {
2016-08-08 19:28:54 +00:00
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
2016-08-09 14:33:41 +00:00
if err = DecodeJSON([]byte{}, &actual); err == nil {
2016-08-08 19:28:54 +00:00
t.Fatalf("expected a failure")
}
// Test invalid data after the canary byte
var buf bytes.Buffer
2016-08-09 14:33:41 +00:00
buf.Write([]byte{compressutil.CompressionCanaryGzip})
if err = DecodeJSON(buf.Bytes(), &actual); err == nil {
2016-08-08 19:28:54 +00:00
t.Fatalf("expected a failure")
}
2016-08-09 14:33:41 +00:00
// Compress an object
compressedBytes, err = EncodeJSONAndCompress(expected, &compressutil.CompressionConfig{
Type: compressutil.CompressionTypeGzip,
GzipCompressionLevel: gzip.BestSpeed,
})
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.CompressionCanaryGzip {
t.Fatalf("canary missing in compressed data")
}
// Decompress and decode the compressed information and verify the functional
// behavior
if err = DecodeJSON(compressedBytes, &actual); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("bad: expected: %#v\nactual: %#v", expected, actual)
}
2016-08-08 19:28:54 +00:00
}
func TestJSONUtil_EncodeJSON(t *testing.T) {
2016-07-07 16:12:51 +00:00
input := map[string]interface{}{
"test": "data",
"validation": "process",
}
actualBytes, err := EncodeJSON(input)
if err != nil {
t.Fatalf("failed to encode JSON: %v", err)
}
actual := strings.TrimSpace(string(actualBytes))
expected := `{"test":"data","validation":"process"}`
if actual != expected {
t.Fatal("bad: encoded JSON: expected:%s\nactual:%s\n", expected, string(actualBytes))
}
}
2016-08-08 19:28:54 +00:00
func TestJSONUtil_DecodeJSON(t *testing.T) {
2016-07-07 16:12:51 +00:00
input := `{"test":"data","validation":"process"}`
var actual map[string]interface{}
err := DecodeJSON([]byte(input), &actual)
if err != nil {
fmt.Printf("decoding err: %v\n", err)
}
expected := map[string]interface{}{
"test": "data",
"validation": "process",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
}
}
2016-08-08 19:28:54 +00:00
func TestJSONUtil_DecodeJSONFromReader(t *testing.T) {
2016-07-07 16:12:51 +00:00
input := `{"test":"data","validation":"process"}`
var actual map[string]interface{}
err := DecodeJSONFromReader(bytes.NewReader([]byte(input)), &actual)
if err != nil {
fmt.Printf("decoding err: %v\n", err)
}
expected := map[string]interface{}{
"test": "data",
"validation": "process",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual)
}
}