Files
WoW-Hybrid-gateway/tests/world_locator_fifo_test.c
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

83 lines
5.8 KiB
C

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "world_locator_fifo.h"
struct heap { unsigned allocations,frees;int fail; };
static void* allocate(void* context,size_t bytes){struct heap* h=context;if(h->fail)return 0;++h->allocations;return malloc(bytes);}
static void release(void* context,void* pointer){struct heap* h=context;++h->frees;free(pointer);}
static void put(unsigned char* p,uint32_t v){for(unsigned i=0;i<4;++i)p[i]=(unsigned char)(v>>(i*8));}
static void put64(unsigned char* p,uint64_t v){put(p,(uint32_t)v);put(p+4,(uint32_t)(v>>32));}
static void bundle(unsigned char* p,uint32_t opcode,uint32_t value){put(p,1);put(p+4,opcode);put(p+8,4);put(p+12,value);}
static void fixture_header(unsigned char* p){
memset(p,0,4096);p[0]='M';p[1]='Z';put(p+0x3c,0x80);memcpy(p+0x80,"PE\0\0",4);
p[0x84]=0x64;p[0x85]=0x86;p[0x86]=1;put(p+0x88,1791563511);p[0x94]=0xf0;
p[0x98]=0x0b;p[0x99]=2;put64(p+0x98+24,UINT64_C(0x140000000));
put(p+0x98+56,139165696);put(p+0x98+60,0x400);
memcpy(p+0x188,".text",5);put(p+0x190,75236444);put(p+0x194,0x1000);put(p+0x1ac,0x60000020);
}
int main(void){
unsigned char first[16],second[16],third[16],ids[24]={0},reply[20]={0},header[4096];
struct gw_world_locator_fifo fifo={0};struct heap h={0};uint32_t count,out[4096];
bundle(first,0x690019,45595);bundle(second,0x69001a,1);bundle(third,0x5e0000,4509);
assert(gw_world_locator_config(0,0,1)==1);
assert(!gw_world_locator_config(0,0,0));
assert(gw_world_locator_config("1",1,0)==1);
assert(!gw_world_locator_config("0",1,1));
assert(gw_world_locator_config("2",1,1)<0);
assert(gw_world_locator_config("11",2,1)<0);
assert(!gw_world_locator_expired(100,5099)&&gw_world_locator_expired(100,5100));
assert(!gw_world_locator_expired(UINT64_MAX-10,10));
put64(ids,4509);put(ids+8,3);put(ids+12,12809);put(ids+16,23922);put(ids+20,45595);
assert(!gw_world_locator_spell_ids(ids,sizeof(ids),4509,out,&count)&&count==3&&out[2]==45595);
assert(gw_world_locator_spell_ids(ids,sizeof(ids),4526,out,&count)<0);
assert(gw_world_locator_spell_ids(ids,sizeof(ids)-1,4509,out,&count)<0);
put(ids+20,12809);assert(gw_world_locator_spell_ids(ids,sizeof(ids),4509,out,&count)<0);
put(ids+20,23922);assert(gw_world_locator_spell_ids(ids,sizeof(ids),4509,out,&count)<0);
put(ids+8,4097);assert(gw_world_locator_spell_ids(ids,sizeof(ids),4509,out,&count)<0);
put(ids+8,0);assert(!gw_world_locator_spell_ids(ids,12,4509,out,&count)&&!count);
put(reply,0xe111669e);put(reply+4,45595);reply[12]=0x20;put(reply+13,3);reply[17]=0;reply[18]=0x80;reply[19]=0xff;
assert(gw_world_locator_registry_status(reply,20,45595)==1);
assert(gw_world_locator_registry_status(reply,20,51602)<0);
reply[12]=0x40;assert(gw_world_locator_registry_status(reply,20,45595)<0);
reply[12]=0x60;put(reply+13,0);assert(!gw_world_locator_registry_status(reply,17,45595));
reply[12]=0x21;assert(gw_world_locator_registry_status(reply,17,45595)<0);
reply[12]=0x20;assert(gw_world_locator_registry_status(reply,17,45595)<0);
assert(!gw_world_locator_fifo_append(&fifo,first,16,allocate,&h));
put(first+12,0);assert(gw_world_locator_u32((unsigned char*)(fifo.head+1)+12)==45595);
assert(!gw_world_locator_fifo_append(&fifo,second,16,allocate,&h));
/* A partly flushed prefix remains owned; new self cannot overtake it. */
fifo.head->cursor=16;
assert(!gw_world_locator_fifo_append(&fifo,third,16,allocate,&h));
assert(fifo.bundles==3&&fifo.records==3&&fifo.bytes==48);
assert(gw_world_locator_u32((unsigned char*)(fifo.head->next+1)+4)==0x69001a);
assert(gw_world_locator_u32((unsigned char*)(fifo.tail+1)+4)==0x5e0000);
gw_world_locator_fifo_pop(&fifo,release,&h);
assert(fifo.records==2&&gw_world_locator_u32((unsigned char*)(fifo.head+1)+4)==0x69001a);
gw_world_locator_fifo_clear(&fifo,release,&h);assert(!fifo.head&&!fifo.tail&&!fifo.bytes&&h.allocations==h.frees);
gw_world_locator_fifo_clear(&fifo,release,&h);assert(h.allocations==h.frees);
h.fail=1;assert(gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-3&&!fifo.head);h.fail=0;
put(third,1025);assert(gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-1&&!fifo.head);
bundle(third,0x5e0000,4509);put(third+8,5);assert(gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-1);
bundle(third,0x5e0000,4509);fifo.records=4096;assert(gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-2);fifo.records=0;
fifo.bytes=GW_WORLD_LOCATOR_MAX_BYTES;assert(gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-2);fifo.bytes=0;
{unsigned char empty[4]={0};assert(!gw_world_locator_fifo_append(&fifo,empty,4,allocate,&h)&&!fifo.head);}
{
unsigned char* large=malloc(1048576);assert(large);memset(large,0,1048576);
put(large,1);put(large+4,0x5e0000);put(large+8,1048576-12);
for(unsigned i=0;i<16;++i)assert(!gw_world_locator_fifo_append(&fifo,large,1048576,allocate,&h));
assert(fifo.bytes==GW_WORLD_LOCATOR_MAX_BYTES&&gw_world_locator_fifo_append(&fifo,third,16,allocate,&h)==-2);
gw_world_locator_fifo_clear(&fifo,release,&h);free(large);assert(h.allocations==h.frees);
}
fixture_header(header);assert(gw_world_locator_main_header(header,4096,UINT64_C(0x140000000)));
assert(!gw_world_locator_main_header(header,4096,UINT64_C(0x140001000)));
put(header+0x88,1791563512);assert(!gw_world_locator_main_header(header,4096,UINT64_C(0x140000000)));
fixture_header(header);put(header+0x190,75236445);assert(!gw_world_locator_main_header(header,4096,UINT64_C(0x140000000)));
fixture_header(header);put(header+0x98+60,0x1000);assert(!gw_world_locator_main_header(header,4096,UINT64_C(0x140000000)));
fixture_header(header);header[0x94]=0xff;header[0x95]=0xff;assert(!gw_world_locator_main_header(header,4096,UINT64_C(0x140000000)));
return 0;
}