commit d345463119a661c910eb059b4e1668beb71ed5f2
parent 393c60a3956751d089415cd24de1d50e7a38c6ef
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 3 Jan 2024 13:09:16 +0900
change auth_test from lib9p_test to lib9p
Diffstat:
M | auth_test.go | | | 87 | ++++++++++++++++++++++++++++++++----------------------------------------------- |
1 file changed, 35 insertions(+), 52 deletions(-)
diff --git a/auth_test.go b/auth_test.go
@@ -1,79 +1,62 @@
-package lib9p_test
+package lib9p
import (
"context"
"io"
"testing"
-
- "git.mtkn.jp/lib9p"
- "git.mtkn.jp/lib9p/testfs"
)
func TestAuth(t *testing.T) {
- conn := testfs.SetupConn()
- defer conn.Close()
+ c, _, rc := setupConn(testfs)
+ atc, otc, rtc, wtc := make(chan *request), make(chan *request),
+ make(chan *request), make(chan *request)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ go sAuth(ctx, c, atc)
+ go sRead(ctx, c, rtc)
+ go sWrite(ctx, c, wtc)
+ go sOpen(ctx, c, otc)
acr, asw := io.Pipe()
asr, acw := io.Pipe()
- defer func() {
- acr.Close()
- acw.Close()
- }()
- conn.S.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().SetFile(&lib9p.AuthFile{
+ c.s.Auth = func(ctx context.Context, r *request) {
+ ifcall := r.Ifcall().(*TAuth)
+ aqid := Qid{Type: QTAUTH, Vers: 0, Path: ^uint64(0)}
+ r.afid.file = &AuthFile{
Qid: aqid,
Uname: ifcall.Uname,
Aname: ifcall.Aname,
W: acw,
R: acr,
- })
- go func() {
- <-ctx.Done()
- asr.Close()
- asw.Close()
- }()
- runAuth(ctx, t, r.Afid().File().(*lib9p.AuthFile), asr, asw)
- r.SetOfcall(&lib9p.RAuth{Tag: ifcall.Tag, Aqid: aqid})
- }
- ctx := context.Background()
- _, _, err := conn.C.Version(ctx, lib9p.NOTAG, 8*1024, "9P2000")
- if err != nil {
- t.Log(err)
- }
- _, err = conn.C.Auth(ctx, 0, 0, "kenji", "")
- if err != nil {
- t.Fatal(err)
- }
- _, err = conn.C.Attach(ctx, 0, 1, 0, "kenji", "")
- if err == nil {
- t.Fatal("authentication skipped")
- }
- _, _, err = conn.C.Open(ctx, 0, 0, lib9p.ORDWR)
- if err != nil {
- t.Fatal(err)
+ }
+ runAuth(ctx, t, r.afid.file.(*AuthFile), asr, asw)
+ r.ofcall = &RAuth{Tag: ifcall.Tag, Aqid: aqid}
}
- _, err = conn.C.Write(ctx, 0, 0, 0, 8, []byte("password"))
- if err != nil {
- t.Fatal(err)
+ atc <- &request{ifcall: &TAuth{Afid: 0, Uname: "kenji"}}
+ ofcall := (<-rc).ofcall
+ if rerror, ok := ofcall.(*RError); ok {
+ t.Fatal(rerror)
}
- _, err = conn.C.Read(ctx, 0, 0, 0, 1024)
- if err != nil {
- t.Fatal(err)
+ otc <- &request{ifcall: &TOpen{Fid: 0, Mode: ORDWR}}
+ ofcall = (<-rc).ofcall
+ if rerror, ok := ofcall.(*RError); ok {
+ t.Fatal(rerror)
}
- _, err = conn.C.Attach(ctx, 0, 1, 0, "kenji", "")
- if err != nil {
- t.Fatal(err)
+ data := []byte("password")
+ wtc <- &request{ifcall: &TWrite{Fid: 0, Count: uint32(len(data)), Data: data}}
+ ofcall = (<-rc).ofcall
+ if rerror, ok := ofcall.(*RError); ok {
+ t.Fatal(rerror)
}
- _, err = conn.C.Attach(ctx, 0, 2, 0, "unko", "")
- if err == nil {
- t.Fatal("authentication skipped")
+ rtc <- &request{ifcall: &TRead{Fid: 0, Count: 1024}}
+ ofcall = (<-rc).ofcall
+ if rerror, ok := ofcall.(*RError); ok {
+ t.Fatal(rerror)
}
}
// Dumb state machine...
// TODO: return when ctx is canceled
-func runAuth(ctx context.Context, t *testing.T, afile *lib9p.AuthFile, r io.Reader, w io.Writer) {
+func runAuth(ctx context.Context, t *testing.T, afile *AuthFile, r io.Reader, w io.Writer) {
go func() {
buf := make([]byte, 10)
uname := "kenji"