-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support odd length strings and error on value too long (#605)
* Support odd length strings and error on value too long Signed-off-by: Joe Elliott <[email protected]> * changelog Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
8e79e79
commit 7539d0f
Showing
4 changed files
with
80 additions
and
16 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
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,53 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHexStringToTraceID(t *testing.T) { | ||
|
||
tc := []struct { | ||
id string | ||
expected []byte | ||
expectError bool | ||
}{ | ||
{ | ||
id: "12", | ||
expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12}, | ||
}, | ||
{ | ||
id: "1234567890abcdef", // 64 bit | ||
expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, | ||
}, | ||
{ | ||
id: "1234567890abcdef1234567890abcdef", // 128 bit | ||
expected: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, | ||
}, | ||
{ | ||
id: "121234567890abcdef1234567890abcdef", // value too long | ||
expected: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, | ||
expectError: true, | ||
}, | ||
{ | ||
id: "234567890abcdef", // odd length | ||
expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}, | ||
}, | ||
} | ||
|
||
for _, tt := range tc { | ||
t.Run(tt.id, func(t *testing.T) { | ||
actual, err := hexStringToTraceID(tt.id) | ||
|
||
if tt.expectError { | ||
assert.Error(t, err) | ||
assert.Nil(t, actual) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |