aboutsummaryrefslogtreecommitdiff
path: root/hotline/server_test.go
blob: 574aae790d697147f9ede87983ca1b4934e65960 (plain)
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
package hotline

import (
	"bytes"
	"context"
	"encoding/binary"
	"fmt"
	"github.com/stretchr/testify/assert"
	"io"
	"log/slog"
	"os"
	"strings"
	"testing"
	"time"
)

type mockReadWriter struct {
	RBuf bytes.Buffer
	WBuf *bytes.Buffer
}

func (mrw mockReadWriter) Read(p []byte) (n int, err error) {
	return mrw.RBuf.Read(p)
}

func (mrw mockReadWriter) Write(p []byte) (n int, err error) {
	return mrw.WBuf.Write(p)
}

func TestServer_handleFileTransfer(t *testing.T) {
	type fields struct {
		ThreadedNews    *ThreadedNews
		FileTransferMgr FileTransferMgr
		Config          Config
		ConfigDir       string
		Stats           *Stats
		Logger          *slog.Logger
		FS              FileStore
	}
	type args struct {
		ctx context.Context
		rwc io.ReadWriter
	}
	tests := []struct {
		name     string
		fields   fields
		args     args
		wantErr  assert.ErrorAssertionFunc
		wantDump string
	}{
		{
			name: "with invalid protocol",
			args: args{
				ctx: func() context.Context {
					ctx := context.Background()
					ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
					return ctx
				}(),
				rwc: func() io.ReadWriter {
					mrw := mockReadWriter{}
					mrw.WBuf = &bytes.Buffer{}
					mrw.RBuf.Write(
						[]byte{
							0, 0, 0, 0,
							0, 0, 0, 5,
							0, 0, 0x01, 0,
							0, 0, 0, 0,
						},
					)
					return mrw
				}(),
			},
			wantErr: assert.Error,
		},
		{
			name: "with invalid transfer Type",
			fields: fields{
				FileTransferMgr: NewMemFileTransferMgr(),
			},
			args: args{
				ctx: func() context.Context {
					ctx := context.Background()
					ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
					return ctx
				}(),
				rwc: func() io.ReadWriter {
					mrw := mockReadWriter{}
					mrw.WBuf = &bytes.Buffer{}
					mrw.RBuf.Write(
						[]byte{
							0x48, 0x54, 0x58, 0x46,
							0, 0, 0, 5,
							0, 0, 0x01, 0,
							0, 0, 0, 0,
						},
					)
					return mrw
				}(),
			},
			wantErr: assert.Error,
		},
		{
			name: "file download",
			fields: fields{
				FS:     &OSFileStore{},
				Logger: NewTestLogger(),
				Stats:  NewStats(),
				FileTransferMgr: &MemFileTransferMgr{
					fileTransfers: map[FileTransferID]*FileTransfer{
						{0, 0, 0, 5}: {
							RefNum:   [4]byte{0, 0, 0, 5},
							Type:     FileDownload,
							FileName: []byte("testfile-8b"),
							FilePath: []byte{},
							FileRoot: func() string {
								path, _ := os.Getwd()
								return path + "/test/config/Files"
							}(),
							ClientConn: &ClientConn{
								Account: &Account{
									Login: "foo",
								},
								ClientFileTransferMgr: ClientFileTransferMgr{
									transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{
										FileDownload: {
											[4]byte{0, 0, 0, 5}: &FileTransfer{},
										},
									},
								},
							},
							bytesSentCounter: &WriteCounter{},
						},
					},
				},
			},
			args: args{
				ctx: func() context.Context {
					ctx := context.Background()
					ctx = context.WithValue(ctx, contextKeyReq, requestCtx{})
					return ctx
				}(),
				rwc: func() io.ReadWriter {
					mrw := mockReadWriter{}
					mrw.WBuf = &bytes.Buffer{}
					mrw.RBuf.Write(
						[]byte{
							0x48, 0x54, 0x58, 0x46,
							0, 0, 0, 5,
							0, 0, 0x01, 0,
							0, 0, 0, 0,
						},
					)
					return mrw
				}(),
			},
			wantErr: assert.NoError,
			wantDump: `00000000  46 49 4c 50 00 01 00 00  00 00 00 00 00 00 00 00  |FILP............|
00000010  00 00 00 00 00 00 00 02  49 4e 46 4f 00 00 00 00  |........INFO....|
00000020  00 00 00 00 00 00 00 55  41 4d 41 43 54 45 58 54  |.......UAMACTEXT|
00000030  54 54 58 54 00 00 00 00  00 00 01 00 00 00 00 00  |TTXT............|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 0b  |................|
00000070  74 65 73 74 66 69 6c 65  2d 38 62 00 00 44 41 54  |testfile-8b..DAT|
00000080  41 00 00 00 00 00 00 00  00 00 00 00 08 7c 39 e0  |A............|9.|
00000090  bc 64 e2 cd de 4d 41 43  52 00 00 00 00 00 00 00  |.d...MACR.......|
000000a0  00 00 00 00 00                                    |.....|
`,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &Server{
				FileTransferMgr: tt.fields.FileTransferMgr,
				Config:          tt.fields.Config,
				Logger:          tt.fields.Logger,
				Stats:           tt.fields.Stats,
				FS:              tt.fields.FS,
			}

			tt.wantErr(t, s.handleFileTransfer(tt.args.ctx, tt.args.rwc), fmt.Sprintf("handleFileTransfer(%v, %v)", tt.args.ctx, tt.args.rwc))

			assertTransferBytesEqual(t, tt.wantDump, tt.args.rwc.(mockReadWriter).WBuf.Bytes())
		})
	}
}

