login_test: refactor, abstracted send and recv

validation
This commit is contained in:
CPunch 2023-12-06 20:15:06 -06:00
parent 77751a2aa0
commit e1b9fa5d99

View File

@ -56,6 +56,19 @@ func makeDummyPeer(ctx context.Context, is *is.I, recv chan<- *cnet.PacketEvent)
return peer return peer
} }
func sendAndRecv(peer *cnet.Peer, recv chan *cnet.PacketEvent, is *is.I, sID, rID uint32, out, in interface{}) {
// send login request (this should create an account)
err := peer.Send(sID, out)
is.NoErr(err) // peer.Send() should not return an error
// receive login response
evnt := <-recv
is.Equal(int(evnt.PktID), rID) // should receive expected type
err = protocol.NewPacket(evnt.Pkt).Decode(in)
is.NoErr(err) // packet.Decode() should not return an error
}
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -126,26 +139,19 @@ func TestLoginSuccSequence(t *testing.T) {
defer peer.Kill() defer peer.Kill()
// send login request (this should create an account) // send login request (this should create an account)
err := peer.Send(protocol.P_CL2LS_REQ_LOGIN, protocol.SP_CL2LS_REQ_LOGIN{ var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
protocol.SP_CL2LS_REQ_LOGIN{
SzID: "testLoginSequence", SzID: "testLoginSequence",
SzPassword: "test", SzPassword: "test",
}) }, &resp)
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 // verify response
is.Equal(resp.SzID, "testLoginSequence") // should have the same ID is.Equal(resp.SzID, "testLoginSequence") // should have the same ID
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
// verify account was created // verify account was created
_, err = testDB.TryLogin("testLoginSequence", "test") _, err := testDB.TryLogin("testLoginSequence", "test")
is.NoErr(err) // TryLogin() should not return an error is.NoErr(err) // TryLogin() should not return an error
} }
@ -160,19 +166,12 @@ func TestLoginFailSequence(t *testing.T) {
defer peer.Kill() defer peer.Kill()
// send login request (this should not create an account) // send login request (this should not create an account)
err := peer.Send(protocol.P_CL2LS_REQ_LOGIN, protocol.SP_CL2LS_REQ_LOGIN{ var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
protocol.SP_CL2LS_REQ_LOGIN{
SzID: "", SzID: "",
SzPassword: "", SzPassword: "",
}) }, &resp)
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 // verify response
is.Equal(resp.SzID, "") // should have the same ID is.Equal(resp.SzID, "") // should have the same ID
@ -190,22 +189,15 @@ func TestCharacterSequence(t *testing.T) {
defer peer.Kill() defer peer.Kill()
// send login request (this should create an account) // send login request (this should create an account)
err := peer.Send(protocol.P_CL2LS_REQ_LOGIN, protocol.SP_CL2LS_REQ_LOGIN{
SzID: "TestCharacterSequence",
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 var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
err = protocol.NewPacket(evnt.Pkt).Decode(&resp) sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
is.NoErr(err) // packet.Decode() should not return an error protocol.SP_CL2LS_REQ_LOGIN{
SzID: "testCharacterSequence",
SzPassword: "test",
}, &resp)
// verify response // verify response
is.Equal(resp.SzID, "TestCharacterSequence") // should have the same ID is.Equal(resp.SzID, "testCharacterSequence") // should have the same ID
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
// perform key swap // perform key swap
@ -221,7 +213,9 @@ func TestCharacterSequence(t *testing.T) {
) )
// send character name check request // send character name check request
err = peer.Send(protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{ var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{
ISlotNum: 1, ISlotNum: 1,
IGender: 1, IGender: 1,
IFNCode: 260, IFNCode: 260,
@ -229,16 +223,7 @@ func TestCharacterSequence(t *testing.T) {
IMNCode: 33, IMNCode: 33,
SzFirstName: testCharCreate.PCStyle.SzFirstName, SzFirstName: testCharCreate.PCStyle.SzFirstName,
SzLastName: testCharCreate.PCStyle.SzLastName, SzLastName: testCharCreate.PCStyle.SzLastName,
}) }, &charResp)
is.NoErr(err) // peer.Send() should not return an error
// receive character creation response
evnt = <-recv
is.Equal(int(evnt.PktID), protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC) // should receive P_LS2CL_REP_SAVE_CHAR_NAME_SUCC
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
err = protocol.NewPacket(evnt.Pkt).Decode(&charResp)
is.NoErr(err) // packet.Decode() should not return an error
// verify response // verify response
is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number
@ -249,16 +234,9 @@ func TestCharacterSequence(t *testing.T) {
// send character create request // send character create request
charCreate := testCharCreate charCreate := testCharCreate
charCreate.PCStyle.IPC_UID = charResp.IPC_UID charCreate.PCStyle.IPC_UID = charResp.IPC_UID
err = peer.Send(protocol.P_CL2LS_REQ_CHAR_CREATE, charCreate)
is.NoErr(err) // peer.Send() should not return an error
// receive character create response
evnt = <-recv
is.Equal(int(evnt.PktID), protocol.P_LS2CL_REP_CHAR_CREATE_SUCC) // should receive P_LS2CL_REP_CHAR_CREATE_SUCC
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
err = protocol.NewPacket(evnt.Pkt).Decode(&charCreateResp) sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
is.NoErr(err) // packet.Decode() should not return an error charCreate, &charCreateResp)
// verify response // verify response
is.Equal(charCreate.PCStyle, charCreateResp.SPC_Style) // should have the same PCStyle is.Equal(charCreate.PCStyle, charCreateResp.SPC_Style) // should have the same PCStyle