lib9p

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

commit 58665125f9ee01e8cd45387d5841035ab970a12b
parent cdf15a98bc23ddc0ae34673d32b4b2f40d49c779
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri,  5 Jan 2024 08:50:01 +0900

add client.TestAuth

Diffstat:
Mclient/client2_test.go | 149++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 106 insertions(+), 43 deletions(-)

diff --git a/client/client2_test.go b/client/client2_test.go @@ -2,21 +2,18 @@ package client import ( "context" + "errors" + "reflect" "sync" "testing" "git.mtkn.jp/lib9p" ) -const ( - mSize = 8192 - uname = "kenji" -) - -func newClientForTest(msize uint32, uname string, tmsgc chan<- lib9p.Msg, rmsgc <-chan lib9p.Msg) *Client { +func newClientForTest(msize uint32, uname string) (*Client, <-chan lib9p.Msg, chan<- lib9p.Msg) { ctx, cancel := context.WithCancel(context.Background()) c := &Client{ - msize: mSize, + msize: msize, mSizeLock: new(sync.Mutex), uname: uname, fPool: allocClientFidPool(), @@ -24,70 +21,136 @@ func newClientForTest(msize uint32, uname string, tmsgc chan<- lib9p.Msg, rmsgc cancel: cancel, wg: new(sync.WaitGroup), } + tmsgc, rmsgc := make(chan lib9p.Msg), make(chan lib9p.Msg) c.txc = c.runMultiplexer(ctx, tmsgc, rmsgc) - return c + return c, tmsgc, rmsgc } -func TestClientVersion(t *testing.T) { +func TestVersion(t *testing.T) { tests := []struct { name string - mSize uint32 - version string + tmsg *lib9p.TVersion rmsg lib9p.Msg - wantmsize uint32 - wantversion string }{ - {"0", mSize, "9P2000", - &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "9P2000"}, - mSize, "9P2000"}, - {"1", mSize, "unko", - &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: mSize, Version: "unknown"}, - mSize, "unknown"}, + {"0", + &lib9p.TVersion{Tag: lib9p.NOTAG, Msize: 1024, Version: "9P2000"}, + &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: 1024, Version: "9P2000"}}, + {"1", + &lib9p.TVersion{Tag: lib9p.NOTAG, Msize: 1024, Version: "unko"}, + &lib9p.RVersion{Tag: lib9p.NOTAG, Msize: 1024, Version: "unknown"}}, } for _, test := range tests { func() { - tmsgc := make(chan lib9p.Msg) - rmsgc := make(chan lib9p.Msg) - c := newClientForTest(mSize, uname, tmsgc, rmsgc) - tPool := newTagPool() + c, tmsgc, rmsgc := newClientForTest(1024, "glenda") defer c.Stop() - bg := context.Background() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() var ( gotmsize uint32 gotversion string goterr error - wg sync.WaitGroup + done = make(chan struct{}) ) - wg.Add(1) go func() { - tag, err := tPool.add() - if err != nil { - t.Fatalf("add tag: %v", err) - } - gotmsize, gotversion, goterr = c.Version(bg, tag, test.mSize, test.version) - tPool.delete(tag) - wg.Done() + ifcall := test.tmsg + gotmsize, gotversion, goterr = + c.Version(ctx, ifcall.Tag, ifcall.Msize, ifcall.Version) + close(done) }() gottmsg := <-tmsgc - tag := gottmsg.GetTag() - test.rmsg.SetTag(tag) + if !reflect.DeepEqual(test.tmsg, gottmsg) { + t.Errorf("%s: tmsg modified:\n\twant: %v\n\tgot: %v", + test.name, test.tmsg, gottmsg) + return + } rmsgc <- test.rmsg - done := make(chan struct{}) - go func() { wg.Wait(); done <- struct{}{}; close(done) }() select { case err := <-c.errc: t.Errorf("client error: %v", err) return case <-done: } - if goterr != nil { - t.Errorf("%s: goterr: %v", test.name, goterr) + switch ofcall := test.rmsg.(type) { + case *lib9p.RVersion: + if goterr != nil { + t.Errorf("%s: unexpected error: %v", test.name, goterr) + return + } + if ofcall.Msize != gotmsize || ofcall.Version != gotversion { + t.Errorf("%s: (mSize, verion) want: %d, %s, got: %d, %s", + test.name, ofcall.Msize, ofcall.Version, + gotmsize, gotversion) + } + case *lib9p.RError: + if goterr == nil { + t.Errorf("%s: error expected: %v", test.name, ofcall) + } + default: + t.Errorf("%s: unexpected message: %v", test.name, ofcall) + } + }() + } +} + +func TestAuth(t *testing.T) { + tests := []struct { + name string + tmsg *lib9p.TAuth + rmsg lib9p.Msg + }{ + {"0", + &lib9p.TAuth{Tag: 0, Afid: lib9p.NOFID, Uname: "glenda", Aname: ""}, + &lib9p.RError{Tag: 0, Ename: errors.New("authentication not required.")}}, + {"1", + &lib9p.TAuth{Tag: 0, Afid: 0, Uname: "glenda", Aname: ""}, + &lib9p.RAuth{Tag: 0, Aqid: lib9p.Qid{0, 1, 2}}}, + } + for _, test := range tests { + func() { + c, tmsgc, rmsgc := newClientForTest(1024, "glenda") + defer c.Stop() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + var ( + gotaqid lib9p.Qid + goterr error + done = make(chan struct{}) + ) + go func() { + ifcall := test.tmsg + gotaqid, goterr = + c.Auth(ctx, ifcall.Tag, ifcall.Afid, ifcall.Uname, ifcall.Aname) + close(done) + }() + gottmsg := <-tmsgc + if !reflect.DeepEqual(test.tmsg, gottmsg) { + t.Errorf("%s: tmsg modified:\n\twant: %v\n\tgot: %v", + test.name, test.tmsg, gottmsg) + return + } + rmsgc <- test.rmsg + select { + case err := <-c.errc: + t.Errorf("client error: %v", err) return + case <-done: } - if test.wantmsize != gotmsize || test.wantversion != gotversion { - t.Errorf("%s: (mSize, verion) want: %d, %s, got: %d, %s", - test.name, test.wantmsize, test.wantversion, - gotmsize, gotversion) + switch ofcall := test.rmsg.(type) { + case *lib9p.RAuth: + if goterr != nil { + t.Errorf("%s: unexpected error: %v", test.name, goterr) + return + } + if ofcall.Aqid != gotaqid { + t.Errorf("%s: (qid) want: %v, got: %v", + test.name, ofcall.Aqid, gotaqid) + } + case *lib9p.RError: + if goterr == nil { + t.Errorf("%s: error expected: %v", test.name, ofcall) + } + default: + t.Errorf("%s: unexpected message: %v", test.name, ofcall) } }() }