func TestParseTrackerPassword(t *testing.T) {
	tests := []struct {
		name         string
		trackerAddr  string
		wantPassword string
	}{
		{
			name:         "tracker address with password",
			trackerAddr:  "tracker.example.com:5500:mypassword",
			wantPassword: "mypassword",
		},
		{
			name:         "tracker address without password",
			trackerAddr:  "tracker.example.com:5500",
			wantPassword: "",
		},
		{
			name:         "tracker address with empty password",
			trackerAddr:  "tracker.example.com:5500:",
			wantPassword: "",
		},
		{
			name:         "tracker address with password containing special characters",
			trackerAddr:  "tracker.example.com:5500:pass@word#123",
			wantPassword: "pass@word#123",
		},
		{
			name:         "tracker address with password containing colons",
			trackerAddr:  "tracker.example.com:5500:pass:word:123",
			wantPassword: "pass:word:123",
		},
		{
			name:         "IPv4 address with password",
			trackerAddr:  "192.168.1.100:5500:secret",
			wantPassword: "secret",
		},
		{
			name:         "IPv4 address without password",
			trackerAddr:  "192.168.1.100:5500",
			wantPassword: "",
		},
		{
			name:         "malformed address - no port",
			trackerAddr:  "tracker.example.com",
			wantPassword: "",
		},
		{
			name:         "malformed address - empty string",
			trackerAddr:  "",
			wantPassword: "",
		},
		{
			name:         "malformed address - only colons",
			trackerAddr:  ":::",
			wantPassword: ":",
		},
		{
			name:         "IPv6 address handling (edge case - not properly supported)",
			trackerAddr:  "[::1]:5500:password",
			wantPassword: "1]:5500:password", // IPv6 addresses aren't properly handled by simple colon splitting
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := parseTrackerPassword(tt.trackerAddr)
			assert.Equal(t, tt.wantPassword, got)
		})
	}
}

// MockTrackerRegistrar is a mock implementation of TrackerRegistrar for testing
type MockTrackerRegistrar struct {
	RegisterCalls []RegisterCall
	RegisterFunc  func(tracker string, registration *TrackerRegistration) error
}

type RegisterCall struct {
	Tracker      string
	Registration *TrackerRegistration
}

func (m *MockTrackerRegistrar) Register(tracker string, registration *TrackerRegistration) error {
	// Record the call
	m.RegisterCalls = append(m.RegisterCalls, RegisterCall{
		Tracker:      tracker,
		Registration: registration,
	})

	// Use custom function if provided, otherwise return nil (success)
	if m.RegisterFunc != nil {
		return m.RegisterFunc(tracker, registration)
	}
	return nil
}

func (m *MockTrackerRegistrar) Reset() {
	m.RegisterCalls = nil
	m.RegisterFunc = nil
}

