OSDN Git Service

test: add utxo test for cross-chain tx (#72)
[bytom/vapor.git] / p2p / node_info_test.go
1 package p2p
2
3 import (
4         "bytes"
5         "reflect"
6         "testing"
7
8         "github.com/davecgh/go-spew/spew"
9         "github.com/tendermint/go-crypto"
10         "github.com/tendermint/go-wire"
11
12         "github.com/vapor/errors"
13 )
14
15 func mockCompatibleWithFalse(remoteVerStr string) (bool, error) {
16         return false, nil
17 }
18
19 func mockCompatibleWithTrue(remoteVerStr string) (bool, error) {
20         return true, nil
21 }
22
23 func TestCompatibleWith(t *testing.T) {
24         nodeInfo := &NodeInfo{Network: "testnet", NetworkID: 0x888}
25
26         cases := []struct {
27                 other                 *NodeInfo
28                 versionCompatibleWith VersionCompatibleWith
29                 err                   error
30         }{
31                 {other: &NodeInfo{Network: "mainnet", NetworkID: 0x888}, versionCompatibleWith: mockCompatibleWithTrue, err: errDiffNetwork},
32                 {other: &NodeInfo{Network: "testnet", NetworkID: 0x888}, versionCompatibleWith: mockCompatibleWithTrue, err: nil},
33                 {other: &NodeInfo{Network: "testnet", NetworkID: 0x999}, versionCompatibleWith: mockCompatibleWithTrue, err: errDiffNetworkID},
34                 {other: &NodeInfo{Network: "testnet", NetworkID: 0x888}, versionCompatibleWith: mockCompatibleWithFalse, err: errDiffMajorVersion},
35         }
36
37         for i, c := range cases {
38                 if err := nodeInfo.compatibleWith(c.other, c.versionCompatibleWith); errors.Root(err) != c.err {
39                         t.Fatalf("index:%d node info compatible test err want:%s result:%s", i, c.err, err)
40                 }
41         }
42 }
43
44 func TestNodeInfoWriteRead(t *testing.T) {
45         nodeInfo := &NodeInfo{PubKey: crypto.GenPrivKeyEd25519().PubKey().Unwrap().(crypto.PubKeyEd25519), Moniker: "bytomd", Network: "mainnet", NetworkID: 0x888, RemoteAddr: "127.0.0.2:0", ListenAddr: "127.0.0.1:0", Version: "1.1.0-test", ServiceFlag: 10, Other: []string{"abc", "bcd"}}
46         n, err, err1 := new(int), new(error), new(error)
47         buf := new(bytes.Buffer)
48
49         wire.WriteBinary(nodeInfo, buf, n, err)
50         if *err != nil {
51                 t.Fatal(*err)
52         }
53
54         peerNodeInfo := new(NodeInfo)
55         wire.ReadBinary(peerNodeInfo, buf, maxNodeInfoSize, new(int), err1)
56         if *err1 != nil {
57                 t.Fatal(*err1)
58         }
59
60         if !reflect.DeepEqual(*nodeInfo, *peerNodeInfo) {
61                 t.Fatal("TestNodeInfoWriteRead err", spew.Sdump(nodeInfo), spew.Sdump(peerNodeInfo))
62         }
63 }