Files
AVA2/tests/protocol_test.cpp

92 lines
4.5 KiB
C++

#include "protocol.h"
#include "map_coordinates.h"
#include "skill_cooldown.h"
#include "eter_net/Packet.h"
#include <iostream>
#include <cassert>
#include <stdexcept>
using namespace ava2;
using namespace ava2::wire;
int main() {
SkillCooldown cooldown{10,100};
assert(cooldown.cooling(109));assert(!cooldown.cooling(110));
assert(cooldown.elapsed(112)==12 && cooldown.total==10);
assert(!cooldown.cooling(101,false));
MapCoordinates maps;
maps.load("first 409600 896000 4 5\nsecond 512000 896000 1 5\n");
assert(maps.find(409600,896000)->name=="first");
assert(maps.find(511999,1023999)->name=="first");
assert(maps.find(512000,896000)->name=="second");
assert(!maps.find(409600,1024000));
maps.baseX=409600; maps.baseY=896000;
int32_t x=410600,y=898000;maps.toLocal(x,y);assert(x==1000&&y==2000);
maps.toGlobal(x,y);assert(x==410600&&y==898000);
x=-1;y=-2;maps.toLocal(x,y);maps.toGlobal(x,y);assert(x==-1&&y==-2);
std::cout << "Testing AVA2 Wire Protocol & Packed Struct Fixtures..." << std::endl;
// 1. Golden bytes taken from AVA2 Packet.h
assert((attack(2, 0x12345678, 0xaa, 0xbb) == Bytes{225, 2, 0x78, 0x56, 0x34, 0x12, 0xaa, 0xbb}) && "Attack packet wire layout mismatch");
assert((pickup(0xdeadbeef) == Bytes{15, 0xef, 0xbe, 0xad, 0xde}) && "Pickup packet wire layout mismatch");
assert((select_character(4) == Bytes{6, 4}) && "Select character packet mismatch");
try {
select_character(5);
assert(false && "Slot 5 must throw out_of_range");
} catch (const std::out_of_range&) {}
assert((character_move(1, 0, 36, 100000, 200000, 0x11223344) ==
Bytes{7, 1, 0, 36, 0xa0, 0x86, 0x01, 0x00, 0x40, 0x0d, 0x03, 0x00, 0x44, 0x33, 0x22, 0x11}) && "Character move wire layout mismatch");
// 2. Client-to-Server packets
assert((entergame() == Bytes{10,1}) && "EnterGame wire mismatch");
assert((warp() == Bytes{65}) && "Warp wire mismatch");
Bytes clickBytes = on_click(1001);
assert(clickBytes.size() == 5 && clickBytes[0] == 26 && "OnClick wire packet mismatch");
Bytes flyTgt = fly_targeting(2001, 100, 200);
assert(flyTgt.size() == 13 && flyTgt[0] == 51 && "FlyTargeting wire packet mismatch");
Bytes itemUseBytes = item_use(1, 5);
assert(itemUseBytes.size() == 4 && itemUseBytes[0] == 11 && itemUseBytes[1] == 1 && itemUseBytes[2] == 5 && itemUseBytes[3] == 0 && "ItemUse packet mismatch");
Bytes itemMoveBytes = item_move(1, 0, 1, 1, 10);
assert(itemMoveBytes.size() == 11 && itemMoveBytes[0] == 13 && "ItemMove packet mismatch");
Bytes itemDropBytes = item_drop(1, 2, 5);
assert(itemDropBytes.size() == 8 && itemDropBytes[0] == 224 && "ItemDrop packet mismatch");
Bytes chatBytes = chat("Hello Metin2", CHAT_TYPE_TALKING);
assert(chatBytes[0] == 231 && chatBytes.size() == 4 + 12 + 1 && "Chat packet wire mismatch");
Bytes whisperBytes = whisper("PlayerOne", "Secret Message");
assert(whisperBytes[0] == 19 && whisperBytes.size() == 3 + 25 + 14 + 1 && "Whisper packet wire mismatch");
assert(chatBytes.back()==0 && chatBytes[1]==chatBytes.size());
assert(whisperBytes.back()==0 && whisperBytes[1]==whisperBytes.size());
assert(chat("").empty() && chat(std::string(512,'x')).empty());
assert(whisper("name",std::string(255,'x')).empty());
static_assert(PHASE_LOADING==4 && PHASE_GAME==5 && PHASE_DEAD==6);
static_assert(sizeof(TPlayerItemAttribute)==3);
static_assert(sizeof(TPacketGCItemSet)==135);
static_assert(sizeof(TPacketGCItemSet2)==144);
// 3. Official Struct Layout & Packed Size Assertions
static_assert(sizeof(TPacketGCPhase) == 2, "TPacketGCPhase must be packed to 2 bytes");
static_assert(sizeof(TPacketGCCharacterDelete) == 5, "TPacketGCCharacterDelete must be packed to 5 bytes");
static_assert(sizeof(TPacketGCDamageInfo) == 10, "TPacketGCDamageInfo must be packed to 10 bytes");
static_assert(sizeof(TPacketGCDead) == 8, "AVA2 renewal death packet must be 8 bytes");
static_assert(sizeof(TPacketGCStun) == 5, "TPacketGCStun must be packed to 5 bytes");
static_assert(sizeof(TPacketGCMove) == 24, "TPacketGCMove must be packed to 24 bytes");
static_assert(sizeof(TPacketGCItemGroundDel) == 5, "TPacketGCItemGroundDel must be packed to 5 bytes");
static_assert(sizeof(TPacketGCCreateFly) == 10, "TPacketGCCreateFly must be packed to 10 bytes");
static_assert(sizeof(TPacketGCWarp) == 15, "TPacketGCWarp must be packed to 15 bytes");
std::cout << "All AVA2 wire packet assertions and packed struct tests passed successfully!\n";
return 0;
}