package test import ( "reflect" "testing" "time" peering "forge.cadoles.com/Cadoles/go-http-peering" "forge.cadoles.com/Cadoles/go-http-peering/client" "forge.cadoles.com/Cadoles/go-http-peering/crypto" 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 TestUpdate(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) } // Accept peer if err := store.Accept(peerID); err != nil { t.Error(err) } // 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 attrs["Label"] = "Foo Bar" // Update attributes if err := c.UpdateAttributes(attrs); err != nil { t.Fatal(err) } // Retrieve peer from store peer, err := store.Get(peerID) if err != nil { t.Fatal(err) } // Assert that peer's ID did not change if g, e := peer.ID, peerID; g != e { t.Errorf("peer.ID: got '%v', expected '%v'", g, e) } // Assert that stored attributes are the same as the local ones if g, e := peer.Attributes, attrs; !reflect.DeepEqual(g, e) { t.Errorf("peer.Attributes: got '%v', expected '%v'", g, e) } // Assert that lastContact has changed var defaultTime time.Time if peer.LastContact == defaultTime { t.Error("peer.LastContact should not be time.Time zero value") } pem, err := crypto.EncodePublicKeyToPEM(clientPK.Public()) 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) } }