You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.8 KiB
102 lines
2.8 KiB
6 years ago
|
package discovery
|
||
6 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
6 years ago
|
"reflect"
|
||
6 years ago
|
"strings"
|
||
|
"testing"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/harmony-one/bls/ffi/go/bls"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto/node"
|
||
6 years ago
|
"github.com/harmony-one/harmony/crypto/pki"
|
||
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
)
|
||
|
|
||
|
var (
|
||
6 years ago
|
pubKey1 = pki.GetBLSPrivateKeyFromInt(333).GetPublicKey()
|
||
6 years ago
|
p1 = p2p.Peer{
|
||
6 years ago
|
IP: "127.0.0.1",
|
||
6 years ago
|
Port: "9999",
|
||
6 years ago
|
ValidatorID: -1,
|
||
6 years ago
|
PubKey: pubKey1,
|
||
6 years ago
|
}
|
||
6 years ago
|
e1 = "ping:Validator/1=>127.0.0.1:9999:-1/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
|
||
|
e3 = "ping:Client/1=>127.0.0.1:9999:-1/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
|
||
6 years ago
|
|
||
6 years ago
|
pubKey2 = pki.GetBLSPrivateKeyFromInt(999).GetPublicKey()
|
||
6 years ago
|
|
||
|
p2 = []p2p.Peer{
|
||
|
{
|
||
6 years ago
|
IP: "127.0.0.1",
|
||
6 years ago
|
Port: "8888",
|
||
|
PubKey: pubKey1,
|
||
6 years ago
|
ValidatorID: -1,
|
||
6 years ago
|
},
|
||
|
{
|
||
6 years ago
|
IP: "127.0.0.1",
|
||
6 years ago
|
Port: "9999",
|
||
|
PubKey: pubKey2,
|
||
6 years ago
|
ValidatorID: -2,
|
||
6 years ago
|
},
|
||
|
}
|
||
6 years ago
|
e2 = "pong:1=>length:2"
|
||
6 years ago
|
|
||
6 years ago
|
pubKeys = []*bls.PublicKey{pubKey1, pubKey2}
|
||
6 years ago
|
|
||
6 years ago
|
buf1 []byte
|
||
|
buf2 []byte
|
||
|
)
|
||
|
|
||
|
func TestString(test *testing.T) {
|
||
6 years ago
|
ping1 := NewPingMessage(p1)
|
||
|
|
||
|
r1 := fmt.Sprintf("%v", *ping1)
|
||
6 years ago
|
if strings.Compare(r1, e1) != 0 {
|
||
|
test.Errorf("expect: %v, got: %v", e1, r1)
|
||
|
}
|
||
|
|
||
6 years ago
|
ping1.Node.Role = node.ClientRole
|
||
6 years ago
|
|
||
|
r3 := fmt.Sprintf("%v", *ping1)
|
||
|
if strings.Compare(r3, e3) != 0 {
|
||
|
test.Errorf("expect: %v, got: %v", e3, r3)
|
||
|
}
|
||
|
|
||
6 years ago
|
pong1 := NewPongMessage(p2, pubKeys)
|
||
6 years ago
|
r2 := fmt.Sprintf("%v", *pong1)
|
||
6 years ago
|
|
||
6 years ago
|
if !strings.HasPrefix(r2, e2) {
|
||
6 years ago
|
test.Errorf("expect: %v, got: %v", e2, r2)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestSerialize(test *testing.T) {
|
||
6 years ago
|
ping1 := NewPingMessage(p1)
|
||
|
buf1 = ping1.ConstructPingMessage()
|
||
|
msg1, err := proto.GetMessagePayload(buf1)
|
||
|
if err != nil {
|
||
|
test.Error("GetMessagePayload Failed!")
|
||
|
}
|
||
|
ping, err := GetPingMessage(msg1)
|
||
6 years ago
|
if err != nil {
|
||
|
test.Error("Ping failed!")
|
||
|
}
|
||
6 years ago
|
if !reflect.DeepEqual(ping, ping1) {
|
||
|
test.Error("Serialize/Deserialze Ping Message Failed")
|
||
|
}
|
||
|
|
||
|
pong1 := NewPongMessage(p2, pubKeys)
|
||
|
buf2 = pong1.ConstructPongMessage()
|
||
6 years ago
|
|
||
6 years ago
|
msg2, err := proto.GetMessagePayload(buf2)
|
||
|
pong, err := GetPongMessage(msg2)
|
||
6 years ago
|
if err != nil {
|
||
|
test.Error("Pong failed!")
|
||
|
}
|
||
|
|
||
6 years ago
|
if !reflect.DeepEqual(pong, pong1) {
|
||
|
test.Error("Serialize/Deserialze Pong Message Failed")
|
||
|
}
|
||
6 years ago
|
}
|