Compare commits

..

No commits in common. "cafca9093cfa82dc041b2a584f70527f1fae1bda" and "de3e067b483514a705e9355295565178c9da9b05" have entirely different histories.

5 changed files with 118 additions and 177 deletions

View File

@ -1,68 +0,0 @@
package testutil
import (
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
)
var (
TestCharCreate = protocol.SP_CL2LS_REQ_CHAR_CREATE{
PCStyle: protocol.SPCStyle{
INameCheck: 1, SzFirstName: "Hector",
SzLastName: "Bannonvenom", IGender: 1, IFaceStyle: 1,
IHairStyle: 17, IHairColor: 11, ISkinColor: 10, IEyeColor: 2,
IHeight: 1, IBody: 0, IClass: 0,
},
SOn_Item: protocol.SOnItem{
IEquipHandID: 0, IEquipUBID: 53, IEquipLBID: 17, IEquipFootID: 58,
IEquipHeadID: 0, IEquipFaceID: 0, IEquipBackID: 0,
},
SOn_Item_Index: protocol.SOnItem_Index{
IEquipUBID_index: 15, IEquipLBID_index: 12, IEquipFootID_index: 17,
IFaceStyle: 2, IHairStyle: 18,
},
}
)
// creates a new account and player in the database
func MakeTestPlayer(db *db.DBHandler, id string, password string) (acc *db.Account, plr *db.Player, err error) {
acc, err = db.NewAccount(id, password)
if err != nil {
return
}
var plrID int
plrID, err = db.NewPlayer(acc.AccountID, TestCharCreate.PCStyle.SzFirstName, TestCharCreate.PCStyle.SzLastName, 1)
if err != nil {
return
}
charCreate := TestCharCreate
charCreate.PCStyle.IPC_UID = int64(plrID)
err = db.FinishPlayer(&charCreate, acc.AccountID)
if err != nil {
return
}
err = db.FinishTutorial(plrID, acc.AccountID)
if err != nil {
return
}
plr, err = db.GetPlayer(plrID)
return
}
func QueueLogin(redisHndlr *redis.RedisHandler, FEKey []byte, plrID, accID int) (int64, error) {
key, err := protocol.GenSerialKey()
if err != nil {
return 0, err
}
return key, redisHndlr.QueueLogin(key, redis.LoginMetadata{
FEKey: FEKey,
PlayerID: int32(plrID),
AccountID: accID,
})
}

View File

@ -1,50 +0,0 @@
package testutil
import (
"context"
"fmt"
"net"
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/matryer/is"
)
type DummyPeer struct {
Recv chan *cnet.PacketEvent
Peer *cnet.Peer
is *is.I
}
// MakeDummyPeer creates a new dummy peer and returns it
func MakeDummyPeer(ctx context.Context, is *is.I, port int) *DummyPeer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
is.NoErr(err)
recv := make(chan *cnet.PacketEvent)
peer := cnet.NewPeer(ctx, conn)
go func() {
peer.Handler(recv)
}()
return &DummyPeer{Recv: recv, Peer: peer, is: is}
}
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
func (dp *DummyPeer) SendAndRecv(sID, rID uint32, out, in interface{}) {
// send out packet
err := dp.Peer.Send(sID, out)
dp.is.NoErr(err) // peer.Send() should not return an error
// receive response
evnt := <-dp.Recv
defer protocol.PutBuffer(evnt.Pkt)
dp.is.Equal(evnt.PktID, rID) // should receive expected type
dp.is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
}
// Kill closes the peer's connection
func (dp *DummyPeer) Kill() {
dp.Peer.Kill()
}

View File

@ -1,52 +0,0 @@
package testutil
import (
"context"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/alicebob/miniredis/v2"
"github.com/bitcomplete/sqltestutil"
)
// SetupEnvironment spawns a postgres container and returns a db and redis handler
// along with a cleanup function
func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, func()) {
// 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 {
psql.Shutdown(ctx)
panic(err)
}
if err = testDB.Setup(); err != nil {
psql.Shutdown(ctx)
panic(err)
}
// start miniredis
r, err := miniredis.Run()
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
// open redis handler
rh, err := redis.OpenRedis(r.Addr())
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
return testDB, rh, func() {
psql.Shutdown(ctx)
rh.Close()
r.Close()
}
}

