keys: simplify tests with assert package

This commit is contained in:
Roman Khimov 2019-08-27 17:06:32 +03:00
parent b77e533d13
commit 483b875f4a
5 changed files with 37 additions and 108 deletions

View file

@ -4,24 +4,19 @@ import (
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/internal/keytestcases" "github.com/CityOfZion/neo-go/pkg/internal/keytestcases"
"github.com/stretchr/testify/assert"
) )
func TestNEP2Encrypt(t *testing.T) { func TestNEP2Encrypt(t *testing.T) {
for _, testCase := range keytestcases.Arr { for _, testCase := range keytestcases.Arr {
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
encryptedWif, err := NEP2Encrypt(privKey, testCase.Passphrase) encryptedWif, err := NEP2Encrypt(privKey, testCase.Passphrase)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
if want, have := testCase.EncryptedWif, encryptedWif; want != have { assert.Equal(t, testCase.EncryptedWif, encryptedWif)
t.Fatalf("expected %s got %s", want, have)
}
} }
} }
@ -29,34 +24,19 @@ func TestNEP2Decrypt(t *testing.T) {
for _, testCase := range keytestcases.Arr { for _, testCase := range keytestcases.Arr {
privKeyString, err := NEP2Decrypt(testCase.EncryptedWif, testCase.Passphrase) privKeyString, err := NEP2Decrypt(testCase.EncryptedWif, testCase.Passphrase)
assert.Nil(t, err)
if err != nil {
t.Fatal(err)
}
privKey, err := NewPrivateKeyFromWIF(privKeyString) privKey, err := NewPrivateKeyFromWIF(privKeyString)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
if want, have := testCase.PrivateKey, privKey.String(); want != have { assert.Equal(t, testCase.PrivateKey, privKey.String())
t.Fatalf("expected %s got %s", want, have)
}
wif, err := privKey.WIF() wif, err := privKey.WIF()
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.Wif, wif)
}
if want, have := testCase.Wif, wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
address, err := privKey.Address() address, err := privKey.Address()
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.Address, address)
}
if want, have := testCase.Address, address; want != have {
t.Fatalf("expected %s got %s", want, have)
}
} }
} }

View file

@ -13,23 +13,14 @@ import (
func TestPrivateKey(t *testing.T) { func TestPrivateKey(t *testing.T) {
for _, testCase := range keytestcases.Arr { for _, testCase := range keytestcases.Arr {
privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey) privKey, err := NewPrivateKeyFromHex(testCase.PrivateKey)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
address, err := privKey.Address() address, err := privKey.Address()
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.Address, address)
}
if want, have := testCase.Address, address; want != have {
t.Fatalf("expected %s got %s", want, have)
}
wif, err := privKey.WIF() wif, err := privKey.WIF()
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.Wif, wif)
}
if want, have := testCase.Wif, wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
pubKey, _ := privKey.PublicKey() pubKey, _ := privKey.PublicKey()
assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.PublicKey) assert.Equal(t, hex.EncodeToString(pubKey.Bytes()), testCase.PublicKey)
} }
@ -38,12 +29,8 @@ func TestPrivateKey(t *testing.T) {
func TestPrivateKeyFromWIF(t *testing.T) { func TestPrivateKeyFromWIF(t *testing.T) {
for _, testCase := range keytestcases.Arr { for _, testCase := range keytestcases.Arr {
key, err := NewPrivateKeyFromWIF(testCase.Wif) key, err := NewPrivateKeyFromWIF(testCase.Wif)
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.PrivateKey, key.String())
}
if want, have := testCase.PrivateKey, key.String(); want != have {
t.Fatalf("expected %s got %s", want, have)
}
} }
} }
@ -55,9 +42,7 @@ func TestSigning(t *testing.T) {
PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721") PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")
data, err := PrivateKey.Sign([]byte("sample")) data, err := PrivateKey.Sign([]byte("sample"))
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716"
s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8"

View file

@ -35,18 +35,13 @@ func TestEncodeDecodePublicKey(t *testing.T) {
func TestDecodeFromString(t *testing.T) { func TestDecodeFromString(t *testing.T) {
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c" str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
pubKey, err := NewPublicKeyFromString(str) pubKey, err := NewPublicKeyFromString(str)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
assert.Equal(t, str, hex.EncodeToString(pubKey.Bytes())) assert.Equal(t, str, hex.EncodeToString(pubKey.Bytes()))
} }
func TestPubkeyToAddress(t *testing.T) { func TestPubkeyToAddress(t *testing.T) {
pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4") pubKey, err := NewPublicKeyFromString("031ee4e73a17d8f76dc02532e2620bcb12425b33c0c9f9694cc2caa8226b68cad4")
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
actual, _ := pubKey.Address() actual, _ := pubKey.Address()
expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx" expected := "AUpGsNCHzSimeMRVPQfhwrVdiUp8Q2N2Qx"
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)

View file

@ -7,38 +7,19 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// SignDataWithRandomPrivateKey will sign data with func TestPubKeyVerify(t *testing.T) {
// a random private key, then verify said data var data = []byte("sample")
// returning true if Verify returns true
func SignDataWithRandomPrivateKey(data []byte) (bool, error) {
hashedData := hash.Sha256(data) hashedData := hash.Sha256(data)
privKey, err := NewPrivateKey() privKey, err := NewPrivateKey()
if err != nil { assert.Nil(t, err)
return false, err
}
signedData, err := privKey.Sign(data) signedData, err := privKey.Sign(data)
if err != nil { assert.Nil(t, err)
return false, err
}
pubKey, err := privKey.PublicKey() pubKey, err := privKey.PublicKey()
if err != nil { assert.Nil(t, err)
return false, err
}
result := pubKey.Verify(signedData, hashedData.Bytes()) 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 expected := true
assert.Equal(t, expected, result)
assert.Equal(t, expected, actual)
} }
func TestWrongPubKey(t *testing.T) { func TestWrongPubKey(t *testing.T) {

View file

@ -3,6 +3,8 @@ package keys
import ( import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
type wifTestCase struct { type wifTestCase struct {
@ -36,29 +38,15 @@ var wifTestCases = []wifTestCase{
func TestWIFEncodeDecode(t *testing.T) { func TestWIFEncodeDecode(t *testing.T) {
for _, testCase := range wifTestCases { for _, testCase := range wifTestCases {
b, err := hex.DecodeString(testCase.privateKey) b, err := hex.DecodeString(testCase.privateKey)
if err != nil { assert.Nil(t, err)
t.Fatal(err)
}
wif, err := WIFEncode(b, testCase.version, testCase.compressed) wif, err := WIFEncode(b, testCase.version, testCase.compressed)
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.wif, wif)
}
if want, have := testCase.wif, wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
WIF, err := WIFDecode(wif, testCase.version) WIF, err := WIFDecode(wif, testCase.version)
if err != nil { assert.Nil(t, err)
t.Fatal(err) assert.Equal(t, testCase.privateKey, WIF.PrivateKey.String())
} assert.Equal(t, testCase.compressed, WIF.Compressed)
if want, have := testCase.privateKey, WIF.PrivateKey.String(); want != have { assert.Equal(t, testCase.version, WIF.Version)
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)
}
} }
} }