test: public_key and resolver (#147)

This commit is contained in:
Richard Ramos 2021-11-10 21:34:39 -04:00 committed by GitHub
parent 6d04308716
commit 54f647aa4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}