116 lines
8.8 KiB
Python
116 lines
8.8 KiB
Python
"""Candidate ground-gold source handoff; real ingress gates stay closed."""
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class CubeGoldOutbox(unittest.TestCase):
|
|
def test_actual_ground_source_and_pickup_order(self):
|
|
actor=(ROOT/'src/server/game/src/port_cube_actor.cpp').read_text()
|
|
helper=actor.split('PortCubeGoldPickup PortCubeDeferGroundGold(',1)[1].split('bool PortCubeBegin(',1)[0]
|
|
# Explicit audited-ingress/freezing substitutes ONLY in this extracted
|
|
# method harness. Production constants remain false in their header.
|
|
helper=('PortCubeGoldPickup PortCubeDeferGroundGold('+helper).replace('PortCube::HandshakeEnabled','testIngress').replace('PortCube::ActorMutationAuditComplete','testFreeze')
|
|
marker='if(f.message!=PortCube::Message::Answer&&'
|
|
self.assertIn(marker,actor)
|
|
reply=actor.split('void PortCubeActorReply(',1)[1].split(marker,1)[0]
|
|
reply=('void AcceptGoldAck('+reply+'}').replace('PortCube::HandshakeEnabled','testIngress').replace('PortCube::ActorMutationAuditComplete','testFreeze')
|
|
pickup=(ROOT/'src/server/game/src/char_item.cpp').read_bytes()
|
|
start=pickup.index(b'const auto cubeGold=PortCubeDeferGroundGold(this,item);')
|
|
end=pickup.index(b'Save();',start)+len(b'Save();')
|
|
branch=pickup[start:end].decode('ascii')
|
|
self.assertLess(pickup.index(b'if (item->DistanceValid(this))',pickup.index(b'bool CHARACTER::PickupItem')),start)
|
|
self.assertLess(pickup.index(b'if (item->IsOwnership(this))',pickup.index(b'bool CHARACTER::PickupItem')),start)
|
|
source=r'''
|
|
#include <unistd.h>
|
|
#include <cassert>
|
|
#include <cerrno>
|
|
static bool failSync=false;
|
|
static int Sync(int fd){if(failSync){errno=EIO;return -1;}return ::fsync(fd);}
|
|
#define fsync Sync
|
|
#include "src/server/common/cube_gold_outbox.h"
|
|
#undef fsync
|
|
#include "src/server/common/cube_handshake.h"
|
|
#include "src/server/game/src/port_cube_actor.h"
|
|
#define sys_err(...) ((void)0)
|
|
#define M2_DESTROY_ITEM(item) ((item)->destroyed=true)
|
|
using namespace PortCube;
|
|
constexpr unsigned ITEM_ELK=1;
|
|
bool testIngress=false,testFreeze=false;
|
|
struct CHARACTER;
|
|
struct CItem {
|
|
unsigned vid=123,count=250,vnum=1,type=1;bool ground=true,near=true,own=true,removed=false,destroyed=false;
|
|
unsigned GetVID(){return vid;}unsigned GetCount(){return count;}unsigned GetOriginalVnum(){return vnum;}unsigned GetType(){return type;}
|
|
void* GetSectree(){return ground?this:nullptr;}bool DistanceValid(CHARACTER*){return near;}bool IsOwnership(CHARACTER*){return own;}
|
|
void RemoveFromGround(){removed=true;ground=false;}
|
|
};
|
|
struct CHARACTER {
|
|
unsigned pid=1;bool party=false;unsigned given=0,saves=0;
|
|
unsigned GetPlayerID(){return pid;}bool GetParty(){return party;}bool IsObserverMode(){return false;}bool IsWarping(){return false;}bool IsDead(){return false;}bool IsStun(){return false;}
|
|
int GetMapIndex(){return 1;}bool GiveGold(unsigned){++given;return true;}void Save(){++saves;}bool Collect(CItem*);
|
|
};
|
|
struct Owner {Batch batch;ActorHandshake flow;unsigned handle=6;};
|
|
std::map<unsigned,Owner> owners;std::set<uint32_t> uncertainGround;GoldOutbox goldOutbox;
|
|
bool matches(CHARACTER* ch,const Owner& o){return ch->pid==o.batch.player;}
|
|
bool goldReady(){return goldOutbox.Ready();}
|
|
bool connected(){return true;}
|
|
unsigned serial=0;
|
|
std::string goldEvent(){return std::string(31,'e')+char('0'+(++serial));}
|
|
'''
|
|
tests=r'''
|
|
int main(int argc,char** argv){assert(argc==2);assert(!HandshakeEnabled&&!ActorMutationAuditComplete);assert(goldOutbox.Open(argv[1]));
|
|
CHARACTER ch,other;other.pid=2;CItem pile;auto& o=owners[1];o.batch=batch();auto& b=o.batch;
|
|
// The established valid batch fixture and explicit audited-freeze substitute
|
|
// do not enable a real actor or the production ingress.
|
|
assert(o.flow.Start(b,8,1,true));auto ack=o.flow.IdentityFrame();ack.message=Message::Answer;ack.body.clear();ack.cookie=7;ack.status=Status::Armed;
|
|
assert(o.flow.Armed(ack,1)&&o.flow.Submit(true,1));
|
|
assert(!ch.Collect(&pile)&&!pile.removed&&!ch.given&&goldOutbox.Pending().empty());
|
|
testIngress=testFreeze=true;ch.party=true;assert(!ch.Collect(&pile));ch.party=false;
|
|
pile.near=false;assert(!ch.Collect(&pile));pile.near=true;pile.own=false;assert(!ch.Collect(&pile));pile.own=true;
|
|
assert(ch.Collect(&pile)&&pile.removed&&pile.destroyed&&!ch.given&&!ch.saves&&goldOutbox.Pending().size()==1);
|
|
const auto saved=goldOutbox.Pending()[0];auto receipt=ArmFrame(b,saved.session);receipt.message=Message::GoldAck;receipt.status=Status::GoldStored;receipt.cookie=saved.cookie;receipt.body=EncodeGoldIntent(saved.intent);
|
|
auto wire=EncodeHandshake(receipt);AcceptGoldAck(99,wire.data(),wire.size());assert(goldOutbox.Pending().size()==1);
|
|
receipt.cookie++;wire=EncodeHandshake(receipt);AcceptGoldAck(6,wire.data(),wire.size());assert(goldOutbox.Pending().size()==1);receipt.cookie--;
|
|
receipt.session++;wire=EncodeHandshake(receipt);AcceptGoldAck(6,wire.data(),wire.size());assert(goldOutbox.Pending().size()==1);receipt.session--;
|
|
wire=EncodeHandshake(receipt);AcceptGoldAck(6,wire.data(),wire.size());assert(goldOutbox.Pending().empty()&&goldOutbox.Next(b.token)==2&&o.flow.BlocksActions());
|
|
// A second batch proves the real source-close ACK handler independently of
|
|
// the original batch used below for an uncertain new source-write failure.
|
|
CHARACTER third;third.pid=3;auto& thirdOwner=owners[3];thirdOwner.batch=batch();thirdOwner.batch.player=3;thirdOwner.batch.account=3;thirdOwner.batch.token=std::string(32,'c');
|
|
assert(thirdOwner.flow.Start(thirdOwner.batch,8,1,true));auto thirdArm=thirdOwner.flow.IdentityFrame();thirdArm.message=Message::Answer;thirdArm.body.clear();thirdArm.status=Status::Armed;thirdArm.cookie=7;
|
|
assert(thirdOwner.flow.Armed(thirdArm,1)&&thirdOwner.flow.Submit(true,1));CItem thirdPile;thirdPile.vid=3000;assert(third.Collect(&thirdPile));
|
|
const auto thirdDelivery=goldOutbox.Pending()[0];assert(thirdDelivery.intent.player==3);auto thirdReceipt=ArmFrame(thirdOwner.batch,8);thirdReceipt.cookie=7;thirdReceipt.message=Message::GoldAck;thirdReceipt.status=Status::GoldStored;thirdReceipt.body=EncodeGoldIntent(thirdDelivery.intent);
|
|
wire=EncodeHandshake(thirdReceipt);AcceptGoldAck(6,wire.data(),wire.size());assert(goldOutbox.CloseSource(GoldSourceFor(thirdDelivery)));
|
|
auto seals=goldOutbox.PendingSources();assert(seals.size()==1);auto sealReceipt=thirdReceipt;sealReceipt.message=Message::GoldCloseAck;sealReceipt.status=Status::GoldClosed;sealReceipt.body=EncodeGoldManifest(seals[0].events);
|
|
wire=EncodeHandshake(sealReceipt);AcceptGoldAck(99,wire.data(),wire.size());assert(!goldOutbox.SourceAcknowledged(seals[0]));
|
|
auto wrongSeal=sealReceipt;wrongSeal.body=EncodeGoldManifest({});wire=EncodeHandshake(wrongSeal);AcceptGoldAck(6,wire.data(),wire.size());assert(!goldOutbox.SourceAcknowledged(seals[0]));
|
|
wire=EncodeHandshake(sealReceipt);AcceptGoldAck(6,wire.data(),wire.size());assert(goldOutbox.SourceAcknowledged(seals[0])&&thirdOwner.flow.BlocksActions());
|
|
CItem latePile;latePile.vid=3001;assert(!third.Collect(&latePile)&&!latePile.removed&&!third.given);
|
|
CItem ordinary;ordinary.vid=456;assert(other.Collect(&ordinary)&&other.given==1&&other.saves==1&&ordinary.destroyed);
|
|
CItem uncertain;uncertain.vid=789;failSync=true;
|
|
assert(!ch.Collect(&uncertain)&&!uncertain.removed&&!uncertain.destroyed&&!ch.given&&uncertainGround.count(789));
|
|
assert(!other.Collect(&uncertain)&&other.given==1);failSync=false;
|
|
puts("PASS actual ground-gold helper and extracted pickup branch: fsync before removal, no local double credit, original ordinary path, party/ownership/distance denial and cross-player uncertain-source quarantine");
|
|
}
|
|
'''
|
|
prefix=(ROOT/'tests/features/fixtures/cube_batch_probe.cpp').read_text().split('int main(int argc,char** argv)',1)[0]
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
cpp=Path(folder)/'source.cpp';exe=Path(folder)/'source'
|
|
cpp.write_text(prefix+source+helper+reply+'bool CHARACTER::Collect(CItem* item){if(!item->DistanceValid(this)||!item->IsOwnership(this))return false;'+branch+'return true;}'+tests)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined',
|
|
'-fno-sanitize-recover=all','-I'+str(ROOT),str(cpp),'-lsqlite3','-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(folder)/'journal')],check=True)
|
|
|
|
def test_journal_faults_crashes_and_sql_handoff(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
exe=Path(folder)/'outbox'
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined',
|
|
'-fno-sanitize-recover=all','-I'+str(ROOT),
|
|
str(ROOT/'tests/features/fixtures/cube_outbox_probe.cpp'),'-lsqlite3','-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(folder)/'cube.db')],check=True)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|