Playground
gnomod.toml
module = "gno.land/p/moul/x/plan9/rc/v0" gno = "0.9" [addpkg] creator = "g1manfred47kzduec920z88wfr64ylksmdcedlf5" height = 272412 max_deposit = "11000000ugnot"
rc.gno
// Package rc is a small shell over a Plan 9 namespace.
//
// It exists because a namespace you cannot inspect is a namespace you cannot
// trust. Everything here operates on a gno.land/p/moul/x/plan9/ns namespace
// and returns text, so one realm's Render becomes a file browser and one
// transaction becomes a shell command:
//
// Exec("bind -a /srv/dev /dev; echo hello > /tmp/greeting")
//
// The mode split is the security model. A Shell in ReadOnly mode refuses every
// mutating command, which is what lets a realm expose the shell through
// Render, where mutating anything would be a bug, while the same code backs a
// crossing Exec that may write.
//
// Quoting follows rc: single quotes, with ” inside a quoted string standing
// for one literal quote. Commands are separated by ';' or newlines, and
// quoting is respected when splitting them.
//
// On the first error the run STOPS and returns it. A realm should let that
// error panic, so a half-applied command line reverts with the transaction
// rather than leaving the namespace in a state nobody asked for.
//
// NOTICE. Plan 9 from Bell Labs is the work of the Computing Science Research
// Center at Bell Labs; the name and the marks are theirs, and the copyright is
// held by the Plan 9 Foundation (https://p9f.org). This package is not
// affiliated with, endorsed by, or sponsored by them, and contains no Plan 9
// code: it borrows the vocabulary so that the design reads without a glossary,
// and it is an homage, asking what that ecosystem's spirit looks like on a
// chain. Full attribution: NOTICE.md at the root of moul/gno-contracts.
package rc
import (
"errors"
"strconv"
"strings"
memfs "gno.land/p/moul/x/plan9/memfs/v0"
ninep "gno.land/p/moul/x/plan9/ninep/v0"
ns "gno.land/p/moul/x/plan9/ns/v0"
)
// Mode decides whether mutating commands are allowed.
type Mode uint8
// Shell modes.
const (
ReadOnly Mode = 0
ReadWrite Mode = 1
)
var (
// ErrReadOnly is returned when a mutating command runs in ReadOnly mode.
ErrReadOnly = errors.New("read-only shell")
// ErrUsage reports a malformed command line.
ErrUsage = errors.New("usage")
// ErrUnknown reports a command this shell does not have.
ErrUnknown = errors.New("command not found")
// ErrQuote reports an unterminated quoted string.
ErrQuote = errors.New("unterminated '")
)
// MaxCommands bounds one run, so a single call cannot loop the VM.
const MaxCommands = 32
// Shell runs commands against one namespace.
type Shell struct {
ns *ns.Ns
mode Mode
now func() int64
}
// New returns a shell over n. now supplies the block height stamped on writes
// and may be nil in ReadOnly mode.
func New(n *ns.Ns, mode Mode, now func() int64) *Shell {
return &Shell{ns: n, mode: mode, now: now}
}
// Ns returns the namespace the shell operates on.
func (s *Shell) Ns() *ns.Ns { return s.ns }
func (s *Shell) clock() int64 {
if s.now == nil {
return 0
}
return s.now()
}
func (s *Shell) writable() error {
if s.mode != ReadWrite {
return ErrReadOnly
}
return nil
}
// Run executes a command line and returns its output. Execution stops at the
// first error.
func (s *Shell) Run(line string) (string, error) {
cmds, err := splitCommands(line)
if err != nil {
return "", err
}
if len(cmds) > MaxCommands {
return "", errors.New("too many commands in one line")
}
var out strings.Builder
for _, c := range cmds {
toks, err := tokenize(c)
if err != nil {
return out.String(), err
}
if len(toks) == 0 {
continue
}
text, err := s.run1(toks)
out.WriteString(text)
if err != nil {
return out.String(), errors.New(toks[0] + ": " + err.Error())
}
}
return out.String(), nil
}
func (s *Shell) run1(toks []string) (string, error) {
cmd, args := toks[0], toks[1:]
switch cmd {
case "help":
return help, nil
case "pwd":
return s.ns.Cwd() + "\n", nil
case "cd":
return "", s.cd(args)
case "ns":
return s.ns.String(), nil
case "ls":
return s.ls(args)
case "cat":
return s.cat(args)
case "stat":
return s.stat(args)
case "walk":
return s.walk(args)
case "bind", "mount":
return "", s.bind(cmd, args)
case "unmount", "unbind":
return "", s.unmount(args)
case "mkdir":
return "", s.mkdir(args)
case "rm":
return "", s.rm(args)
case "echo":
return s.echo(args)
}
return "", ErrUnknown
}
const help = `bind [-a|-b|-c] new old make new visible at old (-b before, -a after, -c creates)
cat file... print files
cd [dir] change directory
echo [-n] word... [>|>> file]
ls [-l] [-u] [path...] list; -l long, -u collapse union duplicates
mkdir path... create directories
mount synonym for bind
ns print the namespace
pwd print the working directory
rm path... remove files and empty directories
stat path... print the 9P stat of each path
unmount [new] old undo a binding
walk path show how each element of path resolves
`
func (s *Shell) cd(args []string) error {
if len(args) == 0 {
return s.ns.Cd("/")
}
if len(args) > 1 {
return ErrUsage
}
return s.ns.Cd(args[0])
}
func (s *Shell) ls(args []string) (string, error) {
long, unique, paths := false, false, []string{}
for _, a := range args {
if strings.HasPrefix(a, "-") && len(a) > 1 {
for _, r := range a[1:] {
switch r {
case 'l':
long = true
case 'u':
unique = true
default:
return "", ErrUsage
}
}
continue
}
paths = append(paths, a)
}
if len(paths) == 0 {
paths = []string{s.ns.Cwd()}
}
var out strings.Builder
for i, p := range paths {
st, err := s.ns.Stat(p)
if err != nil {
return out.String(), err
}
if len(paths) > 1 {
if i > 0 {
out.WriteString("\n")
}
out.WriteString(s.ns.Abs(p) + ":\n")
}
if !st.IsDir() {
out.WriteString(entryLine(st, long))
continue
}
ents, err := s.ns.ReadDir(p, unique)
if err != nil {
return out.String(), err
}
for _, e := range ents {
out.WriteString(entryLine(e, long))
}
}
return out.String(), nil
}
func entryLine(st ninep.Stat, long bool) string {
if long {
return st.Line() + "\n"
}
name := st.Name
if st.IsDir() && name != "/" {
name += "/"
}
return name + "\n"
}
func (s *Shell) cat(args []string) (string, error) {
if len(args) == 0 {
return "", ErrUsage
}
var out strings.Builder
for _, p := range args {
data, err := s.ns.ReadFile(p)
if err != nil {
return out.String(), err
}
out.WriteString(data)
}
return out.String(), nil
}
func (s *Shell) stat(args []string) (string, error) {
if len(args) == 0 {
args = []string{s.ns.Cwd()}
}
var out strings.Builder
for _, p := range args {
st, err := s.ns.Stat(p)
if err != nil {
return out.String(), err
}
members, err := s.ns.Resolve(p)
if err != nil {
return out.String(), err
}
out.WriteString(s.ns.Abs(p) + " " + st.Qid.String() +
" " + st.Mode.String() +
" uid=" + st.Uid +
" length=" + strconv.FormatInt(st.Length, 10) +
" mtime=" + strconv.FormatInt(st.Mtime, 10) +
" union=" + strconv.Itoa(len(members)) + "\n")
}
return out.String(), nil
}
// walk shows the resolution of each element, which is where a namespace stops
// being magic: the union width column says exactly when a bind took effect.
func (s *Shell) walk(args []string) (string, error) {
if len(args) != 1 {
return "", ErrUsage
}
abs := s.ns.Abs(args[0])
var out strings.Builder
prefix := "/"
report := func(p string) error {
st, err := s.ns.Stat(p)
if err != nil {
return err
}
members, err := s.ns.Resolve(p)
if err != nil {
return err
}
out.WriteString(pad(p, 24) + " " + st.Qid.String() + " " +
st.Mode.String() + " union=" + strconv.Itoa(len(members)) + "\n")
return nil
}
if err := report("/"); err != nil {
return out.String(), err
}
for _, e := range ninep.Elems(abs) {
prefix = ninep.Join(prefix, e)
if err := report(prefix); err != nil {
return out.String(), err
}
}
return out.String(), nil
}
func pad(s string, n int) string {
for len(s) < n {
s += " "
}
return s
}
func (s *Shell) bind(verb string, args []string) error {
if err := s.writable(); err != nil {
return err
}
flag := ns.MREPL
rest := []string{}
for _, a := range args {
if strings.HasPrefix(a, "-") && len(a) > 1 {
for _, r := range a[1:] {
switch r {
case 'b':
flag = flag&^0x3 | ns.MBEFORE
case 'a':
flag = flag&^0x3 | ns.MAFTER
case 'c':
flag |= ns.MCREATE
default:
return ErrUsage
}
}
continue
}
rest = append(rest, a)
}
if len(rest) != 2 {
return ErrUsage
}
return s.ns.BindVerb(verb, rest[0], rest[1], flag)
}
func (s *Shell) unmount(args []string) error {
if err := s.writable(); err != nil {
return err
}
switch len(args) {
case 1:
return s.ns.Unmount("", args[0])
case 2:
return s.ns.Unmount(args[0], args[1])
}
return ErrUsage
}
func (s *Shell) mkdir(args []string) error {
if err := s.writable(); err != nil {
return err
}
if len(args) == 0 {
return ErrUsage
}
for _, p := range args {
abs := s.ns.Abs(p)
target, err := s.ns.CreateTarget(ninep.Dir(abs))
if err != nil {
return err
}
if _, err := target.Create(ninep.Base(abs), ninep.DirPerm, s.clock()); err != nil {
return err
}
}
return nil
}
func (s *Shell) rm(args []string) error {
if err := s.writable(); err != nil {
return err
}
if len(args) == 0 {
return ErrUsage
}
for _, p := range args {
abs := s.ns.Abs(p)
name := ninep.Base(abs)
members, err := s.ns.Resolve(ninep.Dir(abs))
if err != nil {
return err
}
found, removed := false, false
for _, m := range members {
if _, err := m.Walk(name); err != nil {
continue
}
found = true
mu, ok := m.(ninep.Mutable)
if !ok {
continue // a read-only server: try the next union member
}
if err := mu.Remove(name); err != nil {
return err
}
removed = true
break
}
switch {
case removed:
case found:
return ninep.ErrReadOnly
default:
return ninep.ErrNotExist
}
}
return nil
}
func (s *Shell) echo(args []string) (string, error) {
newline := true
words := []string{}
redirect, appendTo, dest := false, false, ""
i := 0
for i < len(args) {
a := args[i]
switch {
case a == "-n" && len(words) == 0 && !redirect:
newline = false
case a == ">" || a == ">>":
if i+1 >= len(args) {
return "", ErrUsage
}
redirect, appendTo, dest = true, a == ">>", args[i+1]
i++
default:
words = append(words, a)
}
i++
}
text := strings.Join(words, " ")
if newline {
text += "\n"
}
if !redirect {
return text, nil
}
if err := s.writable(); err != nil {
return "", err
}
return "", s.writeTo(dest, text, appendTo)
}
// writeTo creates or opens dest and writes text, honouring the namespace's
// MCREATE rules when the file has to be created.
func (s *Shell) writeTo(dest, text string, appendTo bool) error {
abs := s.ns.Abs(dest)
f, err := s.ns.Open(abs)
if err == ninep.ErrNotExist {
target, terr := s.ns.CreateTarget(ninep.Dir(abs))
if terr != nil {
return terr
}
created, cerr := target.Create(ninep.Base(abs), ninep.FilePerm, s.clock())
if cerr != nil {
return cerr
}
f = created
} else if err != nil {
return err
}
mu, ok := f.(ninep.Mutable)
if !ok {
return ninep.ErrReadOnly
}
off := int64(0)
if appendTo {
off = mu.Stat().Length
} else if err := mu.Truncate(0, s.clock()); err != nil {
return err
}
_, err = mu.Write(off, text, s.clock())
return err
}
// splitCommands splits on ';' and newlines, respecting rc quoting.
func splitCommands(line string) ([]string, error) {
out := []string{}
var b strings.Builder
quoted := false
for i := 0; i < len(line); i++ {
c := line[i]
if c == '\'' {
quoted = !quoted
}
if !quoted && (c == ';' || c == '\n') {
out = append(out, b.String())
b.Reset()
continue
}
b.WriteByte(c)
}
if quoted {
return nil, ErrQuote
}
out = append(out, b.String())
return out, nil
}
// tokenize splits one command into words using rc's quoting rules.
func tokenize(s string) ([]string, error) {
toks := []string{}
i := 0
for i < len(s) {
for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
i++
}
if i >= len(s) {
break
}
var b strings.Builder
quoted := false
for i < len(s) {
c := s[i]
if c == '\'' {
if quoted && i+1 < len(s) && s[i+1] == '\'' {
b.WriteByte('\'')
i += 2
continue
}
quoted = !quoted
i++
continue
}
if !quoted && (c == ' ' || c == '\t') {
break
}
b.WriteByte(c)
i++
}
if quoted {
return nil, ErrQuote
}
toks = append(toks, b.String())
}
return toks, nil
}
// NewMemShell is the convenience a realm or a test wants: a fresh ram root,
// a namespace over it, and a shell. It exists here rather than in ns so that
// ns keeps no dependency on a particular file server.
func NewMemShell(uid string, mode Mode, now func() int64) (*Shell, *memfs.FS) {
h := int64(0)
if now != nil {
h = now()
}
fs := memfs.New(uid, h)
return New(ns.New(fs.Root()), mode, now), fs
}
rc_test.gno
package rc
import (
"strings"
"testing"
memfs "gno.land/p/moul/x/plan9/memfs/v0"
ninep "gno.land/p/moul/x/plan9/ninep/v0"
ns "gno.land/p/moul/x/plan9/ns/v0"
synfs "gno.land/p/moul/x/plan9/synfs/v0"
)
func clock(h int64) func() int64 { return func() int64 { return h } }
func shell(t *testing.T, files ...string) (*Shell, *memfs.FS) {
t.Helper()
sh, fs := NewMemShell("glenda", ReadWrite, clock(100))
for _, f := range files {
if err := fs.WriteFile(f, "contents of "+f+"\n", 100); err != nil {
t.Fatalf("seed %s: %v", f, err)
}
}
return sh, fs
}
func run(t *testing.T, sh *Shell, line string) string {
t.Helper()
out, err := sh.Run(line)
if err != nil {
t.Fatalf("%s: %v", line, err)
}
return out
}
func TestTokenize(t *testing.T) {
tests := []struct {
name string
in string
want []string
}{
{"plain", "ls -l /dev", []string{"ls", "-l", "/dev"}},
{"extra spaces", " ls /dev ", []string{"ls", "/dev"}},
{"quoted", "echo 'hello world'", []string{"echo", "hello world"}},
{"quote inside quote", "echo 'it''s'", []string{"echo", "it's"}},
{"empty quoted word", "echo ''", []string{"echo", ""}},
{"adjacent", "echo a'b c'd", []string{"echo", "ab cd"}},
{"empty line", "", []string{}},
}
for _, tt := range tests {
got, err := tokenize(tt.in)
if err != nil {
t.Errorf("%s: %v", tt.name, err)
continue
}
if len(got) != len(tt.want) {
t.Errorf("%s: got %v, want %v", tt.name, got, tt.want)
continue
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("%s: token %d got %q, want %q", tt.name, i, got[i], tt.want[i])
}
}
}
if _, err := tokenize("echo 'unterminated"); err != ErrQuote {
t.Errorf("unterminated quote: got %v", err)
}
}
func TestSplitCommands(t *testing.T) {
got, err := splitCommands("pwd; ls /\necho 'a;b'")
if err != nil {
t.Fatalf("%v", err)
}
want := []string{"pwd", " ls /", "echo 'a;b'"}
if len(got) != len(want) {
t.Fatalf("got %v", got)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("%d: got %q, want %q", i, got[i], want[i])
}
}
}
func TestPwdAndCd(t *testing.T) {
sh, _ := shell(t, "/usr/glenda/lib/profile")
if got := run(t, sh, "pwd"); got != "/\n" {
t.Errorf("pwd: %q", got)
}
run(t, sh, "cd /usr/glenda")
if got := run(t, sh, "pwd"); got != "/usr/glenda\n" {
t.Errorf("pwd after cd: %q", got)
}
if got := run(t, sh, "cat lib/profile"); got != "contents of /usr/glenda/lib/profile\n" {
t.Errorf("relative cat: %q", got)
}
run(t, sh, "cd")
if got := run(t, sh, "pwd"); got != "/\n" {
t.Errorf("bare cd should go to the root: %q", got)
}
}
func TestLs(t *testing.T) {
sh, _ := shell(t, "/bin/ls", "/bin/rc", "/tmp/x")
if got := run(t, sh, "ls /"); got != "bin/\ntmp/\n" {
t.Errorf("ls /: %q", got)
}
if got := run(t, sh, "ls /bin"); got != "ls\nrc\n" {
t.Errorf("ls /bin: %q", got)
}
long := run(t, sh, "ls -l /bin")
if !strings.Contains(long, "-rw-r--r-- glenda") {
t.Errorf("ls -l: %q", long)
}
if got := run(t, sh, "ls /bin/ls"); got != "ls\n" {
t.Errorf("ls of a file: %q", got)
}
if _, err := sh.Run("ls -Z"); err == nil {
t.Error("an unknown flag should fail")
}
}
func TestLsUnionAndUnique(t *testing.T) {
sh, fs := shell(t)
fs.WriteFile("/bin/ls", "system\n", 100)
fs.WriteFile("/usr/glenda/bin/ls", "mine\n", 100)
run(t, sh, "bind -b /usr/glenda/bin /bin")
if got := run(t, sh, "ls /bin"); got != "ls\nls\n" {
t.Errorf("a union lists both, as Plan 9 does: %q", got)
}
if got := run(t, sh, "ls -u /bin"); got != "ls\n" {
t.Errorf("ls -u: %q", got)
}
if got := run(t, sh, "cat /bin/ls"); got != "mine\n" {
t.Errorf("the member bound before wins the walk: %q", got)
}
}
func TestCatErrors(t *testing.T) {
sh, _ := shell(t, "/bin/ls")
if _, err := sh.Run("cat /bin"); err == nil {
t.Error("cat of a directory should fail")
}
if _, err := sh.Run("cat /absent"); err == nil {
t.Error("cat of a missing file should fail")
}
if _, err := sh.Run("cat"); err == nil {
t.Error("cat with no argument should fail")
}
}
func TestEchoAndRedirect(t *testing.T) {
sh, _ := shell(t)
if got := run(t, sh, "echo hello world"); got != "hello world\n" {
t.Errorf("echo: %q", got)
}
if got := run(t, sh, "echo -n hello"); got != "hello" {
t.Errorf("echo -n: %q", got)
}
run(t, sh, "echo hello > /greeting")
if got := run(t, sh, "cat /greeting"); got != "hello\n" {
t.Errorf("after redirect: %q", got)
}
run(t, sh, "echo again >> /greeting")
if got := run(t, sh, "cat /greeting"); got != "hello\nagain\n" {
t.Errorf("after append: %q", got)
}
run(t, sh, "echo replaced > /greeting")
if got := run(t, sh, "cat /greeting"); got != "replaced\n" {
t.Errorf("a plain redirect truncates: %q", got)
}
if _, err := sh.Run("echo hi >"); err == nil {
t.Error("a redirect with no target should fail")
}
}
func TestMkdirAndRm(t *testing.T) {
sh, _ := shell(t)
run(t, sh, "mkdir /tmp")
run(t, sh, "echo x > /tmp/f")
if got := run(t, sh, "ls /tmp"); got != "f\n" {
t.Errorf("ls /tmp: %q", got)
}
if _, err := sh.Run("rm /tmp"); err == nil || err.Error() != "rm: "+ninep.ErrNotEmpty.Error() {
t.Errorf("rm of a non-empty directory: got %v", err)
}
run(t, sh, "rm /tmp/f")
run(t, sh, "rm /tmp")
if got := run(t, sh, "ls /"); got != "" {
t.Errorf("root should be empty: %q", got)
}
if _, err := sh.Run("rm /absent"); err == nil {
t.Error("rm of a missing file should fail")
}
}
func TestBindAndNs(t *testing.T) {
sh, _ := shell(t, "/bin/ls", "/usr/glenda/bin/rc")
run(t, sh, "bind -a /usr/glenda/bin /bin")
if got := run(t, sh, "ls /bin"); got != "ls\nrc\n" {
t.Errorf("ls after bind: %q", got)
}
want := "bind -a /usr/glenda/bin /bin\ncd /\n"
if got := run(t, sh, "ns"); got != want {
t.Errorf("ns:\ngot %q\nwant %q", got, want)
}
run(t, sh, "unmount /usr/glenda/bin /bin")
if got := run(t, sh, "ls /bin"); got != "ls\n" {
t.Errorf("after unmount: %q", got)
}
if got := run(t, sh, "ns"); got != "cd /\n" {
t.Errorf("ns after unmount: %q", got)
}
}
func TestMountIsASynonym(t *testing.T) {
sh, _ := shell(t, "/bin/ls", "/srv/dev/height")
run(t, sh, "mkdir /dev")
run(t, sh, "mount /srv/dev /dev")
if got := run(t, sh, "ls /dev"); got != "height\n" {
t.Errorf("ls /dev: %q", got)
}
if !strings.HasPrefix(run(t, sh, "ns"), "mount /srv/dev /dev\n") {
t.Errorf("ns should record the verb used: %q", run(t, sh, "ns"))
}
}
func TestCreateInAUnionNeedsTheFlag(t *testing.T) {
sh, _ := shell(t, "/bin/ls", "/usr/glenda/bin/rc")
run(t, sh, "bind -a /usr/glenda/bin /bin")
if _, err := sh.Run("echo x > /bin/new"); err == nil {
t.Error("creating in a union with no -c should be refused")
}
run(t, sh, "bind -ac /usr/glenda/bin /bin")
run(t, sh, "echo x > /bin/new")
if got := run(t, sh, "cat /usr/glenda/bin/new"); got != "x\n" {
t.Errorf("the create should land in the -c member: %q", got)
}
}
func TestReadOnlyModeRefusesWrites(t *testing.T) {
sh, fs := NewMemShell("glenda", ReadOnly, clock(1))
fs.WriteFile("/bin/ls", "x\n", 1)
// Reads still work.
if got := run(t, sh, "ls /bin"); got != "ls\n" {
t.Errorf("ls: %q", got)
}
if got := run(t, sh, "cat /bin/ls"); got != "x\n" {
t.Errorf("cat: %q", got)
}
if got := run(t, sh, "echo hi"); got != "hi\n" {
t.Errorf("echo without a redirect is not a write: %q", got)
}
for _, line := range []string{
"mkdir /tmp",
"rm /bin/ls",
"bind /bin /bin",
"unmount /bin",
"echo hi > /f",
} {
_, err := sh.Run(line)
if err == nil {
t.Errorf("%q should be refused in ReadOnly mode", line)
continue
}
if !strings.Contains(err.Error(), ErrReadOnly.Error()) {
t.Errorf("%q: got %v, want a read-only error", line, err)
}
}
}
func TestStopsAtTheFirstError(t *testing.T) {
sh, _ := shell(t)
out, err := sh.Run("echo one; cat /absent; echo three")
if err == nil {
t.Fatal("expected an error")
}
if out != "one\n" {
t.Errorf("output should stop at the failure: %q", out)
}
if !strings.HasPrefix(err.Error(), "cat: ") {
t.Errorf("the error should name the command: %v", err)
}
}
func TestUnknownCommand(t *testing.T) {
sh, _ := shell(t)
if _, err := sh.Run("nosuchthing"); err == nil {
t.Error("expected an error")
}
}
func TestStatAndWalkShowTheUnion(t *testing.T) {
sh, fs := shell(t)
fs.WriteFile("/bin/ls", "a\n", 100)
fs.WriteFile("/usr/glenda/bin/rc", "b\n", 100)
run(t, sh, "bind -a /usr/glenda/bin /bin")
st := run(t, sh, "stat /bin")
if !strings.Contains(st, "union=2") {
t.Errorf("stat should report the union width: %q", st)
}
w := run(t, sh, "walk /bin/rc")
lines := strings.Split(strings.TrimSpace(w), "\n")
if len(lines) != 3 {
t.Fatalf("walk should report /, /bin and /bin/rc: %q", w)
}
if !strings.Contains(lines[1], "union=2") {
t.Errorf("the union should appear at /bin: %q", lines[1])
}
if !strings.Contains(lines[2], "union=1") {
t.Errorf("a union is top level only, /bin/rc is not one: %q", lines[2])
}
}
func TestReadOnlyServerMountedInAWritableNamespace(t *testing.T) {
// This is the cross-realm shape: a synthetic, read-only tree grafted into
// a namespace whose root is writable. Reads work; writes are refused
// where the server is read-only and still allowed elsewhere.
sh, fs := shell(t)
fs.MkdirAll("/dev", 100)
tr := synfs.New("dev", "sys", clock(100))
tr.Root().Add("height", func() string { return "100" })
if err := sh.Ns().Mount(tr.Root(), "#dev", "/dev", ns.MREPL); err != nil {
t.Fatalf("mount: %v", err)
}
if got := run(t, sh, "cat /dev/height"); got != "100" {
t.Errorf("read through the mount: %q", got)
}
if got := run(t, sh, "ls /dev"); got != "height\n" {
t.Errorf("ls /dev: %q", got)
}
if _, err := sh.Run("echo x > /dev/height"); err == nil {
t.Error("writing to a read-only server should be refused")
}
if _, err := sh.Run("rm /dev/height"); err == nil || err.Error() != "rm: "+ninep.ErrReadOnly.Error() {
t.Errorf("rm on a read-only server: got %v", err)
}
// The rest of the namespace is unaffected.
run(t, sh, "echo fine > /ok")
if got := run(t, sh, "cat /ok"); got != "fine\n" {
t.Errorf("got %q", got)
}
}
func TestHelpListsEveryCommand(t *testing.T) {
sh, _ := shell(t)
out := run(t, sh, "help")
for _, cmd := range []string{"bind", "cat", "cd", "echo", "ls", "mkdir", "mount", "ns", "pwd", "rm", "stat", "unmount", "walk"} {
if !strings.Contains(out, cmd) {
t.Errorf("help does not mention %q", cmd)
}
}
}