-
Notifications
You must be signed in to change notification settings - Fork 6
/
rcd_test.go
116 lines (107 loc) · 3.28 KB
/
rcd_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
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package factom_test
import (
"math/rand"
"testing"
"github.com/stretchr/testify/require"
. "github.com/Factom-Asset-Tokens/factom"
"github.com/stretchr/testify/assert"
)
var rcdUnmarshalBinaryTests = []struct {
Name string
Data []byte
Error string
RCDType
Address Bytes32
}{
{
Name: "valid",
Data: NewBytes(
"010fd93026041de6387d2dcef0917c06288e690fa7652c20f044746e787b06b2bd"),
RCDType: 1,
Address: NewBytes32("304d80538e27505d44d5ff0ada6a9d420d93a9994da75f0763c12c827b616668"),
},
{
Name: "invalid (too short)",
Data: NewBytes(
""),
Error: "invalid RCD size",
},
{
Name: "invalid (too short)",
Data: NewBytes(
"010fd93026041de6387d2dcef0917c06288e690fa7652c20f044746e787b06b2"),
Error: "invalid RCDType01 size",
},
{
Name: "invalid (unsupported rcd type)",
Data: NewBytes(
"000fd93026041de6387d2dcef0917c06288e690fa7652c20f044746e787b06b2bd"),
Error: "unknown RCDType00",
},
{
Name: "invalid (unsupported rcd type)",
Data: NewBytes(
"020fd93026041de6387d2dcef0917c06288e690fa7652c20f044746e787b06b2bd"),
Error: "unknown RCDType02",
},
}
func TestDecodeRCD(t *testing.T) {
for _, test := range rcdUnmarshalBinaryTests {
t.Run("UnmarshalBinary/"+test.Name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
var rcd RCD
err := rcd.UnmarshalBinary(test.Data)
r := len(rcd)
if len(test.Error) == 0 {
require.NoError(err)
require.Equal(r, len(test.Data))
require.NotNil(rcd)
assert.Equal(rcd.Type(), test.RCDType)
addr := rcd.Hash()
assert.Equal(addr[:], test.Address[:])
} else {
require.EqualError(err, test.Error)
}
})
}
// To ensure there is not panics and all errors are caught
t.Run("UnmarshalBinary/RCD", func(t *testing.T) {
for i := 0; i < 1000; i++ {
d := make([]byte, rand.Intn(500))
rand.Read(d)
var rcd RCD
err := rcd.UnmarshalBinary(d)
r := len(rcd)
if err == nil {
// The RCD 1 type is pretty easy to be randomly valid.
// So if it is type one and we read the right number of bytes,
// there is no expected error.
if r != 33 && rcd.Type() != 1 && d[0] == 0x01 {
t.Errorf("expected an error")
}
}
}
})
}