lib9p

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

commit c1ebe2bda9bdc77e0639e993f8d043713f2a7692
parent 2f6d7d1911a3e7af803753e779ce8e05010350ad
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 28 Dec 2023 15:38:54 +0900

add TestSOpen

Diffstat:
Mexport_test.go | 9++++++++-
Mserver_test.go | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtestfs/fs.go | 5+++++
3 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/export_test.go b/export_test.go @@ -1,6 +1,9 @@ package lib9p -import "context" +import ( + "context" + "sync" +) var ( NewTVersion = newTVersion @@ -32,6 +35,7 @@ var ( SAttach = sAttach SWalk = sWalk + SOpen = sOpen HasPerm = hasPerm NewFidPool = newFidPool @@ -50,6 +54,8 @@ func (s *Server) RunSpeaker(ctx context.Context) { func (s *Server) SetFS(fs FS) { s.fs = fs } func (s *Server) SetRespChan(rc chan *Req) { s.respChan = rc } func (s *Server) SetFPool(fp *FidPool) { s.fPool = fp } +func (s *Server) NewMSizeLock() { s.mSizeLock = new(sync.Mutex) } +func (s *Server) SetMSize(size uint32) { s.setMSize(size) } func (rp *ReqPool) Add(tag uint16) (*Req, error) { return rp.add(tag) } @@ -57,6 +63,7 @@ func (fid *Fid) SetPath(path string) { fid.path = path } func (fp *FidPool) Lookup(fid uint32) (*Fid, bool) { return fp.lookup(fid) } func (fp *FidPool) Add(fid uint32) (*Fid, error) { return fp.add(fid) } +func (fp *FidPool) Delete(fid uint32) { fp.delete(fid) } func MarshalMsg(m Msg) []byte { return m.marshal() } diff --git a/server_test.go b/server_test.go @@ -150,7 +150,70 @@ func TestSWalk(t *testing.T) { // TODO: check error message. } } +} +func TestSOpen(t *testing.T) { + tests := []struct { + filePath string // file to open. + uid string + input lib9p.Msg + wantMsg lib9p.Msg + }{ + // ok + {"a", "glenda", &lib9p.TOpen{Fid: 0, Mode: lib9p.OREAD}, &lib9p.ROpen{}}, + // fid not found + {"a", "glenda", &lib9p.TOpen{Fid: 1, Mode: lib9p.OREAD}, &lib9p.RError{}}, + // permission denied + {"b", "glenda", &lib9p.TOpen{Fid: 0, Mode: lib9p.OREAD}, &lib9p.RError{}}, + {"b", "ken", &lib9p.TOpen{Fid: 0, Mode: lib9p.ORDWR}, &lib9p.ROpen{}}, + {"dir", "glenda", &lib9p.TOpen{Fid: 0, Mode: lib9p.OREAD}, &lib9p.ROpen{}}, + // is a directory + {"dir", "glenda", &lib9p.TOpen{Fid: 0, Mode: lib9p.OWRITE}, &lib9p.RError{}}, + } + tc := make(chan *lib9p.Req) + rc := make(chan *lib9p.Req) + defer close(tc) + defer close(rc) + fp := lib9p.NewFidPool() + s := &lib9p.Server{} + s.SetFS(testfs.Fsys) + s.SetRespChan(rc) + s.SetFPool(fp) + s.NewMSizeLock() + s.SetMSize(1024) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go lib9p.SOpen(ctx, s, tc) + for i, test := range tests { + fp.Delete(0) + fid, err := fp.Add(0) + if err != nil { + t.Error(i, err) + continue + } + fid.OMode = -1 + fid.SetPath(test.filePath) + fid.Uid = test.uid + fid.File, err = testfs.Fsys.WalkPath(test.filePath) + if err != nil { + t.Error(i, err) + continue + } + tc <- &lib9p.Req{Ifcall: test.input} + ofcall := (<-rc).Ofcall + switch test.wantMsg.(type) { + case *lib9p.ROpen: + if _, ok := ofcall.(*lib9p.ROpen); !ok { + t.Errorf("%d: unexpected message: %v", i, ofcall) + } + case *lib9p.RError: + if _, ok := ofcall.(*lib9p.RError); !ok { + t.Errorf("%d: unexpected message: %v", i, ofcall) + } + default: + t.Errorf("%d: unexpected message: %v", i, ofcall) + } + } } // This function does the actual work for TestWalk(). diff --git a/testfs/fs.go b/testfs/fs.go @@ -146,6 +146,11 @@ L: return cwd, nil } +func (fs *FS) WalkPath(pathname string) (*File, error) { + wnames := strings.Split(pathname, "/") + return fs.Walk(wnames) +} + var Fsys *FS func init() {