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.
112 lines
2.5 KiB
112 lines
2.5 KiB
6 years ago
|
package node
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"testing"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/dedis/kyber"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/crypto"
|
||
|
"github.com/harmony-one/harmony/crypto/pki"
|
||
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
)
|
||
|
|
||
|
var (
|
||
6 years ago
|
priKey1 = crypto.Ed25519Curve.Scalar().SetInt64(int64(333))
|
||
|
pubKey1 = pki.GetPublicKeyFromScalar(priKey1)
|
||
|
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/[90 217 28 68 64 211 160 232 61 244 159 244 160 36 61 161 237 242 236 45 147 118 237 88 234 122 198 188 157 116 90 228]"
|
||
|
e3 = "ping:Client/1=>127.0.0.1:9999:-1/[90 217 28 68 64 211 160 232 61 244 159 244 160 36 61 161 237 242 236 45 147 118 237 88 234 122 198 188 157 116 90 228]"
|
||
6 years ago
|
|
||
6 years ago
|
priKey2 = crypto.Ed25519Curve.Scalar().SetInt64(int64(999))
|
||
|
pubKey2 = pki.GetPublicKeyFromScalar(priKey2)
|
||
|
|
||
|
p2 = []p2p.Peer{
|
||
|
{
|
||
6 years ago
|
IP: "127.0.0.1",
|
||
6 years ago
|
Port: "8888",
|
||
|
PubKey: pubKey1,
|
||
|
Ready: true,
|
||
6 years ago
|
ValidatorID: -1,
|
||
6 years ago
|
},
|
||
|
{
|
||
6 years ago
|
IP: "127.0.0.1",
|
||
6 years ago
|
Port: "9999",
|
||
|
PubKey: pubKey2,
|
||
|
Ready: false,
|
||
6 years ago
|
ValidatorID: -2,
|
||
6 years ago
|
},
|
||
|
}
|
||
6 years ago
|
e2 = "pong:1=>length:2"
|
||
6 years ago
|
|
||
6 years ago
|
pubKeys = []kyber.Point{pubKey1, pubKey2}
|
||
|
|
||
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)
|
||
|
} else {
|
||
6 years ago
|
fmt.Println(r1)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
ping1.Node.Role = ClientRole
|
||
|
|
||
|
r3 := fmt.Sprintf("%v", *ping1)
|
||
|
if strings.Compare(r3, e3) != 0 {
|
||
|
test.Errorf("expect: %v, got: %v", e3, r3)
|
||
|
} else {
|
||
|
fmt.Println(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)
|
||
|
} else {
|
||
6 years ago
|
fmt.Println(r2)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func TestSerialize(test *testing.T) {
|
||
6 years ago
|
ping1 := NewPingMessage(p1)
|
||
|
buf1 = ping1.ConstructPingMessage()
|
||
|
fmt.Printf("buf ping: %v\n", buf1)
|
||
6 years ago
|
|
||
6 years ago
|
pong1 := NewPongMessage(p2, pubKeys)
|
||
6 years ago
|
buf2 = pong1.ConstructPongMessage()
|
||
|
fmt.Printf("buf pong: %v\n", buf2)
|
||
6 years ago
|
}
|
||
|
|
||
|
func TestDeserialize(test *testing.T) {
|
||
6 years ago
|
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
|
fmt.Println(ping)
|
||
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
|
fmt.Println(pong)
|
||
6 years ago
|
|
||
|
}
|