250 lines
15 KiB
Python
250 lines
15 KiB
Python
"""Regression checks execute the helpers and loader used by production code."""
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
def module(name,file):
|
|
spec=importlib.util.spec_from_file_location(name,ROOT/file)
|
|
result=importlib.util.module_from_spec(spec);spec.loader.exec_module(result);return result
|
|
|
|
MIG=module('planb_migration','tools/ava2-itemshop/migrate_extended_items.py')
|
|
BUILD=module('planb_build','tools/ava2-itemshop/build_isolated.py')
|
|
|
|
class PlanBSafety(unittest.TestCase):
|
|
def native(self,source):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src=Path(tmp)/'probe.cpp';exe=Path(tmp)/'probe';src.write_text(source)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=undefined,address','-fno-sanitize-recover=all','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
|
|
subprocess.run([str(exe)],check=True)
|
|
|
|
def test_gold_extremes_reject_without_mutation(self):
|
|
self.native(r'''
|
|
#include "src/server/common/gold_bounds.h"
|
|
#include <cassert>
|
|
#include <climits>
|
|
int main(){
|
|
const int64_t limit=2000000000000000LL;
|
|
assert(!PortGoldCanChange(500,-1000,limit));
|
|
assert(!PortGoldCanChange(500,INT64_MIN,limit));
|
|
assert(!PortGoldCanChange(500,INT64_MAX,limit));
|
|
assert(PortGoldCanChange(500,-500,limit));
|
|
assert(PortGoldCanChange(limit-1,1,limit));
|
|
assert(!PortGoldCanChange(limit,1,limit));
|
|
int64_t total=123;assert(!PortGoldTotal(INT64_MAX,UINT32_MAX,limit,total));assert(total==123);
|
|
assert(PortGoldTotal(5000000000LL,20,limit,total));assert(total==100000000000LL);
|
|
}
|
|
''')
|
|
|
|
def test_shop_quote_and_settlement_preserve_64bit_values(self):
|
|
self.native(r'''
|
|
#include "src/server/common/shop_economy.h"
|
|
#include <cassert>
|
|
#include <climits>
|
|
int main(){
|
|
const int64_t cap=2000000000000000LL;
|
|
int64_t quote=123,tax=0,proceeds=0;
|
|
assert(PortShopQuote(100000000000LL,true,cap,quote));assert(quote==300000000000LL);
|
|
assert(PortShopSettlement(quote,3,cap,tax,proceeds));
|
|
assert(tax==9000000000LL && proceeds==291000000000LL && tax+proceeds==quote);
|
|
assert(PortGoldCanChange(cap-proceeds,proceeds,cap));assert(!PortGoldCanChange(cap-proceeds+1,proceeds,cap));
|
|
assert(PortShopQuote(0,true,cap,quote) && quote==0);
|
|
quote=123;assert(!PortShopQuote(INT64_MIN,true,cap,quote) && quote==123);
|
|
assert(!PortShopQuote(INT64_MAX,true,cap,quote) && quote==123);
|
|
assert(!PortShopQuote(cap/3+1,true,cap,quote) && quote==123);
|
|
assert(PortShopSettlement(INT64_MAX,100,INT64_MAX,tax,proceeds));assert(tax==INT64_MAX && proceeds==0);
|
|
assert(PortShopSettlement(101,3,cap,tax,proceeds));assert(tax==3 && proceeds==98);
|
|
assert(!PortShopSettlement(100,-1,cap,tax,proceeds));assert(!PortShopSettlement(100,101,cap,tax,proceeds));
|
|
}
|
|
''')
|
|
|
|
def test_actual_item_loader_rejects_corruption_atomically(self):
|
|
source=(ROOT/'src/server/db/src/ClientManagerPlayer.cpp').read_text(encoding='latin1')
|
|
start=source.index('bool CreateItemTableFromRes(');end=source.index('\nsize_t CreatePlayerSaveQuery',start)
|
|
actual=source[start:end]
|
|
self.native(r'''
|
|
#include "src/server/common/port_item_extra.h"
|
|
#include <vector>
|
|
#include <string>
|
|
#include <cassert>
|
|
#include <cstdlib>
|
|
using DWORD=uint32_t;
|
|
struct Attr{uint8_t bType;int16_t sValue;};
|
|
struct TPlayerItem{uint32_t id,window,pos,count,vnum;int32_t alSockets[3];Attr aAttr[7];PortItemExtra extra;uint32_t owner;};
|
|
constexpr int ITEM_ATTRIBUTE_MAX_NUM=7;
|
|
struct CClientManager{static CClientManager& instance(){static CClientManager c;return c;}bool ValidateLoadedExtra(const TPlayerItem&,const char*){return true;}};
|
|
struct MYSQL_RES{std::vector<std::vector<std::string>> rows;unsigned index=0;std::vector<char*> row;};
|
|
using MYSQL_ROW=char**;
|
|
int mysql_num_rows(MYSQL_RES* r){return r->rows.size();}
|
|
unsigned mysql_num_fields(MYSQL_RES* r){return r->rows[0].size();}
|
|
MYSQL_ROW mysql_fetch_row(MYSQL_RES* r){r->row.clear();for(auto& s:r->rows[r->index++])r->row.push_back(&s[0]);return r->row.data();}
|
|
template<class T>void str_to_number(T& v,const char* s){v=static_cast<T>(strtoll(s,nullptr,10));}
|
|
#define sys_err(...) ((void)0)
|
|
'''+actual+r'''
|
|
int main(){
|
|
MYSQL_RES res;res.rows.resize(2,std::vector<std::string>(23,"0"));
|
|
PortItemExtra extra={};strcpy(extra.name,"pet");
|
|
for(auto& row:res.rows)row[22]=PortItemExtraHex(extra);
|
|
res.rows[1][22]="00";
|
|
std::vector<TPlayerItem> output;assert(!CreateItemTableFromRes(&res,&output,7));assert(output.empty());
|
|
res.index=0;res.rows[1][22]=PortItemExtraHex(extra);assert(CreateItemTableFromRes(&res,&output,7));assert(output.size()==2);assert(!strcmp(output[1].extra.name,"pet"));
|
|
PortItemExtra before=extra;assert(!PortItemExtraRead("00",extra));assert(!memcmp(&extra,&before,sizeof(extra)));
|
|
}
|
|
''')
|
|
|
|
def test_actual_party_gold_distribution_is_all_or_nothing_at_cap(self):
|
|
source=(ROOT/'src/server/game/src/char_item.cpp').read_text(encoding='latin1')
|
|
start=source.index('\tstruct FCountNearMember')
|
|
method=source.index('bool CHARACTER::GiveGold(int64_t iAmount)')
|
|
end=source.index('\nbool CHARACTER::PickupItem',method)
|
|
helpers='namespace NPartyPickupDistribute {\n'+source[start:method]
|
|
self.native(r'''
|
|
#include "src/server/common/gold_bounds.h"
|
|
#include <vector>
|
|
#include <cassert>
|
|
#include <cstdlib>
|
|
constexpr int64_t GOLD_MAX_64=2000000000000000LL;
|
|
constexpr int POINT_GOLD=11,PARTY_DEFAULT_RANGE=5000;
|
|
struct Party;
|
|
struct CHARACTER{int64_t balance=0;Party* party=nullptr;int64_t GetGold(){return balance;}Party* GetParty(){return party;}int GetX(){return 0;}int GetY(){return 0;}const char* GetName(){return "test";}void PointChange(int,int64_t n,bool){assert(PortGoldCanChange(balance,n,GOLD_MAX_64));balance+=n;}bool GiveGold(int64_t);};
|
|
using LPCHARACTER=CHARACTER*;using LPPARTY=Party*;
|
|
struct Party{std::vector<LPCHARACTER> members;template<class F>void ForEachOnlineMember(F& f){for(auto* c:members)f(c);}};
|
|
inline int DISTANCE_APPROX(int x,int y){return abs(x)+abs(y);}
|
|
inline bool LC_IsBrazil(){return false;}
|
|
struct LogManager{static LogManager& instance(){static LogManager m;return m;}template<class... T>void CharLog(T...){};};
|
|
#define sys_log(...) ((void)0)
|
|
'''+helpers+source[method:end]+r'''
|
|
int main(){
|
|
CHARACTER a,b;Party p;p.members={&a,&b};a.party=&p;b.party=&p;
|
|
b.balance=GOLD_MAX_64;
|
|
assert(!a.GiveGold(100));assert(a.balance==0&&b.balance==GOLD_MAX_64);
|
|
b.balance=0;assert(a.GiveGold(10000000000LL));assert(a.balance==5000000000LL&&b.balance==5000000000LL);
|
|
a.party=nullptr;a.balance=GOLD_MAX_64;assert(!a.GiveGold(1));assert(a.balance==GOLD_MAX_64);
|
|
}
|
|
''')
|
|
|
|
def test_real_64bit_integer_parser(self):
|
|
self.native(r'''
|
|
#include "src/server/common/utils.h"
|
|
#include <cassert>
|
|
#include <climits>
|
|
int main(){long long v=7;unsigned long long u=7;
|
|
assert(str_to_number(v,"-9223372036854775808")&&v==LLONG_MIN);
|
|
assert(str_to_number(v,"5000000000")&&v==5000000000LL);
|
|
assert(!str_to_number(v,"9223372036854775808")&&v==5000000000LL);
|
|
assert(!str_to_number(v,"123oops"));
|
|
assert(!str_to_number(u,"-1")&&u==7);
|
|
assert(!str_to_number(u," -1")&&u==7);
|
|
assert(!str_to_number(u,"18446744073709551616")&&u==7);
|
|
}
|
|
''')
|
|
|
|
def test_migration_identity_checksum_and_column_definition(self):
|
|
identity=['player_sb','host','3306','1','uuid']
|
|
plan={'version':2,'target':identity,'items':[[1,'EQUIPMENT',32,40,123,10,1,'']],'quickslots':[],'item_state':{'1':'0'*64}}
|
|
plan['sha256']=MIG.plan_digest(plan);MIG.validate_plan(plan,identity)
|
|
with self.assertRaises(RuntimeError):MIG.validate_plan(plan,identity[:-1]+['different'])
|
|
plan['items'][0][6]=2
|
|
with self.assertRaises(RuntimeError):MIG.validate_plan(plan,identity)
|
|
good=[['port_extra','varbinary(69)','NO','','','']]
|
|
self.assertTrue(MIG.validate_extra_schema(good))
|
|
for name in ('blob','varchar(69)','varbinary(68)'):
|
|
with self.assertRaises(RuntimeError):MIG.validate_extra_schema([['port_extra',name,'NO','','','']])
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path=Path(tmp)/'plan.json';MIG.atomic_json(path,plan)
|
|
self.assertEqual(json.loads(path.read_text()),plan)
|
|
self.assertEqual(path.stat().st_mode & 0o777,0o600)
|
|
|
|
def test_real_migration_control_flow_recovers_without_double_shift(self):
|
|
# This exercises main/SQL ordering, not MySQL's own transaction or DDL behavior.
|
|
from unittest.mock import patch
|
|
from types import SimpleNamespace
|
|
backend={'pos':32,'done':False,'crash':True,'updates':0,'owner':10}
|
|
def execute(command,**kwargs):
|
|
q=kwargs['input']
|
|
rows=[]
|
|
if q.startswith('SELECT DATABASE()'):rows=[['player_sb','host','3306','1','uuid']]
|
|
elif q.startswith('SHOW COLUMNS'):rows=[['port_extra','varbinary(69)','NO','','','']]
|
|
elif q.startswith('SELECT name FROM mt_migration'):
|
|
rows=[['ava2-shop-wear-40']] if backend['done'] else []
|
|
elif q.startswith('SELECT id,window,pos,vnum'):rows=[['1','EQUIPMENT',str(backend['pos']),'123',str(backend['owner']),'1','','0'*64]]
|
|
elif q.startswith('SELECT window,pos,vnum'):rows=[['EQUIPMENT',str(backend['pos']),'123',str(backend['owner']),'1','','0'*64]]
|
|
elif q.startswith('SELECT pos FROM item'):rows=[[str(backend['pos'])]]
|
|
elif q.startswith('UPDATE item SET'):
|
|
backend['updates']+=1;backend['pos']=40
|
|
if backend['crash']:
|
|
backend['crash']=False
|
|
raise subprocess.CalledProcessError(1,command)
|
|
elif q.startswith('INSERT IGNORE INTO mt_migration(name'):backend['done']=True
|
|
elif q.startswith(('CREATE TABLE','INSERT IGNORE INTO mt_migration_target','SELECT i.id','SELECT id FROM item WHERE','SELECT id,HEX(quickslot)')):pass
|
|
else:raise AssertionError('Unexpected SQL: '+q)
|
|
return SimpleNamespace(stdout=''.join('\t'.join(r)+'\n' for r in rows))
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
plan=Path(tmp)/'plan.json';args=['migrate','--player','player_sb','--plan',str(plan),'--apply','--offline','--backup-id','backup-1']
|
|
with patch.object(MIG.subprocess,'run',side_effect=execute),patch('sys.argv',args):
|
|
with self.assertRaises(subprocess.CalledProcessError):MIG.main()
|
|
self.assertTrue(plan.exists());self.assertFalse(backend['done']);self.assertEqual(backend['pos'],40)
|
|
MIG.main();self.assertTrue(backend['done']);self.assertEqual(backend['pos'],40)
|
|
backend['owner']=99;before=backend['updates']
|
|
with self.assertRaises(RuntimeError):MIG.main()
|
|
self.assertEqual(backend['updates'],before)
|
|
|
|
def test_fresh_build_binding_dependencies_and_warnings(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base=Path(tmp);source=base/'source';out=base/'output'
|
|
for part in ('game','db'):
|
|
d=source/'server'/part/'src';d.mkdir(parents=True);(d/'Makefile').write_text('CFLAGS = -w -m32\nDepend: .obj/a.o OBJDIR/b.o\n')
|
|
(source/'extern').mkdir();archive=source/'extern/libfixture.a';archive.write_bytes(b'fixture')
|
|
manifest={'source_sha256':BUILD.source_digest(source),'archives':{'extern/libfixture.a':BUILD.digest(archive)}}
|
|
run,snapshot,rev=BUILD.prepare(source,out,'game',manifest,True)
|
|
flags=(snapshot/'server/game/src/Makefile').read_text()
|
|
self.assertNotIn(' -w ',flags);self.assertIn('-Wformat=2',flags);self.assertIn('-Werror=shorten-64-to-32',flags);self.assertIn('$(OBJDIR)/a.o',flags)
|
|
with self.assertRaises(FileExistsError):BUILD.prepare(source,out,'game',manifest,True)
|
|
BUILD.prepare(source,out,'db',manifest)
|
|
system=base/'system.a';system.write_bytes(b'system archive')
|
|
manifest['system_archives']={str(system):BUILD.digest(system)}
|
|
BUILD.validate_dependencies(source,manifest,BUILD.source_digest(source))
|
|
system.write_bytes(b'foreign archive')
|
|
with self.assertRaises(ValueError):BUILD.validate_dependencies(source,manifest,BUILD.source_digest(source))
|
|
manifest.pop('system_archives')
|
|
archive.write_bytes(b'changed')
|
|
with self.assertRaises(ValueError):BUILD.prepare(source,base/'other','game',manifest)
|
|
|
|
def test_build_records_binary_after_make_moves_it_to_bin(self):
|
|
from unittest.mock import patch
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base=Path(tmp);source=base/'source';out=base/'output'
|
|
for part in ('game','db'):
|
|
d=source/'server'/part/'src';d.mkdir(parents=True);(d/'Makefile').write_text('CFLAGS = -w -m32\n')
|
|
(source/'extern').mkdir();archive=source/'extern/libfixture.a';archive.write_bytes(b'fixture')
|
|
manifest=base/'dependencies.json'
|
|
manifest.write_text(json.dumps({'source_sha256':BUILD.source_digest(source),'archives':{'extern/libfixture.a':BUILD.digest(archive)}}))
|
|
for target in ('game','db'):
|
|
def make(cmd,**kwargs):
|
|
values=dict(arg.split('=',1) for arg in cmd if '=' in arg)
|
|
destination=Path(values['GAMEDIR' if target=='game' else 'DBDIR'])/target
|
|
self.assertEqual(destination.parent.parent,out.resolve()/(target+'-release'))
|
|
destination.write_bytes(b'candidate')
|
|
return subprocess.CompletedProcess(cmd,0)
|
|
argv=['build','--source',str(source),'--output',str(out),'--target',target,'--dependency-manifest',str(manifest)]
|
|
with patch('sys.argv',argv),patch.object(BUILD.platform,'system',return_value='FreeBSD'),patch.object(BUILD.subprocess,'run',side_effect=make),patch.object(BUILD.subprocess,'check_output',return_value='test compiler'):
|
|
BUILD.main()
|
|
result=json.loads((out/(target+'-release')/'result.json').read_text())
|
|
self.assertEqual(result['status'],'PASS')
|
|
self.assertEqual(Path(result['binary']),out.resolve()/(target+'-release')/'bin'/target)
|
|
|
|
def test_gold_migration_rejects_unexpected_column_kinds(self):
|
|
planner=module('gold_plan','tools/gold64/migrate_gold64.py')
|
|
for kind in ('int(11)','bigint(20) unsigned','bigint'):
|
|
planner.validate_column(kind)
|
|
for kind in ('varchar(32)','decimal(20,2)','int(11); DROP TABLE player','float'):
|
|
with self.assertRaises(ValueError):planner.validate_column(kind)
|
|
|
|
def test_offline_unverified_economy_cannot_be_enabled_by_runtime_flag(self):
|
|
self.native('#include "src/server/common/offline_shop_safety.h"\n#include <cassert>\nint main(){assert(!PortOfflineShopEconomyReady());}')
|