From 54f647aa4f1bc3f9ed7e729edc84ff7ff2ac7d33 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 10 Nov 2021 21:34:39 -0400 Subject: [PATCH] test: public_key and resolver (#147) --- waku/v2/discovery/public_key_test.go | 49 ++++++++++++++++++++++++++++ waku/v2/discovery/resolver_test.go | 26 +++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 waku/v2/discovery/public_key_test.go create mode 100644 waku/v2/discovery/resolver_test.go diff --git a/waku/v2/discovery/public_key_test.go b/waku/v2/discovery/public_key_test.go new file mode 100644 index 00000000..6c77d823 --- /dev/null +++ b/waku/v2/discovery/public_key_test.go @@ -0,0 +1,49 @@ +package discovery + +import ( + "crypto/ecdsa" + "crypto/rand" + "testing" + + "github.com/libp2p/go-libp2p-core/crypto" + pb "github.com/libp2p/go-libp2p-core/crypto/pb" + "github.com/minio/sha256-simd" + "github.com/stretchr/testify/require" +) + +func TestBasicEquals(t *testing.T) { + _, pub1, err := crypto.GenerateECDSAKeyPair(rand.Reader) + require.NoError(t, err) + + _, pub2, err := crypto.GenerateECDSAKeyPair(rand.Reader) + require.NoError(t, err) + + require.False(t, basicEquals(pub1, pub2)) + require.True(t, basicEquals(pub1, pub1)) +} + +func TestSignAndVerify(t *testing.T) { + priv1, err := ecdsa.GenerateKey(crypto.ECDSACurve, rand.Reader) + require.NoError(t, err) + pub1 := ECDSAPublicKey{pub: &priv1.PublicKey} + + require.Equal(t, pb.KeyType_Secp256k1, pub1.Type()) + + msg := []byte("hello world") + + data := sha256.Sum256(msg) + sig, err := priv1.Sign(rand.Reader, data[:], nil) + if err != nil { + t.Fatal(err) + } + + ok, err := pub1.Verify(msg, sig) + require.NoError(t, err) + require.True(t, ok) + + // change data + data[0] = ^data[0] + ok, err = pub1.Verify(data[:], sig) + require.NoError(t, err) + require.False(t, ok) +} diff --git a/waku/v2/discovery/resolver_test.go b/waku/v2/discovery/resolver_test.go new file mode 100644 index 00000000..b16a44ba --- /dev/null +++ b/waku/v2/discovery/resolver_test.go @@ -0,0 +1,26 @@ +package discovery + +import ( + "context" + "net" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestResolver(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + resolver1 := GetResolver(ctx, "") + require.Equal(t, net.DefaultResolver, resolver1) + + resolver2 := GetResolver(ctx, "1.1.1.1") + require.NotEqual(t, net.DefaultResolver, resolver2) + require.True(t, resolver2.PreferGo) + + cname, err := resolver2.LookupCNAME(ctx, "status.im") + require.NoError(t, err) + require.Equal(t, "status.im.", cname) +}