93 lines
5.3 KiB
C++
93 lines
5.3 KiB
C++
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#include <windows.h>
|
|
#include "wine_wire_abi.h"
|
|
#include "gateway/realms.hpp"
|
|
#include <atomic>
|
|
#include <array>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <thread>
|
|
using Dispatch=int32_t (WINAPI*)(void*);
|
|
static bool strict_endpoints(){
|
|
if(gateway::NumericIPv4Endpoint("127.0.0.1",1234).host()!="127.0.0.1")return false;
|
|
for(const auto* bad:{"", "127.1", "127.0.0.01", "0177.0.0.1", "0x7f.0.0.1", "+127.0.0.1", "-1.0.0.1",
|
|
"127.0.0.256", "127.0.0.1.", "127.0.0.1.2", "127..0.1", "0.0.0.0", "224.0.0.1"}){
|
|
try{gateway::NumericIPv4Endpoint endpoint(bad,1234);(void)endpoint;return false;}catch(const gateway::Error&){}
|
|
}
|
|
return true;
|
|
}
|
|
static bool socket_auth_refusal(Dispatch dispatch){
|
|
WSADATA startup{};if(WSAStartup(MAKEWORD(2,2),&startup))return false;
|
|
SOCKET listener=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
|
if(listener==INVALID_SOCKET){WSACleanup();return false;}
|
|
sockaddr_in address{};address.sin_family=AF_INET;address.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
|
|
int length=sizeof(address);
|
|
if(bind(listener,reinterpret_cast<sockaddr*>(&address),sizeof(address)) ||
|
|
listen(listener,1) || getsockname(listener,reinterpret_cast<sockaddr*>(&address),&length)){
|
|
closesocket(listener);WSACleanup();return false;
|
|
}
|
|
const auto hash=std::filesystem::temp_directory_path()/(L"gw-windows-auth-smoke-"+std::to_wstring(GetCurrentProcessId())+L".bin");
|
|
if(std::filesystem::exists(hash)){closesocket(listener);WSACleanup();return false;}
|
|
{std::ofstream file(hash,std::ios::binary);std::array<char,20> zeros{};file.write(zeros.data(),zeros.size());
|
|
if(!file){closesocket(listener);std::filesystem::remove(hash);WSACleanup();return false;}}
|
|
std::atomic<bool> observed=false;
|
|
std::thread server([&]{
|
|
WSAPOLLFD pending{listener,POLLIN,0};
|
|
if(WSAPoll(&pending,1,3000)>0){
|
|
SOCKET peer=accept(listener,nullptr,nullptr);
|
|
if(peer!=INVALID_SOCKET){
|
|
const DWORD timeout=3000;setsockopt(peer,SOL_SOCKET,SO_RCVTIMEO,reinterpret_cast<const char*>(&timeout),sizeof(timeout));
|
|
std::array<unsigned char,4> header{};size_t received=0;
|
|
while(received<header.size()){
|
|
const int n=recv(peer,reinterpret_cast<char*>(header.data()+received),int(header.size()-received),0);
|
|
if(n<=0){break;}
|
|
received+=size_t(n);
|
|
}
|
|
const unsigned size=unsigned(header[2])|(unsigned(header[3])<<8);
|
|
if(received==header.size() && header[0]==0 && size>0 && size<=1024){
|
|
std::array<char,1024> body{};received=0;
|
|
while(received<size){const int n=recv(peer,body.data()+received,int(size-received),0);if(n<=0)break;received+=size_t(n);}
|
|
if(received==size){const char reject[3]={0,0,4};observed=send(peer,reject,sizeof(reject),0)==int(sizeof(reject));}
|
|
}
|
|
closesocket(peer);
|
|
}
|
|
}
|
|
closesocket(listener);
|
|
});
|
|
const auto utf8=hash.u8string();const std::string name(reinterpret_cast<const char*>(utf8.data()),utf8.size());
|
|
const uint8_t password[]={'F','I','X','T','U','R','E'};
|
|
gw_wire_call call{};call.abi=GW_WINE_WIRE_ABI;call.size=sizeof(call);call.operation=GW_WIRE_AUTHENTICATE_MEMORY;
|
|
call.auth.abi=GW_NATIVE_ABI;call.auth.auth_port=ntohs(address.sin_port);call.auth.username="FIXTURE";
|
|
call.auth.version_hash_path=name.c_str();call.input=password;call.input_bytes=sizeof(password);
|
|
const auto status=dispatch(&call);
|
|
server.join();std::filesystem::remove(hash);WSACleanup();
|
|
return observed && status==0 && call.result!=0 && call.login==0;
|
|
}
|
|
int main(int argc,char** argv){
|
|
if(argc!=2){std::cerr<<"Expected exact native backend DLL path\n";return 2;}
|
|
const auto path=std::filesystem::canonical(std::filesystem::path(std::u8string(argv[1],argv[1]+std::strlen(argv[1]))));
|
|
// A missing runtime import must reach the test runner without a modal dialog.
|
|
SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS);
|
|
HMODULE module=LoadLibraryExW(path.c_str(),nullptr,LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
|
|
if(!module){
|
|
const DWORD load_error = GetLastError();
|
|
std::cerr << "Native backend load failed: " << load_error << '\n';
|
|
return 1;
|
|
}
|
|
const auto symbol=GetProcAddress(module,"gw_native_wire_dispatch");Dispatch dispatch=nullptr;
|
|
static_assert(sizeof(dispatch)==sizeof(symbol));std::memcpy(&dispatch,&symbol,sizeof(dispatch));
|
|
if(!dispatch){FreeLibrary(module);std::cerr<<"Native dispatch export absent\n";return 1;}
|
|
gw_wire_call call{};call.abi=GW_WINE_WIRE_ABI;call.size=sizeof(call);call.operation=GW_WIRE_PING;
|
|
bool good=strict_endpoints() && dispatch(&call)==0 && call.result==0 && call.value==GW_NATIVE_ABI;
|
|
call.size=1;good=good && dispatch(&call)==int32_t(0xc000000d) && dispatch(nullptr)==int32_t(0xc000000d);
|
|
good=good && socket_auth_refusal(dispatch);FreeLibrary(module);
|
|
if(!good){std::cerr<<"Native registry/socket smoke failed\n";return 1;}
|
|
std::cout<<"Real native backend: registry ABI, malformed refusal and owned Winsock auth-failure loopback PASS\n";return 0;
|
|
}
|