mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-22 19:29:39 +00:00
keys: simplify tests with assert package
This commit is contained in:
parent
b77e533d13
commit
483b875f4a
5 changed files with 37 additions and 108 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue