mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
p2p/simulations: Add a sanity test case for Node.Config UnmarshalJSON
This commit is contained in:
parent
e7fdfbf923
commit
addb5b1e6e
2 changed files with 41 additions and 4 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue