static bool test_fileio_checkPrint(int res,int expRes,char *recvStr,const char *expStr) { if(!test_assertStr(recvStr,expStr)) return false; if(!test_assertInt(res,expRes == -1 ? (int)strlen(expStr) : expRes)) return false; return true; }
static void test_dirname(void) { struct DirnameTest { const char *input; const char *expected; } tests[] = { {NULL,"."}, {"","."}, {"/","/"}, {".","."}, {"..","."}, {"a","."}, {"foo","."}, {"/foo/bar","/foo"}, {"/foo/bar/","/foo"}, {"/foo/bar///","/foo"}, {"/a/","/"}, {"abc///","."}, {"abc//def///","abc"}, {"abc/def/.","abc/def"}, }; test_caseStart("Testing dirname"); for(size_t i = 0; i < ARRAY_SIZE(tests); ++i) { char *cpy = tests[i].input ? strdup(tests[i].input) : NULL; test_assertTrue(tests[i].input == NULL || cpy != NULL); test_assertStr(dirname(cpy),tests[i].expected); free(cpy); } test_caseSucceeded(); }
static bool test_fileio_checkScan(uint recvRes,uint expRes,const char *fmt,...) { va_list ap; char ch; char *rs,*es; uint ru,eu; int rd,ed; if(!test_assertUInt(recvRes,expRes)) return false; va_start(ap,fmt); while((ch = *fmt++)) { if(ch == '%') ch = *fmt++; switch(ch) { /* signed */ case 'c': case 'd': rd = va_arg(ap,int); ed = va_arg(ap,int); if(!test_assertInt(rd,ed)) { va_end(ap); return false; } break; /* unsigned */ case 'x': case 'b': case 'o': case 'u': ru = va_arg(ap,uint); eu = va_arg(ap,uint); if(!test_assertUInt(ru,eu)) { va_end(ap); return false; } break; /* string */ case 's': rs = va_arg(ap,char*); es = va_arg(ap,char*); if(!test_assertStr(rs,es)) { va_end(ap); return false; } break; } } va_end(ap); return true; }
static void fs_readFile(const char *name,const char *content) { char buf[100] = {0}; FILE *f = fopen(name,"r"); test_assertTrue(f != NULL); if(f != NULL) { test_assertInt(fseek(f,0,SEEK_END),0); test_assertInt(ftell(f),strlen(content)); rewind(f); test_assertInt(fread(buf,1,strlen(content),f),strlen(content)); test_assertStr(buf,content); fclose(f); } }
static void test_canonpath(void) { char path[MAX_PATH_LEN]; size_t count; test_caseStart("Testing canonpath"); setenv("CWD","/"); count = canonpath(path,sizeof(path),"/"); test_assertUInt(count,SSTRLEN("/")); test_assertStr(path,"/"); count = canonpath(path,sizeof(path),"/bin/ls"); test_assertUInt(count,SSTRLEN("/bin/ls")); test_assertStr(path,"/bin/ls"); count = canonpath(path,sizeof(path),"/../bin/../.././home"); test_assertUInt(count,SSTRLEN("/home")); test_assertStr(path,"/home"); count = canonpath(path,sizeof(path),"bin/..///.././home"); test_assertUInt(count,SSTRLEN("/home")); test_assertStr(path,"/home"); count = canonpath(path,sizeof(path),"bin/./ls"); test_assertUInt(count,SSTRLEN("/bin/ls")); test_assertStr(path,"/bin/ls"); setenv("CWD","/home"); count = canonpath(path,sizeof(path),"hrniels/./scripts"); test_assertUInt(count,SSTRLEN("/home/hrniels/scripts")); test_assertStr(path,"/home/hrniels/scripts"); setenv("CWD","/home/"); count = canonpath(path,sizeof(path),"hrniels/./scripts"); test_assertUInt(count,SSTRLEN("/home/hrniels/scripts")); test_assertStr(path,"/home/hrniels/scripts"); count = canonpath(path,sizeof(path),".."); test_assertUInt(count,SSTRLEN("/")); test_assertStr(path,"/"); count = canonpath(path,sizeof(path),"../../."); test_assertUInt(count,SSTRLEN("/")); test_assertStr(path,"/"); count = canonpath(path,sizeof(path),"./../bin"); test_assertUInt(count,SSTRLEN("/bin")); test_assertStr(path,"/bin"); count = canonpath(path,3,"/"); if(count > 3) test_caseFailed("Copied too much"); count = canonpath(path,8,"/bin/ls"); if(count > 8) test_caseFailed("Copied too much"); count = canonpath(path,8,"/bin/../home"); if(count > 8) test_caseFailed("Copied too much"); count = canonpath(path,8,"///../bin/ls"); if(count > 8) test_caseFailed("Copied too much"); test_caseSucceeded(); }
static void test_abspath(void) { char *p,path[MAX_PATH_LEN]; test_caseStart("Testing abspath"); setenv("CWD","/home/hrniels"); p = abspath(path,sizeof(path),"."); test_assertStr(p,"/home/hrniels/."); p = abspath(path,sizeof(path),"/"); test_assertStr(p,"/"); p = abspath(path,sizeof(path),"../.."); test_assertStr(p,"/home/hrniels/../.."); p = abspath(path,sizeof(path),"http://www.example.com"); test_assertStr(p,"/dev/http/www.example.com"); p = abspath(path,sizeof(path),"a://"); test_assertStr(p,"/dev/a/"); p = abspath(path,sizeof(path),"a:/"); test_assertStr(p,"/home/hrniels/a:/"); p = abspath(path,8,"/"); test_assertStr(p,"/"); p = abspath(path,8,".."); test_assertStr(p,"/home/h"); p = abspath(path,8,"f://bar"); test_assertStr(p,"/dev/f/"); p = abspath(path,8,"foobar://bar"); test_assertStr(p,"/dev/fo"); p = abspath(path,2,"a://b"); test_assertStr(p,"/"); p = abspath(path,1,"a://b"); test_assertStr(p,""); p = abspath(path,1,"."); test_assertStr(p,""); test_caseSucceeded(); }
static void test_basics(void) { size_t count,oldFree; test_caseStart("Testing basics"); { sGroup *g; oldFree = heapspace(); g = group_parse("0:root:0",&count); test_assertTrue(g != NULL); test_assertSize(count,1); if(g) { test_assertTrue(g->next == NULL); test_assertUInt(g->gid,0); test_assertStr(g->name,"root"); test_assertSize(g->userCount,1); test_assertUInt(g->users[0],0); } group_free(g); test_assertSize(heapspace(),oldFree); } { sGroup *g; oldFree = heapspace(); g = group_parse("0:root\n",&count); test_assertTrue(g != NULL); test_assertSize(count,1); if(g) { test_assertTrue(g->next == NULL); test_assertUInt(g->gid,0); test_assertStr(g->name,"root"); test_assertSize(g->userCount,0); } group_free(g); test_assertSize(heapspace(),oldFree); } { sGroup *g; oldFree = heapspace(); g = group_parse("0:root:",&count); test_assertTrue(g != NULL); test_assertSize(count,1); if(g) { test_assertTrue(g->next == NULL); test_assertUInt(g->gid,0); test_assertStr(g->name,"root"); test_assertSize(g->userCount,0); } group_free(g); test_assertSize(heapspace(),oldFree); } { sGroup *g; oldFree = heapspace(); g = group_parse("2444:a:100:200",&count); test_assertTrue(g != NULL); test_assertSize(count,1); if(g) { test_assertTrue(g->next == NULL); test_assertUInt(g->gid,2444); test_assertStr(g->name,"a"); test_assertSize(g->userCount,2); test_assertUInt(g->users[0],100); test_assertUInt(g->users[1],200); } group_free(g); test_assertSize(heapspace(),oldFree); } { sGroup *g,*res; oldFree = heapspace(); res = group_parse("1:a:1:2\n\n2:b:4",&count); test_assertSize(count,2); g = res; test_assertTrue(g != NULL); if(g) { test_assertUInt(g->gid,1); test_assertStr(g->name,"a"); test_assertSize(g->userCount,2); test_assertUInt(g->users[0],1); test_assertUInt(g->users[1],2); g = g->next; } test_assertTrue(g != NULL); if(g) { test_assertUInt(g->gid,2); test_assertStr(g->name,"b"); test_assertSize(g->userCount,1); test_assertUInt(g->users[0],4); } group_free(res); test_assertSize(heapspace(),oldFree); } test_caseSucceeded(); }