Files
WoW-Hybrid-gateway/tests/legacy_source_key_width_test.cpp
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

92 lines
5.7 KiB
C++

#include "gateway/legacy_stream.hpp"
#include <openssl/bn.h>
#include <algorithm>
#include <array>
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace gateway;
namespace {
void require(bool yes,const char* why){if(!yes)throw std::runtime_error(why);}
Bytes synthetic_key(size_t width){Bytes key(40);for(size_t i=0;i<width;++i)key[i]=uint8_t(17+13*i);return key;}
Bytes source_big_number_bytes(const Bytes& little){
std::unique_ptr<BIGNUM,decltype(&BN_free)> number(BN_lebin2bn(little.data(),int(little.size()),nullptr),BN_free);
require(bool(number),"reference BigNumber allocation");
auto count=BN_num_bytes(number.get());require(count>0,"reference source world rejects zero numeric key");
Bytes result(size_t(count),0);require(BN_bn2lebinpad(number.get(),result.data(),count)==count,"reference BigNumber little-endian conversion");
return result;
}
// Independent reference follows Tortoise AuthCrypt's actual key-vector width;
// BigNumber conversion above supplies that vector independently of Cipher.
struct Reference {
Bytes key;size_t index=0;uint8_t previous=0;
explicit Reference(Bytes input):key(std::move(input)){require(!key.empty(),"reference nonempty key");}
void encrypt(std::span<uint8_t> bytes){for(auto& value:bytes){value=uint8_t((value^key[index])+previous);previous=value;index=(index+1)%key.size();}}
void decrypt(std::span<uint8_t> bytes){for(auto& value:bytes){auto encrypted=value;value=uint8_t(encrypted-previous)^key[index];previous=encrypted;index=(index+1)%key.size();}}
};
void inbound(const Bytes& key,size_t expected_width,size_t chunk){
auto canonical=source_big_number_bytes(key);require(canonical.size()==expected_width,"source numeric width");
Reference server(canonical);LegacyPacketStream reader(key);Bytes combined;std::vector<LegacyPacket> expected;
for(unsigned i=0;i<32;++i){
Bytes body(i<8?5:(i==8?12289:(i%7==0?4073:5)),uint8_t(i));auto opcode=i<8?0x127u:0xa9u;
expected.push_back({opcode,body});auto packet=frame(opcode,body,false);
server.encrypt(std::span(packet).first(4));combined.insert(combined.end(),packet.begin(),packet.end());
}
size_t at=0,got=0;
while(at<combined.size()){
auto end=std::min(combined.size(),at+chunk);
while(at<end){
auto used=reader.feed(std::span(combined).subspan(at,end-at));require(used>0,"partial source stream made progress");at+=used;
if(reader.ready()){
auto packet=reader.take();require(got<expected.size(),"source stream extra packet");
require(packet.opcode==expected[got].opcode && packet.body==expected[got].body,"source decrypt width/fragment boundary differs");++got;
}
}
}
require(got==expected.size() && !reader.ready() && reader.needed()==4,"complete source stream packet count");
}
void outbound(const Bytes& key,size_t expected_width){
auto canonical=source_big_number_bytes(key);require(canonical.size()==expected_width,"source output numeric width");
Reference server(canonical);LegacyPacketStream writer(key);
for(unsigned i=0;i<35;++i){
Bytes body(i%5,uint8_t(i));auto encrypted=writer.encode(0x37,body);
server.decrypt(std::span(encrypted).first(6));require(encrypted==frame(0x37,body,true),"source outgoing header wrap differs");
}
}
void default_fixed_width_preserved(){
auto key=synthetic_key(39);Bytes plain(160,0x5a),expected=plain,actual=plain,source=plain;
Reference fixed(key),numeric(source_big_number_bytes(key));fixed.encrypt(expected);numeric.encrypt(source);
Cipher old_contract(key);old_contract.encrypt(actual);
require(actual==expected,"generic fixed40 Cipher contract changed");require(actual!=source,"synthetic width fixture must distinguish fixed40 from39");
Cipher explicit_source(key,HeaderKeyWidth::SourceBigNumber);actual=plain;explicit_source.encrypt(actual);
require(actual==source,"explicit source BigNumber width policy");
require(std::equal(actual.begin(),actual.begin()+39,expected.begin()),"source headers must agree before39-byte wrap");
require(actual[39]!=expected[39],"source39-byte key must diverge at first fixed40 boundary");
}
void strict_inputs(){
for(size_t size:{size_t(0),size_t(1),size_t(39),size_t(41)}){
for(auto policy:{HeaderKeyWidth::Fixed40,HeaderKeyWidth::SourceBigNumber}){
bool failed=false;try{Cipher bad(Bytes(size,1),policy);}catch(const Error&){failed=true;}require(failed,"cipher SRP input must remain exactly40 bytes");
}
}
bool rejected=false;try{LegacyPacketStream zero(Bytes(40,0));}catch(const Error&){rejected=true;}
require(rejected,"source numeric zero key must reject before modulo");
Cipher generic_zero(Bytes(40,0));Bytes data(6);generic_zero.encrypt(data);
rejected=false;try{Cipher bad(synthetic_key(40),HeaderKeyWidth(99));}catch(const Error&){rejected=true;}
require(rejected,"unknown width policy must reject");
}
}
int main(){try{
strict_inputs();default_fixed_width_preserved();
unsigned cases=0;
for(size_t width:{size_t(40),size_t(39),size_t(38),size_t(32),size_t(2),size_t(1)}){
auto key=synthetic_key(width);
for(size_t chunk:{size_t(1),size_t(2),size_t(3),size_t(7),size_t(31),size_t(4096),size_t(65535)}){inbound(key,width,chunk);++cases;}
outbound(key,width);++cases;
}
auto low_zero=synthetic_key(39);low_zero[0]=0;inbound(low_zero,39,3);outbound(low_zero,39);cases+=2;
auto high_bit=synthetic_key(40);high_bit.back()=0x80;inbound(high_bit,40,1);outbound(high_bit,40);cases+=2;
std::cout<<"Legacy source header width: "<<cases<<" stream cases plus strict/default contracts PASS\n";
return 0;
}catch(const std::exception& error){std::cerr<<"Legacy source header fixture: "<<error.what()<<'\n';return 1;}}