hex_utils_test.go raw
1 package neo4j
2
3 import (
4 "testing"
5
6 "next.orly.dev/pkg/nostr/encoders/tag"
7 )
8
9 // TestIsBinaryEncoded tests the IsBinaryEncoded function
10 func TestIsBinaryEncoded(t *testing.T) {
11 tests := []struct {
12 name string
13 input []byte
14 expected bool
15 }{
16 {
17 name: "Valid binary encoded (33 bytes with null terminator)",
18 input: append(make([]byte, 32), 0),
19 expected: true,
20 },
21 {
22 name: "Invalid - 32 bytes without terminator",
23 input: make([]byte, 32),
24 expected: false,
25 },
26 {
27 name: "Invalid - 33 bytes without null terminator",
28 input: append(make([]byte, 32), 1),
29 expected: false,
30 },
31 {
32 name: "Invalid - 64 bytes (hex string)",
33 input: []byte("0000000000000000000000000000000000000000000000000000000000000001"),
34 expected: false,
35 },
36 {
37 name: "Invalid - empty",
38 input: []byte{},
39 expected: false,
40 },
41 {
42 name: "Invalid - too short",
43 input: []byte{0, 1, 2, 3},
44 expected: false,
45 },
46 }
47
48 for _, tt := range tests {
49 t.Run(tt.name, func(t *testing.T) {
50 result := IsBinaryEncoded(tt.input)
51 if result != tt.expected {
52 t.Errorf("IsBinaryEncoded(%v) = %v, want %v", tt.input, result, tt.expected)
53 }
54 })
55 }
56 }
57
58 // TestNormalizePubkeyHex tests the NormalizePubkeyHex function
59 func TestNormalizePubkeyHex(t *testing.T) {
60 // Create a 32-byte test value
61 testBytes := make([]byte, 32)
62 testBytes[31] = 0x01 // Set last byte to 1
63
64 // Create binary-encoded version (33 bytes with null terminator)
65 binaryEncoded := append(testBytes, 0)
66
67 tests := []struct {
68 name string
69 input []byte
70 expected string
71 }{
72 {
73 name: "Binary encoded to hex",
74 input: binaryEncoded,
75 expected: "0000000000000000000000000000000000000000000000000000000000000001",
76 },
77 {
78 name: "Raw 32-byte binary to hex",
79 input: testBytes,
80 expected: "0000000000000000000000000000000000000000000000000000000000000001",
81 },
82 {
83 name: "Lowercase hex passthrough",
84 input: []byte("0000000000000000000000000000000000000000000000000000000000000001"),
85 expected: "0000000000000000000000000000000000000000000000000000000000000001",
86 },
87 {
88 name: "Uppercase hex to lowercase",
89 input: []byte("ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789"),
90 expected: "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
91 },
92 {
93 name: "Mixed case hex to lowercase",
94 input: []byte("AbCdEf0123456789AbCdEf0123456789AbCdEf0123456789AbCdEf0123456789"),
95 expected: "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
96 },
97 {
98 name: "Prefix hex (shorter than 64)",
99 input: []byte("ABCD"),
100 expected: "abcd",
101 },
102 {
103 name: "Empty input",
104 input: []byte{},
105 expected: "",
106 },
107 }
108
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 result := NormalizePubkeyHex(tt.input)
112 if result != tt.expected {
113 t.Errorf("NormalizePubkeyHex(%v) = %q, want %q", tt.input, result, tt.expected)
114 }
115 })
116 }
117 }
118
119 // TestExtractPTagValue tests the ExtractPTagValue function
120 func TestExtractPTagValue(t *testing.T) {
121 // Create a valid pubkey hex string
122 validHex := "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
123
124 tests := []struct {
125 name string
126 tag *tag.T
127 expected string
128 }{
129 {
130 name: "Nil tag",
131 tag: nil,
132 expected: "",
133 },
134 {
135 name: "Empty tag",
136 tag: &tag.T{T: [][]byte{}},
137 expected: "",
138 },
139 {
140 name: "Tag with only key",
141 tag: &tag.T{T: [][]byte{[]byte("p")}},
142 expected: "",
143 },
144 {
145 name: "Valid p-tag with hex value",
146 tag: &tag.T{T: [][]byte{
147 []byte("p"),
148 []byte(validHex),
149 }},
150 expected: validHex,
151 },
152 {
153 name: "P-tag with uppercase hex",
154 tag: &tag.T{T: [][]byte{
155 []byte("p"),
156 []byte("ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789"),
157 }},
158 expected: "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
159 },
160 }
161
162 for _, tt := range tests {
163 t.Run(tt.name, func(t *testing.T) {
164 result := ExtractPTagValue(tt.tag)
165 if result != tt.expected {
166 t.Errorf("ExtractPTagValue() = %q, want %q", result, tt.expected)
167 }
168 })
169 }
170 }
171
172 // TestExtractETagValue tests the ExtractETagValue function
173 func TestExtractETagValue(t *testing.T) {
174 // Create a valid event ID hex string
175 validHex := "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
176
177 tests := []struct {
178 name string
179 tag *tag.T
180 expected string
181 }{
182 {
183 name: "Nil tag",
184 tag: nil,
185 expected: "",
186 },
187 {
188 name: "Empty tag",
189 tag: &tag.T{T: [][]byte{}},
190 expected: "",
191 },
192 {
193 name: "Tag with only key",
194 tag: &tag.T{T: [][]byte{[]byte("e")}},
195 expected: "",
196 },
197 {
198 name: "Valid e-tag with hex value",
199 tag: &tag.T{T: [][]byte{
200 []byte("e"),
201 []byte(validHex),
202 }},
203 expected: validHex,
204 },
205 {
206 name: "E-tag with uppercase hex",
207 tag: &tag.T{T: [][]byte{
208 []byte("e"),
209 []byte("1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"),
210 }},
211 expected: "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
212 },
213 }
214
215 for _, tt := range tests {
216 t.Run(tt.name, func(t *testing.T) {
217 result := ExtractETagValue(tt.tag)
218 if result != tt.expected {
219 t.Errorf("ExtractETagValue() = %q, want %q", result, tt.expected)
220 }
221 })
222 }
223 }
224
225 // TestIsValidHexPubkey tests the IsValidHexPubkey function
226 func TestIsValidHexPubkey(t *testing.T) {
227 tests := []struct {
228 name string
229 input string
230 expected bool
231 }{
232 {
233 name: "Valid lowercase hex",
234 input: "0000000000000000000000000000000000000000000000000000000000000001",
235 expected: true,
236 },
237 {
238 name: "Valid uppercase hex",
239 input: "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789",
240 expected: true,
241 },
242 {
243 name: "Valid mixed case hex",
244 input: "AbCdEf0123456789AbCdEf0123456789AbCdEf0123456789AbCdEf0123456789",
245 expected: true,
246 },
247 {
248 name: "Too short",
249 input: "0000000000000000000000000000000000000000000000000000000000000",
250 expected: false,
251 },
252 {
253 name: "Too long",
254 input: "00000000000000000000000000000000000000000000000000000000000000001",
255 expected: false,
256 },
257 {
258 name: "Contains non-hex character",
259 input: "000000000000000000000000000000000000000000000000000000000000000g",
260 expected: false,
261 },
262 {
263 name: "Empty string",
264 input: "",
265 expected: false,
266 },
267 {
268 name: "Contains space",
269 input: "0000000000000000000000000000000000000000000000000000000000000 01",
270 expected: false,
271 },
272 }
273
274 for _, tt := range tests {
275 t.Run(tt.name, func(t *testing.T) {
276 result := IsValidHexPubkey(tt.input)
277 if result != tt.expected {
278 t.Errorf("IsValidHexPubkey(%q) = %v, want %v", tt.input, result, tt.expected)
279 }
280 })
281 }
282 }
283