commit 2f6d7d1911a3e7af803753e779ce8e05010350ad
parent 2a696ba0fa97355469abb4f35e521b76ce5b9918
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 28 Dec 2023 12:13:00 +0900
add TestSWalk
Diffstat:
2 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/export_test.go b/export_test.go
@@ -31,6 +31,7 @@ var (
NewRWStat = newRWStat
SAttach = sAttach
+ SWalk = sWalk
HasPerm = hasPerm
NewFidPool = newFidPool
@@ -52,6 +53,8 @@ func (s *Server) SetFPool(fp *FidPool) { s.fPool = fp }
func (rp *ReqPool) Add(tag uint16) (*Req, error) { return rp.add(tag) }
+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) }
diff --git a/server_test.go b/server_test.go
@@ -2,6 +2,7 @@ package lib9p_test
import (
"context"
+ "errors"
"io"
"path"
"strings"
@@ -82,13 +83,76 @@ func TestSAttach(t *testing.T) {
t.Errorf("%d: unexpected message: %v", i, ofcall)
return
}
- t.Logf("%d: %v", i, ofcall)
default:
t.Fatalf("%d: unexpected message: %v", i, wantmsg)
}
}
}
+func TestSWalk(t *testing.T) {
+ tests := []struct {
+ input *lib9p.Req
+ wantLen int // len(Ofcall.(*lib9p.RWalk).Qids)
+ wantErr error
+ }{
+ {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 1, Wnames: []string{"a"}}},
+ 1, nil},
+ {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 2, Wnames: []string{"b"}}},
+ 1, nil},
+ {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 3, Wnames: []string{"dir", "file"}}},
+ 2, nil},
+ {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 4, Wnames: []string{"dir", "unko"}}},
+ 1, nil}, // short walk
+ // 9P document says:
+ // If the first element cant be walked for any reason,
+ // RError is returned.
+ {&lib9p.Req{Ifcall: &lib9p.TWalk{Fid: 0, Newfid: 5, Wnames: []string{"unko", "unko"}}},
+ 0, errors.New("not found")},
+ }
+ tc := make(chan *lib9p.Req)
+ rc := make(chan *lib9p.Req)
+ defer close(tc)
+ defer close(rc)
+ fp := lib9p.NewFidPool()
+ fid, err := fp.Add(0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ fid.OMode = -1
+ fid.SetPath(".")
+ s := &lib9p.Server{}
+ s.SetFS(testfs.Fsys)
+ s.SetRespChan(rc)
+ s.SetFPool(fp)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ go lib9p.SWalk(ctx, s, tc)
+ for i, test := range tests {
+ tc <- test.input
+ ofcall := (<-rc).Ofcall
+ if test.wantErr == nil {
+ gotMsg, ok := ofcall.(*lib9p.RWalk)
+ if !ok {
+ t.Errorf("%d: unexpected message: %v", i, ofcall)
+ continue
+ }
+ if test.wantLen != len(gotMsg.Qids) {
+ t.Errorf("%d: walk length mismatch: want: %d, got: %d",
+ i, test.wantLen, len(gotMsg.Qids))
+ continue
+ }
+ } else {
+ _, ok := ofcall.(*lib9p.RError)
+ if !ok {
+ t.Errorf("%d: unexpected message: %v", i, ofcall)
+ continue
+ }
+ // TODO: check error message.
+ }
+ }
+
+}
+
// 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)