lib9p

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

auth_test.go (2262B)


      1 package lib9p
      2 
      3 import (
      4 	"context"
      5 	"io"
      6 	"testing"
      7 )
      8 
      9 func TestAuth(t *testing.T) {
     10 	c, rc := setupConn(testfs)
     11 	atc, otc, rtc, wtc := make(chan *request), make(chan *request),
     12 		make(chan *request), make(chan *request)
     13 	ctx, cancel := context.WithCancel(context.Background())
     14 	defer cancel()
     15 	go sAuth(ctx, c, atc)
     16 	go sRead(ctx, c, rtc)
     17 	go sWrite(ctx, c, wtc)
     18 	go sOpen(ctx, c, otc)
     19 	acr, asw := io.Pipe()
     20 	asr, acw := io.Pipe()
     21 	c.s.Auth = func(ctx context.Context, r *request) error {
     22 		ifcall := r.ifcall.(*TAuth)
     23 		aqid := Qid{Type: QTAUTH, Vers: 0, Path: ^uint64(0)}
     24 		r.afid.file = &AuthFile{
     25 			Qid:   aqid,
     26 			Uname: ifcall.Uname,
     27 			Aname: ifcall.Aname,
     28 			W:     acw,
     29 			R:     acr,
     30 		}
     31 		runTestAuth(ctx, t, r.afid.file.(*AuthFile), asr, asw)
     32 		r.ofcall = &RAuth{Tag: ifcall.Tag, Aqid: aqid}
     33 		return nil
     34 	}
     35 	atc <- &request{ifcall: &TAuth{Afid: 0, Uname: "kenji"}}
     36 	ofcall := (<-rc).ofcall
     37 	if rerror, ok := ofcall.(*RError); ok {
     38 		t.Fatal(rerror)
     39 	}
     40 	otc <- &request{ifcall: &TOpen{Fid: 0, Mode: ORDWR}}
     41 	ofcall = (<-rc).ofcall
     42 	if rerror, ok := ofcall.(*RError); ok {
     43 		t.Fatal(rerror)
     44 	}
     45 	data := []byte("password")
     46 	wtc <- &request{ifcall: &TWrite{Fid: 0, Count: uint32(len(data)), Data: data}}
     47 	ofcall = (<-rc).ofcall
     48 	if rerror, ok := ofcall.(*RError); ok {
     49 		t.Fatal(rerror)
     50 	}
     51 	rtc <- &request{ifcall: &TRead{Fid: 0, Count: 1024}}
     52 	ofcall = (<-rc).ofcall
     53 	if rerror, ok := ofcall.(*RError); ok {
     54 		t.Fatal(rerror)
     55 	}
     56 }
     57 
     58 // Dumb state machine...
     59 // TODO: return when ctx is canceled
     60 func runTestAuth(ctx context.Context, t *testing.T, afile *AuthFile, r io.Reader, w io.Writer) {
     61 	go func() {
     62 		buf := make([]byte, 10)
     63 		uname := "kenji"
     64 		password := "password"
     65 		state := 0
     66 		for {
     67 			switch state {
     68 			case 0: // start
     69 				if afile.Uname != uname {
     70 					state = 2
     71 					break
     72 				}
     73 				n, err := r.Read(buf)
     74 				if err != nil {
     75 					t.Log(err)
     76 					break
     77 				}
     78 				if string(buf[:n]) == password {
     79 					state = 1
     80 					break
     81 				}
     82 			case 1: // accepted
     83 				afile.AuthOK = true
     84 				go func() {
     85 					for {
     86 						r.Read(buf)
     87 					}
     88 				}()
     89 				for {
     90 					w.Write([]byte("ok"))
     91 				}
     92 			case 2: // unknown user
     93 				go func() {
     94 					for {
     95 						r.Read(buf)
     96 					}
     97 				}()
     98 				for {
     99 					w.Write([]byte("unknown user"))
    100 				}
    101 			}
    102 		}
    103 	}()
    104 }