func TestServer_registerWithTrackers(t *testing.T) {
	tests := []struct {
		name                      string
		config                    Config
		wantImmediateRegistration bool
		wantTrackerCalls          []string
		mockRegisterFunc          func(tracker string, registration *TrackerRegistration) error
		expectError               bool
	}{
		{
			name: "disabled tracker registration",
			config: Config{
				EnableTrackerRegistration: false,
				Trackers:                  []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
			},
			wantImmediateRegistration: false,
			wantTrackerCalls:          []string{},
		},
		{
			name: "enabled tracker registration with multiple trackers",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			wantImmediateRegistration: true,
			wantTrackerCalls:          []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
		},
		{
			name: "enabled tracker registration with empty tracker list",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			wantImmediateRegistration: true,
			wantTrackerCalls:          []string{},
		},
		{
			name: "tracker registration with network errors",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker1.example.com:5500"},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			wantImmediateRegistration: true,
			wantTrackerCalls:          []string{"tracker1.example.com:5500"},
			mockRegisterFunc: func(tracker string, registration *TrackerRegistration) error {
				return assert.AnError // Simulate network error
			},
			expectError: false, // Errors are logged but don't stop the function
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Create mock registrar
			mockRegistrar := &MockTrackerRegistrar{
				RegisterFunc: tt.mockRegisterFunc,
			}

			// Create server with mock registrar
			server, err := NewServer(
				WithConfig(tt.config),
				WithLogger(NewTestLogger()),
				WithTrackerRegistrar(mockRegistrar),
			)
			assert.NoError(t, err)

			// Create a context that we can cancel
			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			// Start the registerWithTrackers function in a goroutine
			done := make(chan struct{})
			go func() {
				defer close(done)
				server.registerWithTrackers(ctx)
			}()

			// Give it a moment to do the immediate registration
			time.Sleep(100 * time.Millisecond)

			// Cancel the context to stop the goroutine
			cancel()

			// Wait for the goroutine to finish (should be quick after cancellation)
			select {
			case <-done:
				// Success
			case <-time.After(1 * time.Second):
				t.Fatal("registerWithTrackers did not exit after context cancellation")
			}

			// Verify the calls made to the mock registrar
			assert.Len(t, mockRegistrar.RegisterCalls, len(tt.wantTrackerCalls))

			for i, expectedTracker := range tt.wantTrackerCalls {
				if i < len(mockRegistrar.RegisterCalls) {
					call := mockRegistrar.RegisterCalls[i]
					assert.Equal(t, expectedTracker, call.Tracker)
					assert.Equal(t, tt.config.Name, call.Registration.Name)
					assert.Equal(t, tt.config.Description, call.Registration.Description)
					assert.Equal(t, parseTrackerPassword(expectedTracker), call.Registration.Password)
				}
			}
		})
	}
}

func TestServer_registerWithTrackers_ContextCancellation(t *testing.T) {
	tests := []struct {
		name           string
		cancelAfter    time.Duration
		expectedCalls  int // Number of expected registration calls before cancellation
		trackerCount   int
	}{
		{
			name:         "immediate cancellation",
			cancelAfter:  10 * time.Millisecond,
			expectedCalls: 2, // Should complete immediate registration
			trackerCount:  2,
		},
		{
			name:         "cancellation after first ticker",
			cancelAfter:  100 * time.Millisecond,
			expectedCalls: 2, // Should only do immediate registration within 100ms
			trackerCount:  2,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockRegistrar := &MockTrackerRegistrar{}
			config := Config{
				EnableTrackerRegistration: true,
				Trackers:                  make([]string, tt.trackerCount),
				Name:                      "Test Server",
				Description:               "Test Description",
			}

			// Fill trackers array
			for i := 0; i < tt.trackerCount; i++ {
				config.Trackers[i] = fmt.Sprintf("tracker%d.example.com:5500", i+1)
			}

			server, err := NewServer(
				WithConfig(config),
				WithLogger(NewTestLogger()),
				WithTrackerRegistrar(mockRegistrar),
			)
			assert.NoError(t, err)

			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			done := make(chan struct{})
			go func() {
				defer close(done)
				server.registerWithTrackers(ctx)
			}()

			// Wait for the specified time then cancel
			time.Sleep(tt.cancelAfter)
			cancel()

			// Wait for graceful shutdown
			select {
			case <-done:
				// Success
			case <-time.After(1 * time.Second):
				t.Fatal("registerWithTrackers did not exit after context cancellation")
			}

			// Verify that the function respects context cancellation
			assert.Equal(t, tt.expectedCalls, len(mockRegistrar.RegisterCalls))
		})
	}
}

