package test import ( "testing" 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" ) func TestPing(t *testing.T) { if t.Skipped() { t.SkipNow() } store := memory.NewStore() serverPK := mustGeneratePrivateKey() clientPK := mustGeneratePrivateKey() peerID := peering.NewPeerID() // 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 attrs := peering.PeerAttributes{} if err := c.Advertise(attrs); err != nil { t.Fatal(err) } // Retrieve peer from store peer, err := store.Get(peerID) if err != nil { t.Fatal(err) } // Store last contact after advertising lastContact := peer.LastContact // Accept peer if err := store.Accept(peerID); err != nil { t.Error(err) } // 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 { t.Fatal(err) } // Retrieve peer peer, err = store.Get(peerID) if err != nil { t.Fatal(err) } // Assert that last contact has changed after ping if peer.LastContact == lastContact { t.Error("peer.LastContact should have been updated") } }