Skip to content

Commit 5e9055e

Browse files
committed
test: add unit tests for localhost and closer
1 parent ef29c27 commit 5e9055e

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

closer_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// CGo binding for Avahi
2+
//
3+
// Copyright (C) 2024 and up by Alexander Pevzner ([email protected])
4+
// See LICENSE for license terms and conditions
5+
//
6+
// Closers tests
7+
//
8+
//go:build linux || freebsd
9+
10+
package avahi
11+
12+
import "testing"
13+
14+
type mockCloser struct {
15+
closed bool
16+
}
17+
18+
func (m *mockCloser) Close() {
19+
m.closed = true
20+
}
21+
22+
func TestClosers(t *testing.T) {
23+
t.Run("Lifecycle", func(t *testing.T) {
24+
var set closers
25+
set.init()
26+
27+
c1 := &mockCloser{}
28+
c2 := &mockCloser{}
29+
30+
set.add(c1)
31+
set.add(c2)
32+
33+
if len(set) != 2 {
34+
t.Errorf("expected 2 closers, got %d", len(set))
35+
}
36+
37+
set.del(c1)
38+
if len(set) != 1 {
39+
t.Errorf("expected 1 closer after deletion, got %d", len(set))
40+
}
41+
42+
set.close()
43+
if !c2.closed {
44+
t.Error("c2 should have been closed")
45+
}
46+
if c1.closed {
47+
t.Error("c1 should not have been closed (it was deleted from the set)")
48+
}
49+
})
50+
51+
t.Run("EmptySet", func(t *testing.T) {
52+
var set closers
53+
set.init()
54+
set.close() // Should not panic
55+
})
56+
}

localhost_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// CGo binding for Avahi
2+
//
3+
// Copyright (C) 2024 and up by Alexander Pevzner ([email protected])
4+
// See LICENSE for license terms and conditions
5+
//
6+
// Localhost handling tests
7+
//
8+
//go:build linux || freebsd
9+
10+
package avahi
11+
12+
import "testing"
13+
14+
func TestIsLocalhost(t *testing.T) {
15+
tests := []struct {
16+
hostname string
17+
expected bool
18+
}{
19+
{
20+
hostname: "localhost",
21+
expected: true,
22+
},
23+
{
24+
hostname: "localhost.localdomain",
25+
expected: true,
26+
},
27+
{
28+
hostname: "LOCALHOST",
29+
expected: true,
30+
},
31+
{
32+
hostname: "LocalHost.LocalDomain",
33+
expected: true,
34+
},
35+
{
36+
hostname: "example.com",
37+
expected: false,
38+
},
39+
{
40+
hostname: "127.0.0.1",
41+
expected: false, // isLocalhost only checks string matches
42+
},
43+
{
44+
hostname: "",
45+
expected: false,
46+
},
47+
}
48+
49+
for _, test := range tests {
50+
result := isLocalhost(test.hostname)
51+
if result != test.expected {
52+
t.Errorf("isLocalhost(%q): expected %v, got %v", test.hostname, test.expected, result)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)