From 936fe85cd6fce7ff0bea8b607aaa1f674dbf8493 Mon Sep 17 00:00:00 2001 From: leitner Date: Sun, 24 Sep 2023 19:43:39 +0000 Subject: [PATCH] add unit tests --- scan_ldapsearchresultentry.c | 113 ++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 16 deletions(-) diff --git a/scan_ldapsearchresultentry.c b/scan_ldapsearchresultentry.c index 747734d..a103242 100644 --- a/scan_ldapsearchresultentry.c +++ b/scan_ldapsearchresultentry.c @@ -1,34 +1,51 @@ #include #include "ldap.h" +#ifdef UNITTEST +void* mycalloc(size_t a,size_t b); +#define calloc mycalloc +#endif + size_t scan_ldapsearchresultentry(const char* src,const char* max,struct SearchResultEntry* sre) { size_t res,tmp,oslen; /* outer sequence length */ struct PartialAttributeList** a=&sre->attributes; *a=0; - if (!(res=scan_ldapstring(src,max,&sre->objectName))) goto error; - if (!(tmp=scan_asn1SEQUENCE(src+res,max,&oslen))) goto error; + if (!(res=scan_ldapstring(src,max,&sre->objectName))) // fail01 + goto error; + if (!(tmp=scan_asn1SEQUENCE(src+res,max,&oslen))) // fail02 + goto error; res+=tmp; - if (src+res+oslen>max) goto error; + if (src+res+oslen != max) // fail03 + goto error; max=src+res+oslen; /* we now may have a stronger limit */ while (src+resmax) goto error; - if (!(tmp=scan_ldapstring(src+res,nmax,&s))) goto error; - if (!(*a=malloc(sizeof(struct PartialAttributeList)))) goto error; - (*a)->next=0; (*a)->values=0; (*a)->type=s; + if (!(tmp=scan_asn1SEQUENCE(src+res,max,&islen))) // fail04 + goto error; res+=tmp; - if (!(tmp=scan_asn1SET(src+res,max,&islen))) goto error; - res+=tmp; if (src+res+islen!=nmax) goto error; + nmax=src+res+islen; + if (!(tmp=scan_ldapstring(src+res,nmax,&s))) // fail05 + goto error; + if (!(*a=calloc(1,sizeof(struct PartialAttributeList)))) // fail06 + goto error; + (*a)->next=0; (*a)->values=0; (*a)->type=s; + x = &(*a)->values; + res+=tmp; + if (!(tmp=scan_asn1SET(src+res,nmax,&islen))) // fail07 + goto error; + res+=tmp; + if (src+res+islen!=nmax) // fail08 + goto error; while (src+resa=s; - x->next=(*a)->values; - (*a)->values=x; + if (!(tmp=scan_ldapstring(src+res,max,&s))) // fail09 + goto error; + if (!((*x)=calloc(1,sizeof(struct AttributeDescriptionList)))) //fail10 + goto error; + (*x)->a=s; + x = &(*x)->next; res+=tmp; } a=&(*a)->next; @@ -41,3 +58,67 @@ error: return 0; } +#ifdef UNITTEST +#undef UNITTEST +#include +#include +#include "freepal.c" +#include "scan_ldapstring.c" +#include "scan_asn1STRING.c" +#include "scan_asn1tag.c" +#include "scan_asn1tagint.c" +#include "scan_asn1length.c" +#include "scan_asn1string.c" +#include "scan_asn1SEQUENCE.c" +#include "scan_asn1SET.c" +#include "free_ldapsearchresultentry.c" + +#undef calloc +size_t callocfail=0; +void* mycalloc(size_t a,size_t b) { + if (--callocfail==0) return 0; + return calloc(a,b); +} + +int main() { + static char buf[512]; + memcpy(buf, + "\x04\x21" // 0 string (len 33) + "ts=1234567890,ou=blog,d=fefe,c=de" + "\x30\x81\xc5" // 35 sequence (len 197) + "\x30\x81\xae" // 38 sequence (len 174) + "\x04\x04" // 41 string (len 4) + "text" + "\x31\x81\xa5" // 47 set (len 165) + "\x04\x81\xa2" // 50 string (len 162) + "Apple konnte kein eigenes 5G-Modem bauen, kauft weiter bei Qualcomm." + "\x30\x12" // 215 sequence (len 18) + "\x04\x02" // 217 string (len 2) + "ts" + "\x31\x0c" // 221 set (len 12) + "\x04\x0a" // 223 string (len 10) + "1234567890", + 235); + struct SearchResultEntry s; + assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 235); + assert(s.objectName.s==buf+2 && s.objectName.l==33); + assert(s.attributes && s.attributes->type.s==buf+43 && s.attributes->type.l==4); + assert(s.attributes->values && s.attributes->values->a.s==buf+53 && s.attributes->values->a.l==162 && s.attributes->values->next==0); + assert(s.attributes->next && s.attributes->next->type.s==buf+219 && s.attributes->next->type.l==2 && s.attributes->next->next==0); + assert(s.attributes->next->values && s.attributes->next->values->a.s==buf+225 && s.attributes->next->values->a.l==10 && s.attributes->next->values->next==0); + free_ldapsearchresultentry(&s); + + callocfail=2; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail10 + callocfail=1; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail06 + buf[224]++; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail09 + buf[222]--; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail08 + buf[222]++; buf[221]=0x30; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail07 + buf[217]=0; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail05 + buf[215]=0; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail04 + buf[37]--; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail03 + buf[35]=0; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail02 + buf[0]=0; assert(scan_ldapsearchresultentry(buf,buf+235,&s) == 0); // fail01 + return 0; +} + +#endif