-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bob_test.go
193 lines (171 loc) · 5 KB
/
bob_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
package bap
import (
"fmt"
"testing"
"github.com/bitcoinschema/go-bob"
"github.com/bitcoinschema/go-bpu"
)
// TestFromTape will test the method NewFromTape()
func TestNewFromTape(t *testing.T) {
// Get BOB data from string
bobData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
// Get from tape
var b *Bap
b, err = NewFromTape(&bobData.Out[0].Tape[1])
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
} else if b.Type != ATTEST {
t.Fatalf("expected: %s got: %s", ATTEST, b.Type)
}
// Wrong tape
_, err = NewFromTape(&bobData.Out[0].Tape[0])
if err == nil {
t.Fatalf("error should have occurred")
}
// Revoke
revoke := string(REVOKE)
bobData.Out[0].Tape[1].Cell[1].S = &revoke
_, err = NewFromTape(&bobData.Out[0].Tape[1])
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
empty := ""
// Bad Sequence
bobData.Out[0].Tape[1].Cell[3].S = &empty
_, err = NewFromTape(&bobData.Out[0].Tape[1])
if err == nil {
t.Fatalf("error should have occurred")
}
id := string(ID)
idKey := "idKey"
address := "Address"
// ID tape
bobData.Out[0].Tape[1].Cell[1].S = &id
bobData.Out[0].Tape[1].Cell[2].S = &idKey
bobData.Out[0].Tape[1].Cell[3].S = &address
_, err = NewFromTape(&bobData.Out[0].Tape[1])
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
}
// ExampleNewFromTape example using NewFromTape()
func ExampleNewFromTape() {
// Get BOB data from string
bobData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get from tape
var b *Bap
b, err = NewFromTape(&bobData.Out[0].Tape[1])
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("BAP type: %s", b.Type)
// Output:BAP type: ATTEST
}
// BenchmarkNewFromTape benchmarks the method NewFromTape()
func BenchmarkNewFromTape(b *testing.B) {
bobData, _ := bob.NewFromString(sampleValidBobTx)
for i := 0; i < b.N; i++ {
_, _ = NewFromTape(&bobData.Out[0].Tape[1])
}
}
// TestFromTapePanic tests for nil case in NewFromTape()
func TestNewFromTapePanic(t *testing.T) {
t.Parallel()
_, err := NewFromTape(nil)
if err == nil {
t.Fatalf("error expected")
}
}
// TestNewFromTapes will test the method NewFromTapes()
func TestNewFromTapes(t *testing.T) {
t.Parallel()
// Parse from string into BOB
bobValidData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var bobInvalidData *bob.Tx
if bobInvalidData, err = bob.NewFromString(sampleInvalidBobTx); err != nil {
t.Fatalf("error occurred: %s", err.Error())
}
var (
// Testing private methods
tests = []struct {
inputTapes []bpu.Tape
expectedType AttestationType
expectedSequence uint64
expectedURNHash string
expectedNil bool
expectedError bool
}{
{
bobValidData.Out[0].Tape,
"ATTEST",
0,
"cf39fc55da24dc23eff1809e6e6cf32a0fe6aecc81296543e9ac84b8c501bac5",
false,
false,
},
{
bobInvalidData.Out[0].Tape,
"",
0,
"",
true,
true,
},
}
)
// Run tests
var b *Bap
for _, test := range tests {
if b, err = NewFromTapes(test.inputTapes); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.inputTapes, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%v] inputted and error was expected", t.Name(), test.inputTapes)
} else if b == nil && !test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and nil was not expected (bap)", t.Name(), test.inputTapes)
} else if b != nil && test.expectedNil {
t.Errorf("%s Failed: [%v] inputted and nil was expected (bap)", t.Name(), test.inputTapes)
} else if b != nil && b.Type != test.expectedType {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedType, b.Type)
} else if b != nil && b.Sequence != test.expectedSequence {
t.Errorf("%s Failed: [%v] inputted and expected [%d] but got [%d]", t.Name(), test.inputTapes, test.expectedSequence, b.Sequence)
} else if b != nil && b.URNHash != test.expectedURNHash {
t.Errorf("%s Failed: [%v] inputted and expected [%s] but got [%s]", t.Name(), test.inputTapes, test.expectedURNHash, b.URNHash)
}
}
}
// ExampleNewFromTapes example using NewFromTapes()
func ExampleNewFromTapes() {
// Get BOB data from string
bobData, err := bob.NewFromString(sampleValidBobTx)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
// Get from tapes
var b *Bap
b, err = NewFromTapes(bobData.Out[0].Tape)
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("BAP type: %s", b.Type)
// Output:BAP type: ATTEST
}
// BenchmarkNewFromTapes benchmarks the method NewFromTapes()
func BenchmarkNewFromTapes(b *testing.B) {
bobData, _ := bob.NewFromString(sampleValidBobTx)
for i := 0; i < b.N; i++ {
_, _ = NewFromTapes(bobData.Out[0].Tape)
}
}