lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 6705e6e75c733db7a67668cfe42820e742abbdac
parent 38c3bc8fa492579416f39451b013bb5d5e2613e0
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 27 Oct 2023 09:06:34 +0900

update tests

Diffstat:
Mclient/client_test.go | 107++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mserver_test.go | 8++++----
Mtest_fs.go | 130++++++++++++++++++++++++++++++++++++++++----------------------------------------
3 files changed, 164 insertions(+), 81 deletions(-)

diff --git a/client/client_test.go b/client/client_test.go @@ -3,8 +3,11 @@ package client import ( "context" "io" + "io/fs" "sync" "testing" + + "git.mtkn.jp/lib9p" ) const ( @@ -12,7 +15,7 @@ const ( uname = "kenji" ) -func newClientForTest(msize uint32, uname string, tmsgc chan<- Msg, rmsgc <-chan Msg) *Client { +func newClientForTest(msize uint32, uname string, tmsgc chan<- lib9p.Msg, rmsgc <-chan lib9p.Msg) *Client { ctx, cancel := context.WithCancel(context.Background()) c := &Client{ msize: mSize, @@ -32,21 +35,21 @@ func TestClientVersion(t *testing.T) { name string mSize uint32 version string - rmsg Msg + rmsg lib9p.Msg wantmsize uint32 wantversion string }{ {"0", mSize, "9P2000", - &RVersion{tag: NOTAG, mSize: mSize, version: "9P2000"}, + &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "9P2000"}, mSize, "9P2000"}, {"1", mSize, "unko", - &RVersion{tag: NOTAG, mSize: mSize, version: "unknown"}, + &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "unknown"}, mSize, "unknown"}, } for _, test := range tests { func() { - tmsgc := make(chan Msg) - rmsgc := make(chan Msg) + tmsgc := make(chan lib9p.Msg) + rmsgc := make(chan lib9p.Msg) c := newClientForTest(mSize, uname, tmsgc, rmsgc) tPool := newTagPool() defer c.Stop() @@ -68,7 +71,7 @@ func TestClientVersion(t *testing.T) { wg.Done() }() gottmsg := <-tmsgc - tag := gottmsg.Tag() + tag := gottmsg.GetTag() test.rmsg.SetTag(tag) rmsgc <- test.rmsg done := make(chan struct{}) @@ -98,7 +101,7 @@ func TestTransaction(t *testing.T) { cr, sw := io.Pipe() sr, cw := io.Pipe() - server := NewServer(fsys, mSize, sr, sw) + server := lib9p.NewServer(fsys, mSize, sr, sw) //server.Chatty() client := NewClient(mSize, uname, cr, cw) tPool := newTagPool() @@ -113,7 +116,7 @@ func TestTransaction(t *testing.T) { if err != nil { t.Log(err) } else { - t.Log(&RVersion{mSize: rmsize, version: rversion}) + t.Log(&lib9p.RVersion{Msize: rmsize, Version: rversion}) } tag, err = tPool.add() if err != nil { @@ -130,7 +133,7 @@ func TestTransaction(t *testing.T) { if err != nil { t.Fatalf("add tag: %v", err) } - rattach, err := client.Attach(bg, tag, 0, NOFID, "kenji", "") + rattach, err := client.Attach(bg, tag, 0, lib9p.NOFID, "kenji", "") tPool.delete(tag) if err != nil { t.Log(err) @@ -143,7 +146,7 @@ func TestClient2(t *testing.T) { cr, sw := io.Pipe() sr, cw := io.Pipe() - server := NewServer(fsys, mSize, sr, sw) + server := lib9p.NewServer(fsys, mSize, sr, sw) //server.Chatty() go server.Serve(context.Background()) @@ -152,7 +155,7 @@ func TestClient2(t *testing.T) { t.Errorf("mount: %v", err) } - a0, err := fs.OpenFile("a", OREAD, 0) + a0, err := fs.OpenFile("a", lib9p.OREAD, 0) if err != nil { t.Errorf("open: %v", err) } @@ -166,3 +169,83 @@ func TestClient2(t *testing.T) { } t.Log(string(b[:n])) } + +var fsys *lib9p.TestFS + +func init() { + fsys = &lib9p.TestFS{ + Root: &lib9p.TestFile{ + St: lib9p.Stat{ + Qid: lib9p.Qid{Path: 0, Type: lib9p.QTDIR}, + Mode: lib9p.FileMode(fs.ModeDir | 0755), + Name: "root", + Uid: "glenda", + Gid: "glenda", + Muid: "glenda", + }, + Children: []*lib9p.TestFile{ + &lib9p.TestFile{ + Content: []byte("a\n"), + St: lib9p.Stat{ + Qid: lib9p.Qid{Path: 1, Type: lib9p.QTFILE}, + Mode: lib9p.FileMode(0644), + Name: "a", + Uid: "glenda", + Gid: "glenda", + Muid: "glenda", + }, + }, + &lib9p.TestFile{ + Content: []byte("b\n"), + St: lib9p.Stat{ + Qid: lib9p.Qid{Path: 2, Type: lib9p.QTFILE}, + Mode: lib9p.FileMode(0400), + Name: "b", + Uid: "ken", + Gid: "ken", + Muid: "ken", + }, + }, + &lib9p.TestFile{ + St: lib9p.Stat{ + Qid: lib9p.Qid{Path: 3, Type: lib9p.QTDIR}, + Mode: lib9p.FileMode(fs.ModeDir | 0755), + Name: "dir", + Uid: "rob", + Gid: "rob", + Muid: "rob", + }, + Children: []*lib9p.TestFile{ + &lib9p.TestFile{ + Content: []byte("unko\n"), + St: lib9p.Stat{ + Qid: lib9p.Qid{Path: 4, Type: lib9p.QTFILE}, + Mode: lib9p.FileMode(0666), + Name: "file", + Uid: "brian", + Gid: "brian", + Muid: "dennis", + }, + }, + }, + }, + }, + }, + } + SetFsysAndParent(fsys, nil) +} + +// SetFsysAndParent sets file.fsys and file.parent for every file in the fsys. +// Pass nil as file to setup entire file system. +func SetFsysAndParent(fsys *lib9p.TestFS, file *lib9p.TestFile) { + if file == nil { + file = fsys.Root + file.Parent = fsys.Root + file.Fsys = fsys + } + for _, child := range file.Children { + child.Parent = file + child.Fsys = fsys + SetFsysAndParent(fsys, child) + } +} diff --git a/server_test.go b/server_test.go @@ -51,7 +51,7 @@ func handleReq(ctx context.Context, s *Server, r *Req) { } // This function does the actual work for TestWalk(). -func testWalk(t *testing.T, fs *testFS, path string, file *testFile) { +func testWalk(t *testing.T, fs *TestFS, path string, file *TestFile) { t.Logf("walk %s", path) f, err := fs.walk(split9path(path)) if err != nil { @@ -60,8 +60,8 @@ func testWalk(t *testing.T, fs *testFS, path string, file *testFile) { if f != file { t.Errorf("open %s: wrong file", path) } - for _, child := range file.children { - childpath := filepath.Join(path, child.stat.Name) + for _, child := range file.Children { + childpath := filepath.Join(path, child.St.Name) testWalk(t, fs, childpath, child) } } @@ -70,7 +70,7 @@ func testWalk(t *testing.T, fs *testFS, path string, file *testFile) { // checks if all files can be opened without error and if // the opened file is the same as the file accessed via testFS.child func TestWalk(t *testing.T) { - testWalk(t, fsys, ".", fsys.root) + testWalk(t, fsys, ".", fsys.Root) } func TestServer(t *testing.T) { diff --git a/test_fs.go b/test_fs.go @@ -11,89 +11,89 @@ import ( const sleepTime = 1 * time.Second -type testFile struct { - fsys *testFS - parent *testFile - children []*testFile - content []byte - reader *bytes.Reader - - stat Stat +type TestFile struct { + Fsys *TestFS + Parent *TestFile + Children []*TestFile + Content []byte + Reader *bytes.Reader + + St Stat } -func (f *testFile) Stat() (*FileInfo, error) { - return &FileInfo{Stat: f.stat}, nil +func (f *TestFile) Stat() (*FileInfo, error) { + return &FileInfo{Stat: f.St}, nil } -func (f *testFile) Close() error { - f.reader = nil +func (f *TestFile) Close() error { + f.Reader = nil return nil } -func (f *testFile) Read(b []byte) (int, error) { - if f.fsys.slow { +func (f *TestFile) Read(b []byte) (int, error) { + if f.Fsys.Slow { time.Sleep(sleepTime) } - return f.reader.Read(b) + return f.Reader.Read(b) } -func (f *testFile) ReadAt(b []byte, off int64) (n int, err error) { - if f.fsys.slow { +func (f *TestFile) ReadAt(b []byte, off int64) (n int, err error) { + if f.Fsys.Slow { time.Sleep(sleepTime) } - return f.reader.ReadAt(b, off) + return f.Reader.ReadAt(b, off) } -func (f *testFile) ReadDir(n int) ([]*DirEntry, error) { - de := make([]*DirEntry, len(f.children)) - for i, c := range f.children { +func (f *TestFile) ReadDir(n int) ([]*DirEntry, error) { + de := make([]*DirEntry, len(f.Children)) + for i, c := range f.Children { de[i], _ = c.Stat() } return de, nil } -func (f *testFile) WriteAt(p []byte, off int64) (int, error) { - if f.fsys.slow { +func (f *TestFile) WriteAt(p []byte, off int64) (int, error) { + if f.Fsys.Slow { time.Sleep(sleepTime) } - if f.reader == nil { + if f.Reader == nil { return 0, fmt.Errorf("not open") } - if off < 0 || off > int64(len(f.content)) { + if off < 0 || off > int64(len(f.Content)) { return 0, fmt.Errorf("bad offset") } - if off+int64(len(p)) > int64(len(f.content)) { + if off+int64(len(p)) > int64(len(f.Content)) { newcon := make([]byte, off+int64(len(p))) - copy(newcon, f.content) - f.content = newcon + copy(newcon, f.Content) + f.Content = newcon } - copy(f.content[off:], p) - f.reader.Reset(f.content) + copy(f.Content[off:], p) + f.Reader.Reset(f.Content) return len(p), nil } -type testFS struct { - root *testFile - slow bool +type TestFS struct { + Root *TestFile + Slow bool } -func (fs *testFS) OpenFile(path string, omode OpenMode, perm fs.FileMode) (File, error) { +func (fs *TestFS) OpenFile(path string, omode OpenMode, perm fs.FileMode) (File, error) { path = clean9path(path) wnames := split9path(path) f, err := fs.walk(wnames) if err != nil { return nil, fmt.Errorf("walk: %v", err) } - f.reader = bytes.NewReader(f.content) + f.Reader = bytes.NewReader(f.Content) return f, nil } -func (fs *testFS) walk(wnames []string) (*testFile, error) { - cwd := fs.root +func (fs *TestFS) walk(wnames []string) (*TestFile, error) { + cwd := fs.Root L: for i, name := range wnames { - for _, child := range cwd.children { - if child.stat.Name == name { + for _, child := range cwd.Children { + if child.St.Name == name { cwd = child continue L } @@ -116,12 +116,12 @@ func split9path(path string) []string { return strings.Split(path, "/") } -var fsys *testFS +var fsys *TestFS func init() { - fsys = &testFS{ - root: &testFile{ - stat: Stat{ + fsys = &TestFS{ + Root: &TestFile{ + St: Stat{ Qid: Qid{Path: 0, Type: QTDIR}, Mode: FileMode(fs.ModeDir | 0755), Name: "root", @@ -129,10 +129,10 @@ func init() { Gid: "glenda", Muid: "glenda", }, - children: []*testFile{ - &testFile{ - content: []byte("a\n"), - stat: Stat{ + Children: []*TestFile{ + &TestFile{ + Content: []byte("a\n"), + St: Stat{ Qid: Qid{Path: 1, Type: QTFILE}, Mode: FileMode(0644), Name: "a", @@ -141,9 +141,9 @@ func init() { Muid: "glenda", }, }, - &testFile{ - content: []byte("b\n"), - stat: Stat{ + &TestFile{ + Content: []byte("b\n"), + St: Stat{ Qid: Qid{Path: 2, Type: QTFILE}, Mode: FileMode(0400), Name: "b", @@ -152,8 +152,8 @@ func init() { Muid: "ken", }, }, - &testFile{ - stat: Stat{ + &TestFile{ + St: Stat{ Qid: Qid{Path: 3, Type: QTDIR}, Mode: FileMode(fs.ModeDir | 0755), Name: "dir", @@ -161,10 +161,10 @@ func init() { Gid: "rob", Muid: "rob", }, - children: []*testFile{ - &testFile{ - content: []byte("unko\n"), - stat: Stat{ + Children: []*TestFile{ + &TestFile{ + Content: []byte("unko\n"), + St: Stat{ Qid: Qid{Path: 4, Type: QTFILE}, Mode: FileMode(0666), Name: "file", @@ -178,20 +178,20 @@ func init() { }, }, } - setFsysAndParent(fsys, nil) + SetFsysAndParent(fsys, nil) } // SetFsysAndParent sets file.fsys and file.parent for every file in the fsys. // Pass nil as file to setup entire file system. -func setFsysAndParent(fsys *testFS, file *testFile) { +func SetFsysAndParent(fsys *TestFS, file *TestFile) { if file == nil { - file = fsys.root - file.parent = fsys.root - file.fsys = fsys + file = fsys.Root + file.Parent = fsys.Root + file.Fsys = fsys } - for _, child := range file.children { - child.parent = file - child.fsys = fsys - setFsysAndParent(fsys, child) + for _, child := range file.Children { + child.Parent = file + child.Fsys = fsys + SetFsysAndParent(fsys, child) } }