40 lines
2.4 KiB
Python
40 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise channel ownership, peer identity, expiry, ambiguity, disconnect and malformed snapshots."""
|
|
from pathlib import Path
|
|
import subprocess,tempfile
|
|
root=Path(__file__).resolve().parents[2]/'src/server/game/src'
|
|
code=r'''
|
|
#include "ava2_channel_registry.h"
|
|
#include <cassert>
|
|
int main(){
|
|
Ava2ChannelRegistry r;Ava2ChannelRoute out;
|
|
auto receive=[&](const std::string& h,unsigned ch,unsigned p,const std::string& s,long long now){return r.Receive(h,ch,p,s.data(),s.size(),now);};
|
|
const auto five=Ava2ChannelRegistry::Encode(5,13040,14040,{1,61});
|
|
assert(receive("192.0.2.1",5,14040,five,100));assert(r.Find(5,1,100,out) && out.game==13040 && out.host=="192.0.2.1");
|
|
assert(!r.Find(6,1,100,out) && !r.Find(5,62,100,out));
|
|
assert(!receive("192.0.2.1",6,14040,five,101));assert(!receive("192.0.2.1",5,14041,five,101));
|
|
assert(r.Find(5,61,101,out));
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 13040 14040 2 1",102)); // truncated; old route is atomic
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 13040 14040 2 1 1",102));
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 13040 14040 1 10001",102));
|
|
assert(!receive("192.0.2.1",5,14040,five+" extra",102));
|
|
assert(!receive("192.0.2.1",5,14040,std::string(2049,'1'),102));
|
|
assert(r.Find(5,1,129,out));assert(!r.Find(5,1,130,out));
|
|
const auto six=Ava2ChannelRegistry::Encode(6,13050,14050,{1});
|
|
assert(receive("192.0.2.2",6,14050,six,200));assert(r.Find(6,1,200,out) && out.game==13050);
|
|
const auto duplicate=Ava2ChannelRegistry::Encode(6,13051,14051,{1});
|
|
assert(receive("192.0.2.2",6,14051,duplicate,200));assert(!r.Find(6,1,200,out));
|
|
r.Forget("192.0.2.2",14051);assert(r.Find(6,1,200,out));r.Forget("192.0.2.2",14050);assert(!r.Find(6,1,200,out));
|
|
assert(!receive("192.0.2.1",99,14040,"1 99 13040 14040 1 1",300));
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 14040 14040 1 1",300));
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 99999 14040 1 1",300));
|
|
assert(!receive("192.0.2.1",5,14040,"1 5 13040 14040 129",300));
|
|
assert(receive("192.0.2.1",5,14040,five,300));assert(r.Channels(1,300)==std::vector<unsigned>{5});
|
|
}
|
|
'''
|
|
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('Native channel registry: owner/peer validation, map guards, atomic malformed rejection, TTL, duplicate owner, disconnect: PASS')
|