feat: Implement platform-specific hosts file path retrieval
- Created a new `platform` package to handle OS-specific hosts file paths. - Added implementations for macOS, Linux, and Windows to retrieve the hosts file path. - Introduced tests for the `GetHostsFilePath` function to ensure correct behavior across platforms. feat: Add DNS resolver for IP lookups - Implemented a `resolver` package for performing DNS lookups. - Created a `DNSResolver` struct with a `LookupIP` method to handle A-record lookups and CNAME resolution. - Added comprehensive tests for various DNS scenarios, including CNAME chains and error handling. chore: Refactor project structure and update dependencies - Restructured the project to follow Go's standard package layout. - Updated `go.mod` to Go 1.20 and added `golang.org/x/net` dependency. - Removed old source files and ensured a clean build with all tests passing.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package platform_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"dns-helper/platform"
|
||||
)
|
||||
|
||||
// T007-1: GetHostsFilePath returns a non-empty path with no error on the current OS.
|
||||
func TestGetHostsFilePathReturnsPath(t *testing.T) {
|
||||
path, err := platform.GetHostsFilePath()
|
||||
if err != nil {
|
||||
t.Fatalf("GetHostsFilePath returned unexpected error: %v", err)
|
||||
}
|
||||
if path == "" {
|
||||
t.Error("GetHostsFilePath returned empty path")
|
||||
}
|
||||
}
|
||||
|
||||
// T007-2: The returned path exists on the filesystem.
|
||||
func TestGetHostsFilePathExists(t *testing.T) {
|
||||
path, err := platform.GetHostsFilePath()
|
||||
if err != nil {
|
||||
t.Fatalf("GetHostsFilePath returned unexpected error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Errorf("hosts file path %q does not exist or cannot be accessed: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
// T024-1: GetHostsFilePath returns a path that is an absolute path (contains a
|
||||
// directory separator), ensuring the error messages that embed the path are descriptive.
|
||||
func TestGetHostsFilePathIsAbsolute(t *testing.T) {
|
||||
path, err := platform.GetHostsFilePath()
|
||||
if err != nil {
|
||||
t.Fatalf("GetHostsFilePath returned unexpected error: %v", err)
|
||||
}
|
||||
if !strings.ContainsAny(path, `/\`) {
|
||||
t.Errorf("expected an absolute path containing a directory separator, got %q", path)
|
||||
}
|
||||
}
|
||||
|
||||
// T024-2: GetHostsFilePath function signature returns (string, error) — process
|
||||
// termination (os.Exit, log.Fatal, term()) is absent from the platform package.
|
||||
// This is verified statically by T023 grep/vet; the runtime counterpart is that the
|
||||
// function can be called without panicking or exiting.
|
||||
func TestGetHostsFilePathDoesNotPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Errorf("GetHostsFilePath panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
platform.GetHostsFilePath() //nolint:errcheck // return values checked in other tests
|
||||
}
|
||||
Reference in New Issue
Block a user