208 lines
15 KiB
C++
208 lines
15 KiB
C++
// Exercised only by the independent loopback fixture, never as client-engine proof.
|
|
#include "session.h"
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <fstream>
|
|
#include <openssl/crypto.h>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include <chrono>
|
|
#include "native_transport_fixture.hpp"
|
|
static void enter_and_logout(gw_native_session* session, unsigned round, bool switched_account){
|
|
const uint64_t selected_guid=switched_account ? 72 : 42;
|
|
const std::string_view selected_name=switched_account ? "Visitor" : "Fixture";
|
|
gw_native_character selected{};
|
|
const auto count=gw_native_character_count(session);
|
|
for(unsigned i=0;i<count;++i){
|
|
gw_native_character row{};
|
|
if(gw_native_character_at(session,i,&row))throw std::runtime_error("could not read current roster");
|
|
if(switched_account && (row.guid==42 || row.guid==43))
|
|
throw std::runtime_error("previous account character in new roster");
|
|
if(std::string_view(row.name)==selected_name){
|
|
if(i!=round-1 || selected.guid)throw std::runtime_error("unexpected fixture roster");
|
|
selected=row;
|
|
}
|
|
}
|
|
gw_native_world world{};
|
|
if(count!=2 || selected.guid!=selected_guid)throw std::runtime_error("missing selected character");
|
|
if(switched_account && gw_native_begin_world(session,42)!=-1)
|
|
throw std::runtime_error("previous account character was selectable");
|
|
const int entered=gw_native_enter(session,selected.guid,&world);
|
|
if(round==2 && std::getenv("GW_TEST_WRONG_WORLD_SELF")){
|
|
// The server closed after creating another player as SELF and the
|
|
// selected character as a nearby player. Neither is a successful entry.
|
|
if(entered==0 || world.map!=12 || world.x!=float(round) || world.objects!=2)
|
|
throw std::runtime_error("wrong character was accepted as the selected player");
|
|
return;
|
|
}
|
|
if(entered || world.map!=12 || world.x!=float(round) || world.objects!=1)
|
|
throw std::runtime_error("selected character did not enter this account session");
|
|
if(gw_native_send(session,0x4b,nullptr,0))throw std::runtime_error("logout request failed");
|
|
gw_native_packet packet{};
|
|
uint8_t response[5]{};
|
|
const uint8_t accepted[5]{};
|
|
if(gw_native_receive(session,&packet,response,sizeof(response)) || packet.opcode!=0x4c ||
|
|
packet.payload_bytes!=sizeof(response) || std::memcmp(response,accepted,sizeof(response)) ||
|
|
gw_native_receive(session,&packet,nullptr,0) || packet.opcode!=0x4d || packet.payload_bytes)
|
|
throw std::runtime_error("logout did not complete");
|
|
}
|
|
|
|
static int run_session(char** argv,unsigned account_round,bool switched_account){
|
|
const char* username=switched_account ? argv[8] : argv[4];
|
|
const char* password_path=switched_account ? argv[9] : argv[5];
|
|
gw_native_auth_config c{GW_NATIVE_ABI,uint16_t(std::strtoul(argv[2],nullptr,10)),username,password_path,argv[6]};
|
|
gw_native_login* login=nullptr;gw_native_session* session=nullptr;
|
|
int status;
|
|
const bool ipv4=std::getenv("GW_TEST_AUTH_IPV4")!=nullptr;
|
|
if(const char* memory=std::getenv("GW_TEST_AUTH_MEMORY");ipv4 || (memory && std::string_view(memory)=="1")){
|
|
// Synthetic fixture inputs only; never writes a password file for the
|
|
// ABI. The actual UI will supply a borrowed in-process buffer instead.
|
|
std::ifstream input(password_path,std::ios::binary);std::string password;char ch;
|
|
while(input.get(ch)){if(password.size()==65)return 1;password.push_back(ch);}
|
|
while(!password.empty() && (password.back()=='\n'||password.back()=='\r'))password.pop_back();
|
|
gw_native_auth_memory_config m{GW_NATIVE_ABI,c.auth_port,c.username,reinterpret_cast<const uint8_t*>(password.data()),uint32_t(password.size()),c.version_hash_path};
|
|
if(ipv4){
|
|
gw_native_auth_memory_ipv4_config explicit_config{GW_NATIVE_IPV4_ABI,sizeof(gw_native_auth_memory_ipv4_config),c.auth_port,0,
|
|
"127.0.0.1",c.username,m.password,m.password_bytes,c.version_hash_path};
|
|
status=gw_native_authenticate_memory_ipv4(&explicit_config,&login);
|
|
}else status=gw_native_authenticate_memory(&m,&login);
|
|
OPENSSL_cleanse(password.data(),password.size());
|
|
}else status=gw_native_authenticate(&c,&login);
|
|
if(status){std::cerr<<"native authentication rejected\n";return 1;}
|
|
try{
|
|
if(gw_native_discover(login) || gw_native_discover(login))throw std::runtime_error("discovery failed");
|
|
if(gw_native_realm_count(login)!=3)throw std::runtime_error("realm count");
|
|
if(const char* refresh=std::getenv("GW_TEST_REALM_REFRESH")){
|
|
const bool stable=std::string_view(refresh)=="refresh-stable";
|
|
const unsigned count=stable?5:1;
|
|
for(unsigned i=0;i<count;++i){
|
|
if((gw_native_confirm_realm_list(login)==0)!=stable)
|
|
throw std::runtime_error("unexpected realm refresh result");
|
|
}
|
|
gw_native_realm first{};
|
|
if(stable){
|
|
if(gw_native_realm_at(login,0,&first) || std::string_view(first.name)!="Realm0")
|
|
throw std::runtime_error("refresh changed selection index");
|
|
}else if(gw_native_realm_count(login) || !gw_native_discover(login)){
|
|
throw std::runtime_error("retired realm snapshot remained usable");
|
|
}
|
|
gw_native_login_close(login);
|
|
std::cout<<"realm refresh verified\n";
|
|
return 0;
|
|
}
|
|
gw_native_realm realm{};
|
|
if(gw_native_realm_at(login,0,&realm) || realm.type!=6 || realm.characters!=4 || realm.population!=.5f)throw std::runtime_error("realm metadata");
|
|
if(gw_native_select(login,1,argv[7],5875,&session)!=-1 || session)throw std::runtime_error("offline selection accepted");
|
|
if(gw_native_select(login,2,argv[7],5875,&session)!=-1 || session)throw std::runtime_error("nonlocal selection accepted");
|
|
if(gw_native_select(login,99,argv[7],5875,&session)!=-1 || session)throw std::runtime_error("invalid selection accepted");
|
|
gw_native_transport* prepared=nullptr;
|
|
if(gw_native_transport_open(login,2,argv[7],5875,&prepared)!=-1 || prepared)
|
|
throw std::runtime_error("version1 transport nonlocal guard changed");
|
|
if(ipv4){
|
|
// Authenticated advertisement192.0.2.1 is parsed/prepared only;
|
|
// there is deliberately NO world socket or public network test.
|
|
if(gw_native_transport_open_ipv4(login,1,argv[7],5875,&prepared)!=-1 || prepared ||
|
|
gw_native_transport_open_ipv4(login,99,argv[7],5875,&prepared)!=-1 || prepared ||
|
|
gw_native_transport_open_ipv4(login,2,argv[7],5875,&prepared) || !prepared ||
|
|
gw_native_transport_port(prepared)!=8090 || gw_native_transport_authenticated(prepared))
|
|
throw std::runtime_error("explicit numeric socket-free realm preparation failed");
|
|
gw_native_transport_close(prepared);prepared=nullptr;
|
|
}else if(gw_native_transport_open_ipv4(login,0,argv[7],5875,&prepared)!=-1 || prepared)
|
|
throw std::runtime_error("explicit realm profile accepted version1 login");
|
|
if(std::getenv("GW_TEST_NATIVE_TRANSPORT")){exercise_transport(login,argv[7]);return 0;}
|
|
if(gw_native_select(login,0,argv[7],5875,&session))throw std::runtime_error("world selection failed");
|
|
auto count=gw_native_character_count(session);std::cout<<"characters="<<count<<'\n';
|
|
uint32_t required=0;uint8_t tiny=0xa5;
|
|
if(gw_native_encode_roster(session,8192,0,0,nullptr,0,&required)!=-1 || required ||
|
|
gw_native_encode_roster(session,70,0x01010046,2,nullptr,0,&required)!=-3 || required<34 ||
|
|
gw_native_encode_roster(session,70,0x01010046,2,&tiny,1,&required)!=-3 || tiny!=0xa5)throw std::runtime_error("native roster buffer contract");
|
|
std::vector<uint8_t> roster(required);
|
|
if(gw_native_encode_roster(session,70,0x01010046,2,roster.data(),uint32_t(roster.size()),&required) || roster[0]!=0xc0)throw std::runtime_error("native roster encoding");
|
|
uint32_t feature_size=0;uint8_t canary=0xa5;
|
|
if(gw_native_encode_glue_features(session,0,nullptr,0,&feature_size)!=-1 || feature_size ||
|
|
gw_native_encode_glue_features(session,11,nullptr,0,&feature_size)!=-1 ||
|
|
gw_native_encode_glue_features(session,10,nullptr,0,&feature_size)!=-3 || feature_size!=106 ||
|
|
gw_native_encode_glue_features(session,10,&canary,1,&feature_size)!=-3 || canary!=0xa5)throw std::runtime_error("native glue capacity contract");
|
|
std::vector<uint8_t> features(feature_size);
|
|
if(gw_native_encode_glue_features(session,10,features.data(),uint32_t(features.size()),&feature_size) || features[18]!=10 || features[74]!=50 || features[76]!=0x58 || features[77]!=2 ||
|
|
features[94]!=93 || features[98]!=2)throw std::runtime_error("native glue fields");
|
|
for(unsigned i=0;i<count;++i){gw_native_character ch{};if(gw_native_character_at(session,i,&ch))throw std::runtime_error("character read");std::cout<<ch.guid<<' '<<ch.name<<" level="<<ch.level<<" race="<<ch.race<<" class="<<ch.class_id<<'\n';}
|
|
if(account_round)enter_and_logout(session,account_round,switched_account);
|
|
if(const char* mode=std::getenv("GW_TEST_NATIVE_LIFETIME");mode && count){
|
|
auto require=[](bool okay){if(!okay)throw std::runtime_error("native session-lifetime regression");};
|
|
gw_native_lifecycle state{};
|
|
require(!gw_native_lifecycle_at(session,&state) && state.server_proof_verified && state.world_authenticated && !state.character_login_sent && !state.stream_error);
|
|
require(gw_native_service(session,1001)==-1);
|
|
require(!gw_native_ping(session,11,12) && gw_native_ping(session,99,0)==-1);
|
|
require(!gw_native_lifecycle_at(session,&state) && state.ping_pending && state.pings_sent==1);
|
|
auto deadline=std::chrono::steady_clock::now()+std::chrono::seconds(2);
|
|
int serviced=0;
|
|
do{serviced=gw_native_service(session,5);}while(!serviced && std::chrono::steady_clock::now()<deadline);
|
|
auto kind=std::string_view(mode);
|
|
if(kind=="lifetime-success"){
|
|
require(serviced==1 && !gw_native_lifecycle_at(session,&state) && !state.ping_pending && state.pongs_received==1);
|
|
require(!gw_native_ping(session,12,12));
|
|
do{serviced=gw_native_service(session,5);}while(!serviced && std::chrono::steady_clock::now()<deadline);
|
|
require(serviced==1 && !gw_native_lifecycle_at(session,&state) && state.pongs_received==2);
|
|
}else if(kind=="lifetime-deferred"){
|
|
require(serviced==2 && gw_native_service(session,0)==2 && gw_native_ping(session,12,0)==-1);
|
|
require(!gw_native_lifecycle_at(session,&state) && state.packet_pending);
|
|
require(!gw_native_begin_world(session,42));
|
|
gw_native_packet packet{};uint8_t body[20]{};
|
|
require(gw_native_receive(session,&packet,nullptr,0)==-3 && packet.opcode==0x236 && packet.payload_bytes==20);
|
|
require(!gw_native_receive(session,&packet,body,sizeof(body)) && body[0]==12);
|
|
require(!gw_native_lifecycle_at(session,&state) && state.character_login_sent && !state.packet_pending);
|
|
}else{
|
|
int expected=kind=="lifetime-integrity"?-4:-2;
|
|
require(serviced==expected && gw_native_service(session,0)==expected && gw_native_ping(session,12,0)==expected);
|
|
require(!gw_native_lifecycle_at(session,&state) && state.stream_error==expected && !state.pongs_received);
|
|
}
|
|
std::cout<<"native session lifetime verified\n";
|
|
}
|
|
if(std::getenv("GW_TEST_NATIVE_STREAM") && count){
|
|
auto require=[](bool okay){if(!okay)throw std::runtime_error("native ordered-stream regression");};
|
|
gw_native_packet p{};uint8_t buffer[32];std::memset(buffer,0xa5,sizeof(buffer));
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==-1);
|
|
require(gw_native_send(session,0x1dc,nullptr,0)==-1);
|
|
require(gw_native_begin_world(session,43)==-1);
|
|
require(gw_native_begin_world(session,42)==0 && gw_native_begin_world(session,42)==-1);
|
|
require(gw_native_send(session,0x1ed,nullptr,0)==-1 && gw_native_send(session,0x2e7,nullptr,0)==-1 && gw_native_send(session,0x3d,nullptr,0)==-1);
|
|
require(gw_native_send(session,0x1dc,buffer,10237)==-1);
|
|
if(std::getenv("GW_TEST_STREAM_FAILURE")){
|
|
int expected=std::string_view(std::getenv("GW_TEST_STREAM_FAILURE"))=="integrity"?-4:-2;
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==expected);
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==expected && !p.opcode && !p.payload_bytes);
|
|
require(gw_native_send(session,0x1dc,buffer,8)==expected);
|
|
}else{
|
|
require(gw_native_receive(session,&p,nullptr,0)==-3 && p.opcode==0x236 && p.payload_bytes==20);
|
|
require(gw_native_receive(session,&p,buffer,19)==-3 && p.opcode==0x236 && p.payload_bytes==20);
|
|
for(auto b:buffer)require(b==0xa5); // No partial copy on failure.
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==0 && p.opcode==0x236 && p.payload_bytes==20);
|
|
require(buffer[0]==12 && buffer[1]==0 && buffer[2]==0 && buffer[3]==0);
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==0 && p.opcode==0xa9 && p.payload_bytes==5);
|
|
const uint8_t ping[8]={7,0,0,0,12,0,0,0};
|
|
require(gw_native_send(session,0x1dc,ping,sizeof(ping))==0);
|
|
require(gw_native_receive(session,&p,buffer,sizeof(buffer))==0 && p.opcode==0x1dd && p.payload_bytes==4 && buffer[0]==7);
|
|
require(gw_native_receive(session,&p,nullptr,0)==0 && p.opcode==0x4d && !p.payload_bytes);
|
|
}
|
|
std::cout<<"native ordered stream verified\n";
|
|
}
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';gw_native_close(session);gw_native_login_close(login);return 1;}
|
|
gw_native_close(session);gw_native_login_close(login);return 0;
|
|
}
|
|
|
|
int main(int argc,char** argv){
|
|
const bool full_logout=std::getenv("GW_TEST_FULL_ACCOUNT_REENTRY")!=nullptr;
|
|
const bool switch_accounts=full_logout && argc==10;
|
|
if(argc!=8 && !switch_accounts)return 1;
|
|
if(!full_logout)return run_session(argv,0,false);
|
|
for(unsigned round=1;round<=2;++round){
|
|
if(run_session(argv,round,switch_accounts && round==2))return 1;
|
|
}
|
|
if(switch_accounts)std::cout<<"different account entered its own character and logged out PASS\n";
|
|
else if(std::getenv("GW_TEST_WRONG_WORLD_SELF"))std::cout<<"wrong second-world self rejected\n";
|
|
else std::cout<<"same-account same-character world reentry after full logout PASS\n";
|
|
return 0;
|
|
}
|