354 lines
12 KiB
Go
354 lines
12 KiB
Go
package nas
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// recordingRunner captures the commands a connector would run instead of
|
|
// running them. This is what lets the Windows path be tested from a Linux
|
|
// development machine: the decision logic and the exact argv are asserted
|
|
// without ever invoking net.exe.
|
|
type recordingRunner struct {
|
|
calls [][]string
|
|
output []byte
|
|
err error
|
|
}
|
|
|
|
func (r *recordingRunner) run(_ context.Context, name string, args ...string) ([]byte, error) {
|
|
r.calls = append(r.calls, append([]string{name}, args...))
|
|
return r.output, r.err
|
|
}
|
|
|
|
func (r *recordingRunner) lastCall() string {
|
|
if len(r.calls) == 0 {
|
|
return ""
|
|
}
|
|
return strings.Join(r.calls[len(r.calls)-1], " ")
|
|
}
|
|
|
|
func remoteLocation(t *testing.T, raw string) Location {
|
|
t.Helper()
|
|
loc, err := ParseLocation(raw)
|
|
if err != nil {
|
|
t.Fatalf("ParseLocation(%q) failed: %v", raw, err)
|
|
}
|
|
return loc
|
|
}
|
|
|
|
func TestConnectorSelection(t *testing.T) {
|
|
remote := remoteLocation(t, `\\nas\dci\streams`)
|
|
local := remoteLocation(t, "/mnt/dci/streams")
|
|
|
|
cases := []struct {
|
|
name string
|
|
loc Location
|
|
goos string
|
|
want string
|
|
}{
|
|
{"windows share", remote, "windows", "net use"},
|
|
{"linux share", remote, "linux", "mount"},
|
|
{"darwin share", remote, "darwin", "unsupported"},
|
|
{"local path on windows", local, "windows", "local"},
|
|
{"local path on linux", local, "linux", "local"},
|
|
// A already-mounted path needs no network step even where shares are
|
|
// unsupported, so it must not fall through to the error connector.
|
|
{"local path on darwin", local, "darwin", "local"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
if got := connectorFor(tc.loc, tc.goos).Name(); got != tc.want {
|
|
t.Errorf("%s: connectorFor = %q, want %q", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsConnectorAttach(t *testing.T) {
|
|
runner := &recordingRunner{}
|
|
c := &windowsConnector{run: runner.run}
|
|
loc := remoteLocation(t, `\\HomeLabNAS\dci\streams`)
|
|
|
|
att, err := c.Attach(context.Background(), loc, NASConfig{Username: "NASAdmin", Password: "secret"})
|
|
if err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
|
|
// The base path is the full UNC path; net use authenticates only the share.
|
|
if want := `\\HomeLabNAS\dci\streams`; att.BasePath != want {
|
|
t.Errorf("BasePath = %q, want %q", att.BasePath, want)
|
|
}
|
|
if want := `net use \\HomeLabNAS\dci /user:NASAdmin secret /persistent:no`; runner.lastCall() != want {
|
|
t.Errorf("command = %q, want %q", runner.lastCall(), want)
|
|
}
|
|
if !att.Owned {
|
|
t.Error("a successful net use should be owned so it can be released on shutdown")
|
|
}
|
|
}
|
|
|
|
func TestWindowsConnectorOmitsCredentialsWhenUnset(t *testing.T) {
|
|
runner := &recordingRunner{}
|
|
c := &windowsConnector{run: runner.run}
|
|
|
|
if _, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci`), NASConfig{}); err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
|
|
if want := `net use \\nas\dci /persistent:no`; runner.lastCall() != want {
|
|
t.Errorf("command = %q, want %q", runner.lastCall(), want)
|
|
}
|
|
}
|
|
|
|
func TestWindowsConnectorContinuesWhenAlreadyConnected(t *testing.T) {
|
|
// net use fails when a connection already exists, but the share is usable;
|
|
// refusing to run would be wrong. The write probe is the real verdict.
|
|
runner := &recordingRunner{
|
|
err: fmt.Errorf("exit status 2"),
|
|
output: []byte("The network connection could not be found.\nMore help is available..."),
|
|
}
|
|
c := &windowsConnector{run: runner.run}
|
|
|
|
att, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci`), NASConfig{})
|
|
if err != nil {
|
|
t.Fatalf("Attach should tolerate a net use failure, got: %v", err)
|
|
}
|
|
if att.Owned {
|
|
t.Error("a failed net use must not be marked owned, or Detach would drop someone else's connection")
|
|
}
|
|
|
|
notes := strings.Join(att.Notes, "\n")
|
|
if !strings.Contains(notes, "net use did not complete") {
|
|
t.Errorf("expected the failure to be recorded in notes:\n%s", notes)
|
|
}
|
|
// Only the first line of output is worth surfacing.
|
|
if strings.Contains(notes, "More help is available") {
|
|
t.Errorf("notes should not carry the full usage dump:\n%s", notes)
|
|
}
|
|
}
|
|
|
|
func TestWindowsConnectorDetachOnlyReleasesOwned(t *testing.T) {
|
|
runner := &recordingRunner{}
|
|
c := &windowsConnector{run: runner.run}
|
|
|
|
if err := c.Detach(context.Background(), Attachment{share: `\\nas\dci`, Owned: false}); err != nil {
|
|
t.Fatalf("Detach failed: %v", err)
|
|
}
|
|
if len(runner.calls) != 0 {
|
|
t.Errorf("must not disconnect a share this process did not connect, ran: %v", runner.calls)
|
|
}
|
|
|
|
if err := c.Detach(context.Background(), Attachment{share: `\\nas\dci`, Owned: true}); err != nil {
|
|
t.Fatalf("Detach failed: %v", err)
|
|
}
|
|
if want := `net use \\nas\dci /delete`; runner.lastCall() != want {
|
|
t.Errorf("command = %q, want %q", runner.lastCall(), want)
|
|
}
|
|
}
|
|
|
|
// writeMountTable stages a /proc/mounts-format file.
|
|
func writeMountTable(t *testing.T, lines ...string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "mounts")
|
|
body := strings.Join(lines, "\n") + "\n"
|
|
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
|
t.Fatalf("failed to write mount table: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLinuxConnectorResolvesExistingMount(t *testing.T) {
|
|
mounts := writeMountTable(t,
|
|
"sysfs /sys sysfs rw,nosuid 0 0",
|
|
"//homelabnas/dci /mnt/dci cifs rw,relatime,username=nasadmin 0 0",
|
|
)
|
|
c := &linuxConnector{run: (&recordingRunner{}).run, mountsPath: mounts}
|
|
|
|
att, err := c.Attach(context.Background(), remoteLocation(t, `\\HomeLabNAS\dci\streams`), NASConfig{})
|
|
if err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
|
|
// Server and share names are case-insensitive on SMB, so the configured
|
|
// \\HomeLabNAS\dci must match a //homelabnas/dci mount.
|
|
if want := "/mnt/dci/streams"; att.BasePath != want {
|
|
t.Errorf("BasePath = %q, want %q", att.BasePath, want)
|
|
}
|
|
if att.Owned {
|
|
t.Error("a pre-existing mount must not be owned, or shutdown would unmount it")
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorPrefersDeepestMount(t *testing.T) {
|
|
// Both mounts could serve the destination; the more specific one is the
|
|
// only one guaranteed to actually contain it.
|
|
mounts := writeMountTable(t,
|
|
"//nas/dci /mnt/dci cifs rw 0 0",
|
|
"//nas/dci/streams /mnt/streams cifs rw 0 0",
|
|
)
|
|
c := &linuxConnector{run: (&recordingRunner{}).run, mountsPath: mounts}
|
|
|
|
att, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci\streams\2026`), NASConfig{})
|
|
if err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
if want := "/mnt/streams/2026"; att.BasePath != want {
|
|
t.Errorf("BasePath = %q, want %q", att.BasePath, want)
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorMatchesNFSAndEscapedPoints(t *testing.T) {
|
|
mounts := writeMountTable(t,
|
|
`nas:/dci /mnt/my\040share nfs4 rw 0 0`,
|
|
)
|
|
c := &linuxConnector{run: (&recordingRunner{}).run, mountsPath: mounts}
|
|
|
|
att, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci\streams`), NASConfig{})
|
|
if err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
if want := "/mnt/my share/streams"; att.BasePath != want {
|
|
t.Errorf("BasePath = %q, want %q", att.BasePath, want)
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorIgnoresNonNetworkMounts(t *testing.T) {
|
|
// A local directory that merely looks like the share must not match.
|
|
mounts := writeMountTable(t, "//nas/dci /mnt/dci ext4 rw 0 0")
|
|
c := &linuxConnector{run: (&recordingRunner{}).run, mountsPath: mounts}
|
|
|
|
if _, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci`), NASConfig{}); err == nil {
|
|
t.Fatal("expected an ext4 mount not to satisfy a network share")
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorUnmountedGivesActionableError(t *testing.T) {
|
|
mounts := writeMountTable(t, "sysfs /sys sysfs rw 0 0")
|
|
c := &linuxConnector{run: (&recordingRunner{}).run, mountsPath: mounts}
|
|
|
|
_, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci\streams`), NASConfig{Username: "admin"})
|
|
if err == nil {
|
|
t.Fatal("expected an unmounted share to fail")
|
|
}
|
|
|
|
msg := err.Error()
|
|
for _, want := range []string{"//nas/dci", "mount -t cifs", "NAS_AUTO_MOUNT", "NAS_OUTPUT_PATH"} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Errorf("error should mention %q:\n%s", want, msg)
|
|
}
|
|
}
|
|
// The remedy must not echo the real password.
|
|
if strings.Contains(msg, "hunter2") {
|
|
t.Error("error message leaked a credential")
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorAutoMountRequiresRoot(t *testing.T) {
|
|
mounts := writeMountTable(t, "sysfs /sys sysfs rw 0 0")
|
|
runner := &recordingRunner{}
|
|
c := &linuxConnector{run: runner.run, mountsPath: mounts, geteuid: func() int { return 1000 }}
|
|
|
|
_, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci`), NASConfig{AutoMount: true})
|
|
if err == nil || !strings.Contains(err.Error(), "requires root") {
|
|
t.Fatalf("expected a root requirement error, got: %v", err)
|
|
}
|
|
if len(runner.calls) != 0 {
|
|
t.Errorf("must not attempt mount without privileges, ran: %v", runner.calls)
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorAutoMount(t *testing.T) {
|
|
mounts := writeMountTable(t, "sysfs /sys sysfs rw 0 0")
|
|
runner := &recordingRunner{}
|
|
point := filepath.Join(t.TempDir(), "mnt", "dci")
|
|
c := &linuxConnector{run: runner.run, mountsPath: mounts, geteuid: func() int { return 0 }}
|
|
|
|
cfg := NASConfig{AutoMount: true, MountPoint: point, Username: "admin", Password: "hunter2", Timeout: 5 * time.Second}
|
|
att, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci\streams`), cfg)
|
|
if err != nil {
|
|
t.Fatalf("Attach failed: %v", err)
|
|
}
|
|
|
|
if want := filepath.Join(point, "streams"); att.BasePath != want {
|
|
t.Errorf("BasePath = %q, want %q", att.BasePath, want)
|
|
}
|
|
if !att.Owned || att.mountPoint != point {
|
|
t.Errorf("a self-created mount must be owned and recorded, got owned=%v point=%q", att.Owned, att.mountPoint)
|
|
}
|
|
|
|
call := runner.lastCall()
|
|
if !strings.HasPrefix(call, "mount -t cifs //nas/dci "+point+" -o rw,credentials=") {
|
|
t.Errorf("unexpected mount command: %q", call)
|
|
}
|
|
// The password must reach mount.cifs through a credentials file, never
|
|
// through argv where any local user could read it from the process table.
|
|
if strings.Contains(call, "hunter2") {
|
|
t.Errorf("password leaked into the mount command line: %q", call)
|
|
}
|
|
// The staged credentials file must not outlive the mount call.
|
|
credPath := call[strings.Index(call, "credentials=")+len("credentials="):]
|
|
if _, err := os.Stat(credPath); !os.IsNotExist(err) {
|
|
t.Errorf("credentials file %s should have been removed, stat err = %v", credPath, err)
|
|
}
|
|
|
|
if err := c.Detach(context.Background(), att); err != nil {
|
|
t.Fatalf("Detach failed: %v", err)
|
|
}
|
|
if want := "umount " + point; runner.lastCall() != want {
|
|
t.Errorf("detach command = %q, want %q", runner.lastCall(), want)
|
|
}
|
|
}
|
|
|
|
func TestLinuxConnectorDetachLeavesForeignMounts(t *testing.T) {
|
|
runner := &recordingRunner{}
|
|
c := &linuxConnector{run: runner.run, mountsPath: writeMountTable(t)}
|
|
|
|
if err := c.Detach(context.Background(), Attachment{BasePath: "/mnt/dci", Owned: false}); err != nil {
|
|
t.Fatalf("Detach failed: %v", err)
|
|
}
|
|
if len(runner.calls) != 0 {
|
|
t.Errorf("must not unmount a share the host mounted, ran: %v", runner.calls)
|
|
}
|
|
}
|
|
|
|
func TestLocalConnectorRejectsWindowsPathsOffWindows(t *testing.T) {
|
|
c := &localConnector{goos: "linux"}
|
|
|
|
for _, raw := range []string{`C:\data\streams`, `data\streams`} {
|
|
loc, err := ParseLocation(raw)
|
|
if err != nil {
|
|
t.Fatalf("ParseLocation(%q) failed: %v", raw, err)
|
|
}
|
|
if _, err := c.Attach(context.Background(), loc, NASConfig{}); err == nil {
|
|
t.Errorf("%q should be rejected on Linux: backslashes are legal filename characters there, "+
|
|
"so it would silently create a local directory instead of writing to the NAS", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLocalConnectorAcceptsWindowsPathsOnWindows(t *testing.T) {
|
|
c := &localConnector{goos: "windows"}
|
|
loc, err := ParseLocation(`C:\data\streams`)
|
|
if err != nil {
|
|
t.Fatalf("ParseLocation failed: %v", err)
|
|
}
|
|
|
|
if _, err := c.Attach(context.Background(), loc, NASConfig{}); err != nil {
|
|
t.Errorf("a drive path is valid on Windows: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnsupportedConnectorExplainsItself(t *testing.T) {
|
|
c := &unsupportedConnector{goos: "plan9"}
|
|
_, err := c.Attach(context.Background(), remoteLocation(t, `\\nas\dci`), NASConfig{})
|
|
if err == nil {
|
|
t.Fatal("expected an error on an unsupported platform")
|
|
}
|
|
if !strings.Contains(err.Error(), "plan9") || !strings.Contains(err.Error(), "NAS_OUTPUT_PATH") {
|
|
t.Errorf("error should name the platform and the remedy: %v", err)
|
|
}
|
|
}
|