func TestServer_registerWithTrackers_PeriodicRegistration(t *testing.T) {
	t.Skip("Skipping timing-sensitive test - would take 5+ minutes to run reliably")
	
	// This test would verify that periodic re-registration happens every trackerUpdateFrequency seconds
	// but it's impractical to run in normal test suites due to the 300-second interval
	
	mockRegistrar := &MockTrackerRegistrar{}
	config := Config{
		EnableTrackerRegistration: true,
		Trackers:                  []string{"tracker1.example.com:5500"},
		Name:                      "Test Server",
		Description:               "Test Description",
	}

	server, err := NewServer(
		WithConfig(config),
		WithLogger(NewTestLogger()),
		WithTrackerRegistrar(mockRegistrar),
	)
	assert.NoError(t, err)

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	done := make(chan struct{})
	go func() {
		defer close(done)
		server.registerWithTrackers(ctx)
	}()

	// Wait for timeout or completion
	<-ctx.Done()

	// Should have done immediate registration only (1 call) in 10 seconds
	// since trackerUpdateFrequency is 300 seconds
	assert.Equal(t, 1, len(mockRegistrar.RegisterCalls))
}

func TestServer_registerWithTrackers_ErrorHandling(t *testing.T) {
	tests := []struct {
		name             string
		trackers         []string
		mockRegisterFunc func(tracker string, registration *TrackerRegistration) error
		expectPanic      bool
	}{
		{
			name:     "handles network errors gracefully",
			trackers: []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
			mockRegisterFunc: func(tracker string, registration *TrackerRegistration) error {
				if tracker == "tracker1.example.com:5500" {
					return fmt.Errorf("network error: connection refused")
				}
				return nil // Second tracker succeeds
			},
			expectPanic: false,
		},
		{
			name:     "handles all trackers failing",
			trackers: []string{"tracker1.example.com:5500", "tracker2.example.com:5500"},
			mockRegisterFunc: func(tracker string, registration *TrackerRegistration) error {
				return fmt.Errorf("network error")
			},
			expectPanic: false,
		},
		{
			name:     "handles empty tracker addresses",
			trackers: []string{"", "valid.tracker.com:5500", ""},
			mockRegisterFunc: func(tracker string, registration *TrackerRegistration) error {
				if tracker == "" {
					return fmt.Errorf("invalid tracker address")
				}
				return nil
			},
			expectPanic: false,
		},
		{
			name:     "handles malformed tracker addresses",
			trackers: []string{"invalid-address", "another:invalid", "valid.tracker.com:5500:password"},
			mockRegisterFunc: func(tracker string, registration *TrackerRegistration) error {
				return nil // Accept all for this test
			},
			expectPanic: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockRegistrar := &MockTrackerRegistrar{
				RegisterFunc: tt.mockRegisterFunc,
			}

			config := Config{
				EnableTrackerRegistration: true,
				Trackers:                  tt.trackers,
				Name:                      "Test Server",
				Description:               "Test Description",
			}

			server, err := NewServer(
				WithConfig(config),
				WithLogger(NewTestLogger()),
				WithTrackerRegistrar(mockRegistrar),
			)
			assert.NoError(t, err)

			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			if tt.expectPanic {
				assert.Panics(t, func() {
					server.registerWithTrackers(ctx)
				})
				return
			}

			done := make(chan struct{})
			go func() {
				defer close(done)
				server.registerWithTrackers(ctx)
			}()

			// Give it time to process
			time.Sleep(50 * time.Millisecond)
			cancel()

			select {
			case <-done:
				// Success - function completed without panicking
			case <-time.After(1 * time.Second):
				t.Fatal("registerWithTrackers did not exit after context cancellation")
			}

			// Verify all trackers were attempted
			assert.Equal(t, len(tt.trackers), len(mockRegistrar.RegisterCalls))
		})
	}
}

