#include "../shared/protocol.h" #include "../shared/pipe.h" #include #include #include #include using namespace browser_bridge; static void Check(bool ok,const char* text) { if (!ok) throw std::runtime_error(text); } int main() { try { Message fields; std::string error; Check(Parse(R"({"action":"add","request_id":"abc-1","method":"GET","url":"https://example.test/中文.zip","filename":"\ud83d\ude80.zip"})",fields,error) && Validate(fields,error),"parse UTF-8 and surrogate pair"); Message restored; Check(Parse(Serialize(fields),restored,error) && fields==restored,"JSON roundtrip"); for (const std::string& invalid : {R"({"x":"1","x":"2"})",R"({"x":1})",R"({"x":"\ud800"})",R"({"x":{}})",R"({"x":"a"} trailing)"}) Check(!Parse(invalid,restored,error),"reject malformed flat protocol"); auto invalid=fields; invalid["user_agent"]="test\r\nInjected: yes"; Check(!Validate(invalid,error),"reject header injection"); invalid=fields; invalid["method"]="POST"; Check(!Validate(invalid,error),"reject POST"); std::stringstream framed; Check(WriteFrame(framed,Serialize(fields)),"write native frame"); std::string json; Check(ReadFrame(framed,json) && json==Serialize(fields),"read native frame"); std::stringstream truncated(std::string("\xff\xff\xff\x7f",4)); Check(!ReadFrame(truncated,json),"bound frame allocation"); std::stringstream partial(std::string("\x05\0\0\0ab",6)); Check(!ReadFrame(partial,json),"reject truncated frame"); PipeServer server; auto name=PipeName()+L".Test."+std::to_wstring(GetCurrentProcessId()); server.Start([](const std::string& json,HANDLE) { Message message; std::string error; if (!Parse(json,message,error) || !Validate(message,error)) return Serialize(Reply("invalid_request",error)); return Serialize(Reply(Get(message,"action")=="ping"?"ready":"accepted","已接收",Get(message,"request_id"),"42")); },name); Message ping{{"action","ping"},{"request_id","ping-1"}}; bool ready=false; for (int i=0;i<50;++i) { auto response=Exchange(Serialize(ping),name); Check(Parse(response,restored,error),"pipe reply JSON"); if (Get(restored,"status")=="ready") { ready=true; break; } Sleep(20); } Check(ready,"private per-user named pipe handshake"); auto response=Exchange(Serialize(fields),name); Check(Parse(response,restored,error) && Get(restored,"status")=="accepted" && Get(restored,"task_id")=="42","native IPC request/ack including Unicode"); // A stalled peer must not keep shutdown blocked in a read operation. HANDLE peer=INVALID_HANDLE_VALUE; for (int i=0;i<50 && peer==INVALID_HANDLE_VALUE;++i) { peer=CreateFileW(name.c_str(),GENERIC_READ|GENERIC_WRITE,0,nullptr,OPEN_EXISTING,0,nullptr); if (peer==INVALID_HANDLE_VALUE) Sleep(20); } Check(peer!=INVALID_HANDLE_VALUE,"connect stalled peer"); auto start=std::chrono::steady_clock::now(); server.Stop(); CloseHandle(peer); Check(std::chrono::steady_clock::now()-start