/
controlPanel_test.go
209 lines (176 loc) · 4.7 KB
/
controlPanel_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
package controlPanel_test
import (
"fmt"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/FactomProject/factomd/common/entryBlock"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
. "github.com/FactomProject/factomd/controlPanel"
"github.com/FactomProject/factomd/database/databaseOverlay"
//"github.com/FactomProject/factomd/p2p"
"github.com/FactomProject/factomd/state"
//"github.com/FactomProject/factomd/common/primitives/random"
. "github.com/FactomProject/factomd/testHelper"
)
var _ = time.Now()
var _ = fmt.Sprintf("")
// Enable for long test
var LongTest bool = false
func TestFactoidHas(t *testing.T) {
rc := new(LastDirectoryBlockTransactions)
rc.FactoidTransactions = append(rc.FactoidTransactions)
for i := 0; i < 10; i++ {
addTrans(rc)
}
for i := 0; i < len(rc.FactoidTransactions); i++ {
if !rc.ContainsTrans(rc.FactoidTransactions[i].TxID) {
t.Error("This should be true")
}
}
for i := 0; i < len(rc.Entries); i++ {
if !rc.ContainsEntry(rc.Entries[i].Hash) {
t.Error("This should be true")
}
}
}
func addTrans(rc *LastDirectoryBlockTransactions) {
rc.FactoidTransactions = append(rc.FactoidTransactions, struct {
TxID string
Hash string
TotalInput string
Status string
TotalInputs int
TotalOutputs int
}{primitives.RandomHash().String(), primitives.RandomHash().String(), "1", "Confirmed", 1, 1})
e := new(EntryHolder)
e.Hash = primitives.RandomHash().String()
rc.Entries = append(rc.Entries, *e)
}
func TestControlPanel(t *testing.T) {
if LongTest {
var i uint32
connections := make(chan interface{})
emptyState := CreateAndPopulateTestStateAndStartValidator()
gitBuild := "Test Is Running"
go ServeControlPanel(emptyState.ControlPanelChannel, emptyState, connections, nil, gitBuild)
emptyState.CopyStateToControlPanel()
for count := 0; count < 1000; count++ {
for i = 0; i < 5; i++ {
PopulateConnectionChan(i, connections)
}
for i = 5; i > 0; i-- {
PopulateConnectionChan(i, connections)
}
}
}
}
func TestDataDump(t *testing.T) {
AllConnections = NewConnectionsMap()
s := CreateAndPopulateTestStateAndStartValidator()
ds, err := state.DeepStateDisplayCopy(s)
if err != nil {
t.Error(err)
}
DisplayState = *ds
d := GetDataDumps()
if len(d) == 0 {
t.Error("No data")
}
}
func TestSearching(t *testing.T) {
var err error
InitTemplates()
s := CreateAndPopulateTestStateAndStartValidator()
StatePointer = s
c := new(SearchedStruct)
c.Type = "entry"
c.Input = ""
// Search for an entry
var e interfaces.IEBEntry
i := uint32(0)
for e == nil {
d, err := s.DB.FetchDBlockByHeight(i)
if err != nil {
t.Error(err)
t.FailNow()
}
if len(d.GetEBlockDBEntries()) > 0 {
for _, ebhash := range d.GetEBlockDBEntries() {
eblock, err := s.DB.FetchEBlock(ebhash.GetKeyMR())
if err != nil {
t.Error(err)
t.FailNow()
}
for _, ehash := range eblock.GetEntryHashes() {
if ehash.IsMinuteMarker() == true {
continue
}
e, err = s.DB.FetchEntry(ehash)
if err != nil {
t.Error(err)
t.FailNow()
}
c.Input = e.GetHash().String()
content, err := searchfor(c)
if err != nil {
t.Error(err)
t.FailNow()
}
if !strings.Contains(string(content), e.GetChainID().String()) {
t.Error("Does not contain correct content")
}
}
}
}
}
// Insert a special crafted entry
entry := entryBlock.NewEntry()
var one primitives.ByteSlice
one.Bytes = []byte{0xFF, 0x00}
entry.ExtIDs = []primitives.ByteSlice{one}
entry.GetHash()
db := s.DB.(*databaseOverlay.Overlay)
db.InsertEntry(entry)
c.Input = entry.GetHash().String()
content, err := searchfor(c)
if err != nil {
t.Error(err)
t.FailNow()
}
if !strings.Contains(string(content), entry.GetChainID().String()) {
t.Error("Does not contain correct content")
}
c.Input = primitives.RandomHash().String()
content, err = searchfor(c)
if err != nil {
t.Error(err)
t.FailNow()
}
if !strings.Contains(string(content), "It seems there has been an error while ") {
t.Error("Does not contain correct content")
}
}
// Trip some code that is sometimes tested in other unit tests. These lines don't have
func TestRequest(t *testing.T) {
LastRequest = time.Time{}
SetRequestMutex(true)
RequestData()
if time.Since(LastRequest).Seconds() < 1 {
t.Error("Should have not updated the time")
}
SetRequestMutex(false)
RequestData()
if time.Since(LastRequest).Seconds() > 1 {
t.Error("Should have updated the time")
}
RequestData()
}
func searchfor(ss *SearchedStruct) ([]byte, error) {
w := httptest.NewRecorder()
HandleSearchResult(ss, w)
return ioutil.ReadAll(w.Body)
}