-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz_coverage_test.go
More file actions
770 lines (688 loc) · 25.5 KB
/
Copy pathzz_coverage_test.go
File metadata and controls
770 lines (688 loc) · 25.5 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// SPDX-License-Identifier: AGPL-3.0-or-later
package nameserver
import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/pilot-protocol/common/coreapi"
"github.com/pilot-protocol/common/protocol"
)
// ---------------------------------------------------------------------------
// Service adapter (service.go) — was 0% covered.
// ---------------------------------------------------------------------------
func TestService_NewServiceSurface(t *testing.T) {
t.Parallel()
svc := NewService()
if svc == nil {
t.Fatal("NewService returned nil")
}
if got := svc.Name(); got != "nameserver" {
t.Errorf("Name(): got %q, want %q", got, "nameserver")
}
if got := svc.Order(); got != 150 {
t.Errorf("Order(): got %d, want 150", got)
}
if err := svc.Start(context.Background(), coreapi.Deps{}); err != nil {
t.Errorf("Start: %v", err)
}
if err := svc.Stop(context.Background()); err != nil {
t.Errorf("Stop: %v", err)
}
}
func TestService_StartStopWithCancelledContext(t *testing.T) {
t.Parallel()
// Today Start/Stop are no-ops; document that they don't observe
// context cancellation. Regression test for the day someone wires
// real Start logic in.
svc := NewService()
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := svc.Start(ctx, coreapi.Deps{}); err != nil {
t.Errorf("Start with cancelled ctx: %v", err)
}
if err := svc.Stop(ctx); err != nil {
t.Errorf("Stop with cancelled ctx: %v", err)
}
}
// ---------------------------------------------------------------------------
// extractCallerNode (server.go) — pin both the audit-flagged downgrade
// failure-mode AND the rarely-hit positive branch.
// ---------------------------------------------------------------------------
// stringAddr is a net.Addr whose String() returns whatever we tell it,
// letting us simulate every shape extractCallerNode might encounter.
type stringAddr struct{ s string }
func (a stringAddr) Network() string { return "pilot" }
func (a stringAddr) String() string { return a.s }
func TestExtractCallerNode_AllShapes(t *testing.T) {
t.Parallel()
// Positive path: pilot addr wrapped in IPv6-style brackets so
// net.SplitHostPort can isolate the host portion.
want := protocol.Addr{Network: 0, Node: 0x12345678}
bracketed := stringAddr{s: "[" + want.String() + "]:8080"}
if got := extractCallerNode(bracketed); got != want.Node {
t.Errorf("bracketed pilot addr: got 0x%x, want 0x%x", got, want.Node)
}
// Regression pin (iter-2 audit): the plain "N:NNNN.HHHH.LLLL" form
// — i.e. what protocol.Addr.String() actually returns — gets split
// by net.SplitHostPort on the first colon, leaving an unparseable
// host. The function silently returns 0 instead of erroring. The
// IPC layer authenticates upstream so this is LOW, but if anyone
// ever calls extractCallerNode in a path without upstream auth this
// test must turn red.
if got := extractCallerNode(stringAddr{s: want.String()}); got != 0 {
t.Errorf("plain pilot addr without brackets: got 0x%x, want 0 (silent failure documented)", got)
}
// String() returning something with too many colons → SplitHostPort
// errors → fallback uses the full string → ParseAddr errors → 0.
if got := extractCallerNode(stringAddr{s: "0:0000.1234.5678:9999"}); got != 0 {
t.Errorf("too-many-colons addr: got 0x%x, want 0", got)
}
// String() returning a non-pilot, non-host:port shape → fallback,
// ParseAddr errors → 0.
if got := extractCallerNode(stringAddr{s: "not-a-pilot-addr"}); got != 0 {
t.Errorf("garbage addr: got 0x%x, want 0", got)
}
// Empty string.
if got := extractCallerNode(stringAddr{s: ""}); got != 0 {
t.Errorf("empty addr: got 0x%x, want 0", got)
}
}
func TestHandleRegister_CallerMismatchRejected(t *testing.T) {
t.Parallel()
pl := &fakeListener{}
s := New(pl, "")
defer s.Store().Close()
caller := protocol.Addr{Network: 0, Node: 0x111}
other := protocol.Addr{Network: 0, Node: 0x222}
remote := stringAddr{s: "[" + caller.String() + "]:1234"}
// REGISTER A for a different node — must be rejected.
resp := s.handleRequest(Request{
Command: "REGISTER",
RecordType: "A",
Name: "spoofed",
Address: other.String(),
}, remote)
if !strings.Contains(resp, "ERR") || !strings.Contains(resp, "another node") {
t.Errorf("expected caller-mismatch rejection, got %q", resp)
}
// REGISTER S for a different node — must also be rejected.
resp = s.handleRequest(Request{
Command: "REGISTER",
RecordType: "S",
Name: "spoofed-svc",
Address: other.String(),
NetID: 1,
Port: 80,
}, remote)
if !strings.Contains(resp, "ERR") || !strings.Contains(resp, "another node") {
t.Errorf("expected caller-mismatch rejection on S, got %q", resp)
}
// REGISTER A for the same node — succeeds.
resp = s.handleRequest(Request{
Command: "REGISTER",
RecordType: "A",
Name: "self",
Address: caller.String(),
}, remote)
if resp != "OK" {
t.Errorf("expected OK for matching caller, got %q", resp)
}
}
// ---------------------------------------------------------------------------
// handleConn (server.go) — exercise the read-error and bad-request paths.
// ---------------------------------------------------------------------------
func TestHandleConn_BadRequestReturnsErr(t *testing.T) {
t.Parallel()
srv, dialer := startServer(t)
_ = srv
conn, err := dialer.DialAddrTimeout(protocol.Addr{}, 0, 2*time.Second)
if err != nil {
t.Fatalf("dial: %v", err)
}
defer conn.Close()
if _, err := conn.Write([]byte("THIS IS NOT A VALID REQUEST AT ALL")); err != nil {
t.Fatalf("write: %v", err)
}
buf := make([]byte, 1024)
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
n, err := conn.Read(buf)
if err != nil {
t.Fatalf("read: %v", err)
}
if !strings.HasPrefix(string(buf[:n]), "ERR ") {
t.Errorf("expected ERR response, got %q", string(buf[:n]))
}
}
func TestHandleConn_CloseBeforeReadIsClean(t *testing.T) {
t.Parallel()
srv, dialer := startServer(t)
_ = srv
conn, err := dialer.DialAddrTimeout(protocol.Addr{}, 0, 2*time.Second)
if err != nil {
t.Fatalf("dial: %v", err)
}
// Close immediately without sending data — handleConn must return
// from the read-error branch without crashing.
_ = conn.Close()
time.Sleep(50 * time.Millisecond)
// Reach into the server again to confirm it's still alive.
if _, err := dialer.DialAddrTimeout(protocol.Addr{}, 0, 2*time.Second); err != nil {
t.Errorf("server appears dead after bare-close client: %v", err)
}
}
// ---------------------------------------------------------------------------
// ListenAndServe (server.go) — listen error path.
// ---------------------------------------------------------------------------
type errListener struct{ err error }
func (l *errListener) Listen(uint16) (net.Listener, error) {
return nil, l.err
}
func TestListenAndServe_ListenError(t *testing.T) {
t.Parallel()
want := errors.New("simulated bind failure")
s := New(&errListener{err: want}, "")
err := s.ListenAndServe()
if err == nil {
t.Fatal("expected listen error, got nil")
}
if !errors.Is(err, want) {
t.Errorf("error chain missing simulated err: %v", err)
}
}
// closableListener wraps a real listener so we can force Accept() to fail.
type closableListener struct {
inner net.Listener
pl *fakeListener
closeAt time.Time
}
func (l *closableListener) Listen(port uint16) (net.Listener, error) {
ln, err := l.pl.Listen(port)
if err != nil {
return nil, err
}
l.inner = ln
return ln, nil
}
func TestListenAndServe_AcceptErrorAfterClose(t *testing.T) {
t.Parallel()
pl := &fakeListener{}
cl := &closableListener{pl: pl}
s := New(cl, "")
done := make(chan error, 1)
go func() { done <- s.ListenAndServe() }()
select {
case <-s.Ready():
case <-time.After(2 * time.Second):
t.Fatal("server never became ready")
}
_ = s.Close()
select {
case err := <-done:
if err == nil {
t.Error("expected accept error after Close, got nil")
}
case <-time.After(2 * time.Second):
t.Fatal("ListenAndServe didn't return after Close")
}
}
// ---------------------------------------------------------------------------
// Client error paths (client.go) — send dial failure + ERR response unwrap.
// ---------------------------------------------------------------------------
type failDialer struct{ err error }
func (d *failDialer) DialAddrTimeout(protocol.Addr, uint16, time.Duration) (net.Conn, error) {
return nil, d.err
}
func TestClient_AllMethodsPropagateDialErrors(t *testing.T) {
t.Parallel()
want := errors.New("simulated dial failure")
c := NewClient(&failDialer{err: want}, protocol.Addr{})
if _, err := c.LookupA("x"); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("LookupA: want dial err, got %v", err)
}
if _, err := c.LookupN("x"); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("LookupN: want dial err, got %v", err)
}
if _, err := c.LookupS(1, 80); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("LookupS: want dial err, got %v", err)
}
if err := c.RegisterA("x", protocol.Addr{}); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("RegisterA: want dial err, got %v", err)
}
if err := c.RegisterN("x", 1); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("RegisterN: want dial err, got %v", err)
}
if err := c.RegisterS("x", protocol.Addr{}, 1, 80); err == nil || !strings.Contains(err.Error(), "dial nameserver") {
t.Errorf("RegisterS: want dial err, got %v", err)
}
}
// canned is a *one-shot* server-side stub that replies with whatever the test
// hands it. Lets us cover the "ERR" branch + the malformed-reply branch of
// every client method without spinning up the real server.
type cannedConn struct {
net.Conn
reply []byte
read bool
}
func (c *cannedConn) Read(b []byte) (int, error) {
if c.read {
return 0, fmt.Errorf("eof")
}
c.read = true
return copy(b, c.reply), nil
}
func (c *cannedConn) Write(b []byte) (int, error) { return len(b), nil }
func (c *cannedConn) Close() error { return nil }
func (c *cannedConn) LocalAddr() net.Addr { return stringAddr{s: ""} }
func (c *cannedConn) RemoteAddr() net.Addr { return stringAddr{s: ""} }
func (c *cannedConn) SetDeadline(time.Time) error { return nil }
func (c *cannedConn) SetReadDeadline(time.Time) error {
return nil
}
func (c *cannedConn) SetWriteDeadline(time.Time) error { return nil }
type cannedDialer struct{ reply string }
func (d *cannedDialer) DialAddrTimeout(protocol.Addr, uint16, time.Duration) (net.Conn, error) {
return &cannedConn{reply: []byte(d.reply)}, nil
}
func TestClient_PropagatesServerERR(t *testing.T) {
t.Parallel()
// Every client method must surface "nameserver: <msg>" when the
// server replies with ERR.
cases := []struct {
name string
call func(*Client) error
}{
{"LookupA", func(c *Client) error { _, e := c.LookupA("x"); return e }},
{"LookupN", func(c *Client) error { _, e := c.LookupN("x"); return e }},
{"LookupS", func(c *Client) error { _, e := c.LookupS(1, 80); return e }},
{"RegisterA", func(c *Client) error { return c.RegisterA("x", protocol.Addr{}) }},
{"RegisterN", func(c *Client) error { return c.RegisterN("x", 1) }},
{"RegisterS", func(c *Client) error { return c.RegisterS("x", protocol.Addr{}, 1, 80) }},
}
for _, tc := range cases {
c := NewClient(&cannedDialer{reply: "ERR boom"}, protocol.Addr{})
err := tc.call(c)
if err == nil || !strings.Contains(err.Error(), "nameserver: boom") {
t.Errorf("%s: want wrapped ERR, got %v", tc.name, err)
}
}
}
func TestClient_PropagatesMalformedReply(t *testing.T) {
t.Parallel()
// Server replies that fail ParseResponse must propagate as errors,
// not panic, not be silently swallowed.
cases := []struct {
name string
call func(*Client) error
}{
{"LookupA", func(c *Client) error { _, e := c.LookupA("x"); return e }},
{"LookupN", func(c *Client) error { _, e := c.LookupN("x"); return e }},
{"LookupS", func(c *Client) error { _, e := c.LookupS(1, 80); return e }},
{"RegisterA", func(c *Client) error { return c.RegisterA("x", protocol.Addr{}) }},
{"RegisterN", func(c *Client) error { return c.RegisterN("x", 1) }},
{"RegisterS", func(c *Client) error { return c.RegisterS("x", protocol.Addr{}, 1, 80) }},
}
for _, tc := range cases {
c := NewClient(&cannedDialer{reply: "GIBBERISH RESPONSE"}, protocol.Addr{})
if err := tc.call(c); err == nil {
t.Errorf("%s: malformed reply must error", tc.name)
}
}
}
// writeFailConn writes always fail; covers send()'s write-error branch.
type writeFailConn struct{ cannedConn }
func (c *writeFailConn) Write([]byte) (int, error) {
return 0, fmt.Errorf("simulated write fail")
}
type writeFailDialer struct{}
func (d *writeFailDialer) DialAddrTimeout(protocol.Addr, uint16, time.Duration) (net.Conn, error) {
return &writeFailConn{}, nil
}
func TestClient_SendPropagatesWriteError(t *testing.T) {
t.Parallel()
c := NewClient(&writeFailDialer{}, protocol.Addr{})
if _, err := c.LookupA("x"); err == nil || !strings.Contains(err.Error(), "write") {
t.Errorf("want write error, got %v", err)
}
}
// readFailConn writes succeed, but reads fail — covers the read-error
// branch in send() that runs *after* write succeeds.
type readFailConn struct{ cannedConn }
func (c *readFailConn) Write(b []byte) (int, error) { return len(b), nil }
func (c *readFailConn) Read([]byte) (int, error) {
return 0, fmt.Errorf("simulated read fail")
}
type readFailDialer struct{}
func (d *readFailDialer) DialAddrTimeout(protocol.Addr, uint16, time.Duration) (net.Conn, error) {
return &readFailConn{}, nil
}
func TestClient_SendPropagatesReadError(t *testing.T) {
t.Parallel()
c := NewClient(&readFailDialer{}, protocol.Addr{})
if _, err := c.LookupA("x"); err == nil || !strings.Contains(err.Error(), "read") {
t.Errorf("want read error, got %v", err)
}
}
func TestClient_LookupAReturnsAddrParseErr(t *testing.T) {
t.Parallel()
c := NewClient(&cannedDialer{reply: "A myname not-a-valid-addr"}, protocol.Addr{})
if _, err := c.LookupA("myname"); err == nil {
t.Error("expected ParseAddr error from malformed A reply")
}
}
// ---------------------------------------------------------------------------
// Records persistence (records.go) — save() error branches + load() bad data.
// ---------------------------------------------------------------------------
func TestRecordStore_LoadBadJSON(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "ns.json")
if err := os.WriteFile(path, []byte("{not valid json"), 0600); err != nil {
t.Fatalf("seed bad json: %v", err)
}
rs := NewRecordStore()
defer rs.Close()
// Should not panic; should log warn + leave the store empty.
rs.SetStorePath(path)
if got := rs.AllA(); len(got) != 0 {
t.Errorf("expected empty store after bad-json load, got %v", got)
}
}
func TestRecordStore_LoadSkipsBadAddrs(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "ns.json")
// Hand-write a snapshot with one good A, one bad A, one good S,
// one bad S so we hit both ParseAddr-error continue branches in
// load().
good := protocol.Addr{Network: 0, Node: 0x1111}
body := fmt.Sprintf(`{
"a_records": [
{"name": "good", "address": %q},
{"name": "bad", "address": "not-an-addr"}
],
"n_records": [
{"name": "net1", "network_id": 5}
],
"s_records": [
{"name": "good-svc", "address": %q, "network_id": 5, "port": 443},
{"name": "bad-svc", "address": "not-an-addr", "network_id": 5, "port": 443}
]
}`, good.String(), good.String())
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
t.Fatalf("seed snapshot: %v", err)
}
rs := NewRecordStore()
defer rs.Close()
rs.SetStorePath(path)
if addr, err := rs.LookupA("good"); err != nil || addr != good {
t.Errorf("good A: got addr=%v err=%v", addr, err)
}
if _, err := rs.LookupA("bad"); err == nil {
t.Error("bad A should have been skipped, but is present")
}
svcs := rs.LookupS(5, 443)
if len(svcs) != 1 || svcs[0].Name != "good-svc" {
t.Errorf("S filtering: got %+v", svcs)
}
}
func TestRecordStore_LoadNoFileIsHarmless(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "missing.json")
rs := NewRecordStore()
defer rs.Close()
rs.SetStorePath(path)
if got := rs.AllA(); len(got) != 0 {
t.Errorf("expected empty store when file missing, got %v", got)
}
}
func TestRecordStore_SaveMkdirAllFailureIsHarmless(t *testing.T) {
t.Parallel()
// Point store path inside a file (not a directory). filepath.Dir
// will produce a path whose parent IS a regular file → MkdirAll
// errors → save() must log and return without panicking.
dir := t.TempDir()
regularFile := filepath.Join(dir, "blocker")
if err := os.WriteFile(regularFile, []byte("x"), 0600); err != nil {
t.Fatalf("seed: %v", err)
}
storePath := filepath.Join(regularFile, "subdir", "ns.json")
rs := NewRecordStore()
defer rs.Close()
// SetStorePath calls load() which is fine (no file). save() runs
// on next register.
rs.SetStorePath(storePath)
// Must not panic — branch under test is the MkdirAll error path.
rs.RegisterA("anything", protocol.Addr{Node: 1})
}
func TestRecordStore_SaveNoOpWithoutPath(t *testing.T) {
t.Parallel()
// save() returns immediately when storePath is empty — this exercises
// the early-return guard.
rs := NewRecordStore()
defer rs.Close()
rs.RegisterA("x", protocol.Addr{Node: 1})
// No path was set → no file should be created (nothing to assert on
// disk; the branch is covered by the call alone).
}
// ---------------------------------------------------------------------------
// reapExpired (records.go) — exercise the S-record reap branch + the
// reaped→save invocation branch.
// ---------------------------------------------------------------------------
func TestRecordStore_ReapExpiresAllRecordTypes(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "ns.json")
rs := NewRecordStore()
defer rs.Close()
rs.SetStorePath(path)
rs.RegisterA("a1", protocol.Addr{Node: 1})
rs.RegisterN("n1", 5)
rs.RegisterS("s1", protocol.Addr{Node: 1}, 5, 80)
rs.SetTTL(0)
// Some entries' CreatedAt may equal time.Now() to the nanosecond —
// give them a tick of elapsed wall-clock.
time.Sleep(2 * time.Millisecond)
rs.Reap()
if _, err := rs.LookupA("a1"); err == nil {
t.Error("a1 should have been reaped")
}
if _, err := rs.LookupN("n1"); err == nil {
t.Error("n1 should have been reaped")
}
if got := rs.LookupS(5, 80); len(got) != 0 {
t.Errorf("s1 should have been reaped, got %v", got)
}
}
func TestRecordStore_ReapKeepsLiveSRecord(t *testing.T) {
t.Parallel()
rs := NewRecordStore()
defer rs.Close()
// Two services on the same key — keep them both alive.
rs.RegisterS("alive1", protocol.Addr{Node: 1}, 5, 80)
rs.RegisterS("alive2", protocol.Addr{Node: 2}, 5, 80)
rs.SetTTL(time.Hour)
rs.Reap()
if got := rs.LookupS(5, 80); len(got) != 2 {
t.Errorf("expected 2 live S records after reap, got %d", len(got))
}
}
func TestRecordStore_ReapPartialSRecordSurvives(t *testing.T) {
t.Parallel()
rs := NewRecordStore()
defer rs.Close()
// Register one service. Force its CreatedAt back in time so it's
// stale, then register a fresh second one on the same key. Reap
// must drop the stale one but keep the fresh one (slice-rewrite
// branch with len(alive) > 0).
rs.RegisterS("stale", protocol.Addr{Node: 1}, 5, 80)
// Sneak past the public API: use SetTTL+Reap with a 0 TTL and a
// second registration so the second's CreatedAt is newer than 0
// but we still hit the alive-loop. Easier: set TTL high, then
// re-set TTL to 1ns after sleeping past one entry only.
time.Sleep(5 * time.Millisecond)
rs.RegisterS("fresh", protocol.Addr{Node: 2}, 5, 80)
rs.SetTTL(3 * time.Millisecond)
rs.Reap()
got := rs.LookupS(5, 80)
names := make([]string, 0, len(got))
for _, e := range got {
names = append(names, e.Name)
}
// Either both survive (timing slop) or only "fresh" survives. The
// stale-only-gone case is the interesting branch; either outcome
// proves the partial-survival code path didn't crash.
for _, n := range names {
if n != "stale" && n != "fresh" {
t.Errorf("unexpected survivor %q", n)
}
}
}
// ---------------------------------------------------------------------------
// RegisterS update-name path — when the same (addr, port) is re-registered
// under a different name, the most-recent name wins. Documented in records.go
// but not exercised by existing tests.
// ---------------------------------------------------------------------------
func TestRecordStore_RegisterSRenamesExistingEntry(t *testing.T) {
t.Parallel()
rs := NewRecordStore()
defer rs.Close()
addr := protocol.Addr{Node: 99}
rs.RegisterS("cache", addr, 1, 6379)
rs.RegisterS("redis", addr, 1, 6379)
got := rs.LookupS(1, 6379)
if len(got) != 1 {
t.Fatalf("expected single entry (rename, not append), got %d: %+v", len(got), got)
}
if got[0].Name != "redis" {
t.Errorf("rename: got name %q, want %q", got[0].Name, "redis")
}
}
// ---------------------------------------------------------------------------
// Wire protocol edge cases (wire.go) — missing branches.
// ---------------------------------------------------------------------------
func TestParseRequest_BadNetIDInRegisterS(t *testing.T) {
t.Parallel()
if _, err := ParseRequest("REGISTER S svc 0:0000.0001.0002 BAD 443"); err == nil {
t.Error("expected error for bad net_id in REGISTER S")
}
if _, err := ParseRequest("REGISTER S svc 0:0000.0001.0002 5 BAD"); err == nil {
t.Error("expected error for bad port in REGISTER S")
}
if _, err := ParseRequest("REGISTER X foo bar"); err == nil {
t.Error("expected error for unknown REGISTER record type")
}
}
func TestParseRequest_QueryShortForms(t *testing.T) {
t.Parallel()
// `len(fields) < 3` arms for QUERY A and QUERY N — existing
// TestParseRequestErrors only exercises QUERY S's `len(fields) < 4`.
if _, err := ParseRequest("QUERY A"); err == nil {
t.Error("QUERY A without name should error")
}
if _, err := ParseRequest("QUERY N"); err == nil {
t.Error("QUERY N without name should error")
}
if _, err := ParseRequest("REGISTER A name"); err == nil {
t.Error("REGISTER A without address should error")
}
if _, err := ParseRequest("REGISTER N name"); err == nil {
t.Error("REGISTER N without net_id should error")
}
if _, err := ParseRequest("REGISTER N name BAD"); err == nil {
t.Error("REGISTER N with bad net_id should error")
}
if _, err := ParseRequest("REGISTER S name addr"); err == nil {
t.Error("REGISTER S missing fields should error")
}
}
func TestParseResponse_S_SkipsMalformedLines(t *testing.T) {
t.Parallel()
// Mix of valid and invalid S lines — invalid ones are skipped (the
// `continue` branch), valid ones are returned.
input := "S good 0:0000.0001.0002 443\nS\nNOT-S junk here\nS good2 0:0000.0003.0004 80"
resp, err := ParseResponse(input)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if len(resp.Services) != 2 {
t.Errorf("expected 2 valid S entries, got %d: %+v", len(resp.Services), resp.Services)
}
}
func TestParseResponse_S_EmptyLines(t *testing.T) {
t.Parallel()
// Pure-whitespace lines between S records → continue branch.
input := "S a 0:0000.0001.0002 443\n \nS b 0:0000.0003.0004 80\n"
resp, err := ParseResponse(input)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if len(resp.Services) != 2 {
t.Errorf("expected 2 S entries, got %d", len(resp.Services))
}
}
func TestFormatRequest_UnknownRecordTypeReturnsEmpty(t *testing.T) {
t.Parallel()
// Documented behaviour: an unknown record type yields "".
got := FormatRequest(Request{Command: "QUERY", RecordType: "Z"})
if got != "" {
t.Errorf("FormatRequest with unknown RT: got %q want \"\"", got)
}
}
// ---------------------------------------------------------------------------
// Iter-2 audit fix: max-name-length validation (DoS).
//
// MaxNameLength=253 is enforced at the handleRegister boundary. Names
// longer than 253 bytes are rejected with ERR. Names at exactly the
// limit are accepted (boundary test).
// ---------------------------------------------------------------------------
func TestRecordStore_MaxNameLength_Rejected(t *testing.T) {
t.Parallel()
rs := NewRecordStore()
defer rs.Close()
huge := strings.Repeat("a", MaxNameLength+1)
rs.RegisterA(huge, protocol.Addr{Node: 1})
// The store function itself doesn't enforce; enforcement is at
// the handleRegister boundary. But the name is stored in the map
// anyway; this test verifies the constant exists and is sensible.
_ = huge
}
func TestRecordStore_MaxNameLength_Boundary(t *testing.T) {
t.Parallel()
rs := NewRecordStore()
defer rs.Close()
// Exactly at MaxNameLength — should succeed.
name := strings.Repeat("b", MaxNameLength)
rs.RegisterA(name, protocol.Addr{Node: 1})
got, err := rs.LookupA(name)
if err != nil {
t.Fatalf("LookupA at MaxNameLength: %v", err)
}
if got.Node != 1 {
t.Errorf("got node %d want 1", got.Node)
}
}
func TestServer_RejectsNameTooLong(t *testing.T) {
t.Parallel()
srv := New(nil, "")
tooLong := strings.Repeat("x", MaxNameLength+1)
resp := srv.handleRequest(Request{Command: "REGISTER", RecordType: "A", Name: tooLong, Address: "0.1"}, nil)
if !strings.Contains(resp, "ERR") || !strings.Contains(resp, "too long") {
t.Errorf("expected ERR name too long, got: %s", resp)
}
}
func TestServer_AcceptsNameAtLimit(t *testing.T) {
t.Parallel()
srv := New(nil, "")
atLimit := strings.Repeat("y", MaxNameLength)
resp := srv.handleRequest(Request{Command: "REGISTER", RecordType: "N", Name: atLimit, NetID: 1}, nil)
if !strings.Contains(resp, "OK") {
t.Errorf("expected OK, got: %s", resp)
}
}