From b5bcf754caee5106c26dca48b282214da54a24a5 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Tue, 13 Jul 2021 13:13:59 +0300 Subject: [PATCH] assert -> require --- cert_test.go | 44 ++++++++++++++-------------- client_test.go | 60 +++++++++++++++++++------------------- encrypted_query_test.go | 10 +++---- encrypted_response_test.go | 18 ++++++------ generate_test.go | 26 ++++++++--------- server_test.go | 50 +++++++++++++++---------------- 6 files changed, 104 insertions(+), 104 deletions(-) diff --git a/cert_test.go b/cert_test.go index 4b1d5e1..f9242f3 100644 --- a/cert_test.go +++ b/cert_test.go @@ -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) { @@ -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) diff --git a/client_test.go b/client_test.go index 6bbd4bd..8f75281 100644 --- a/client_test.go +++ b/client_test.go @@ -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) { @@ -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" @@ -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) { @@ -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) { @@ -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" @@ -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) { @@ -105,7 +105,7 @@ 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{ @@ -113,10 +113,10 @@ func TestFetchCertPublicResolvers(t *testing.T) { 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)) }) } } @@ -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") @@ -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) } @@ -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()) } diff --git a/encrypted_query_test.go b/encrypted_query_test.go index cffb75c..d07a668 100644 --- a/encrypted_query_test.go +++ b/encrypted_query_test.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDNSCryptQueryEncryptDecryptXSalsa20Poly1305(t *testing.T) { @@ -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[:]) @@ -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{ @@ -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)) } diff --git a/encrypted_response_test.go b/encrypted_response_test.go index 6c755cc..e09ad4c 100644 --- a/encrypted_response_test.go +++ b/encrypted_response_test.go @@ -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) { @@ -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, @@ -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{ @@ -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) } diff --git a/generate_test.go b/generate_test.go index a6099d2..5db0f23 100644 --- a/generate_test.go +++ b/generate_test.go @@ -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)) } diff --git a/server_test.go b/server_test.go index d1063e0..de9cf90 100644 --- a/server_test.go +++ b/server_test.go @@ -12,7 +12,7 @@ import ( "github.com/ameshkov/dnsstamps" "github.com/miekg/dns" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestServer_Shutdown(t *testing.T) { @@ -24,7 +24,7 @@ func TestServer_Shutdown(t *testing.T) { // Serve* methods are called in different goroutines // give them at least a moment to actually start the server time.Sleep(10 * time.Millisecond) - assert.NoError(t, srv.Close()) + require.NoError(t, srv.Close()) } func TestServer_UDPServeCert(t *testing.T) { @@ -46,7 +46,7 @@ func TestServer_TCPRespondMessages(t *testing.T) { func TestServer_ReadTimeout(t *testing.T) { srv := newTestServer(t, &testHandler{}) t.Cleanup(func() { - assert.NoError(t, srv.Close()) + require.NoError(t, srv.Close()) }) // Sleep for "defaultReadTimeout" before trying to shutdown the server // The point is to make sure readTimeout is properly handled by @@ -59,7 +59,7 @@ func TestServer_ReadTimeout(t *testing.T) { func testServerServeCert(t *testing.T, network string) { srv := newTestServer(t, &testHandler{}) t.Cleanup(func() { - assert.NoError(t, srv.Close()) + require.NoError(t, srv.Close()) }) client := &Client{ @@ -79,23 +79,23 @@ func testServerServeCert(t *testing.T, network string) { Proto: dnsstamps.StampProtoTypeDNSCrypt, } ri, err := client.DialStamp(stamp) - assert.NoError(t, err) - assert.NotNil(t, ri) + require.NoError(t, err) + require.NotNil(t, ri) - assert.Equal(t, ri.ProviderName, srv.server.ProviderName) - assert.True(t, bytes.Equal(srv.server.ResolverCert.ClientMagic[:], ri.ResolverCert.ClientMagic[:])) - assert.Equal(t, srv.server.ResolverCert.EsVersion, ri.ResolverCert.EsVersion) - assert.Equal(t, srv.server.ResolverCert.Signature, ri.ResolverCert.Signature) - assert.Equal(t, srv.server.ResolverCert.NotBefore, ri.ResolverCert.NotBefore) - assert.Equal(t, srv.server.ResolverCert.NotAfter, ri.ResolverCert.NotAfter) - assert.True(t, bytes.Equal(srv.server.ResolverCert.ResolverPk[:], ri.ResolverCert.ResolverPk[:])) - assert.True(t, bytes.Equal(srv.server.ResolverCert.ResolverPk[:], ri.ResolverCert.ResolverPk[:])) + require.Equal(t, ri.ProviderName, srv.server.ProviderName) + require.True(t, bytes.Equal(srv.server.ResolverCert.ClientMagic[:], ri.ResolverCert.ClientMagic[:])) + require.Equal(t, srv.server.ResolverCert.EsVersion, ri.ResolverCert.EsVersion) + require.Equal(t, srv.server.ResolverCert.Signature, ri.ResolverCert.Signature) + require.Equal(t, srv.server.ResolverCert.NotBefore, ri.ResolverCert.NotBefore) + require.Equal(t, srv.server.ResolverCert.NotAfter, ri.ResolverCert.NotAfter) + require.True(t, bytes.Equal(srv.server.ResolverCert.ResolverPk[:], ri.ResolverCert.ResolverPk[:])) + require.True(t, bytes.Equal(srv.server.ResolverCert.ResolverPk[:], ri.ResolverCert.ResolverPk[:])) } func testServerRespondMessages(t *testing.T, network string) { srv := newTestServer(t, &testHandler{}) t.Cleanup(func() { - assert.NoError(t, srv.Close()) + require.NoError(t, srv.Close()) }) testThisServerRespondMessages(t, network, srv) } @@ -118,16 +118,16 @@ func testThisServerRespondMessages(t *testing.T, network string, srv *testServer Proto: dnsstamps.StampProtoTypeDNSCrypt, } ri, err := client.DialStamp(stamp) - assert.NoError(t, err) - assert.NotNil(t, ri) + require.NoError(t, err) + require.NotNil(t, ri) conn, err := net.Dial(network, stamp.ServerAddrStr) - assert.NoError(t, err) + require.NoError(t, err) for i := 0; i < 10; i++ { m := createTestMessage() res, err := client.ExchangeConn(conn, m, ri) - assert.NoError(t, err) + require.NoError(t, err) assertTestMessageResponse(t, res) } } @@ -159,11 +159,11 @@ func (s *testServer) Close() error { return err } -func newTestServer(t assert.TestingT, handler Handler) *testServer { +func newTestServer(t require.TestingT, handler Handler) *testServer { rc, err := GenerateResolverConfig("example.org", nil) - assert.NoError(t, err) + require.NoError(t, err) cert, err := rc.CreateCert() - assert.NoError(t, err) + require.NoError(t, err) s := &Server{ ProviderName: rc.ProviderName, @@ -172,7 +172,7 @@ func newTestServer(t assert.TestingT, handler Handler) *testServer { } privateKey, err := HexDecodeKey(rc.PrivateKey) - assert.NoError(t, err) + require.NoError(t, err) publicKey := ed25519.PrivateKey(privateKey).Public().(ed25519.PublicKey) srv := &testServer{ server: s, @@ -180,9 +180,9 @@ func newTestServer(t assert.TestingT, handler Handler) *testServer { } srv.tcpListen, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4zero, Port: 0}) - assert.NoError(t, err) + require.NoError(t, err) srv.udpConn, err = net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0}) - assert.NoError(t, err) + require.NoError(t, err) go s.ServeUDP(srv.udpConn) go s.ServeTCP(srv.tcpListen)