-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction_test.go
79 lines (55 loc) · 1.97 KB
/
transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package core
import (
"github.com/izqui/helpers"
"reflect"
"testing"
)
func TestTransactionMarshalling(t *testing.T) {
kp := GenerateNewKeypair()
tr := NewTransaction(kp.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024*1024))))
tr.Header.Nonce = tr.GenerateNonce(helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX))
tr.Signature = tr.Sign(kp)
data, err := tr.MarshalBinary()
if err != nil {
t.Error(err)
}
newT := &Transaction{}
rem, err := newT.UnmarshalBinary(data)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(*newT, *tr) || len(rem) < 0 {
t.Error("Marshall, unmarshall failed")
}
}
func TestTransactionVerification(t *testing.T) {
pow := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX)
kp := GenerateNewKeypair()
tr := NewTransaction(kp.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024))))
tr.Header.Nonce = tr.GenerateNonce(pow)
tr.Signature = tr.Sign(kp)
if !tr.VerifyTransaction(pow) {
t.Error("Validation failing")
}
}
func TestIncorrectTransactionPOWVerification(t *testing.T) {
pow := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX)
powIncorrect := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, 'a')
kp := GenerateNewKeypair()
tr := NewTransaction(kp.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024))))
tr.Header.Nonce = tr.GenerateNonce(powIncorrect)
tr.Signature = tr.Sign(kp)
if tr.VerifyTransaction(pow) {
t.Error("Passed validation without pow")
}
}
func TestIncorrectTransactionSignatureVerification(t *testing.T) {
pow := helpers.ArrayOfBytes(TEST_TRANSACTION_POW_COMPLEXITY, TEST_POW_PREFIX)
kp1, kp2 := GenerateNewKeypair(), GenerateNewKeypair()
tr := NewTransaction(kp2.Public, nil, []byte(helpers.RandomString(helpers.RandomInt(0, 1024))))
tr.Header.Nonce = tr.GenerateNonce(pow)
tr.Signature = tr.Sign(kp1)
if tr.VerifyTransaction(pow) {
t.Error("Passed validation with incorrect key")
}
}