2019-02-03 20:56:58 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-16 11:09:29 +02:00
|
|
|
"forge.cadoles.com/Cadoles/go-http-peering/client"
|
|
|
|
peeringCrypto "forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
|
|
|
"forge.cadoles.com/Cadoles/go-http-peering/memory"
|
|
|
|
"forge.cadoles.com/Cadoles/go-http-peering/server"
|
2019-02-22 17:35:49 +01:00
|
|
|
|
2019-10-16 11:09:29 +02:00
|
|
|
peering "forge.cadoles.com/Cadoles/go-http-peering"
|
|
|
|
"forge.cadoles.com/Cadoles/go-http-peering/crypto"
|
2019-02-03 20:56:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdvertise(t *testing.T) {
|
|
|
|
|
|
|
|
if t.Skipped() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
store := memory.NewStore()
|
|
|
|
serverPK := mustGeneratePrivateKey()
|
|
|
|
clientPK := mustGeneratePrivateKey()
|
|
|
|
peerID := peering.NewPeerID()
|
|
|
|
|
|
|
|
serverToken, err := peeringCrypto.CreateServerToken(serverPK, "test", peerID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
advertise := server.AdvertiseHandler(store, &serverPK.PublicKey)
|
|
|
|
|
|
|
|
client := client.New(
|
|
|
|
client.WithHTTPClient(NewHTTPClientMock(advertise)),
|
|
|
|
client.WithPrivateKey(clientPK),
|
|
|
|
client.WithServerToken(serverToken),
|
|
|
|
)
|
2019-02-03 20:56:58 +01:00
|
|
|
|
|
|
|
attrs := peering.PeerAttributes{}
|
|
|
|
if err := client.Advertise(attrs); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
peer, err := store.Get(peerID)
|
2019-02-03 20:56:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
if g, e := peer.ID, peerID; g != e {
|
2019-02-03 20:56:58 +01:00
|
|
|
t.Errorf("peer.ID: got '%v', expected '%v'", g, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g, e := peer.Attributes, attrs; !reflect.DeepEqual(g, e) {
|
|
|
|
t.Errorf("peer.Attributes: got '%v', expected '%v'", g, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultTime time.Time
|
|
|
|
if peer.LastContact == defaultTime {
|
|
|
|
t.Error("peer.LastContact should not be time.Time zero value")
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
pem, err := crypto.EncodePublicKeyToPEM(clientPK.Public())
|
2019-02-03 20:56:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g, e := peer.PublicKey, pem; !reflect.DeepEqual(g, e) {
|
|
|
|
t.Errorf("peer.PublicKey: got '%v', expected '%v'", g, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|