commit c89eeddf70bff42358191ad1c57690eb25ffdc72
parent bc92ce34656ccfb2ae4ca7a2b1c440f690578268
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 22 Dec 2023 12:08:18 +0900
update auth test
Diffstat:
2 files changed, 35 insertions(+), 16 deletions(-)
diff --git a/auth_test.go b/auth_test.go
@@ -33,19 +33,33 @@ func TestAuth(t *testing.T) {
asr.Close()
asw.Close()
}()
- server.Auth = func(ctx context.Context, r *lib9p.Req) {
- ifcall := r.Ifcall.(*lib9p.TAuth)
- aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)}
- r.Afid.File = &lib9p.AuthFile{
- Qid: aqid,
- Uname: ifcall.Uname,
- Aname: ifcall.Aname,
- W: acw,
- R: acr,
- }
- runAuth(ctx, t, r.Afid.File.(*lib9p.AuthFile), asr, asw)
- r.Ofcall = &lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid}
- lib9p.Respond(ctx, r, nil)
+ server.Auth = func(ctx context.Context, respc chan<- *lib9p.Req) chan<- *lib9p.Req {
+ c := make(chan *lib9p.Req)
+ go func () {
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case r := <-c:
+ if r == nil {
+ return
+ }
+ ifcall := r.Ifcall.(*lib9p.TAuth)
+ aqid := lib9p.Qid{Type: lib9p.QTAUTH, Vers: 0, Path: ^uint64(0)}
+ r.Afid.File = &lib9p.AuthFile{
+ Qid: aqid,
+ Uname: ifcall.Uname,
+ Aname: ifcall.Aname,
+ W: acw,
+ R: acr,
+ }
+ runAuth(ctx, t, r.Afid.File.(*lib9p.AuthFile), asr, asw)
+ r.Ofcall = &lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid}
+ respc <- r
+ }
+ }
+ }()
+ return c
}
clnt := client.NewClient(mSize, uname, cr, cw)
ctx, cancel := context.WithCancel(context.Background())
diff --git a/server.go b/server.go
@@ -69,7 +69,7 @@ type Server struct {
// set Req.Afid.File to an *AuthFile and Req.Ofcall.Qid, and prepare to
// authenticate via the Read()/Write() calls to Req.Afid.File
// If this is nil, no authentication is performed.
- Auth func(context.Context, *Req, chan<- *Req)
+ Auth func(context.Context, chan<- *Req) chan<- *Req
}
// NewServer creates a Server and runs listener and speaker goroutines.
@@ -251,6 +251,11 @@ func sAuth(ctx context.Context, s *Server, c <-chan *Req) {
rc := make(chan *Req)
defer close(rc)
go rAuth(ctx, rc)
+ var authc chan<- *Req
+ if s.Auth != nil {
+ authc = s.Auth(ctx, rc)
+ defer close(authc)
+ }
for {
select {
case <-ctx.Done():
@@ -267,8 +272,8 @@ func sAuth(ctx context.Context, s *Server, c <-chan *Req) {
rc <- r
continue
}
- if s.Auth != nil {
- s.Auth(ctx, r, rc)
+ if authc != nil {
+ authc <- r
} else {
r.err = fmt.Errorf("authentication not required")
rc <- r