119 lines
8.1 KiB
Python
119 lines
8.1 KiB
Python
"""Execute DB reward handler control flow with transactional SQL fault boundaries.
|
|
This is not a real MySQL or a full db-process crash acceptance test.
|
|
"""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
class PointSettlement(unittest.TestCase):
|
|
def test_commit_uncertainty_replay_and_configuration_changes(self):
|
|
source=(ROOT/'src/server/db/src/Ava2ItemShop.cpp').read_text()
|
|
body=source[source.index(' if(r.operation==3) {'):source.index(' auto login=GetLoginDataByAID')]
|
|
harness=r'''
|
|
#include "src/server/common/ava2_itemshop.h"
|
|
#include "src/server/common/point_drop.h"
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
using DWORD=uint32_t;
|
|
struct TMobTable{DWORD dwVnum;int bType,bLevel;};
|
|
struct CPeer{int GetChannel(){return 1;}};
|
|
struct Reward{unsigned account,player,vnum,requested,amount;};
|
|
struct State{int64_t coins=100;std::map<std::string,Reward> rewards;};
|
|
State durable,working;
|
|
Ava2ShopRequest current={};bool ownerPresent=true,walletPresent=true;unsigned ownerAccount=1;int ownershipQueries=0;bool active=false,commitLost=false,commitRefused=false,failInsert=false,engineOK=true;
|
|
std::string queryFailure;int writes=0,quotas=0;
|
|
struct Rows{std::vector<std::vector<std::string>> values;std::vector<char*> output;};
|
|
struct Result{unsigned uiNumRows=0;Rows rows;Rows* pSQLResult=&rows;};
|
|
struct SQL{unsigned uiSQLErrno=0;Result result;Result* Get(){return &result;}};
|
|
using Query=std::unique_ptr<SQL>;
|
|
Query result(std::vector<std::vector<std::string>> rows){Query q(new SQL);q->result.uiNumRows=rows.size();q->result.rows.values=std::move(rows);return q;}
|
|
char** mysql_fetch_row(Rows* rows){assert(!rows->values.empty());rows->output.clear();for(auto& v:rows->values[0])rows->output.push_back(v.data());return rows->output.data();}
|
|
bool ok(const Query& q){return q && !q->uiSQLErrno;}
|
|
std::string decimal(uint64_t v){return std::to_string(v);}std::string accountDB(){return "`account`";}
|
|
Query query(const std::string& sql){
|
|
if(!queryFailure.empty() && sql.find(queryFailure)!=std::string::npos){Query q(new SQL);q->uiSQLErrno=1;return q;}
|
|
if(sql.find("information_schema.tables")!=std::string::npos){assert(sql.find("ishop")==std::string::npos && sql.find("port_point_rewards")!=std::string::npos && sql.find("account")!=std::string::npos);return result({{engineOK?"2":"1"}});}
|
|
assert(active);
|
|
if(sql.find("SELECT account_id FROM player")==0){++ownershipQueries;return result(ownerPresent?std::vector<std::vector<std::string>>{{decimal(ownerAccount)}}:std::vector<std::vector<std::string>>{});}
|
|
if(sql.find("SELECT coins FROM")==0){assert(sql.find("FOR UPDATE")!=std::string::npos);return result(walletPresent?std::vector<std::vector<std::string>>{{std::to_string(working.coins)}}:std::vector<std::vector<std::string>>{});}
|
|
if(sql=="SELECT CURDATE()")return result({{"2026-10-20"}});
|
|
if(sql.find("SELECT account_id,vnum,requested,amount,player_id FROM port_point_rewards")==0){
|
|
auto it=working.rewards.find(current.token);if(it==working.rewards.end())return result({});const auto& r=it->second;
|
|
return result({{decimal(r.account),decimal(r.vnum),decimal(r.requested),decimal(r.amount),decimal(r.player)}});
|
|
}
|
|
if(sql.find("SELECT COALESCE(SUM(amount),0)")==0){++quotas;assert(sql.find("reward_day='2026-10-20'")!=std::string::npos);unsigned n=0;for(auto& row:working.rewards)if(row.second.account==current.account)n+=row.second.amount;return result({{decimal(n)}});}
|
|
assert(false);return {};
|
|
}
|
|
bool run(const std::string& sql){
|
|
assert(active);++writes;
|
|
if(sql.find("UPDATE `account`.account SET coins=coins+")==0){auto a=sql.find("coins+")+6;auto b=sql.find(" WHERE",a);working.coins+=std::stoll(sql.substr(a,b-a));return true;}
|
|
if(sql.find("INSERT INTO port_point_rewards")==0){
|
|
if(failInsert)return false;
|
|
auto before=durable.coins;unsigned amount=unsigned(working.coins-before);
|
|
assert(sql.find(","+decimal(current.count)+","+decimal(amount)+",'2026-10-20')")!=std::string::npos);
|
|
working.rewards[current.token]={current.account,current.player,current.product,current.count,amount};return true;
|
|
}
|
|
assert(false);return false;
|
|
}
|
|
struct Transaction{bool active=true;Transaction(){assert(!::active);::active=true;working=durable;}
|
|
bool commit(){if(commitRefused)return false;durable=working;if(commitLost)return false;active=false;return true;}
|
|
~Transaction(){::active=false;}
|
|
};
|
|
template<class T>void append(std::vector<char>& out,const T& v){auto p=reinterpret_cast<const char*>(&v);out.insert(out.end(),p,p+sizeof(v));}
|
|
std::vector<char> response;
|
|
void reply(CPeer*,DWORD,const Ava2ShopRequest&,const std::vector<char>& out){response=out;}
|
|
struct Handler{
|
|
std::vector<TMobTable> m_vec_mobTable={{8021,2,100}};
|
|
std::map<std::string,long> m_map_lEventFlag={{"points_stone_chance",10000},{"points_stone_min",3},{"points_stone_max",8},{"points_max_overlevel",15},{"points_daily_cap",500}};
|
|
void Handle(CPeer* peer,DWORD handle,const Ava2ShopRequest& r){ BODY }
|
|
};
|
|
uint8_t settle(Handler& handler,Ava2ShopRequest r){current=r;response.clear();CPeer peer;handler.Handle(&peer,0,r);assert(response.size()==46);return uint8_t(response[1]);}
|
|
unsigned granted(){unsigned n=0;memcpy(&n,response.data()+34,4);return n;}
|
|
'''.replace('BODY',body)
|
|
main=r'''
|
|
int main(){
|
|
Ava2ShopRequest r={};r.operation=3;r.account=1;r.player=2;r.product=8021;r.count=8;r.expectedVnum=110;strcpy(r.token,"0123456789abcdef0123456789abcdef");
|
|
Handler handler;durable.rewards["older"]={1,3,8021,498,498};
|
|
commitLost=true;assert(settle(handler,r)==8);assert(durable.coins==102 && durable.rewards[r.token].amount==2);int quotaCalls=quotas;
|
|
commitLost=false;ownerPresent=false;int ownershipBefore=ownershipQueries;handler.m_vec_mobTable.clear();handler.m_map_lEventFlag.clear();
|
|
assert(settle(handler,r)==0 && granted()==2 && durable.coins==102 && quotas==quotaCalls); // original committed result survives source changes.
|
|
assert(ownershipQueries==ownershipBefore);ownerPresent=true;
|
|
r.player=3;assert(settle(handler,r)==7 && durable.coins==102);r.player=2;
|
|
r.count=7;assert(settle(handler,r)==7 && durable.coins==102);r.count=8;
|
|
r.product=999;assert(settle(handler,r)==7 && durable.coins==102);r.product=8021;
|
|
// A new token with a removed mob is terminally refused, never dereferenced.
|
|
strcpy(r.token,"1123456789abcdef0123456789abcdef");assert(settle(handler,r)==1 && durable.coins==102);
|
|
handler=Handler();durable=State();failInsert=true;
|
|
assert(settle(handler,r)==6 && durable.coins==100 && durable.rewards.empty());failInsert=false;
|
|
assert(settle(handler,r)==0 && granted()==8 && durable.coins==108 && durable.rewards.size()==1);
|
|
assert(settle(handler,r)==0 && durable.coins==108);
|
|
strcpy(r.token,"2123456789abcdef0123456789abcdef");commitRefused=true;
|
|
assert(settle(handler,r)==8 && durable.coins==108 && durable.rewards.size()==1);commitRefused=false;
|
|
assert(settle(handler,r)==0 && durable.coins==116 && durable.rewards.size()==2);
|
|
strcpy(r.token,"3123456789abcdef0123456789abcdef");queryFailure="SELECT COALESCE";
|
|
assert(settle(handler,r)==6 && durable.coins==116);queryFailure.clear();
|
|
engineOK=false;assert(settle(handler,r)==6 && durable.coins==116);engineOK=true;
|
|
ownerPresent=false;assert(settle(handler,r)==2 && durable.coins==116);ownerPresent=true;
|
|
ownerAccount=99;assert(settle(handler,r)==2 && durable.coins==116);ownerAccount=1;
|
|
walletPresent=false;assert(settle(handler,r)==2 && durable.coins==116);walletPresent=true;
|
|
// Zero grants are durable results; later replay does not recalculate the cap.
|
|
handler.m_map_lEventFlag["points_daily_cap"]=16;assert(settle(handler,r)==0 && granted()==0 && durable.coins==116);
|
|
handler.m_map_lEventFlag["points_daily_cap"]=500;assert(settle(handler,r)==0 && granted()==0 && durable.coins==116);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src=Path(tmp)/'probe.cpp';exe=Path(tmp)/'probe';src.write_text(harness+main)
|
|
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)
|
|
|
|
if __name__=='__main__':unittest.main()
|