lib9p

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

commit 2b9eb28563897f60948af9e606f8786cafc6555c
parent 1951910a2e7788c7a3d2fe2732f5e4df9c9698fc
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 23 Dec 2023 09:19:08 +0900

add setupTestConn and use it for tests

Diffstat:
Mauth_test.go | 48++++++++++++++++++++----------------------------
Mfile_test.go | 26+++++++-------------------
Mreq_test.go | 33+++++++++++----------------------
Mserver_test.go | 45+++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 83 insertions(+), 69 deletions(-)

diff --git a/auth_test.go b/auth_test.go @@ -6,25 +6,13 @@ import ( "testing" "git.mtkn.jp/lib9p" - "git.mtkn.jp/lib9p/client" - "git.mtkn.jp/lib9p/testfs" +// "git.mtkn.jp/lib9p/client" +// "git.mtkn.jp/lib9p/testfs" ) func TestAuth(t *testing.T) { - const ( - mSize = 8 * 1024 - uname = "kenji" - ) - cr, sw := io.Pipe() - sr, cw := io.Pipe() - defer func() { - cr.Close() - cw.Close() - sr.Close() - sw.Close() - }() - server := lib9p.NewServer(testfs.FS, mSize, sr, sw) - server.Chatty() + conn := setupTestConn() + defer conn.close() acr, asw := io.Pipe() asr, acw := io.Pipe() defer func() { @@ -33,7 +21,7 @@ func TestAuth(t *testing.T) { asr.Close() asw.Close() }() - server.Auth = func(ctx context.Context, respc chan<- *lib9p.Req) chan<- *lib9p.Req { + conn.s.Auth = func(ctx context.Context, respc chan<- *lib9p.Req) chan<- *lib9p.Req { c := make(chan *lib9p.Req) go func () { for { @@ -61,40 +49,38 @@ func TestAuth(t *testing.T) { }() return c } - clnt := client.NewClient(mSize, uname, cr, cw) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go server.Serve(ctx) - _, _, err := clnt.Version(ctx, lib9p.NOTAG, mSize, "9P2000") + ctx := context.Background() + _, _, err := conn.c.Version(ctx, lib9p.NOTAG, 8 * 1024, "9P2000") if err != nil { t.Log(err) } - _, err = clnt.Auth(ctx, 0, 0, "kenji", "") + _, err = conn.c.Auth(ctx, 0, 0, "kenji", "") if err != nil { t.Error(err) } - _, err = clnt.Attach(ctx, 0, 1, 0, "kenji", "") + _, err = conn.c.Attach(ctx, 0, 1, 0, "kenji", "") if err == nil { t.Error("authentication skipped") } - _, _, err = clnt.Open(ctx, 0, 0, lib9p.ORDWR) + _, _, err = conn.c.Open(ctx, 0, 0, lib9p.ORDWR) if err != nil { t.Error(err) } - _, err = clnt.Write(ctx, 0, 0, 0, 5, []byte("kenji")) + _, err = conn.c.Write(ctx, 0, 0, 0, 5, []byte("kenji")) if err != nil { t.Error(err) } - _, err = clnt.Write(ctx, 0, 0, 0, 8, []byte("password")) + _, err = conn.c.Write(ctx, 0, 0, 0, 8, []byte("password")) if err != nil { t.Error(err) } - _, err = clnt.Attach(ctx, 0, 1, 0, "kenji", "") + _, err = conn.c.Attach(ctx, 0, 1, 0, "kenji", "") if err != nil { t.Error(err) } } +// Dumb state machine... func runAuth(ctx context.Context, t *testing.T, afile *lib9p.AuthFile, r io.Reader, w io.Writer) { go func() { buf := make([]byte, 10) @@ -104,5 +90,11 @@ func runAuth(ctx context.Context, t *testing.T, afile *lib9p.AuthFile, r io.Read t.Logf("read password: %s", string(buf)) afile.AuthOK = true t.Log("authenticated") + for { + _, err := r.Read(buf) + if err != nil { + return + } + } }() } diff --git a/file_test.go b/file_test.go @@ -2,41 +2,29 @@ package lib9p_test import ( "context" - "io" "testing" "git.mtkn.jp/lib9p" - "git.mtkn.jp/lib9p/client" - "git.mtkn.jp/lib9p/testfs" ) func BenchmarkRead(b *testing.B) { - sr, cw := io.Pipe() - defer sr.Close() - defer cw.Close() - cr, sw := io.Pipe() - defer cr.Close() - defer sw.Close() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - s := lib9p.NewServer(testfs.FS, 8*1024, sr, sw) - go s.Serve(ctx) - c := client.NewClient(8*1024, "kenji", cr, cw) - defer c.Stop() - _, err := c.Attach(ctx, ^uint16(0), 0, lib9p.NOFID, "kenji", "") + conn := setupTestConn() + defer conn.close() + ctx := context.Background() + _, err := conn.c.Attach(ctx, ^uint16(0), 0, lib9p.NOFID, "kenji", "") if err != nil { b.Fatalf("attach: %v", err) } - wqid, err := c.Walk(ctx, 0, 0, 1, []string{"dir", "file"}) + wqid, err := conn.c.Walk(ctx, 0, 0, 1, []string{"dir", "file"}) if err != nil || len(wqid) != 2 { b.Fatalf("walk: %v, %v", wqid, err) } - _, _, err = c.Open(ctx, 0, 1, lib9p.OREAD) + _, _, err = conn.c.Open(ctx, 0, 1, lib9p.OREAD) if err != nil { b.Fatalf("open: %v", err) } for i := 0; i < b.N; i++ { - _, err := c.Read(ctx, 0, 1, 0, 8*1024) + _, err := conn.c.Read(ctx, 0, 1, 0, 8*1024) if err != nil { b.Fatalf("read: %v", err) } diff --git a/req_test.go b/req_test.go @@ -2,53 +2,42 @@ package lib9p_test import ( "context" - "io" "testing" "time" "git.mtkn.jp/lib9p" - "git.mtkn.jp/lib9p/client" "git.mtkn.jp/lib9p/testfs" ) func TestFlush(t *testing.T) { const ( - mSize = 1024 + mSize = 8 * 1024 uname = "kenji" ) testfs.FS.Slow = true defer func() { testfs.FS.Slow = false }() - sr, cw := io.Pipe() - defer sr.Close() - defer cw.Close() - cr, sw := io.Pipe() - defer cr.Close() - defer sw.Close() - s := lib9p.NewServer(testfs.FS, mSize, sr, sw) - s.Chatty() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go s.Serve(ctx) - c := client.NewClient(mSize, uname, cr, cw) - defer c.Stop() - _, _, err := c.Version(ctx, lib9p.NOTAG, mSize, "9P2000") + conn := setupTestConn() + defer conn.close() + ctx := context.Background() + conn.s.Chatty() + _, _, err := conn.c.Version(ctx, lib9p.NOTAG, mSize, "9P2000") if err != nil { t.Fatalf("version: %v", err) } - _, err = c.Attach(ctx, 0, 0, lib9p.NOFID, uname, "") - _, err = c.Walk(ctx, 0, 0, 1, []string{"a"}) - _, _, err = c.Open(ctx, 0, 1, lib9p.OREAD) + _, err = conn.c.Attach(ctx, 0, 0, lib9p.NOFID, uname, "") + _, err = conn.c.Walk(ctx, 0, 0, 1, []string{"a"}) + _, _, err = conn.c.Open(ctx, 0, 1, lib9p.OREAD) done := make(chan string) ctx1, cancel1 := context.WithCancel(ctx) var data []byte go func() { - data, err = c.Read(ctx1, 0, 1, 0, mSize-lib9p.IOHDRSZ) + data, err = conn.c.Read(ctx1, 0, 1, 0, mSize-lib9p.IOHDRSZ) close(done) }() // I want to send Tflush message after sending Tread and before recieving // Rread. But I think this method is not relyable. time.Sleep(1 * time.Millisecond) - err = c.Flush(ctx, 1, 0) + err = conn.c.Flush(ctx, 1, 0) if err != nil { t.Errorf("flush: %v", err) } diff --git a/server_test.go b/server_test.go @@ -8,9 +8,52 @@ import ( "testing" "git.mtkn.jp/lib9p" + "git.mtkn.jp/lib9p/client" "git.mtkn.jp/lib9p/testfs" ) +type testConn struct { + s *lib9p.Server + c *client.Client + cr, sr *io.PipeReader + cw, sw *io.PipeWriter + cancel context.CancelFunc +} + +// SetupTestConn setups a connection between a server and a client and +// returns those. +func setupTestConn() (*testConn) { + const ( + mSize = 8 * 1024 + uname = "kenji" + ) + cr, sw := io.Pipe() + sr, cw := io.Pipe() + // TODO: fix the inconsistency of server and client api. + s := lib9p.NewServer(testfs.FS, mSize, sr, sw) + c := client.NewClient(mSize, uname, cr, cw) + ctx, cancel := context.WithCancel(context.Background()) + go s.Serve(ctx) + return &testConn{ + s: s, + c: c, + cr: cr, + cw: cw, + sr: sr, + sw: sw, + cancel: cancel, + } +} + +func (conn *testConn) close() { + conn.cancel() + conn.c.Stop() + conn.cw.Close() + conn.cr.Close() + conn.sw.Close() + conn.sr.Close() +} + // This function does the actual work for TestWalk(). func testWalk(t *testing.T, fs *testfs.TestFS, pathname string, file *testfs.TestFile) { t.Logf("walk %s", pathname) @@ -34,6 +77,8 @@ func TestWalk(t *testing.T) { testWalk(t, testfs.FS, ".", testfs.FS.Root) } + +// TODO: I don't know what this test acturally tests. func TestServer(t *testing.T) { sr, cw := io.Pipe() defer sr.Close()