import pathlib, tempfile, subprocess root=pathlib.Path(__file__).resolve().parents[1] fixture='''#include typedef unsigned char uint8; typedef struct {uint8 task,sequence;} NCPREQUEST; typedef struct {uint8 a[8];} NCPRESPONSE; static uint8 ipxdata[4096],readbuff[4096]; static uint8 *responsedata=((uint8*)&ipxdata)+sizeof(NCPRESPONSE); static uint8 *requestdata=readbuff+sizeof(NCPREQUEST); int run(NCPREQUEST *ncprequest,int ncp_type,int function,int data_len){int requestlen,completition=0; requestlen = data_len - sizeof(NCPREQUEST); if (ncp_type == 0x2222) { switch (function) { default: completition=0xfb; break; }} return completition;} int main(void){NCPREQUEST r={1,1};return run(&r,0x2222,104,2)==0xfb?0:1;} ''' with tempfile.TemporaryDirectory() as t: p=pathlib.Path(t);(p/'src').mkdir();f=p/'src/nwconn.c';f.write_text(fixture) subprocess.run(['python3',str(root/'upstream-adapter/add-nds.py'),t],check=True) (p/'src/mars_nds_server.h').write_bytes((root/'nds/server.h').read_bytes()) subprocess.run(['cc','-D_DEFAULT_SOURCE','-std=c11',str(f),str(root/'nds/server.c'),str(root/'nds/fragger.c'),str(root/'nds/nds_directory.c'),'-ljson-c','-lsqlite3','-o',str(p/'test')],check=True) subprocess.run([str(p/'test')],check=True,env={'MARS_NDS_LAB':'0'}) assert subprocess.run(['python3',str(root/'upstream-adapter/add-nds.py'),t],capture_output=True).returncode!=0 stub = 'case 0x68 : /* NDS NCP, NDS Fragger Protokoll ?? */\n XDPRINTF((2,0, "NDS Fragger Protokoll not supportet"));\n nw_debug=0;\n completition = 0xfb; /* unknown request */\n break;' legacy = fixture.replace('default: completition=0xfb;', stub+' default: completition=0xfb;') + '\n/* legacy umlaut: ΓΌ */\n' f.write_bytes(legacy.encode('latin-1')) subprocess.run(['python3',str(root/'upstream-adapter/add-nds.py'),t],check=True) patched=f.read_bytes() assert b'\xfc' in patched and patched.count(b'case 0x68')==1 assert b'mars_nds_request' in patched f.write_text(fixture.replace('switch (function)','switch (unknown)')) assert subprocess.run(['python3',str(root/'upstream-adapter/add-nds.py'),t],capture_output=True).returncode!=0 print('PASS dispatcher hook compile/link fixture, rejects repeated or incompatible patch; actual upstream not tested')