mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package ipgate
|
|
|
|
import (
|
|
"net/netip"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/therootcompany/golib/net/ipcohort"
|
|
)
|
|
|
|
func TestPrefixSetContainsZeroValue(t *testing.T) {
|
|
var ps PrefixSet
|
|
if ps.Contains(netip.MustParseAddr("192.0.2.1")) {
|
|
t.Fatal("zero-value PrefixSet must not contain an address")
|
|
}
|
|
}
|
|
|
|
func TestCachedCohortValidRequiresFiles(t *testing.T) {
|
|
dir := t.TempDir()
|
|
present := filepath.Join(dir, "present")
|
|
if err := writeFile(present); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cohort, err := ipcohort.Parse([]string{"192.0.2.0/24"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !cachedCohortValid(false, cohort, []string{present}) {
|
|
t.Fatal("fresh metadata and present files should use the cache")
|
|
}
|
|
if cachedCohortValid(false, cohort, []string{present, filepath.Join(dir, "gone")}) {
|
|
t.Fatal("missing data file must invalidate the cache")
|
|
}
|
|
if cachedCohortValid(false, nil, []string{present}) {
|
|
t.Fatal("nil cohort must not be used")
|
|
}
|
|
if cachedCohortValid(true, cohort, []string{present}) {
|
|
t.Fatal("updated repo must reload")
|
|
}
|
|
}
|
|
|
|
func writeFile(path string) error {
|
|
return os.WriteFile(path, nil, 0o600)
|
|
}
|