From 483b875f4a49d14477d7179f2ab24fbbff336181 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 27 Aug 2019 17:06:32 +0300 Subject: [PATCH] keys: simplify tests with assert package --- pkg/crypto/keys/nep2_test.go | 42 ++++++++--------------------- pkg/crypto/keys/private_key_test.go | 33 +++++++---------------- pkg/crypto/keys/publickey_test.go | 9 ++----- pkg/crypto/keys/sign_verify_test.go | 31 +++++---------------- pkg/crypto/keys/wif_test.go | 30 +++++++-------------- 5 files changed, 37 insertions(+), 108 deletions(-) diff --git a/pkg/crypto/keys/nep2_test.go b/pkg/crypto/keys/nep2_test.go index e5162e1d4..e3a7a1ebc 100644 --- a/pkg/crypto/keys/nep2_test.go +++ b/pkg/crypto/keys/nep2_test.go @@ -4,24 +4,19 @@ import ( "testing" "github.com/CityOfZion/neo-go/pkg/internal/keytestcases" + "github.com/stretchr/testify/assert" ) func TestNEP2Encrypt(t *testing.T) { for _, testCase := range keytestcases.Arr { privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) encryptedWif, err := NEP2Encrypt(privKey, testCase.Passphrase) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) - if want, have := testCase.EncryptedWif, encryptedWif; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Equal(t, testCase.EncryptedWif, encryptedWif) } } @@ -29,34 +24,19 @@ func TestNEP2Decrypt(t *testing.T) { for _, testCase := range keytestcases.Arr { privKeyString, err := NEP2Decrypt(testCase.EncryptedWif, testCase.Passphrase) - - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) privKey, err := NewPrivateKeyFromWIF(privKeyString) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) - if want, have := testCase.PrivateKey, privKey.String(); want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Equal(t, testCase.PrivateKey, privKey.String()) wif, err := privKey.WIF() - if err != nil { - t.Fatal(err) - } - if want, have := testCase.Wif, wif; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.Wif, wif) address, err := privKey.Address() - if err != nil { - t.Fatal(err) - } - if want, have := testCase.Address, address; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.Address, address) } } diff --git a/pkg/crypto/keys/private_key_test.go b/pkg/crypto/keys/private_key_test.go index f5289acbc..dc45ef547 100644 --- a/pkg/crypto/keys/private_key_test.go +++ b/pkg/crypto/keys/private_key_test.go @@ -13,23 +13,14 @@ import ( func TestPrivateKey(t *testing.T) { for _, testCase := range keytestcases.Arr { privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) address, err := privKey.Address() - if err != nil { - t.Fatal(err) - } - if want, have := testCase.Address, address; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.Address, address) + wif, err := privKey.WIF() - if err != nil { - t.Fatal(err) - } - if want, have := testCase.Wif, wif; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.Wif, wif) pubKey, _ := privKey.PublicKey() assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.PublicKey) } @@ -38,12 +29,8 @@ func TestPrivateKey(t *testing.T) { func TestPrivateKeyFromWIF(t *testing.T) { for _, testCase := range keytestcases.Arr { key, err := NewPrivateKeyFromWIF(testCase.Wif) - if err != nil { - t.Fatal(err) - } - if want, have := testCase.PrivateKey, key.String(); want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.PrivateKey, key.String()) } } @@ -55,9 +42,7 @@ func TestSigning(t *testing.T) { PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721") data, err := PrivateKey.Sign([]byte("sample")) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index da2e5c5cd..5ff9e5597 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -35,18 +35,13 @@ func TestEncodeDecodePublicKey(t *testing.T) { func TestDecodeFromString(t *testing.T) { str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c" pubKey, err := NewPublicKeyFromString(str) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) assert.Equal(t, str, hex.EncodeToString(pubKey.Bytes())) } func TestPubkeyToAddress(t *testing.T) { pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4") - if err != nil { - t.Fatal(err) - } - + assert.Nil(t, err) actual, _ := pubKey.Address() expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx" assert.Equal(t, expected, actual) diff --git a/pkg/crypto/keys/sign_verify_test.go b/pkg/crypto/keys/sign_verify_test.go index 5a8e11c64..3eb5c1b8e 100755 --- a/pkg/crypto/keys/sign_verify_test.go +++ b/pkg/crypto/keys/sign_verify_test.go @@ -7,38 +7,19 @@ import ( "github.com/stretchr/testify/assert" ) -// SignDataWithRandomPrivateKey will sign data with -// a random private key, then verify said data -// returning true if Verify returns true -func SignDataWithRandomPrivateKey(data []byte) (bool, error) { +func TestPubKeyVerify(t *testing.T) { + var data = []byte("sample") hashedData := hash.Sha256(data) privKey, err := NewPrivateKey() - if err != nil { - return false, err - } + assert.Nil(t, err) signedData, err := privKey.Sign(data) - if err != nil { - return false, err - } + assert.Nil(t, err) pubKey, err := privKey.PublicKey() - if err != nil { - return false, err - } + assert.Nil(t, err) result := pubKey.Verify(signedData, hashedData.Bytes()) - - return result, nil -} - -func TestPubKeyVerify(t *testing.T) { - actual, err := SignDataWithRandomPrivateKey([]byte("sample")) - - if err != nil { - t.Fatal(err) - } expected := true - - assert.Equal(t, expected, actual) + assert.Equal(t, expected, result) } func TestWrongPubKey(t *testing.T) { diff --git a/pkg/crypto/keys/wif_test.go b/pkg/crypto/keys/wif_test.go index 5113f7a93..cccc20949 100644 --- a/pkg/crypto/keys/wif_test.go +++ b/pkg/crypto/keys/wif_test.go @@ -3,6 +3,8 @@ package keys import ( "encoding/hex" "testing" + + "github.com/stretchr/testify/assert" ) type wifTestCase struct { @@ -36,29 +38,15 @@ var wifTestCases = []wifTestCase{ func TestWIFEncodeDecode(t *testing.T) { for _, testCase := range wifTestCases { b, err := hex.DecodeString(testCase.privateKey) - if err != nil { - t.Fatal(err) - } + assert.Nil(t, err) wif, err := WIFEncode(b, testCase.version, testCase.compressed) - if err != nil { - t.Fatal(err) - } - if want, have := testCase.wif, wif; want != have { - t.Fatalf("expected %s got %s", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.wif, wif) WIF, err := WIFDecode(wif, testCase.version) - if err != nil { - t.Fatal(err) - } - if want, have := testCase.privateKey, WIF.PrivateKey.String(); want != have { - t.Fatalf("expected %s got %s", want, have) - } - if want, have := testCase.compressed, WIF.Compressed; want != have { - t.Fatalf("expected %v got %v", want, have) - } - if want, have := testCase.version, WIF.Version; want != have { - t.Fatalf("expected %d got %d", want, have) - } + assert.Nil(t, err) + assert.Equal(t, testCase.privateKey, WIF.PrivateKey.String()) + assert.Equal(t, testCase.compressed, WIF.Compressed) + assert.Equal(t, testCase.version, WIF.Version) } }