-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
94 lines (86 loc) · 2.62 KB
/
utils_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
package otp
import (
"encoding/hex"
"reflect"
"testing"
)
func TestParseDecimalToBigEndian8(t *testing.T) {
input := "12345678"
expected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xBC, 0x61, 0x4E}
got, err := ParseDecimalToBigEndian8(input)
if err != nil {
t.Fatalf("ParseDecimalToBigEndian8(%q) returned error: %v", input, err)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("ParseDecimalToBigEndian8(%q) = %v, want %v", input, got, expected)
}
}
func TestLeftPadHex(t *testing.T) {
tests := []struct {
input string
totalLen int
expected string
}{
{"ABC", 6, "000ABC"},
{"123456", 4, "3456"},
{"DEAD", 4, "DEAD"},
}
for _, tt := range tests {
got := LeftPadHex(tt.input, tt.totalLen)
if got != tt.expected {
t.Errorf("LeftPadHex(%q, %d) = %q, want %q", tt.input, tt.totalLen, got, tt.expected)
}
}
}
func TestMustHexPadLeft(t *testing.T) {
input := "ABC"
expectedBytes, _ := hex.DecodeString("00000ABC")
got := MustHexPadLeft(input, 4)
if !reflect.DeepEqual(got, expectedBytes) {
t.Errorf("MustHexPadLeft(%q, 4) = %v, want %v", input, got, expectedBytes)
}
}
func TestParseDecimal64BigEndian(t *testing.T) {
input := "12345678"
expected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xBC, 0x61, 0x4E}
got, err := ParseDecimal64BigEndian(input)
if err != nil {
t.Fatalf("ParseDecimal64BigEndian(%q) returned error: %v", input, err)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("ParseDecimal64BigEndian(%q) = %v, want %v", input, got, expected)
}
}
func TestParseHexTimestamp(t *testing.T) {
input := "132D0B6"
expected, _ := hex.DecodeString("000000000132D0B6")
got, err := ParseHexTimestamp(input)
if err != nil {
t.Fatalf("ParseHexTimestamp(%q) returned error: %v", input, err)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("ParseHexTimestamp(%q) = %v, want %v", input, got, expected)
}
}
func TestParseDecimalChallengeRFC6287(t *testing.T) {
input := "11111111"
got, err := ParseDecimalChallengeRFC6287(input)
if err != nil {
t.Fatalf("ParseDecimalChallengeRFC6287(%q) error: %v", input, err)
}
if len(got) != 128 {
t.Errorf("ParseDecimalChallengeRFC6287(%q) length = %d, want 128", input, len(got))
}
expectedPrefix := []byte{0xA9, 0x8A, 0xC7}
if !reflect.DeepEqual(got[:3], expectedPrefix) {
t.Errorf("ParseDecimalChallengeRFC6287(%q) prefix = % X, want % X", input, got[:3], expectedPrefix)
}
}
func TestTo8ByteBigEndian(t *testing.T) {
var input uint64 = 12345678
expected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xBC, 0x61, 0x4E}
got := To8ByteBigEndian(input)
if !reflect.DeepEqual(got, expected) {
t.Errorf("To8ByteBigEndian(%d) = %v, want %v", input, got, expected)
}
}