diff --git a/pkg/crypto/hash/hash_test.go b/pkg/crypto/hash/hash_test.go index 272b744fd..105bfd4b6 100644 --- a/pkg/crypto/hash/hash_test.go +++ b/pkg/crypto/hash/hash_test.go @@ -1,10 +1,12 @@ package hash import ( + "encoding/binary" "encoding/hex" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestSha256(t *testing.T) { @@ -47,3 +49,18 @@ func TestHash160(t *testing.T) { actual := hex.EncodeToString(data.Bytes()) assert.Equal(t, expected, actual) } + +func TestChecksum(t *testing.T) { + testCases := []struct { + data []byte + sum uint32 + }{ + {nil, 0xe2e0f65d}, + {[]byte{}, 0xe2e0f65d}, + {[]byte{1, 2, 3, 4}, 0xe272e48d}, + } + + for _, tc := range testCases { + require.Equal(t, tc.sum, binary.LittleEndian.Uint32(Checksum(tc.data))) + } +} diff --git a/pkg/crypto/keys/nep2_test.go b/pkg/crypto/keys/nep2_test.go index 995677a7a..4c5e86c23 100644 --- a/pkg/crypto/keys/nep2_test.go +++ b/pkg/crypto/keys/nep2_test.go @@ -11,6 +11,11 @@ func TestNEP2Encrypt(t *testing.T) { for _, testCase := range keytestcases.Arr { privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) + if testCase.Invalid { + assert.Error(t, err) + continue + } + assert.Nil(t, err) encryptedWif, err := NEP2Encrypt(privKey, testCase.Passphrase) @@ -24,6 +29,11 @@ func TestNEP2Decrypt(t *testing.T) { for _, testCase := range keytestcases.Arr { privKeyString, err := NEP2Decrypt(testCase.EncryptedWif, testCase.Passphrase) + if testCase.Invalid { + assert.Error(t, err) + continue + } + assert.Nil(t, err) privKey, err := NewPrivateKeyFromWIF(privKeyString) diff --git a/pkg/crypto/keys/private_key_test.go b/pkg/crypto/keys/private_key_test.go index fd71d6812..570ba021d 100644 --- a/pkg/crypto/keys/private_key_test.go +++ b/pkg/crypto/keys/private_key_test.go @@ -12,6 +12,11 @@ import ( func TestPrivateKey(t *testing.T) { for _, testCase := range keytestcases.Arr { privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) + if testCase.Invalid { + assert.Error(t, err) + continue + } + assert.Nil(t, err) address := privKey.Address() assert.Equal(t, testCase.Address, address) @@ -26,6 +31,11 @@ func TestPrivateKey(t *testing.T) { func TestPrivateKeyFromWIF(t *testing.T) { for _, testCase := range keytestcases.Arr { key, err := NewPrivateKeyFromWIF(testCase.Wif) + if testCase.Invalid { + assert.Error(t, err) + continue + } + assert.Nil(t, err) assert.Equal(t, testCase.PrivateKey, key.String()) } diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index b35784bbf..f85407a23 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -2,6 +2,8 @@ package keys import ( "encoding/hex" + "math/rand" + "sort" "testing" "github.com/CityOfZion/neo-go/pkg/io" @@ -35,6 +37,16 @@ func TestEncodeDecodePublicKey(t *testing.T) { require.NoError(t, pDecode.DecodeBytes(b)) require.Equal(t, p.X, pDecode.X) } + + errCases := [][]byte{{}, {0x02}, {0x04}} + + for _, tc := range errCases { + r := io.NewBinReaderFromBuf(tc) + + var pDecode PublicKey + pDecode.DecodeBinary(r) + require.Error(t, r.Err) + } } func TestDecodeFromString(t *testing.T) { @@ -42,6 +54,13 @@ func TestDecodeFromString(t *testing.T) { pubKey, err := NewPublicKeyFromString(str) require.NoError(t, err) require.Equal(t, str, hex.EncodeToString(pubKey.Bytes())) + + _, err = NewPublicKeyFromString(str[2:]) + require.Error(t, err) + + str = "zzb209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c" + _, err = NewPublicKeyFromString(str) + require.Error(t, err) } func TestPubkeyToAddress(t *testing.T) { @@ -60,6 +79,28 @@ func TestDecodeBytes(t *testing.T) { require.Equal(t, pubKey,decodedPubKey) } +func TestSort(t *testing.T) { + pubs1 := make(PublicKeys, 10) + for i := range pubs1 { + priv, err := NewPrivateKey() + require.NoError(t, err) + pubs1[i] = priv.PublicKey() + } + + pubs2 := make(PublicKeys, len(pubs1)) + copy(pubs2, pubs1) + + sort.Sort(pubs1) + + rand.Shuffle(len(pubs2), func(i, j int) { + pubs2[i], pubs2[j] = pubs2[j], pubs2[i] + }) + sort.Sort(pubs2) + + // Check that sort on the same set of values produce the same result. + require.Equal(t, pubs1, pubs2) +} + func TestContains(t *testing.T) { pubKey := getPubKey(t) pubKeys := &PublicKeys{getPubKey(t)} diff --git a/pkg/crypto/keys/sign_verify_test.go b/pkg/crypto/keys/sign_verify_test.go index 3c359d6ff..7856b22e0 100644 --- a/pkg/crypto/keys/sign_verify_test.go +++ b/pkg/crypto/keys/sign_verify_test.go @@ -19,6 +19,9 @@ func TestPubKeyVerify(t *testing.T) { result := pubKey.Verify(signedData, hashedData.Bytes()) expected := true assert.Equal(t, expected, result) + + pubKey = &PublicKey{} + assert.False(t, pubKey.Verify(signedData, hashedData.Bytes())) } func TestWrongPubKey(t *testing.T) { diff --git a/pkg/crypto/keys/wif_test.go b/pkg/crypto/keys/wif_test.go index cccc20949..6fae58167 100644 --- a/pkg/crypto/keys/wif_test.go +++ b/pkg/crypto/keys/wif_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type wifTestCase struct { @@ -33,6 +34,12 @@ var wifTestCases = []wifTestCase{ privateKey: "2bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e", version: 0x80, }, + { + wif: "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o", + compressed: true, + privateKey: "2bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e", + version: 0x00, + }, } func TestWIFEncodeDecode(t *testing.T) { @@ -47,6 +54,14 @@ func TestWIFEncodeDecode(t *testing.T) { 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) + if testCase.version != 0 { + assert.Equal(t, testCase.version, WIF.Version) + } else { + assert.EqualValues(t, WIFVersion, WIF.Version) + } } + + wifInv := []byte{0, 1, 2} + _, err := WIFEncode(wifInv, 0, true) + require.Error(t, err) } diff --git a/pkg/internal/keytestcases/testcases.go b/pkg/internal/keytestcases/testcases.go index 5213b26d2..059a82744 100644 --- a/pkg/internal/keytestcases/testcases.go +++ b/pkg/internal/keytestcases/testcases.go @@ -8,6 +8,7 @@ type Ktype struct { Wif, Passphrase, EncryptedWif string + Invalid bool } // Arr contains a set of known keys in Ktype format. @@ -36,4 +37,13 @@ var Arr = []Ktype{ Passphrase: "MyL33tP@33w0rd", EncryptedWif: "6PYNoc1EG5J38MTqGN9Anphfdd6UwbS4cpFCzHhrkSKBBbV1qkbJJZQnkn", }, + { + Address: "xdf4UGKevVrMR1j3UkPsuoYKSC4ocoAkKx", + PrivateKey: "zzdee7036b8fd9cef91de47386b191dd76db2888a553e7736bb02808932a915b", + PublicKey: "zz232ce8d2e2063dce0451131851d47421bfc4fc1da4db116fca5302c0756462fa", + Wif: "zzKvWLZsNwBJx5j9nurHYRwhYfdQUu9tTEDsLCUHDbYBL8cHxMiG", + Passphrase: "zzL33tP@33w0rd", + EncryptedWif: "6PYNoc1EG5J38MTqGN9Anphfdd6UwbS4cpFCzHhrkSKBBbV1qkbJJZQnkn", + Invalid: true, + }, } diff --git a/pkg/wallet/account_test.go b/pkg/wallet/account_test.go index cc46f1557..343c993bc 100644 --- a/pkg/wallet/account_test.go +++ b/pkg/wallet/account_test.go @@ -5,14 +5,18 @@ import ( "testing" "github.com/CityOfZion/neo-go/pkg/internal/keytestcases" + "github.com/stretchr/testify/assert" ) func TestNewAccount(t *testing.T) { for _, testCase := range keytestcases.Arr { acc, err := NewAccountFromWIF(testCase.Wif) - if err != nil { - t.Fatal(err) + if testCase.Invalid { + assert.Error(t, err) + continue } + + assert.NoError(t, err) compareFields(t, testCase, acc) } } @@ -20,9 +24,12 @@ func TestNewAccount(t *testing.T) { func TestDecryptAccount(t *testing.T) { for _, testCase := range keytestcases.Arr { acc, err := DecryptAccount(testCase.EncryptedWif, testCase.Passphrase) - if err != nil { - t.Fatal(err) + if testCase.Invalid { + assert.Error(t, err) + continue } + + assert.NoError(t, err) compareFields(t, testCase, acc) } } @@ -30,9 +37,12 @@ func TestDecryptAccount(t *testing.T) { func TestNewFromWif(t *testing.T) { for _, testCase := range keytestcases.Arr { acc, err := NewAccountFromWIF(testCase.Wif) - if err != nil { - t.Fatal(err) + if testCase.Invalid { + assert.Error(t, err) + continue } + + assert.NoError(t, err) compareFields(t, testCase, acc) } }