forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_test.go
216 lines (177 loc) · 6.07 KB
/
chain_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom_test
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
. "github.com/FactomProject/factom"
)
var ()
func TestNewChain(t *testing.T) {
ent := new(Entry)
ent.ChainID = "5a402200c5cf278e47905ce52d7d64529a0291829a7bd230072c5468be709069"
ent.Content = []byte("This is a test Entry.")
ent.ExtIDs = append(ent.ExtIDs, []byte("This is the first extid."))
ent.ExtIDs = append(ent.ExtIDs, []byte("This is the second extid."))
newChain := NewChain(ent)
//fmt.Println(newChain.ChainID)
expectedID := "5a402200c5cf278e47905ce52d7d64529a0291829a7bd230072c5468be709069"
if newChain.ChainID != expectedID {
fmt.Println(newChain.ChainID)
fmt.Println(expectedID)
t.Fail()
}
}
func TestIfExists(t *testing.T) {
simlatedFactomdResponse := `{
"jsonrpc": "2.0",
"id": 0,
"result": {
"ChainHead": "f65f67774139fa78344dcdd302631a0d646db0c2be4d58e3e48b2a188c1b856c"
}
}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, simlatedFactomdResponse)
}))
defer ts.Close()
url := ts.URL[7:]
SetFactomdServer(url)
expectedID := "f65f67774139fa78344dcdd302631a0d646db0c2be4d58e3e48b2a188c1b856c"
//fmt.Println(ChainExists(expectedID))
if ChainExists(expectedID) != true {
fmt.Println("chain should exist")
t.Fail()
}
}
func TestIfNotExists(t *testing.T) {
simlatedFactomdResponse := `{"jsonrpc":"2.0","id":0,"error":{"code":-32009,"message":"Missing Chain Head"}}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, simlatedFactomdResponse)
}))
defer ts.Close()
url := ts.URL[7:]
SetFactomdServer(url)
unexpectedID := "5a402200c5cf278e47905ce52d7d64529a0291829a7bd230072c5468be709069"
if ChainExists(unexpectedID) != false {
fmt.Println("chain shouldn't exist")
t.Fail()
}
}
func TestComposeChainCommit(t *testing.T) {
type response struct {
Message string `json:"message"`
}
ecAddr, _ := GetECAddress("Es2Rf7iM6PdsqfYCo3D1tnAR65SkLENyWJG1deUzpRMQmbh9F3eG")
ent := new(Entry)
ent.ChainID = "954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f4"
ent.Content = []byte("test!")
ent.ExtIDs = append(ent.ExtIDs, []byte("test"))
newChain := NewChain(ent)
cCommit, _ := ComposeChainCommit(newChain, ecAddr)
r := new(response)
json.Unmarshal(cCommit.Params, r)
binCommit, _ := hex.DecodeString(r.Message)
//fmt.Printf("%x\n",binCommit)
//the commit has a timestamp which is updated new for each time it is called. This means it is different after each call.
//we will check the non-changing parts
if len(binCommit) != 200 {
fmt.Println("expected commit to be 200 bytes long, instead got", len(binCommit))
t.Fail()
}
result := binCommit[0:1]
expected := []byte{0x00}
if !bytes.Equal(result, expected) {
fmt.Printf("found %x expected %x\n", result, expected)
t.Fail()
}
//skip the 6 bytes of the timestamp
result = binCommit[7:136]
expected, _ = hex.DecodeString("516870d4c0e1ee2d5f0d415e51fc10ae6b8d895561e9314afdc33048194d76f07cc61c8a81aea23d76ff6447689757dc1e36af66e300ce3e06b8d816c79acfd2285ed45081d5b8819a678d13c7c2d04f704b34c74e8aaecd9bd34609bee047200b3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29")
if !bytes.Equal(result, expected) {
fmt.Printf("found %x expected %x\n", result, expected)
t.Fail()
}
}
func TestComposeChainReveal(t *testing.T) {
ent := new(Entry)
ent.ChainID = "954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f4"
ent.Content = []byte("test!")
ent.ExtIDs = append(ent.ExtIDs, []byte("test"))
newChain := NewChain(ent)
cReveal, _ := ComposeChainReveal(newChain)
expectedResponse := `{"entry":"00954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f400060004746573747465737421"}`
if expectedResponse != string(cReveal.Params) {
fmt.Println(cReveal.Params)
fmt.Println(expectedResponse)
t.Fail()
}
}
func TestCommitChain(t *testing.T) {
simlatedFactomdResponse := `{
"jsonrpc":"2.0",
"id":0,
"result":{
"message":"Chain Commit Success",
"txid":"76e123d133a841fe3e08c5e3f3d392f8431f2d7668890c03f003f541efa8fc61"
}
}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, simlatedFactomdResponse)
}))
defer ts.Close()
url := ts.URL[7:]
SetFactomdServer(url)
ent := new(Entry)
ent.ChainID = "954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f4"
ent.Content = []byte("test!")
ent.ExtIDs = append(ent.ExtIDs, []byte("test"))
newChain := NewChain(ent)
ecAddr, _ := GetECAddress("Es2Rf7iM6PdsqfYCo3D1tnAR65SkLENyWJG1deUzpRMQmbh9F3eG")
response, _ := CommitChain(newChain, ecAddr)
//fmt.Println(response)
expectedResponse := "76e123d133a841fe3e08c5e3f3d392f8431f2d7668890c03f003f541efa8fc61"
if expectedResponse != response {
fmt.Println(response)
fmt.Println(expectedResponse)
t.Fail()
}
}
func TestRevealChain(t *testing.T) {
simlatedFactomdResponse := `{
"jsonrpc": "2.0",
"id": 0,
"result": {
"message": "Entry Reveal Success",
"entryhash": "f5c956749fc3eba4acc60fd485fb100e601070a44fcce54ff358d60669854734"
}
}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, simlatedFactomdResponse)
}))
defer ts.Close()
url := ts.URL[7:]
SetFactomdServer(url)
ent := new(Entry)
ent.ChainID = "954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f4"
ent.Content = []byte("test!")
ent.ExtIDs = append(ent.ExtIDs, []byte("test"))
newChain := NewChain(ent)
response, _ := RevealChain(newChain)
//fmt.Println(response)
expectedResponse := "f5c956749fc3eba4acc60fd485fb100e601070a44fcce54ff358d60669854734"
if expectedResponse != response {
fmt.Println(response)
fmt.Println(expectedResponse)
t.Fail()
}
}