func TestServer_registerWithTrackers_EdgeCases(t *testing.T) {
	tests := []struct {
		name           string
		config         Config
		expectedCalls  int
		validateResult func(t *testing.T, calls []RegisterCall)
	}{
		{
			name: "server with zero port",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker.example.com:5500"},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			expectedCalls: 1,
			validateResult: func(t *testing.T, calls []RegisterCall) {
				assert.Equal(t, uint16(0), binary.BigEndian.Uint16(calls[0].Registration.Port[:]))
			},
		},
		{
			name: "server with very long name and description",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker.example.com:5500"},
				Name:                      strings.Repeat("A", 255), // Max uint8 length
				Description:               strings.Repeat("B", 255),
			},
			expectedCalls: 1,
			validateResult: func(t *testing.T, calls []RegisterCall) {
				assert.Equal(t, strings.Repeat("A", 255), calls[0].Registration.Name)
				assert.Equal(t, strings.Repeat("B", 255), calls[0].Registration.Description)
			},
		},
		{
			name: "empty server name and description",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker.example.com:5500"},
				Name:                      "",
				Description:               "",
			},
			expectedCalls: 1,
			validateResult: func(t *testing.T, calls []RegisterCall) {
				assert.Equal(t, "", calls[0].Registration.Name)
				assert.Equal(t, "", calls[0].Registration.Description)
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockRegistrar := &MockTrackerRegistrar{}

			server, err := NewServer(
				WithConfig(tt.config),
				WithLogger(NewTestLogger()),
				WithTrackerRegistrar(mockRegistrar),
			)
			assert.NoError(t, err)

			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			done := make(chan struct{})
			go func() {
				defer close(done)
				server.registerWithTrackers(ctx)
			}()

			time.Sleep(50 * time.Millisecond)
			cancel()

			select {
			case <-done:
				// Success
			case <-time.After(1 * time.Second):
				t.Fatal("registerWithTrackers did not exit after context cancellation")
			}

			assert.Equal(t, tt.expectedCalls, len(mockRegistrar.RegisterCalls))
			if tt.validateResult != nil {
				tt.validateResult(t, mockRegistrar.RegisterCalls)
			}
		})
	}
}

func TestServer_registerWithAllTrackers(t *testing.T) {
	tests := []struct {
		name                      string
		config                    Config
		expectRegistrationAttempt bool
		expectedTrackerCalls      []string
	}{
		{
			name: "disabled tracker registration",
			config: Config{
				EnableTrackerRegistration: false,
				Trackers:                  []string{"tracker1.example.com:5500"},
			},
			expectRegistrationAttempt: false,
			expectedTrackerCalls:      []string{},
		},
		{
			name: "enabled tracker registration with multiple trackers",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			expectRegistrationAttempt: true,
			expectedTrackerCalls:      []string{"tracker1.example.com:5500", "tracker2.example.com:5500:password"},
		},
		{
			name: "enabled tracker registration with empty tracker list",
			config: Config{
				EnableTrackerRegistration: true,
				Trackers:                  []string{},
				Name:                      "Test Server",
				Description:               "Test Description",
			},
			expectRegistrationAttempt: true,
			expectedTrackerCalls:      []string{},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockRegistrar := &MockTrackerRegistrar{}

			server, err := NewServer(
				WithConfig(tt.config),
				WithLogger(NewTestLogger()),
				WithTrackerRegistrar(mockRegistrar),
			)
			assert.NoError(t, err)

			// Call the extracted function directly
			server.registerWithAllTrackers()

			// Verify the expected number of calls
			assert.Equal(t, len(tt.expectedTrackerCalls), len(mockRegistrar.RegisterCalls))

			// Verify each call
			for i, expectedTracker := range tt.expectedTrackerCalls {
				if i < len(mockRegistrar.RegisterCalls) {
					call := mockRegistrar.RegisterCalls[i]
					assert.Equal(t, expectedTracker, call.Tracker)
					assert.Equal(t, tt.config.Name, call.Registration.Name)
					assert.Equal(t, tt.config.Description, call.Registration.Description)
					assert.Equal(t, parseTrackerPassword(expectedTracker), call.Registration.Password)
				}
			}
		})
	}
}