Files
metin2-client/tests/wiki/test_ava2_shop_wire.py
T

36 lines
1.9 KiB
Python

"""Run the shipped shop decoder against malformed and valid wire frames."""
import os,subprocess,tempfile,unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[2]
class WireTests(unittest.TestCase):
def test_complete_frame_and_no_partial_mutation(self):
code=r'''
#include "src/port/UserInterface/Ava2ShopWire.h"
#include <cassert>
#include <string>
#pragma pack(push,1)
struct Item { uint32_t vnum; char body[39]; };
struct Price { uint32_t vnum,count; };
struct Frame { uint32_t owner; Item items[40]; Price prices[40]; };
#pragma pack(pop)
static_assert(sizeof(Item)==43,"native baseline wire size");
static_assert(sizeof(Frame)==2044,"shop start payload size");
int main(){
Frame frame={};frame.items[0].vnum=25163;frame.prices[0]={90011,10};
Item items[40]={};Price prices[40]={};uint32_t owner=99;
const char* data=reinterpret_cast<const char*>(&frame);
assert(!DecodeAva2ShopStart(data,sizeof(frame)-1,owner,items,prices));assert(owner==99);
assert(!DecodeAva2ShopStart(data,sizeof(frame)+1,owner,items,prices));assert(owner==99);
assert(!DecodeAva2ShopStart(nullptr,sizeof(frame),owner,items,prices));
frame.prices[1]={90010,0};assert(!DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));assert(items[0].vnum==0);
frame.prices[1]={90010,20};assert(!DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));assert(owner==99);
frame.prices[1]={0,0};assert(DecodeAva2ShopStart(data,sizeof(frame),owner,items,prices));
assert(owner==0 && items[0].vnum==25163 && prices[0].vnum==90011 && prices[0].count==10);
}
'''
with tempfile.TemporaryDirectory() as d:
p=Path(d);(p/'test.cpp').write_text(code)
subprocess.run([os.environ.get('CXX','c++'),'-std=c++17','-I'+str(ROOT),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
subprocess.run([str(p/'test')],check=True)
if __name__=='__main__':unittest.main()