go-http-peering/test/update_test.go

102 lines
2.4 KiB
Go
Raw Normal View History

2019-02-03 20:56:58 +01:00
package test
import (
"reflect"
"testing"
"time"
peering "forge.cadoles.com/wpetit/go-http-peering"
2019-02-22 17:35:49 +01:00
"forge.cadoles.com/wpetit/go-http-peering/client"
2019-02-03 20:56:58 +01:00
"forge.cadoles.com/wpetit/go-http-peering/crypto"
2019-02-22 17:35:49 +01:00
peeringCrypto "forge.cadoles.com/wpetit/go-http-peering/crypto"
"forge.cadoles.com/wpetit/go-http-peering/memory"
"forge.cadoles.com/wpetit/go-http-peering/server"
2019-02-03 20:56:58 +01:00
)
func TestUpdate(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()
2019-02-03 20:56:58 +01:00
2019-02-22 17:35:49 +01:00
// Generate a server token for the peer client
serverToken, err := peeringCrypto.CreateServerToken(serverPK, "test", peerID)
if err != nil {
t.Fatal(err)
}
advertise := server.AdvertiseHandler(store, &serverPK.PublicKey)
// Create advertise client
c := client.New(
client.WithHTTPClient(NewHTTPClientMock(advertise)),
client.WithPrivateKey(clientPK),
client.WithServerToken(serverToken),
)
2019-02-03 20:56:58 +01:00
2019-02-22 17:35:49 +01:00
// Advertise client with empty peer attributes
attrs := peering.PeerAttributes{}
if err := c.Advertise(attrs); err != nil {
2019-02-03 20:56:58 +01:00
t.Fatal(err)
}
2019-02-22 17:35:49 +01:00
// Accept peer
if err := store.Accept(peerID); err != nil {
2019-02-03 20:56:58 +01:00
t.Error(err)
}
2019-02-22 17:35:49 +01:00
// Create authenticated update handler
update := server.Authenticate(store, &serverPK.PublicKey)(server.UpdateHandler(store))
// Create update client
c = client.New(
client.WithHTTPClient(NewHTTPClientMock(update)),
client.WithPrivateKey(clientPK),
client.WithServerToken(serverToken),
)
// Update local attributes
2019-02-03 20:56:58 +01:00
attrs["Label"] = "Foo Bar"
2019-02-22 17:35:49 +01:00
// Update attributes
if err := c.UpdateAttributes(attrs); err != nil {
2019-02-03 20:56:58 +01:00
t.Fatal(err)
}
2019-02-22 17:35:49 +01:00
// Retrieve peer from store
peer, err := store.Get(peerID)
2019-02-03 20:56:58 +01:00
if err != nil {
t.Fatal(err)
}
2019-02-22 17:35:49 +01:00
// Assert that peer's ID did not change
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)
}
2019-02-22 17:35:49 +01:00
// Assert that stored attributes are the same as the local ones
2019-02-03 20:56:58 +01:00
if g, e := peer.Attributes, attrs; !reflect.DeepEqual(g, e) {
t.Errorf("peer.Attributes: got '%v', expected '%v'", g, e)
}
2019-02-22 17:35:49 +01:00
// Assert that lastContact has changed
2019-02-03 20:56:58 +01:00
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)
}
}