//go:build linux package command import ( "strings" "testing" ) // The kernel escapes some characters of the paths in /proc/self/mountinfo; a // mount directory with a space must still be found there. func TestUnescapeMountInfoPath(t *testing.T) { for in, want := range map[string]string{ `/srv/team\040share`: "/srv/team share", `/a\011b\012c\134d`: "/a\tb\nc\\d", "/plain": "/plain", `/short\04`: `/short\04`, `/not\09octal`: `/not\09octal`, } { if got := unescapeMountInfoPath(in); got != want { t.Errorf("unescapeMountInfoPath(%q) = %q, want %q", in, got, want) } } } // A kernel mount option counts whether it comes from its flag or from -o. func TestHasExtraMountOption(t *testing.T) { option := &MountOptions{extraOptions: []string{"noatime,allow_other", " ro "}} for name, want := range map[string]bool{"allow_other": true, "ro": true, "default_permissions": false, "noatime": true} { if got := hasExtraMountOption(option, name); got != want { t.Errorf("hasExtraMountOption(%q) = %v, want %v", name, got, want) } } } // The mount flags are those fusermount3 and go-fuse's direct mount give the // kernel: from nosuid and nodev, the options in order. -readOnly puts "ro" // before the -o options, so -readOnly with -o rw mounts read-write. func TestKernelMountFlags(t *testing.T) { for _, c := range []struct { options []string want string }{ {nil, "nodev,nosuid"}, {[]string{"ro"}, "nodev,nosuid,ro"}, {[]string{"ro", "rw"}, "nodev,nosuid"}, {[]string{"rw", "ro"}, "nodev,nosuid,ro"}, {[]string{"noexec"}, "nodev,noexec,nosuid"}, {[]string{"noexec", "exec"}, "nodev,nosuid"}, {[]string{"suid", "dev"}, ""}, {[]string{"default_permissions", "allow_other", "noatime", "fsname=a:/b"}, "noatime,nodev,nosuid"}, {[]string{"relatime"}, "nodev,nosuid"}, } { if got := strings.Join(kernelMountFlags(c.options), ","); got != c.want { t.Errorf("kernelMountFlags(%q) = %q, want %q", c.options, got, c.want) } } }