Skip to content

Commit

Permalink
assert -> require
Browse files Browse the repository at this point in the history
  • Loading branch information
ameshkov committed Jul 13, 2021
1 parent 42711ad commit b5bcf75
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 105 deletions.
44 changes: 22 additions & 22 deletions cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCertSerialize(t *testing.T) {
cert, publicKey, _ := generateValidCert(t)

// not empty anymore
assert.False(t, bytes.Equal(cert.Signature[:], make([]byte, 64)))
require.False(t, bytes.Equal(cert.Signature[:], make([]byte, 64)))

// verify the signature
assert.True(t, cert.VerifySignature(publicKey))
require.True(t, cert.VerifySignature(publicKey))

// serialize
b, err := cert.Serialize()
assert.NoError(t, err)
assert.Equal(t, 124, len(b))
require.NoError(t, err)
require.Equal(t, 124, len(b))

// check that we can deserialize it
cert2 := Cert{}
err = cert2.Deserialize(b)
assert.NoError(t, err)
assert.Equal(t, cert.Serial, cert2.Serial)
assert.Equal(t, cert.NotBefore, cert2.NotBefore)
assert.Equal(t, cert.NotAfter, cert2.NotAfter)
assert.Equal(t, cert.EsVersion, cert2.EsVersion)
assert.True(t, bytes.Equal(cert.ClientMagic[:], cert2.ClientMagic[:]))
assert.True(t, bytes.Equal(cert.ResolverPk[:], cert2.ResolverPk[:]))
assert.True(t, bytes.Equal(cert.Signature[:], cert2.Signature[:]))
require.NoError(t, err)
require.Equal(t, cert.Serial, cert2.Serial)
require.Equal(t, cert.NotBefore, cert2.NotBefore)
require.Equal(t, cert.NotAfter, cert2.NotAfter)
require.Equal(t, cert.EsVersion, cert2.EsVersion)
require.True(t, bytes.Equal(cert.ClientMagic[:], cert2.ClientMagic[:]))
require.True(t, bytes.Equal(cert.ResolverPk[:], cert2.ResolverPk[:]))
require.True(t, bytes.Equal(cert.Signature[:], cert2.Signature[:]))
}

func TestCertDeserialize(t *testing.T) {
// dig -t txt 2.dnscrypt-cert.opendns.com. -p 443 @208.67.220.220
certBytes, err := ioutil.ReadFile("testdata/dnscrypt-cert.opendns.txt")
assert.NoError(t, err)
require.NoError(t, err)

b, err := unpackTxtString(string(certBytes))
assert.NoError(t, err)
require.NoError(t, err)

cert := &Cert{}
err = cert.Deserialize(b)
assert.NoError(t, err)
assert.Equal(t, uint32(1574811744), cert.Serial)
assert.Equal(t, XSalsa20Poly1305, cert.EsVersion)
assert.Equal(t, uint32(1574811744), cert.NotBefore)
assert.Equal(t, uint32(1606347744), cert.NotAfter)
require.NoError(t, err)
require.Equal(t, uint32(1574811744), cert.Serial)
require.Equal(t, XSalsa20Poly1305, cert.EsVersion)
require.Equal(t, uint32(1574811744), cert.NotBefore)
require.Equal(t, uint32(1606347744), cert.NotAfter)
}

