diff --git a/api/params_test.go b/api/params_test.go index df884878b4317cf1b984771db02285b7391b00c9..82de1d01783f7717716e7f3ca55455cc1bd1b6d2 100644 --- a/api/params_test.go +++ b/api/params_test.go @@ -8,30 +8,42 @@ package api import ( + "bytes" "encoding/json" "testing" ) +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. func TestParams_MarshalUnmarshal(t *testing.T) { + // Construct a set of params p := GetDefaultParams() - data, err := json.Marshal(p) + + // Marshal the params + data, err := json.Marshal(&p) if err != nil { - t.Fatalf("Marshal error: %+v", err) + t.Fatalf("Marshal error: %v", err) } t.Logf("%s", string(data)) - received := GetDefaultParams() + // Unmarshal the params object + received := Params{} err = json.Unmarshal(data, &received) if err != nil { - t.Fatalf("Unmarshal error: %+v", err) + t.Fatalf("Unmarshal error: %v", err) } + // Re-marshal this params object data2, err := json.Marshal(received) if err != nil { - t.Fatalf("Marshal error: %+v", err) + t.Fatalf("Marshal error: %v", err) } - t.Logf("%s", string(data2)) - + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } } diff --git a/cmix/params_test.go b/cmix/params_test.go index bb19499b3640c5234ece0b9a14d2cd7789649872..3a3a2a37ebbecf61ffbde44f799b7de0c9d8f15f 100644 --- a/cmix/params_test.go +++ b/cmix/params_test.go @@ -8,6 +8,7 @@ package cmix import ( + "bytes" "encoding/json" "gitlab.com/xx_network/primitives/id" "reflect" @@ -15,6 +16,41 @@ import ( "time" ) +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. +func TestParams_MarshalJSON(t *testing.T) { + // Construct a set of params + p := GetDefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} + func TestCMIXParams_JSON_Marshal_Unmarshal(t *testing.T) { p := CMIXParams{ RoundTries: 5, diff --git a/fileTransfer2/params_test.go b/fileTransfer2/params_test.go index 849db501a81c9639e12890f8337a55ef85165263..25fbdea22852252095a63539f4611c76aafb260c 100644 --- a/fileTransfer2/params_test.go +++ b/fileTransfer2/params_test.go @@ -8,11 +8,49 @@ package fileTransfer2 import ( + "bytes" + "encoding/json" "gitlab.com/elixxir/client/cmix" "reflect" "testing" ) +// Tests that no data is lost when marshaling and +// unmarshaling the Params object. +func TestParams_MarshalJSON(t *testing.T) { + // Construct a set of params + p := DefaultParams() + + // Marshal the params + data, err := json.Marshal(&p) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + t.Logf("%s", string(data)) + + // Unmarshal the params object + received := Params{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %v", err) + } + + // Re-marshal this params object + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %v", err) + } + + // Check that they match (it is done this way to avoid + // false failures with the reflect.DeepEqual function and + // pointers) + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } + +} + // Tests that DefaultParams returns a Params object with the expected defaults. func TestDefaultParams(t *testing.T) { expected := Params{ diff --git a/single/params_test.go b/single/params_test.go index 39f56c80c888e1496dffebb2b9f60d8951cd055e..7d8224c6127366af542baecb2e755475311967a0 100644 --- a/single/params_test.go +++ b/single/params_test.go @@ -8,11 +8,42 @@ package single import ( + "bytes" + "encoding/json" "gitlab.com/elixxir/client/cmix" "reflect" "testing" ) +// Tests that no data is lost when marshaling and +// unmarshaling the RequestParams object. +func TestParams_MarshalUnmarshal(t *testing.T) { + p := GetDefaultRequestParams() + data, err := json.Marshal(p) + if err != nil { + t.Fatalf("Marshal error: %+v", err) + } + + t.Logf("%s", string(data)) + + received := RequestParams{} + err = json.Unmarshal(data, &received) + if err != nil { + t.Fatalf("Unmarshal error: %+v", err) + } + + data2, err := json.Marshal(received) + if err != nil { + t.Fatalf("Marshal error: %+v", err) + } + + t.Logf("%s", string(data2)) + + if !bytes.Equal(data, data2) { + t.Fatalf("Data was lost in marshal/unmarshal.") + } +} + // Tests that GetDefaultRequestParams returns a RequestParams with the expected // default values. func TestGetDefaultRequestParams(t *testing.T) {