-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parseNamespace for <= 10 byte namespace IDs (#2325)
Co-authored-by: Ryan <ryanford@poluglottos.com>
- Loading branch information
1 parent
d5b571d
commit 258dc78
Showing
2 changed files
with
121 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/celestiaorg/nmt/namespace" | ||
) | ||
|
||
func Test_parseNamespaceID(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
param string | ||
want namespace.ID | ||
wantErr bool | ||
} | ||
testCases := []testCase{ | ||
{ | ||
param: "0x0c204d39600fddd3", | ||
name: "8 byte hex encoded namespace ID gets left padded", | ||
want: namespace.ID{ | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "10 byte hex encoded namespace ID", | ||
param: "0x42690c204d39600fddd3", | ||
want: namespace.ID{ | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x42, 0x69, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "29 byte hex encoded namespace ID", | ||
param: "0x0000000000000000000000000000000000000001010101010101010101", | ||
want: namespace.ID{ | ||
0x0, // namespace version | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // v0 ID prefix | ||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // namespace ID | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "11 byte hex encoded namespace ID returns error", | ||
param: "0x42690c204d39600fddd3a3", | ||
want: namespace.ID{}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "10 byte base64 encoded namespace ID", | ||
param: "QmkMIE05YA/d0w==", | ||
want: namespace.ID{ | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x42, 0x69, 0xc, 0x20, 0x4d, 0x39, 0x60, 0xf, 0xdd, 0xd3, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "not base64 or hex encoded namespace ID returns error", | ||
param: "5748493939429", | ||
want: namespace.ID{}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := parseV0NamespaceID(tc.param) | ||
if tc.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
|
||
} | ||
} |