Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions closer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// CGo binding for Avahi
//
// Copyright (C) 2024 and up by Alexander Pevzner (pzz@apevzner.com)
// See LICENSE for license terms and conditions
//
// Closers tests
//
//go:build linux || freebsd

package avahi

import "testing"

type mockCloser struct {
closed bool
}

func (m *mockCloser) Close() {
m.closed = true
}

func TestClosers(t *testing.T) {
t.Run("Lifecycle", func(t *testing.T) {
var set closers
set.init()

c1 := &mockCloser{}
c2 := &mockCloser{}

set.add(c1)
set.add(c2)

if len(set) != 2 {
t.Errorf("expected 2 closers, got %d", len(set))
}

set.del(c1)
if len(set) != 1 {
t.Errorf("expected 1 closer after deletion, got %d", len(set))
}

set.close()
if !c2.closed {
t.Error("c2 should have been closed")
}
if c1.closed {
t.Error("c1 should not have been closed (it was deleted from the set)")
}
})

t.Run("EmptySet", func(t *testing.T) {
var set closers
set.init()
set.close() // Should not panic
})
}
55 changes: 55 additions & 0 deletions localhost_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// CGo binding for Avahi
//
// Copyright (C) 2024 and up by Alexander Pevzner (pzz@apevzner.com)
// See LICENSE for license terms and conditions
//
// Localhost handling tests
//
//go:build linux || freebsd

package avahi

import "testing"

func TestIsLocalhost(t *testing.T) {
tests := []struct {
hostname string
expected bool
}{
{
hostname: "localhost",
expected: true,
},
{
hostname: "localhost.localdomain",
expected: true,
},
{
hostname: "LOCALHOST",
expected: true,
},
{
hostname: "LocalHost.LocalDomain",
expected: true,
},
{
hostname: "example.com",
expected: false,
},
{
hostname: "127.0.0.1",
expected: false, // isLocalhost only checks string matches
},
{
hostname: "",
expected: false,
},
}

for _, test := range tests {
result := isLocalhost(test.hostname)
if result != test.expected {
t.Errorf("isLocalhost(%q): expected %v, got %v", test.hostname, test.expected, result)
}
}
}