forked from FactomProject/factoid
/
transaction_test.go
192 lines (153 loc) · 3.79 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factoid
import (
"fmt"
"github.com/FactomProject/ed25519"
"math/rand"
"testing"
)
// Random first "address". It isn't a real one, but one we are using for now.
var adr1 = [ADDRESS_LENGTH]byte{
0x61, 0xe3, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
}
type zeroReader struct{}
var r *rand.Rand
func (zeroReader) Read(buf []byte) (int, error) {
//if r==nil { r = rand.New(rand.NewSource(time.Now().Unix())) }
if r == nil {
r = rand.New(rand.NewSource(1))
}
for i := range buf {
buf[i] = byte(r.Int())
}
return len(buf), nil
}
var zero zeroReader
func nextAddress() IAddress {
public, _, _ := ed25519.GenerateKey(zero)
addr := new(Address)
addr.SetBytes(public[:])
return addr
}
func nextSig() []byte {
// Get me a private key.
public, _, _ := ed25519.GenerateKey(zero)
return public[:]
}
func nextAuth2() IRCD {
if r == nil {
r = rand.New(rand.NewSource(1))
}
n := r.Int()%4 + 1
m := r.Int()%4 + n
addresses := make([]IAddress, m, m)
for j := 0; j < m; j++ {
addresses[j] = nextAddress()
}
rcd, _ := NewRCD_2(n, m, addresses)
return rcd
}
var nb IBlock
func getSignedTrans() IBlock {
if nb != nil {
return nb
}
nb = new(Transaction)
t := nb.(*Transaction)
for i := 0; i < 5; i++ {
t.AddInput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddOutput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddECOutput(nextAddress(), uint64(rand.Int63n(10000000)))
}
for i := 0; i < 3; i++ {
sig := NewRCD_1(nextSig())
t.AddAuthorization(sig)
}
for i := 0; i < 2; i++ {
t.AddAuthorization(nextAuth2())
}
return nb
}
// This test prints bunches of stuff that must be visually checked.
// Mostly we keep it commented out.
func xTestTransaction(test *testing.T) {
nb = getSignedTrans()
bytes, _ := nb.CustomMarshalText()
fmt.Printf("Transaction:\n%slen: %d\n", string(bytes), len(bytes))
fmt.Println("\n---------------------------------------------------------------------")
}
func Test_Address_MarshalUnMarshal(test *testing.T) {
a := nextAddress()
adr, err := a.MarshalBinary()
if err != nil {
Prtln(err)
test.Fail()
}
_, err = a.UnmarshalBinaryData(adr)
if err != nil {
Prtln(err)
test.Fail()
}
}
func Test_Multisig_MarshalUnMarshal(test *testing.T) {
rcd := nextAuth2()
auth2, err := rcd.MarshalBinary()
if err != nil {
Prtln(err)
test.Fail()
}
_, err = rcd.UnmarshalBinaryData(auth2)
if err != nil {
Prtln(err)
test.Fail()
}
}
func xTest_Transaction_MarshalUnMarshal(test *testing.T) {
getSignedTrans() // Make sure we have a signed transaction
data, err := nb.MarshalBinary() // Marshal our signed transaction
if err != nil { // If we have an error, print our stack
Prtln(err) // and fail our test
test.Fail()
}
xb := new(Transaction)
err = xb.UnmarshalBinary(data) // Now Unmarshal
if err != nil {
Prtln(err)
test.Fail()
return
}
// txt1,_ := xb.CustomMarshalText()
// txt2,_ := nb.CustomMarshalText()
// Prtln(string(txt1))
// Prtln(string(txt2))
if xb.IsEqual(nb) != nil {
Prtln("Trans\n", nb, "Unmarshal Trans\n", xb)
test.Fail()
}
}
func Test_ValidateAmounts(test *testing.T) {
var zero uint64
_, err := ValidateAmounts(zero - 1)
if err != nil {
test.Failed()
}
_, err = ValidateAmounts(1, 2, 3, 4, 5, zero-1)
if err != nil {
test.Failed()
}
_, err = ValidateAmounts(0x6FFFFFFFFFFFFFFF, 1)
if err != nil {
test.Failed()
}
_, err = ValidateAmounts(1, 0x6FFFFFFFFFFFFFFF, 1)
if err != nil {
test.Failed()
}
}