forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace_test.go
216 lines (200 loc) · 5.68 KB
/
namespace_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
package share
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
)
var (
validID = append(
appns.NamespaceVersionZeroPrefix,
bytes.Repeat([]byte{1}, appns.NamespaceVersionZeroIDSize)...,
)
tooShortID = append(appns.NamespaceVersionZeroPrefix, []byte{1}...)
tooLongID = append(appns.NamespaceVersionZeroPrefix, bytes.Repeat([]byte{1}, NamespaceSize)...)
invalidPrefixID = bytes.Repeat([]byte{1}, NamespaceSize)
)
func TestNewNamespaceV0(t *testing.T) {
type testCase struct {
name string
subNid []byte
expected Namespace
wantErr bool
}
testCases := []testCase{
{
name: "8 byte id, gets left padded",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
expected: Namespace{
0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // filled zeros
0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}, // id with left padding
wantErr: false,
},
{
name: "10 byte id, no padding",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10},
expected: Namespace{
0x0, // version
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // filled zeros
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10}, // id
wantErr: false,
},
{
name: "11 byte id",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10, 0x11},
expected: []byte{},
wantErr: true,
},
{
name: "nil id",
subNid: nil,
expected: []byte{},
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := NewBlobNamespaceV0(tc.subNid)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
}
func TestFrom(t *testing.T) {
type testCase struct {
name string
bytes []byte
wantErr bool
want Namespace
}
validNamespace := []byte{}
validNamespace = append(validNamespace, appns.NamespaceVersionZero)
validNamespace = append(validNamespace, appns.NamespaceVersionZeroPrefix...)
validNamespace = append(validNamespace, bytes.Repeat([]byte{0x1}, appns.NamespaceVersionZeroIDSize)...)
parityNamespace := bytes.Repeat([]byte{0xFF}, NamespaceSize)
testCases := []testCase{
{
name: "valid namespace",
bytes: validNamespace,
wantErr: false,
want: append([]byte{appns.NamespaceVersionZero}, validID...),
},
{
name: "parity namespace",
bytes: parityNamespace,
wantErr: false,
want: append([]byte{appns.NamespaceVersionMax}, bytes.Repeat([]byte{0xFF}, appns.NamespaceIDSize)...),
},
{
name: "unsupported version",
bytes: append([]byte{1}, append(
appns.NamespaceVersionZeroPrefix,
bytes.Repeat([]byte{1}, NamespaceSize-len(appns.NamespaceVersionZeroPrefix))...,
)...),
wantErr: true,
},
{
name: "unsupported id: too short",
bytes: append([]byte{appns.NamespaceVersionZero}, tooShortID...),
wantErr: true,
},
{
name: "unsupported id: too long",
bytes: append([]byte{appns.NamespaceVersionZero}, tooLongID...),
wantErr: true,
},
{
name: "unsupported id: invalid prefix",
bytes: append([]byte{appns.NamespaceVersionZero}, invalidPrefixID...),
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := NamespaceFromBytes(tc.bytes)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.want, got)
})
}
}
func TestValidateForBlob(t *testing.T) {
type testCase struct {
name string
ns Namespace
wantErr bool
}
validNamespace, err := NewBlobNamespaceV0(bytes.Repeat([]byte{0x1}, appns.NamespaceVersionZeroIDSize))
require.NoError(t, err)
testCases := []testCase{
{
name: "valid blob namespace",
ns: validNamespace,
wantErr: false,
},
{
name: "invalid blob namespace: parity shares namespace",
ns: ParitySharesNamespace,
wantErr: true,
},
{
name: "invalid blob namespace: tail padding namespace",
ns: TailPaddingNamespace,
wantErr: true,
},
{
name: "invalid blob namespace: tx namespace",
ns: TxNamespace,
wantErr: true,
},
{
name: "invalid blob namespace: namespace version max",
ns: append([]byte{appns.NamespaceVersionMax}, bytes.Repeat([]byte{0x0}, appns.NamespaceIDSize)...),
wantErr: true,
},
{
name: "invalid blob namespace: primary reserved namespace",
ns: primaryReservedNamespace(0x10),
wantErr: true,
},
{
name: "invalid blob namespace: secondary reserved namespace",
ns: secondaryReservedNamespace(0x10),
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.ns.ValidateForBlob()
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
})
}
}
func primaryReservedNamespace(lastByte byte) Namespace {
result := make([]byte, NamespaceSize)
result = append(result, appns.NamespaceVersionZero)
result = append(result, appns.NamespaceVersionZeroPrefix...)
result = append(result, bytes.Repeat([]byte{0x0}, appns.NamespaceVersionZeroIDSize-1)...)
result = append(result, lastByte)
return result
}
func secondaryReservedNamespace(lastByte byte) Namespace {
result := make([]byte, NamespaceSize)
result = append(result, appns.NamespaceVersionMax)
result = append(result, bytes.Repeat([]byte{0xFF}, appns.NamespaceIDSize-1)...)
result = append(result, lastByte)
return result
}