Merge pull request #516 from nspcc-dev/feat/crypto_invalid
crypto: add more test for error cases
This commit is contained in:
commit
821c9b2851
8 changed files with 123 additions and 7 deletions
|
@ -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)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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)}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue