-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleaseset_shared_test.go
More file actions
432 lines (354 loc) · 13.3 KB
/
Copy pathleaseset_shared_test.go
File metadata and controls
432 lines (354 loc) · 13.3 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package common
import (
"crypto/ed25519"
"encoding/binary"
"testing"
"github.com/go-i2p/common/data"
"github.com/go-i2p/common/key_certificate"
sig "github.com/go-i2p/common/signature"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// --- helpers ---------------------------------------------------------------
// buildTestDestinationBytes builds a minimal 391-byte destination payload:
//
// 256 (public key) + 128 (signing key) = 384 bytes keys
// + 7 bytes KEY certificate (type=5, len=4, sigType, cryptoType)
func buildTestDestinationBytes(sigType uint16) []byte {
keysData := make([]byte, 384)
for i := range keysData {
keysData[i] = byte(i % 256)
}
certData := []byte{
0x05, // Certificate type = KEY (5)
0x00, 0x04, // Certificate length = 4 bytes
0x00, 0x00, // Signing key type
0x00, 0x00, // Crypto key type = ElGamal
}
binary.BigEndian.PutUint16(certData[3:5], sigType)
return append(keysData, certData...)
}
// buildHeaderAfterDest constructs published(4)+expires(2)+flags(2)+emptyOptions(2).
func buildHeaderAfterDest(published uint32, expires, flags uint16) []byte {
buf := make([]byte, 0, 10)
buf = AppendBigEndianUint32(buf, published)
buf = AppendBigEndianUint16(buf, expires)
buf = AppendBigEndianUint16(buf, flags)
buf = append(buf, 0x00, 0x00) // empty mapping
return buf
}
// testApplier is a minimal LeaseSetFieldApplier for testing ParseAndApplyCommonPrefix.
type testApplier struct {
fields LeaseSetCommonFields
}
func (a *testApplier) ApplyCommonFields(f LeaseSetCommonFields) {
a.fields = f
}
// --- ValidateMinDataSize ---------------------------------------------------
func TestValidateMinDataSize(t *testing.T) {
tests := []struct {
name string
dataLen int
minSize int
wantErr bool
}{
{"exact match", 100, 100, false},
{"larger", 200, 100, false},
{"too short", 5, 100, true},
{"zero length", 0, 1, true},
{"zero min", 0, 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateMinDataSize(tt.dataLen, tt.minSize, "TestStruct")
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), "too short")
} else {
assert.NoError(t, err)
}
})
}
}
// --- ValidateLeaseSetHeaderSize --------------------------------------------
func TestValidateLeaseSetHeaderSize(t *testing.T) {
tests := []struct {
name string
dataLen int
wantErr bool
}{
{"sufficient", LeaseSetHeaderFieldsSize, false},
{"more than needed", LeaseSetHeaderFieldsSize + 10, false},
{"too short", LeaseSetHeaderFieldsSize - 1, true},
{"zero", 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateLeaseSetHeaderSize(tt.dataLen, "TestStruct")
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), "header")
} else {
assert.NoError(t, err)
}
})
}
}
// --- ParseLeaseSetHeaderFields ---------------------------------------------
func TestParseLeaseSetHeaderFields(t *testing.T) {
buf := make([]byte, 12) // 8 header + 4 trailing
binary.BigEndian.PutUint32(buf[0:4], 1735689600)
binary.BigEndian.PutUint16(buf[4:6], 600)
binary.BigEndian.PutUint16(buf[6:8], 0x0001)
buf[8] = 0xAA
buf[9] = 0xBB
buf[10] = 0xCC
buf[11] = 0xDD
published, expires, flags, remainder := ParseLeaseSetHeaderFields(buf)
assert.Equal(t, uint32(1735689600), published)
assert.Equal(t, uint16(600), expires)
assert.Equal(t, uint16(1), flags)
assert.Equal(t, []byte{0xAA, 0xBB, 0xCC, 0xDD}, remainder)
}
// --- AppendBigEndianUint16 / AppendBigEndianUint32 -------------------------
func TestAppendBigEndianUint16(t *testing.T) {
buf := AppendBigEndianUint16(nil, 0x1234)
require.Len(t, buf, 2)
assert.Equal(t, uint16(0x1234), binary.BigEndian.Uint16(buf))
}
func TestAppendBigEndianUint32(t *testing.T) {
buf := AppendBigEndianUint32(nil, 0xDEADBEEF)
require.Len(t, buf, 4)
assert.Equal(t, uint32(0xDEADBEEF), binary.BigEndian.Uint32(buf))
}
func TestAppendBigEndianUint16_Chained(t *testing.T) {
buf := AppendBigEndianUint16([]byte{0xFF}, 0x0001)
require.Len(t, buf, 3)
assert.Equal(t, byte(0xFF), buf[0])
assert.Equal(t, uint16(1), binary.BigEndian.Uint16(buf[1:3]))
}
func TestAppendBigEndianUint32_Chained(t *testing.T) {
buf := AppendBigEndianUint32([]byte{0x01, 0x02}, 42)
require.Len(t, buf, 6)
assert.Equal(t, uint32(42), binary.BigEndian.Uint32(buf[2:6]))
}
// --- PrependLeaseSetTypeByte -----------------------------------------------
func TestPrependLeaseSetTypeByte(t *testing.T) {
content := []byte{0x01, 0x02, 0x03}
result := PrependLeaseSetTypeByte(0x05, content)
require.Len(t, result, 4)
assert.Equal(t, byte(0x05), result[0])
assert.Equal(t, content, result[1:])
}
func TestPrependLeaseSetTypeByte_EmptyContent(t *testing.T) {
result := PrependLeaseSetTypeByte(0xFF, nil)
require.Len(t, result, 1)
assert.Equal(t, byte(0xFF), result[0])
}
// --- ExtractEd25519PrivateKey ----------------------------------------------
func TestExtractEd25519PrivateKey_FromPrivateKey(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
extracted, err := ExtractEd25519PrivateKey(priv)
assert.NoError(t, err)
assert.Equal(t, priv, extracted)
}
func TestExtractEd25519PrivateKey_FromBytes(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
extracted, err := ExtractEd25519PrivateKey([]byte(priv))
assert.NoError(t, err)
assert.Equal(t, ed25519.PrivateKey(priv), extracted)
}
func TestExtractEd25519PrivateKey_WrongSizeBytes(t *testing.T) {
_, err := ExtractEd25519PrivateKey([]byte{0x01, 0x02, 0x03})
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid signing key length")
}
func TestExtractEd25519PrivateKey_Nil(t *testing.T) {
_, err := ExtractEd25519PrivateKey(nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "nil")
}
func TestExtractEd25519PrivateKey_UnsupportedType(t *testing.T) {
_, err := ExtractEd25519PrivateKey("not a key")
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported signing key type")
}
// --- SignLeaseSetData ------------------------------------------------------
func TestSignLeaseSetData_Ed25519(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
data := []byte("test data to sign")
sigType := uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519)
sigBytes, err := SignLeaseSetData(priv, data, sigType)
assert.NoError(t, err)
require.Len(t, sigBytes, ed25519.SignatureSize)
// Verify the signature is valid
pub := priv.Public().(ed25519.PublicKey)
assert.True(t, ed25519.Verify(pub, data, sigBytes))
}
func TestSignLeaseSetData_RedDSA(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
data := []byte("test data for reddsa")
sigType := uint16(sig.SIGNATURE_TYPE_REDDSA_SHA512_ED25519)
sigBytes, err := SignLeaseSetData(priv, data, sigType)
assert.NoError(t, err)
assert.Len(t, sigBytes, 64) // RedDSA produces 64-byte signatures
}
func TestSignLeaseSetData_Ed25519ph(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
data := []byte("test data for ed25519ph")
sigType := uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519PH)
sigBytes, err := SignLeaseSetData(priv, data, sigType)
assert.NoError(t, err)
assert.Len(t, sigBytes, 64)
}
func TestSignLeaseSetData_UnsupportedType(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
_, err = SignLeaseSetData(priv, []byte("data"), 9999)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not implemented")
}
func TestSignLeaseSetData_InvalidKey(t *testing.T) {
_, err := SignLeaseSetData("bad key", []byte("data"),
uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519))
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported signing key type")
}
// --- DetermineSignatureType ------------------------------------------------
func TestDetermineSignatureType_NoOfflineSig(t *testing.T) {
result := DetermineSignatureType(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519, nil)
assert.Equal(t, uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519), result)
}
// --- ParseDestinationFromData ----------------------------------------------
func TestParseDestinationFromData_Valid(t *testing.T) {
destData := buildTestDestinationBytes(key_certificate.KEYCERT_SIGN_ED25519)
trailing := []byte{0xDE, 0xAD}
input := append(destData, trailing...)
dest, rem, err := ParseDestinationFromData(input, "TestStruct")
assert.NoError(t, err)
assert.NotNil(t, dest)
assert.Equal(t, trailing, rem)
}
func TestParseDestinationFromData_TruncatedData(t *testing.T) {
_, _, err := ParseDestinationFromData([]byte{0x01, 0x02}, "TestStruct")
assert.Error(t, err)
assert.Contains(t, err.Error(), "destination")
}
// --- ParseEmbeddedMapping --------------------------------------------------
func TestParseEmbeddedMapping_EmptyMapping(t *testing.T) {
// Empty mapping: 2-byte length = 0
input := []byte{0x00, 0x00, 0xAA, 0xBB}
mapping, rem, err := ParseEmbeddedMapping(input, "TestStruct")
assert.NoError(t, err)
_ = mapping
// Remainder should be the trailing bytes
assert.Equal(t, []byte{0xAA, 0xBB}, rem)
}
// --- ParseOfflineSignatureField --------------------------------------------
func TestParseOfflineSignatureField_NoOfflineKeys(t *testing.T) {
input := []byte{0x01, 0x02, 0x03}
offlineSig, rem, err := ParseOfflineSignatureField(false, 0, input, "TestStruct")
assert.NoError(t, err)
assert.Nil(t, offlineSig)
assert.Equal(t, input, rem)
}
func TestParseOfflineSignatureField_WithOfflineKeys_TruncatedData(t *testing.T) {
// Only a few bytes — too short for a valid offline signature
input := []byte{0x01, 0x02}
_, _, err := ParseOfflineSignatureField(true, uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519), input, "TestStruct")
assert.Error(t, err)
}
// --- ParseLeaseSetSignature ------------------------------------------------
func TestParseLeaseSetSignature_Valid(t *testing.T) {
// Build a 64-byte Ed25519 signature + trailing data
sigData := make([]byte, sig.EdDSA_SHA512_Ed25519_SIZE)
for i := range sigData {
sigData[i] = byte(i)
}
trailing := []byte{0xDE, 0xAD}
input := append(sigData, trailing...)
signature, rem, err := ParseLeaseSetSignature(
input,
sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519,
false, nil, "TestStruct",
)
assert.NoError(t, err)
assert.True(t, signature.IsValid())
assert.Equal(t, trailing, rem)
}
func TestParseLeaseSetSignature_TooShort(t *testing.T) {
_, _, err := ParseLeaseSetSignature(
[]byte{0x01},
sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519,
false, nil, "TestStruct",
)
assert.Error(t, err)
}
// --- ParseAndApplyCommonPrefix / ParseLeaseSetCommonPrefix -----------------
func TestParseAndApplyCommonPrefix_Valid(t *testing.T) {
destBytes := buildTestDestinationBytes(key_certificate.KEYCERT_SIGN_ED25519)
header := buildHeaderAfterDest(1735689600, 600, 0)
input := append(destBytes, header...)
applier := &testApplier{}
rem, err := ParseAndApplyCommonPrefix(applier, input, 10, "TestStruct")
assert.NoError(t, err)
assert.Empty(t, rem)
assert.Equal(t, uint32(1735689600), applier.fields.Published)
assert.Equal(t, uint16(600), applier.fields.Expires)
assert.Equal(t, uint16(0), applier.fields.Flags)
}
func TestParseAndApplyCommonPrefix_TooShort(t *testing.T) {
applier := &testApplier{}
_, err := ParseAndApplyCommonPrefix(applier, []byte{0x01}, 100, "TestStruct")
assert.Error(t, err)
}
func TestParseLeaseSetCommonPrefix_TooShortForHeader(t *testing.T) {
// Destination is valid but no header fields follow
destBytes := buildTestDestinationBytes(key_certificate.KEYCERT_SIGN_ED25519)
_, _, err := ParseLeaseSetCommonPrefix(destBytes, 10, "TestStruct")
assert.Error(t, err)
assert.Contains(t, err.Error(), "header")
}
// --- SerializeLeaseSetHeader -----------------------------------------------
func TestSerializeLeaseSetHeader_RoundTrip(t *testing.T) {
// Build a real destination for serialization
destBytes := buildTestDestinationBytes(key_certificate.KEYCERT_SIGN_ED25519)
dest, _, err := ParseDestinationFromData(destBytes, "test")
require.NoError(t, err)
published := uint32(1735689600)
expires := uint16(600)
flags := uint16(0)
headerBytes, err := SerializeLeaseSetHeader(dest, published, expires, flags, nil, data.Mapping{})
assert.NoError(t, err)
require.NotNil(t, headerBytes)
// The header should start with the destination bytes
assert.True(t, len(headerBytes) > len(destBytes))
}
// --- CreateLeaseSetSignature -----------------------------------------------
func TestCreateLeaseSetSignature_Ed25519(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
sigType := uint16(sig.SIGNATURE_TYPE_EDDSA_SHA512_ED25519)
data := []byte("lease set content")
signature, err := CreateLeaseSetSignature(priv, data, sigType, SignLeaseSetData)
assert.NoError(t, err)
assert.True(t, signature.IsValid())
}
func TestCreateLeaseSetSignature_UnsupportedType(t *testing.T) {
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
// Signature type 0 (DSA_SHA1) has a known size but our signing dispatch
// doesn't support it — SignLeaseSetData should error.
_, err = CreateLeaseSetSignature(priv, []byte("data"), 9999, SignLeaseSetData)
assert.Error(t, err)
}
// --- Constants -------------------------------------------------------------
func TestLeaseSetConstants(t *testing.T) {
assert.Equal(t, 8, LeaseSetHeaderFieldsSize)
assert.Equal(t, 1, LeaseSetFlagOfflineKeys)
}