github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/common/message_test.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 // github.com/stretchr/testify/assert 19 20 package common 21 22 import ( 23 "bytes" 24 "net" 25 "testing" 26 27 "github.com/ontio/ontology/common" 28 "github.com/stretchr/testify/assert" 29 comm "github.com/sixexorg/magnetic-ring/p2pserver/common" 30 // cm "github.com/ontio/ontology/common" 31 ) 32 33 func TestVerackSerializationDeserialization(t *testing.T) { 34 var msg VerACK 35 msg.IsConsensus = false 36 37 MessageTest(t, &msg) 38 } 39 40 func TestAddrReqSerializationDeserialization(t *testing.T) { 41 var msg AddrReq 42 43 MessageTest(t, &msg) 44 } 45 46 // addr 47 func MessageTest(t *testing.T, msg Message) { 48 sink := common.NewZeroCopySink(nil) 49 err := WriteMessage(sink, msg) 50 assert.Nil(t, err) 51 52 demsg, _, err := ReadMessage(bytes.NewBuffer(sink.Bytes())) 53 assert.Nil(t, err) 54 55 assert.Equal(t, msg, demsg) 56 } 57 58 func TestAddressSerializationDeserialization(t *testing.T) { 59 var msg Addr 60 var addr [16]byte 61 ip := net.ParseIP("192.168.0.1") 62 ip.To16() 63 copy(addr[:], ip[:16]) 64 nodeAddr := comm.PeerAddr{ 65 Time: 12345678, 66 Services: 100, 67 IpAddr: addr, 68 Port: 8080, 69 ConsensusPort: 8081, 70 ID: 987654321, 71 } 72 msg.NodeAddrs = append(msg.NodeAddrs, nodeAddr) 73 74 MessageTest(t, &msg) 75 } 76 77 // ping 78 func TestPingSerializationDeserialization(t *testing.T) { 79 var msg Ping 80 msg.Height = 1 81 82 MessageTest(t, &msg) 83 } 84 85 //pong 86 func TestPongSerializationDeserialization(t *testing.T) { 87 var msg Pong 88 msg.Height = 1 89 90 MessageTest(t, &msg) 91 } 92 93 //getheaders 94 func TestBlkHdrReqSerializationDeserialization(t *testing.T) { 95 var msg HeadersReq 96 msg.Len = 1 97 98 hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 99 msg.HashStart, _ = common.Uint256FromHexString(hashstr) 100 101 MessageTest(t, &msg) 102 } 103 104 //headers 105 106 // inv 107 108 // getdata 109 func TestBlkReqSerializationDeserialization(t *testing.T) { 110 var msg BlocksReq 111 msg.HeaderHashCount = 1 112 113 hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 114 msg.HashStart, _ = common.Uint256FromHexString(hashstr) 115 116 MessageTest(t, &msg) 117 } 118 119 func TestDataReqSerializationDeserialization(t *testing.T) { 120 var msg DataReq 121 msg.DataType = 0x02 122 123 hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 124 bhash, _ := common.HexToBytes(hashstr) 125 copy(msg.Hash[:], bhash) 126 127 MessageTest(t, &msg) 128 } 129 130 // notfound 131 func Uint256ParseFromBytes(f []byte) common.Uint256 { 132 if len(f) != 32 { 133 return common.Uint256{} 134 } 135 136 var hash [32]uint8 137 for i := 0; i < 32; i++ { 138 hash[i] = f[i] 139 } 140 return common.Uint256(hash) 141 } 142 143 func TestNotFoundSerializationDeserialization(t *testing.T) { 144 var msg NotFound 145 str := "123456" 146 hash := []byte(str) 147 msg.Hash = Uint256ParseFromBytes(hash) 148 MessageTest(t, &msg) 149 }