func generateValidCert(t *testing.T) (*Cert, ed25519.PublicKey, ed25519.PrivateKey) {
Expand All @@ -69,11 +69,11 @@ func generateValidCert(t *testing.T) (*Cert, ed25519.PublicKey, ed25519.PrivateK
copy(cert.ResolverSk[:], resolverSk[:])

// empty at first
assert.True(t, bytes.Equal(cert.Signature[:], make([]byte, 64)))
require.True(t, bytes.Equal(cert.Signature[:], make([]byte, 64)))

// generate private key
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
assert.NoError(t, err)
require.NoError(t, err)

// sign the data
cert.Sign(privateKey)
Expand Down
60 changes: 30 additions & 30 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/ameshkov/dnsstamps"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseStamp(t *testing.T) {
Expand All @@ -20,10 +20,10 @@ func TestParseStamp(t *testing.T) {
t.Fatalf("Could not parse stamp %s: %s", stampStr, err)
}

assert.Equal(t, stampStr, stamp.String())
assert.Equal(t, dnsstamps.StampProtoTypeDoH, stamp.Proto)
assert.Equal(t, "dns.google.com", stamp.ProviderName)
assert.Equal(t, "/experimental", stamp.Path)
require.Equal(t, stampStr, stamp.String())
require.Equal(t, dnsstamps.StampProtoTypeDoH, stamp.Proto)
require.Equal(t, "dns.google.com", stamp.ProviderName)
require.Equal(t, "/experimental", stamp.Path)

// AdGuard DNSCrypt
stampStr = "sdns://AQIAAAAAAAAAFDE3Ni4xMDMuMTMwLjEzMDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"
Expand All @@ -33,18 +33,18 @@ func TestParseStamp(t *testing.T) {
t.Fatalf("Could not parse stamp %s: %s", stampStr, err)
}

assert.Equal(t, stampStr, stamp.String())
assert.Equal(t, dnsstamps.StampProtoTypeDNSCrypt, stamp.Proto)
assert.Equal(t, "2.dnscrypt.default.ns1.adguard.com", stamp.ProviderName)
assert.Equal(t, "", stamp.Path)
assert.Equal(t, "176.103.130.130:5443", stamp.ServerAddrStr)
assert.Equal(t, keySize, len(stamp.ServerPk))
require.Equal(t, stampStr, stamp.String())
require.Equal(t, dnsstamps.StampProtoTypeDNSCrypt, stamp.Proto)
require.Equal(t, "2.dnscrypt.default.ns1.adguard.com", stamp.ProviderName)
require.Equal(t, "", stamp.Path)
require.Equal(t, "176.103.130.130:5443", stamp.ServerAddrStr)
require.Equal(t, keySize, len(stamp.ServerPk))
}

func TestInvalidStamp(t *testing.T) {
client := Client{}
_, err := client.Dial("sdns://AQIAAAAAAAAAFDE")
assert.NotNil(t, err)
require.NotNil(t, err)
}

func TestTimeoutOnDialError(t *testing.T) {
Expand All @@ -53,8 +53,8 @@ func TestTimeoutOnDialError(t *testing.T) {
client := Client{Timeout: 300 * time.Millisecond}

_, err := client.Dial(stampStr)
assert.NotNil(t, err)
assert.True(t, os.IsTimeout(err))
require.NotNil(t, err)
require.True(t, os.IsTimeout(err))
}

func TestTimeoutOnDialExchange(t *testing.T) {
Expand All @@ -63,7 +63,7 @@ func TestTimeoutOnDialExchange(t *testing.T) {
client := Client{Timeout: 300 * time.Millisecond}

serverInfo, err := client.Dial(stampStr)
assert.NoError(t, err)
require.NoError(t, err)

// Point it to an IP where there's no DNSCrypt server
serverInfo.ServerAddress = "8.8.8.8:5443"
Expand All @@ -73,8 +73,8 @@ func TestTimeoutOnDialExchange(t *testing.T) {
_, err = client.Exchange(req, serverInfo)

// Check error
assert.NotNil(t, err)
assert.True(t, os.IsTimeout(err))
require.NotNil(t, err)
require.True(t, os.IsTimeout(err))
}

func TestFetchCertPublicResolvers(t *testing.T) {
Expand Down Expand Up @@ -105,18 +105,18 @@ func TestFetchCertPublicResolvers(t *testing.T) {

for _, test := range stamps {
stamp, err := dnsstamps.NewServerStampFromString(test.stampStr)
assert.NoError(t, err)
require.NoError(t, err)

t.Run(stamp.ProviderName, func(t *testing.T) {
c := &Client{
Net: "udp",
Timeout: time.Second * 5,
}
resolverInfo, err := c.DialStamp(stamp)
assert.NoError(t, err)
assert.NotNil(t, resolverInfo)
assert.True(t, resolverInfo.ResolverCert.VerifyDate())
assert.True(t, resolverInfo.ResolverCert.VerifySignature(stamp.ServerPk))
require.NoError(t, err)
require.NotNil(t, resolverInfo)
require.True(t, resolverInfo.ResolverCert.VerifyDate())
require.True(t, resolverInfo.ResolverCert.VerifySignature(stamp.ServerPk))
})
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestExchangePublicResolvers(t *testing.T) {

for _, test := range stamps {
stamp, err := dnsstamps.NewServerStampFromString(test.stampStr)
assert.NoError(t, err)
require.NoError(t, err)

t.Run(stamp.ProviderName, func(t *testing.T) {
checkDNSCryptServer(t, test.stampStr, "udp")
Expand All @@ -161,12 +161,12 @@ func TestExchangePublicResolvers(t *testing.T) {
func checkDNSCryptServer(t *testing.T, stampStr string, network string) {
client := Client{Net: network, Timeout: 10 * time.Second}
resolverInfo, err := client.Dial(stampStr)
assert.NoError(t, err)
require.NoError(t, err)

req := createTestMessage()

reply, err := client.Exchange(req, resolverInfo)
assert.NoError(t, err)
require.NoError(t, err)
assertTestMessageResponse(t, reply)
}

Expand All @@ -180,10 +180,10 @@ func createTestMessage() *dns.Msg {
return &req
}

func assertTestMessageResponse(t assert.TestingT, reply *dns.Msg) {
assert.NotNil(t, reply)
assert.Equal(t, 1, len(reply.Answer))
func assertTestMessageResponse(t require.TestingT, reply *dns.Msg) {
require.NotNil(t, reply)
require.Equal(t, 1, len(reply.Answer))
a, ok := reply.Answer[0].(*dns.A)
assert.True(t, ok)
assert.Equal(t, net.IPv4(8, 8, 8, 8).To4(), a.A.To4())
require.True(t, ok)
require.Equal(t, net.IPv4(8, 8, 8, 8).To4(), a.A.To4())
}
10 changes: 5 additions & 5 deletions encrypted_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDNSCryptQueryEncryptDecryptXSalsa20Poly1305(t *testing.T) {
Expand All @@ -23,7 +23,7 @@ func testDNSCryptQueryEncryptDecrypt(t *testing.T, esVersion CryptoConstruction)

// Generate client shared key
clientSharedKey, err := computeSharedKey(esVersion, &clientSecretKey, &serverPublicKey)
assert.NoError(t, err)
require.NoError(t, err)

clientMagic := [clientMagicSize]byte{}
_, _ = rand.Read(clientMagic[:])
Expand All @@ -40,7 +40,7 @@ func testDNSCryptQueryEncryptDecrypt(t *testing.T, esVersion CryptoConstruction)

// Encrypt it
encrypted, err := q1.Encrypt(packet, clientSharedKey)
assert.NoError(t, err)
require.NoError(t, err)

// Now let's try decrypting it
q2 := EncryptedQuery{
Expand All @@ -50,8 +50,8 @@ func testDNSCryptQueryEncryptDecrypt(t *testing.T, esVersion CryptoConstruction)

// Decrypt it
decrypted, err := q2.Decrypt(encrypted, serverSecretKey)
assert.NoError(t, err)
require.NoError(t, err)

// Check that packet is the same
assert.True(t, bytes.Equal(packet, decrypted))
require.True(t, bytes.Equal(packet, decrypted))
}
18 changes: 9 additions & 9 deletions encrypted_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/ameshkov/dnscrypt/v2/xsecretbox"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDNSCryptResponseEncryptDecryptXSalsa20Poly1305(t *testing.T) {
Expand All @@ -24,11 +24,11 @@ func testDNSCryptResponseEncryptDecrypt(t *testing.T, esVersion CryptoConstructi

// Generate client shared key
clientSharedKey, err := computeSharedKey(esVersion, &clientSecretKey, &serverPublicKey)
assert.NoError(t, err)
require.NoError(t, err)

// Generate server shared key
serverSharedKey, err := computeSharedKey(esVersion, &serverSecretKey, &clientPublicKey)
assert.NoError(t, err)
require.NoError(t, err)

r1 := &EncryptedResponse{
EsVersion: esVersion,
Expand All @@ -42,7 +42,7 @@ func testDNSCryptResponseEncryptDecrypt(t *testing.T, esVersion CryptoConstructi

// Encrypt it
encrypted, err := r1.Encrypt(packet, serverSharedKey)
assert.NoError(t, err)
require.NoError(t, err)

// Now let's try decrypting it
r2 := &EncryptedResponse{
Expand All @@ -51,22 +51,22 @@ func testDNSCryptResponseEncryptDecrypt(t *testing.T, esVersion CryptoConstructi

// Decrypt it
decrypted, err := r2.Decrypt(encrypted, clientSharedKey)
assert.NoError(t, err)
require.NoError(t, err)

// Check that packet is the same
assert.True(t, bytes.Equal(packet, decrypted))
require.True(t, bytes.Equal(packet, decrypted))

// Now check invalid data (some random stuff)
_, err = r2.Decrypt(packet, clientSharedKey)
assert.NotNil(t, err)
require.NotNil(t, err)

// Empty array
_, err = r2.Decrypt([]byte{}, clientSharedKey)
assert.NotNil(t, err)
require.NotNil(t, err)

// Minimum valid size
b := make([]byte, len(resolverMagic)+nonceSize+xsecretbox.TagSize+minDNSPacketSize)
_, _ = rand.Read(b)
_, err = r2.Decrypt(b, clientSharedKey)
assert.NotNil(t, err)
require.NotNil(t, err)
}
26 changes: 13 additions & 13 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
"crypto/ed25519"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHexEncodeKey(t *testing.T) {
str := HexEncodeKey([]byte{1, 2, 3, 4})
assert.Equal(t, "01020304", str)
require.Equal(t, "01020304", str)
}

func TestHexDecodeKey(t *testing.T) {
b, err := HexDecodeKey("01:02:03:04")
assert.NoError(t, err)
assert.True(t, bytes.Equal(b, []byte{1, 2, 3, 4}))
require.NoError(t, err)
require.True(t, bytes.Equal(b, []byte{1, 2, 3, 4}))
}

func TestGenerateResolverConfig(t *testing.T) {
rc, err := GenerateResolverConfig("example.org", nil)
assert.NoError(t, err)
assert.Equal(t, "2.dnscrypt-cert.example.org", rc.ProviderName)
assert.Equal(t, ed25519.PrivateKeySize*2, len(rc.PrivateKey))
assert.Equal(t, keySize*2, len(rc.ResolverSk))
assert.Equal(t, keySize*2, len(rc.ResolverPk))
require.NoError(t, err)
require.Equal(t, "2.dnscrypt-cert.example.org", rc.ProviderName)
require.Equal(t, ed25519.PrivateKeySize*2, len(rc.PrivateKey))
require.Equal(t, keySize*2, len(rc.ResolverSk))
require.Equal(t, keySize*2, len(rc.ResolverPk))

cert, err := rc.CreateCert()
assert.NoError(t, err)
require.NoError(t, err)

assert.True(t, cert.VerifyDate())
require.True(t, cert.VerifyDate())

publicKey, err := HexDecodeKey(rc.PublicKey)
assert.NoError(t, err)
assert.True(t, cert.VerifySignature(publicKey))
require.NoError(t, err)
require.True(t, cert.VerifySignature(publicKey))
}
Loading

0 comments on commit b5bcf75

Please sign in to comment.