lib9p

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

commit 98d89fa079b3fe3c485c938a7fbd1e16d17b0345
parent 1ba9da1f4914f5a94fb1028a580201da68e9ab71
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 28 Dec 2023 08:24:58 +0900

add TestSAttach

Diffstat:
Mexport_test.go | 10+++++++++-
Mserver_test.go | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/export_test.go b/export_test.go @@ -29,6 +29,11 @@ var ( NewRStat = newRStat NewTWStat = newTWStat NewRWStat = newRWStat + + SAttach = sAttach + + HasPerm = hasPerm + NewFidPool = newFidPool ) type BufMsg = bufMsg @@ -41,11 +46,14 @@ func (s *Server) RunListener(ctx context.Context) { func (s *Server) RunSpeaker(ctx context.Context) { s.runSpeaker(ctx) } +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 (rp *ReqPool) Add(tag uint16) (*Req, error) { return rp.add(tag) } 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 MarshalMsg(m Msg) []byte { return m.marshal() } -var HasPerm = hasPerm diff --git a/server_test.go b/server_test.go @@ -11,6 +11,84 @@ import ( "git.mtkn.jp/lib9p/testfs" ) +func TestSAttach(t *testing.T) { + dammyAuth := func(context.Context, <-chan *lib9p.Req, chan<- *lib9p.Req) {} + af := &lib9p.AuthFile{ + Qid: lib9p.Qid{Type: lib9p.QTAUTH}, + Uname: "kenji", + Aname: "", + } + fp := lib9p.NewFidPool() + fid, err := fp.Add(0) + if err != nil { + t.Fatal(err) + } + fid.File = af + tests := []struct { + input *lib9p.Req + auth bool + authOK bool + want *lib9p.Req + }{ + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 0, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + false, false, + &lib9p.Req{Ofcall: &lib9p.RError{}}}, // duplicate fid + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 1, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + false, true, + &lib9p.Req{Ofcall: &lib9p.RAttach{}}}, // ok + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: lib9p.NOFID, Uname: "kenji", Aname: ""}}, + true, false, + &lib9p.Req{Ofcall: &lib9p.RError{}}}, // afid is not set + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}}, + true, false, + &lib9p.Req{Ofcall: &lib9p.RError{}}}, // not authOK + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "unko", Aname: ""}}, + true, true, + &lib9p.Req{Ofcall: &lib9p.RError{}}}, // wrong user + {&lib9p.Req{Ifcall: &lib9p.TAttach{Fid: 2, Afid: 0, Uname: "kenji", Aname: ""}}, + true, true, + &lib9p.Req{Ofcall: &lib9p.RAttach{}}}, // ok + } + tc := make(chan *lib9p.Req) + rc := make(chan *lib9p.Req) + defer close(tc) + defer close(rc) + s := &lib9p.Server{} + s.SetFS(testfs.Fsys) + s.SetRespChan(rc) + s.SetFPool(fp) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go lib9p.SAttach(ctx, s, tc) + for i, test := range tests { + af.AuthOK = test.authOK + if test.auth { + s.Auth = dammyAuth + } else { + s.Auth = nil + } + tc <- test.input + ofcall := (<-rc).Ofcall + switch wantmsg := test.want.Ofcall.(type) { + case *lib9p.RAttach: + _, ok := ofcall.(*lib9p.RAttach) + if !ok { + t.Errorf("%d: unexpected message: %v", i, ofcall) + return + } + case *lib9p.RError: + _, ok := ofcall.(*lib9p.RError) + if !ok { + t.Errorf("%d: unexpected message: %v", i, ofcall) + return + } + t.Logf("%d: %v", i, ofcall) + default: + t.Fatalf("%d: unexpected message: %v", i, wantmsg) + } + } +} + // This function does the actual work for TestWalk(). func testWalk(t *testing.T, fs *testfs.FS, pathname string, file *testfs.File) { t.Logf("walk %s", pathname)