package test import ( "reflect" "testing" "time" "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" peering "forge.cadoles.com/Cadoles/go-http-peering" "forge.cadoles.com/Cadoles/go-http-peering/crypto" ) func TestAdvertise(t *testing.T) { if t.Skipped() { t.SkipNow() } 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), ) attrs := peering.PeerAttributes{} if err := client.Advertise(attrs); err != nil { t.Fatal(err) } peer, err := store.Get(peerID) if err != nil { t.Error(err) } if g, e := peer.ID, peerID; g != e { 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") } 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) } }