Files
metin2-server/tests/features/test_attr_lock_snapshots.py
T

57 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Compile the owner snapshot sender, including client/window gates and failed DB reads."""
from pathlib import Path
import subprocess,tempfile
root=Path(__file__).resolve().parents[2]/'src/server/game/src'
s=(root/'port_attr_lock.cpp').read_text();s=s[s.index('void PortAttrLockSend('):s.index('\nvoid PortAttrLockBoot(')]
code=r'''
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <string>
#define INVENTORY 1
#define EQUIPMENT 2
#define SWITCHBOT 7
#define CHAT_TYPE_COMMAND 1
#define ACMD(name) void name(CHARACTER* ch,const char* argument)
#define INVENTORY_AND_EQUIP_SLOT_MAX 300
#define SWITCHBOT_SLOT_COUNT_REAL 5
using LPITEM=struct CItem*;
struct TItemPos {unsigned window,cell;TItemPos(unsigned w,unsigned c):window(w),cell(c){}};
int get_global_time(){return 100;}
struct Proto {int sAddonType=0;};
struct CItem {unsigned vnum=19,mask=8,filled=31;bool m_portAttrLockLoaded=true;Proto proto;
unsigned GetCell(){return 159;}unsigned GetVnum(){return vnum;}unsigned GetAttributeType(unsigned i){return (filled>>i)&1;}Proto* GetProto(){return &proto;}};
struct CHARACTER {bool desc=true,supported=true;std::string reply;int protect=0,calls=0;CItem* item=nullptr;
bool IsPC(){return true;}int GetProtectTime(const char*){return protect;}void SetProtectTime(const char*,int n){protect=n;}
CItem* GetItem(TItemPos pos){return (pos.window==INVENTORY && (pos.cell==159 || pos.cell==160)) || (pos.window==SWITCHBOT && pos.cell==0)?item:nullptr;}
void* GetDesc(){return desc?this:nullptr;}
void ChatPacket(int,const char* fmt,...){++calls;char b[512];va_list a;va_start(a,fmt);vsnprintf(b,sizeof(b),fmt,a);va_end(a);reply=b;}};
#include "port_attr_lock.h"
unsigned PortAttrLockGet(CItem* item){return item->mask;}
struct SwitchbotHelper {static bool IsClientSupported(CHARACTER* ch){return ch->supported;}};
'''+s+r'''
int main(){
CHARACTER ch;CItem item;
PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply=="AttrLockItem 1 159 19 8");
PortAttrLockSend(&ch,&item,EQUIPMENT,229);assert(ch.reply=="AttrLockItem 2 229 19 8");
PortAttrLockSend(&ch,&item,SWITCHBOT,0);assert(ch.reply=="AttrLockItem 7 0 19 8");
PortAttrLockSend(&ch,nullptr,INVENTORY,159);assert(ch.reply=="AttrLockItem 1 159 0 0");
item.mask=1;item.proto.sAddonType=1;PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply=="AttrLockItem 1 159 19 3");
item.m_portAttrLockLoaded=false;PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply=="AttrLockItem 1 159 19 -1");
item.filled=0;PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply=="AttrLockItem 1 159 19 0");
ch.reply.clear();PortAttrLockSend(&ch,&item,3,0);assert(ch.reply.empty());
ch.supported=false;PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply.empty());
ch.supported=true;ch.desc=false;PortAttrLockSend(&ch,&item,INVENTORY,159);assert(ch.reply.empty());
PortAttrLockSend(nullptr,&item,INVENTORY,159);
ch.desc=true;ch.item=&item;ch.calls=0;do_attr_lock_sync(&ch,"");assert(ch.calls==3 && ch.protect==102 && ch.reply=="AttrLockSync");
do_attr_lock_sync(&ch,"");assert(ch.calls==3); // repeated requests throttled, footprint cell 160 not duplicated
ch.protect=0;ch.supported=false;do_attr_lock_sync(&ch,"");assert(ch.calls==3);
}
'''
with tempfile.TemporaryDirectory() as d:
p=Path(d);(p/'test.cpp').write_text(code)
subprocess.run(['c++','-std=c++17','-I'+str(root),str(p/'test.cpp'),'-o',str(p/'test')],check=True)
subprocess.run([str(p/'test')],check=True)
print('Attr lock owner snapshots: version/window gates, removal, addon pair and SQL failure: PASS')