PR #159 round 2: with the brain root at mode 0100 (enterable, not listable) WalkDir failed at the root, Scan discarded the error and reported clean, and startup passed while search, which goes straight into wiki/, served a planted symlink. Scan now returns the root's walk error, so startup refuses on anything but a missing root. Also pins that a cleared set is reported again when it reappears. Refs #157 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// hyperguild#157: the server refuses to start while a symlink exists in the
|
|
// brain dir, or while any part of it cannot be scanned (which could hide
|
|
// one); a missing brain dir is the only thing it lets pass.
|
|
func TestStartupSymlinkCheckRefusesABrainWithASymlink(t *testing.T) {
|
|
brain := t.TempDir()
|
|
require.NoError(t, os.Symlink(t.TempDir(), filepath.Join(brain, "planted")))
|
|
err := startupSymlinkCheck(brain)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "planted")
|
|
}
|
|
|
|
func TestStartupSymlinkCheckRefusesABrainWithAnUnreadableDir(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root reads mode-000 directories")
|
|
}
|
|
brain := t.TempDir()
|
|
locked := filepath.Join(brain, "aaa")
|
|
require.NoError(t, os.MkdirAll(locked, 0o755))
|
|
require.NoError(t, os.Chmod(locked, 0o000))
|
|
t.Cleanup(func() { _ = os.Chmod(locked, 0o755) })
|
|
err := startupSymlinkCheck(brain)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "aaa")
|
|
}
|
|
|
|
func TestStartupSymlinkCheckPassesACleanOrMissingBrain(t *testing.T) {
|
|
brain := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(brain, "n.md"), []byte("x"), 0o644))
|
|
assert.NoError(t, startupSymlinkCheck(brain))
|
|
assert.NoError(t, startupSymlinkCheck(filepath.Join(brain, "missing")))
|
|
}
|
|
|
|
func TestSymlinkScanIntervalFallsBackOnNonPositive(t *testing.T) {
|
|
for _, v := range []string{"0", "-5", "", "junk"} {
|
|
got := symlinkScanInterval(func(string) string { return v })
|
|
assert.Positive(t, got, "env %q", v)
|
|
}
|
|
}
|
|
|
|
func TestAlertingWarningsNamesTheSymlinkTripwire(t *testing.T) {
|
|
w := strings.Join(alertingWarnings(func(string) string { return "" }), " ")
|
|
assert.Contains(t, w, "symlink tripwire")
|
|
}
|
|
|
|
func TestStartupSymlinkCheckRefusesAnUnlistableBrainRoot(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root lists mode-0100 directories")
|
|
}
|
|
brain := t.TempDir()
|
|
require.NoError(t, os.Chmod(brain, 0o100))
|
|
t.Cleanup(func() { _ = os.Chmod(brain, 0o755) })
|
|
assert.Error(t, startupSymlinkCheck(brain))
|
|
}
|