2019-02-03 20:56:58 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-10-16 11:09:29 +02:00
|
|
|
peering "forge.cadoles.com/Cadoles/go-http-peering"
|
|
|
|
"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-03 20:56:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPing(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),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Advertise client with empty peer attributes
|
2019-02-03 20:56:58 +01:00
|
|
|
attrs := peering.PeerAttributes{}
|
2019-02-22 17:35:49 +01:00
|
|
|
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
|
|
|
// 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
|
|
|
// Store last contact after advertising
|
2019-02-03 20:56:58 +01:00
|
|
|
lastContact := peer.LastContact
|
|
|
|
|
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 ping authenticated handler
|
|
|
|
ping := server.Authenticate(store, &serverPK.PublicKey)(server.PingHandler(store))
|
|
|
|
|
|
|
|
// Create client
|
|
|
|
c = client.New(
|
|
|
|
client.WithHTTPClient(NewHTTPClientMock(ping)),
|
|
|
|
client.WithPrivateKey(clientPK),
|
|
|
|
client.WithServerToken(serverToken),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Do ping
|
|
|
|
if err := c.Ping(); err != nil {
|
2019-02-03 20:56:58 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:35:49 +01:00
|
|
|
// Retrieve peer
|
|
|
|
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 last contact has changed after ping
|
2019-02-03 20:56:58 +01:00
|
|
|
if peer.LastContact == lastContact {
|
|
|
|
t.Error("peer.LastContact should have been updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|