X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=adns.git;a=blobdiff_plain;f=regress%2Fhcommon.c.m4;h=311087e2c9e902873320acb3a8728856da3ae4f9;hp=b64dbd7a32353c983ba9711d3810b1716e954b85;hb=e3a821df6cbbca034915317ea147133504b96649;hpb=79833e62bc1a9625cc29b644e89be02b01e24dec diff --git a/regress/hcommon.c.m4 b/regress/hcommon.c.m4 index b64dbd7..311087e 100644 --- a/regress/hcommon.c.m4 +++ b/regress/hcommon.c.m4 @@ -28,6 +28,7 @@ m4_include(hmacros.i4) #include #include #include +#include #include #include @@ -51,6 +52,9 @@ const struct Terrno Terrnos[]= { { "ENOPROTOOPT", ENOPROTOOPT }, { "ENOSPC", ENOSPC }, { "EWOULDBLOCK", EWOULDBLOCK }, + { "EHOSTUNREACH", EHOSTUNREACH }, + { "ECONNREFUSED", ECONNREFUSED }, + { "EPIPE", EPIPE }, { 0, 0 } }; @@ -128,7 +132,7 @@ void Tvbbytes(const void *buf, int len) { const byte *bp; int i; - if (!len) { Tvba(" empty"); return; } + if (!len) { Tvba("\n ."); return; } for (i=0, bp=buf; in && te->v != e; te++); - if (te->n) Tvba(te->n); - else Tvbf("E#%d",e); + assert(te->n); + Tvba(te->n); } void Tvba(const char *str) { @@ -227,3 +229,80 @@ void Tnomem(void) { void Toutputerr(void) { Tfailed("write error on test harness output"); } + +struct malloced { + struct malloced *next, *back; + size_t sz; + unsigned long count; + struct { double d; long ul; void *p; void (*fp)(void); } data; +}; + +static unsigned long malloccount, mallocfailat; +static struct { struct malloced *head, *tail; } mallocedlist; + +#define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head) + +void *Hmalloc(size_t sz) { + struct malloced *newnode; + const char *mfavar; + char *ep; + + assert(sz); + + newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem(); + + LIST_LINK_TAIL(mallocedlist,newnode); + newnode->sz= sz; + newnode->count= ++malloccount; + if (!mallocfailat) { + mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT"); + if (mfavar) { + mallocfailat= strtoul(mfavar,&ep,10); + if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value"); + } else { + mallocfailat= ~0UL; + } + } + assert(newnode->count != mallocfailat); + memset(&newnode->data,0xc7,sz); + return &newnode->data; +} + +void Hfree(void *ptr) { + struct malloced *oldnode; + + if (!ptr) return; + + oldnode= (void*)((char*)ptr - MALLOCHSZ); + LIST_UNLINK(mallocedlist,oldnode); + memset(&oldnode->data,0x38,oldnode->sz); + free(oldnode); +} + +void *Hrealloc(void *op, size_t nsz) { + struct malloced *oldnode; + void *np; + size_t osz; + + if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; } + np= Hmalloc(nsz); + memcpy(np,op, osz>nsz ? nsz : osz); + Hfree(op); + return np; +} + +void Hexit(int rv) { + struct malloced *loopnode; + + Tshutdown(); + adns__vbuf_free(&vb); + adns__vbuf_free(&vbw); + if (mallocedlist.head) { + fprintf(stderr,"adns test harness: memory leaked:"); + for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next) + fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz); + putc('\n',stderr); + if (ferror(stderr)) exit(-1); + } + exit(rv); +}