change fs.json format to include checksum fields (#5685)

This commit is contained in:
poornas
2018-03-27 17:23:10 -07:00
committed by kannappanr
parent 3ebe61abdf
commit 76d1e8bbcd
3 changed files with 115 additions and 28 deletions

View File

@@ -21,6 +21,7 @@ import (
"context"
"os"
"path/filepath"
"reflect"
"testing"
)
@@ -111,7 +112,47 @@ func TestWriteFSMetadata(t *testing.T) {
if fsMeta.Version != fsMetaVersion {
t.Fatalf("Unexpected version %s", fsMeta.Version)
}
if fsMeta.Format != fsMetaFormat {
t.Fatalf("Unexpected format %s", fsMeta.Format)
}
func TestFSChecksumV1MarshalJSON(t *testing.T) {
var cs FSChecksumInfoV1
testCases := []struct {
checksum FSChecksumInfoV1
expectedResult string
}{
{cs, `{"algorithm":"","blocksize":0,"hashes":null}`},
{FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 500}, `{"algorithm":"highwayhash","blocksize":500,"hashes":null}`},
{FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 10, Hashes: [][]byte{[]byte("hello")}}, `{"algorithm":"highwayhash","blocksize":10,"hashes":["68656c6c6f"]}`},
}
for _, testCase := range testCases {
data, _ := testCase.checksum.MarshalJSON()
if testCase.expectedResult != string(data) {
t.Fatalf("expected: %v, got: %v", testCase.expectedResult, string(data))
}
}
}
func TestFSChecksumV1UnMarshalJSON(t *testing.T) {
var cs FSChecksumInfoV1
testCases := []struct {
data []byte
expectedResult FSChecksumInfoV1
}{
{[]byte(`{"algorithm":"","blocksize":0,"hashes":null}`), cs},
{[]byte(`{"algorithm":"highwayhash","blocksize":500,"hashes":null}`), FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 500}},
{[]byte(`{"algorithm":"highwayhash","blocksize":10,"hashes":["68656c6c6f"]}`), FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 10, Hashes: [][]byte{[]byte("hello")}}},
}
for _, testCase := range testCases {
err := (&cs).UnmarshalJSON(testCase.data)
if err != nil {
t.Fatal("Unexpected error during checksum unmarshalling ", err)
}
if !reflect.DeepEqual(testCase.expectedResult, cs) {
t.Fatalf("expected: %v, got: %v", testCase.expectedResult, cs)
}
}
}