Compare commits

..

5 Commits

Author SHA1 Message Date
2a6fb25f03 use passed context.Context 2023-12-04 20:45:23 -06:00
0ebd162af0 login_test: minor cleanup 2023-12-04 20:40:48 -06:00
d1763418a8 removed useless closure 2023-12-04 20:33:53 -06:00
c4d885cf6d Login && Password need to be at least 4 long 2023-12-04 20:30:58 -06:00
afd5c9ef23 added login_test 2023-12-04 20:28:17 -06:00
3 changed files with 167 additions and 0 deletions

View File

@ -54,6 +54,11 @@ func (db *DBHandler) TryLogin(Login, Password string) (*Account, error) {
return nil, err return nil, err
} }
// make sure id && pw are valid
if len(Login) < 4 || len(Password) < 4 {
return nil, ErrLoginInvalidPassword
}
var account Account var account Account
row.Next() row.Next()
if err := sqlscan.ScanRow(&account, row); err != nil { if err := sqlscan.ScanRow(&account, row); err != nil {

158
login/login_test.go Normal file
View File

@ -0,0 +1,158 @@
package login_test
import (
"context"
"fmt"
"net"
"os"
"testing"
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/login"
"github.com/alicebob/miniredis/v2"
"github.com/bitcomplete/sqltestutil"
"github.com/matryer/is"
)
var (
loginSrv *login.LoginServer
loginPort int
testDB *db.DBHandler
rh *redis.RedisHandler
)
func makeDummyPeer(ctx context.Context, is *is.I, recv chan<- *cnet.PacketEvent) *cnet.Peer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", loginPort))
is.NoErr(err)
peer := cnet.NewPeer(ctx, conn)
go func() {
peer.Handler(recv)
}()
return peer
}
func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// spawn postgres container
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
if err != nil {
panic(err)
}
// open db handler
testDB, err = db.OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
if err != nil {
panic(err)
}
if err = testDB.Setup(); err != nil {
panic(err)
}
// start miniredis
r, err := miniredis.Run()
if err != nil {
panic(err)
}
defer r.Close()
// open redis handler
rh, err = redis.OpenRedis(r.Addr())
if err != nil {
panic(err)
}
defer rh.Close()
loginPort, err = cnet.RandomPort()
if err != nil {
panic(err)
}
// start login server
loginSrv, err = login.NewLoginServer(ctx, testDB, rh, loginPort)
if err != nil {
panic(err)
}
go func() {
if err := loginSrv.Start(); err != nil {
panic(err)
}
}()
// wait for login server to start, then start tests
<-loginSrv.Service().Started()
ret := m.Run()
cancel()
<-loginSrv.Service().Stopped()
os.Exit(ret)
}
func TestLoginSuccSequence(t *testing.T) {
is := is.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := makeDummyPeer(ctx, is, recv)
defer peer.Kill()
// send login request (this should create an account)
err := peer.Send(protocol.P_CL2LS_REQ_LOGIN, protocol.SP_CL2LS_REQ_LOGIN{
SzID: "testLoginSequence",
SzPassword: "test",
})
is.NoErr(err) // peer.Send() should not return an error
// receive login response
evnt := <-recv
is.Equal(int(evnt.PktID), protocol.P_LS2CL_REP_LOGIN_SUCC) // should receive P_LS2CL_REP_LOGIN_SUCC
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
err = protocol.NewPacket(evnt.Pkt).Decode(&resp)
is.NoErr(err) // packet.Decode() should not return an error
// verify response
is.Equal(resp.SzID, "testLoginSequence") // should have the same ID
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
// verify account was created
_, err = testDB.TryLogin("testLoginSequence", "test")
is.NoErr(err) // TryLogin() should not return an error
}
func TestLoginFailSequence(t *testing.T) {
is := is.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := makeDummyPeer(ctx, is, recv)
defer peer.Kill()
// send login request (this should create an account)
err := peer.Send(protocol.P_CL2LS_REQ_LOGIN, protocol.SP_CL2LS_REQ_LOGIN{
SzID: "",
SzPassword: "",
})
is.NoErr(err) // peer.Send() should not return an error
// receive login response
evnt := <-recv
is.Equal(int(evnt.PktID), protocol.P_LS2CL_REP_LOGIN_FAIL) // should receive P_LS2CL_REP_LOGIN_FAIL
var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
err = protocol.NewPacket(evnt.Pkt).Decode(&resp)
is.NoErr(err) // packet.Decode() should not return an error
// verify response
is.Equal(resp.SzID, "") // should have the same ID
is.Equal(resp.IErrorCode, int32(login.LOGIN_ID_AND_PASSWORD_DO_NOT_MATCH)) // should respond with LOGIN_ID_AND_PASSWORD_DO_NOT_MATCH
}

View File

@ -42,6 +42,10 @@ func NewLoginServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redi
return server, nil return server, nil
} }
func (server *LoginServer) Service() *cnet.Service {
return server.service
}
func (server *LoginServer) Start() error { func (server *LoginServer) Start() error {
return server.service.Start() return server.service.Start()
} }