View File

@ -1,10 +1,102 @@
package testutil
import (
"context"
"fmt"
"net"
"sync"
"time"
"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/alicebob/miniredis/v2"
"github.com/bitcomplete/sqltestutil"
"github.com/matryer/is"
)
type DummyPeer struct {
Recv chan *cnet.PacketEvent
Peer *cnet.Peer
is *is.I
}
// MakeDummyPeer creates a new dummy peer and returns it
func MakeDummyPeer(ctx context.Context, is *is.I, port int) *DummyPeer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
is.NoErr(err)
recv := make(chan *cnet.PacketEvent)
peer := cnet.NewPeer(ctx, conn)
go func() {
peer.Handler(recv)
}()
return &DummyPeer{Recv: recv, Peer: peer, is: is}
}
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
func (dp *DummyPeer) SendAndRecv(sID, rID uint32, out, in interface{}) {
// send out packet
err := dp.Peer.Send(sID, out)
dp.is.NoErr(err) // peer.Send() should not return an error
// receive response
evnt := <-dp.Recv
defer protocol.PutBuffer(evnt.Pkt)
dp.is.Equal(evnt.PktID, rID) // should receive expected type
dp.is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
}
// Kill closes the peer's connection
func (dp *DummyPeer) Kill() {
dp.Peer.Kill()
}
// SetupEnvironment spawns a postgres container and returns a db and redis handler
// along with a cleanup function
func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, func()) {
// 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 {
psql.Shutdown(ctx)
panic(err)
}
if err = testDB.Setup(); err != nil {
psql.Shutdown(ctx)
panic(err)
}
// start miniredis
r, err := miniredis.Run()
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
// open redis handler
rh, err := redis.OpenRedis(r.Addr())
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
return testDB, rh, func() {
psql.Shutdown(ctx)
rh.Close()
r.Close()
}
}
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
select {
case <-ch:

View File

@ -27,6 +27,25 @@ test data was scraped by dumping packets, just adding a println to the LoginServ
to print the packet data
*/
var (
testCharCreate = protocol.SP_CL2LS_REQ_CHAR_CREATE{
PCStyle: protocol.SPCStyle{
INameCheck: 1, SzFirstName: "Hector",
SzLastName: "Bannonvenom", IGender: 1, IFaceStyle: 1,
IHairStyle: 17, IHairColor: 11, ISkinColor: 10, IEyeColor: 2,
IHeight: 1, IBody: 0, IClass: 0,
},
SOn_Item: protocol.SOnItem{
IEquipHandID: 0, IEquipUBID: 53, IEquipLBID: 17, IEquipFootID: 58,
IEquipHeadID: 0, IEquipFaceID: 0, IEquipBackID: 0,
},
SOn_Item_Index: protocol.SOnItem_Index{
IEquipUBID_index: 15, IEquipLBID_index: 12, IEquipFootID_index: 17,
IFaceStyle: 2, IHairStyle: 18,
},
}
)
func TestMain(m *testing.M) {
ret := 1
defer func() {
@ -156,18 +175,18 @@ func TestCharacterSequence(t *testing.T) {
IFNCode: 260,
ILNCode: 551,
IMNCode: 33,
SzFirstName: testutil.TestCharCreate.PCStyle.SzFirstName,
SzLastName: testutil.TestCharCreate.PCStyle.SzLastName,
SzFirstName: testCharCreate.PCStyle.SzFirstName,
SzLastName: testCharCreate.PCStyle.SzLastName,
}, &charResp)
// verify response
is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number
is.Equal(charResp.IGender, int8(1)) // should have the same gender
is.Equal(charResp.SzFirstName, testutil.TestCharCreate.PCStyle.SzFirstName) // should have the same first name
is.Equal(charResp.SzLastName, testutil.TestCharCreate.PCStyle.SzLastName) // should have the same last name
is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number
is.Equal(charResp.IGender, int8(1)) // should have the same gender
is.Equal(charResp.SzFirstName, testCharCreate.PCStyle.SzFirstName) // should have the same first name
is.Equal(charResp.SzLastName, testCharCreate.PCStyle.SzLastName) // should have the same last name
// send character create request
charCreate := testutil.TestCharCreate
charCreate := testCharCreate
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
dummy.SendAndRecv(protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,