61 lines
5.9 KiB
Python
61 lines
5.9 KiB
Python
"""Owned image transaction and rollback/crash boundaries; no actor loading or thaw."""
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class CubeImage(unittest.TestCase):
|
|
def test_owner_authorized_terminal_release(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
exe=Path(folder)/'release'
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all',
|
|
'-I'+str(ROOT),str(ROOT/'tests/features/fixtures/cube_release_probe.cpp'),'-lsqlite3','-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(folder)/'cube.db')],check=True)
|
|
|
|
def test_durable_exact_live_image_ack(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
exe=Path(folder)/'ack'
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all',
|
|
'-I'+str(ROOT),str(ROOT/'tests/features/fixtures/cube_image_ack_probe.cpp'),'-lsqlite3','-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(folder)/'cube.db')],check=True)
|
|
|
|
def test_live_image_wire_and_exact_local_expectation(self):
|
|
source=r'''
|
|
#define main cubeBaseMain
|
|
#include "tests/features/fixtures/cube_batch_probe.cpp"
|
|
#undef main
|
|
#include "src/server/common/cube_live_image.h"
|
|
int main(){auto b=batch();const auto plan=SettlePlan(b,std::string("\1\144\1",3),proto);CraftReceipt receipt{plan.result,plan.successes,plan.fee,plan.awards};
|
|
std::string expected;assert(CraftImage(b,receipt,60,proto,expected));InventoryImage parsed;assert(DecodeInventoryImage(expected,parsed,proto)&&parsed.gold==760&&parsed.items.size()==3);
|
|
auto identity=ArmFrame(b,8);identity.cookie=7;identity.body.clear();identity.message=Message::LiveImageRequest;assert(ValidHandshake(identity)&&EncodeHandshake(identity).size()==102&&!IsDbHandshakeReply(identity.message)&&IsDbHandshakeReply(Message::LiveImageAnswer));
|
|
LiveImageFlow flow;assert(!flow.Start(identity,b,receipt,60,proto,false)&&flow.Start(identity,b,receipt,60,proto,true));assert(flow.Request().message==Message::LiveImageRequest);
|
|
auto reply=identity;reply.message=Message::LiveImageAnswer;reply.status=Status::LiveImageReady;reply.body=expected;auto stale=reply;stale.cookie++;assert(!flow.Answer(stale)&&flow.MayRetry());
|
|
assert(flow.Answer(reply)&&flow.State()==LiveImageFlow::Ready&&flow.Image()==expected&&flow.Answer(reply));parsed.gold++;reply.body=EncodeInventoryImage(parsed);assert(!flow.Answer(reply)&&flow.State()==LiveImageFlow::Held);
|
|
auto nonok=receipt;nonok.result="gold_changed";assert(!CraftImage(b,nonok,0,proto,expected));assert(!CraftImage(b,receipt,uint64_t(PortEconomy::GoldLimit),proto,expected));
|
|
LiveImageFlow acknowledged;assert(acknowledged.Start(identity,b,receipt,60,proto,true));auto issued=identity;issued.message=Message::LiveImageAnswer;issued.status=Status::LiveImageReady;issued.body=acknowledged.Image();assert(acknowledged.Answer(issued)&&acknowledged.MarkApplied(issued.body)&&acknowledged.BeginAcknowledge(issued.body));
|
|
auto ack=acknowledged.Acknowledgement();const auto wire=EncodeHandshake(ack);Handshake decoded;assert(!wire.empty()&&DecodeHandshake(wire.data(),wire.size(),decoded)&&decoded.body==issued.body&&!IsDbHandshakeReply(ack.message));for(size_t n=0;n<wire.size();++n)assert(!DecodeHandshake(wire.data(),n,decoded));
|
|
auto release=ack;release.message=Message::LiveReleaseRequest;const auto releaseWire=EncodeHandshake(release);assert(!releaseWire.empty()&&DecodeHandshake(releaseWire.data(),releaseWire.size(),decoded)&&!IsDbHandshakeReply(decoded.message));for(size_t n=0;n<releaseWire.size();++n)assert(!DecodeHandshake(releaseWire.data(),n,decoded));release.message=Message::LiveReleaseAnswer;release.status=Status::LiveReleased;assert(ValidHandshake(release)&&IsDbHandshakeReply(release.message));release.body.clear();assert(!ValidHandshake(release));release.status=Status::Held;assert(ValidHandshake(release));
|
|
auto confirmed=ack;confirmed.message=Message::LiveImageAckAnswer;confirmed.status=Status::LiveImageAcknowledged;assert(IsDbHandshakeReply(confirmed.message)&&acknowledged.Answer(confirmed)&&acknowledged.Answer(confirmed)&&acknowledged.Answer(issued)&&acknowledged.State()==LiveImageFlow::Acknowledged);confirmed.body.clear();assert(!ValidHandshake(confirmed));
|
|
confirmed.body=issued.body;InventoryImage changed;assert(DecodeInventoryImage(confirmed.body,changed));changed.gold++;confirmed.body=EncodeInventoryImage(changed);assert(!acknowledged.Answer(confirmed)&&acknowledged.State()==LiveImageFlow::Held);
|
|
auto zero=b;zero.recipe.percent=0;const auto noAwards=SettlePlan(zero,std::string(3,char(100)),proto);assert(CraftImage(zero,{noAwards.result,noAwards.successes,noAwards.fee,noAwards.awards},0,proto,expected)&&DecodeInventoryImage(expected,parsed,proto)&&parsed.gold==700&&parsed.items.size()==1);
|
|
puts("PASS live image canonical wire, receipt+full inventory/credit expectation, stale/conflicting image refusal, paid zero-success image, no actor mutation/ACK/thaw");}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
cpp=Path(folder)/'live.cpp';exe=Path(folder)/'live';cpp.write_text(source)
|
|
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)],check=True)
|
|
|
|
def test_authorized_image_transaction(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
exe = Path(folder)/'image'
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all',
|
|
'-I'+str(ROOT),str(ROOT/'tests/features/fixtures/cube_image_probe.cpp'),'-lsqlite3','-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(folder)/'cube.db')],check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|