From addb5b1e6ecb3a7cfaeb5363a81bf6371c04914b Mon Sep 17 00:00:00 2001 From: Ferenc Szabo Date: Thu, 31 Jan 2019 12:45:04 +0100 Subject: [PATCH] p2p/simulations: Add a sanity test case for Node.Config UnmarshalJSON --- p2p/simulations/network.go | 8 +++---- p2p/simulations/network_test.go | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index f0c9714498..e375123211 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -653,16 +653,16 @@ func (n *Node) MarshalJSON() ([]byte, error) { func (n *Node) UnmarshalJSON(raw []byte) error { // TODO: How should we turn back NodeInfo into n.Node? // Ticket: https://github.com/ethersphere/go-ethereum/issues/1177 - no := struct { + node := struct { Config *adapters.NodeConfig `json:"config,omitempty"` Up bool `json:"up"` }{} - if err := json.Unmarshal(raw, &no); err != nil { + if err := json.Unmarshal(raw, &node); err != nil { return err } - n.SetUp(no.Up) - n.Config = no.Config + n.SetUp(node.Up) + n.Config = node.Config return nil } diff --git a/p2p/simulations/network_test.go b/p2p/simulations/network_test.go index 7a7e64d1eb..700e0cfa54 100644 --- a/p2p/simulations/network_test.go +++ b/p2p/simulations/network_test.go @@ -494,6 +494,12 @@ func TestNode_UnmarshalJSON(t *testing.T) { runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONUpField()) }, ) + t.Run( + "test unmarshal of Node Config field", + func(t *testing.T) { + runNodeUnmarshalJSON(t, casesNodeUnmarshalJSONConfig()) + }, + ) } func runNodeUnmarshalJSON(t *testing.T, tests []nodeUnmarshalTestCase) { @@ -583,3 +589,34 @@ func casesNodeUnmarshalJSONUpField() []nodeUnmarshalTestCase { }, } } + +func casesNodeUnmarshalJSONConfig() []nodeUnmarshalTestCase { + // Don't do a big fuss around testing, as adapters.NodeConfig should + // handle it's own serialization. Just do a sanity check. + return []nodeUnmarshalTestCase{ + { + name: "missing Config field", + marshaled: "{}", + want: Node{ + Config: nil, + }, + }, + { + name: "Config field is nil", + marshaled: "{\"config\": nil}", + want: Node{ + Config: nil, + }, + }, + { + name: "a non default Config field", + marshaled: "{\"config\":{\"name\":\"node_ecdd0\",\"port\":44665}}", + want: Node{ + Config: &adapters.NodeConfig{ + Name: "node_ecdd0", + Port: 44665, + }, + }, + }, + } +}