static void psetup_impl(int load2) { FILE *f; int rc; unsigned signo=0; engine = cl_engine_new(); fail_unless(!!engine , "cl_engine_new"); phishing_init(engine); fail_unless(!!engine->phishcheck, "phishing_init"); rc = init_domainlist(engine); fail_unless(rc == 0,"init_domainlist"); f = fdopen(open_testfile("input/daily.pdb"),"r"); fail_unless(!!f, "fopen daily.pdb"); rc = load_regex_matcher(engine, engine->domainlist_matcher, f, &signo, 0, 0, NULL, 1); fail_unless(rc == 0, "load_regex_matcher"); fclose(f); fail_unless_fmt(signo == 201, "Incorrect number of signatures: %u, expected %u", signo, 201); if(load2) { f = fdopen(open_testfile("input/daily.gdb"),"r"); fail_unless(!!f, "fopen daily.gdb"); signo = 0; rc = load_regex_matcher(engine, engine->domainlist_matcher, f, &signo, 0, 0, NULL, 1); fail_unless(rc == 0, "load_regex_matcher"); fclose(f); fail_unless_fmt(signo == 4, "Incorrect number of signatures: %u, expected %u", signo, 4); } loaded_2 = load2; rc = init_whitelist(engine); fail_unless(rc == 0,"init_whitelist"); f = fdopen(open_testfile("input/daily.wdb"),"r"); signo = 0; rc = load_regex_matcher(engine, engine->whitelist_matcher, f, &signo, 0, 1, NULL, 1); fail_unless(rc == 0,"load_regex_matcher"); fclose(f); fail_unless_fmt(signo == 31, "Incorrect number of signatures: %u, expected %u", signo, 31); rc = cli_build_regex_list(engine->whitelist_matcher); fail_unless(rc == 0,"cli_build_regex_list"); rc = cli_build_regex_list(engine->domainlist_matcher); fail_unless(rc == 0,"cli_build_regex_list"); fail_unless(is_regex_ok(engine->whitelist_matcher),"is_regex_ok"); fail_unless(is_regex_ok(engine->domainlist_matcher),"is_regex_ok"); }
int reverse_test() { char wbuf[1024]; char rbuf[1024]; printf("Reverse write test...\n"); for(int pass=1;pass<=2;pass++){ AFFILE *af = open_testfile("test_reverse",pass==1); for(int i=MAX_FMTS-1;i>=0;i--){ sprintf(wbuf,fmt,i); af_seek(af,strlen(wbuf)*i,SEEK_SET); if(pass==1){ if(af_write(af,(unsigned char *)wbuf,strlen(wbuf))!=(int)strlen(wbuf)){ err(1,"Attempt to write buffer %d failed\n",i); } } if(pass==2){ memset(rbuf,0,sizeof(rbuf)); if(af_read(af,(unsigned char *)rbuf,strlen(wbuf))!=(int)strlen(wbuf)){ err(1,"Attempt to read buffer %d failed\n",i); } if(strcmp(rbuf,wbuf)!=0){ errx(1,"Data doesn't verify.\nWrote: '%s'\nRead: '%s'\n",wbuf,rbuf); } } } af_close(af); } printf("\nReverse test passes.\n"); printf("======================\n\n"); return 0; }
static int open_testfile_fds(void) { char *filename; unsigned int i = 1; unsigned int fails = 0; filename = zmalloc(64); while (i <= MAX_TESTFILE_FDS) { int fd; sprintf(filename, "trinity-testfile%d", i); fd = open_testfile(filename); if (fd != -1) { shm->testfile_fds[i - 1] = fd; i++; fails = 0; } else { fails++; if (fails == 100) { output(2, "testfile creation is failing a lot. last error:%s\n", strerror(errno)); } } } free(filename); return TRUE; }
static void wait_siblings(void) { int pids[2], fd, rv, x; /* Note: this may also blow up if FS synchronization is substandard */ fd = open_testfile(NULL); if (fd<0) { return; } pids[0] = fork(); if (pids[0]<0) { warn("UH-OH: can't fork"); return; } if (pids[0]==0) { close(fd); wait_siblings_child(); _exit(0); } pids[1] = fork(); if (pids[1]<0) { warn("UH-OH: can't fork"); /* abandon the other child process :( */ return; } if (pids[1]==0) { close(fd); wait_siblings_child(); _exit(0); } rv = write(fd, pids, sizeof(pids)); if (rv < 0) { warn("UH-OH: write error on %s", TESTFILE); /* abandon child procs :( */ return; } if (rv != (int)sizeof(pids)) { warnx("UH-OH: write error on %s: short count", TESTFILE); /* abandon child procs :( */ return; } rv = waitpid(pids[0], &x, 0); if (rv<0) { warn("UH-OH: error waiting for child 0 (pid %d)", pids[0]); } rv = waitpid(pids[1], &x, 0); if (rv<0) { warn("UH-OH: error waiting for child 1 (pid %d)", pids[1]); } warnx("passed: siblings wait for each other"); close(fd); remove(TESTFILE); }
int sequential_test() { char buf[1024]; const char *fmt = "this is line %d\n"; printf("Sequential test...\n"); AFFILE *af = open_testfile("test_sequential",1); for(int i=0;i<MAX_FMTS;i++){ if(i%250==0) printf("\rwriting %d/%d...",i,MAX_FMTS); sprintf(buf,fmt,i); if(af_write(af,(unsigned char *)buf,strlen(buf))!=(int)strlen(buf)){ err(1,"Attempt to write buffer %d failed\n",i); } } /* Test for a random bug that was reported */ af_update_seg(af,"test",0,(const u_char *)"foo",3); af_update_seg(af,"test",0,(const u_char *)"bar",3); af_del_seg(af,"test"); af_del_seg(af,"test"); af_close(af); printf("\nSequential file written.\n"); printf("\n"); printf("Now verifying the string...\n"); af = open_testfile("test_sequential",0); if(!af) err(1,"af_open"); for(int i=0;i<MAX_FMTS;i++){ char rbuf[1024]; sprintf(buf,fmt,i); int len = strlen(buf); if(af_read(af,(unsigned char *)rbuf,len)!=len){ err(1,"Attempt to read entry %d failed\n",i); } rbuf[len] = 0; // terminate the string if(strcmp(buf,rbuf)!=0){ err(1,"Attempt to verify entry %d failed.\nExpected: (len=%zd) '%s'\nGot: (len=%zd) '%s'\n", i,strlen(buf),buf,strlen(rbuf),rbuf); } } af_close(af); printf("===========================\n\n"); return 0; }
static void lseek_loc_pasteof(void) { const char *message = "blahblah"; int fd; off_t pos; fd = open_testfile(message); if (fd<0) { return; } pos = lseek(fd, 5340, SEEK_SET); if (pos == -1) { warn("FAILURE: lseek past EOF failed"); goto out; } if (pos != 5340) { warnx("FAILURE: lseek to 5340 got offset %ld", (long) pos); goto out; } pos = lseek(fd, -50, SEEK_CUR); if (pos == -1) { warn("FAILURE: small seek beyond EOF failed"); goto out; } if (pos != 5290) { warnx("FAILURE: SEEK_CUR to 5290 got offset %ld", (long) pos); goto out; } pos = lseek(fd, 0, SEEK_END); if (pos == -1) { warn("FAILURE: seek to EOF failed"); goto out; } if (pos != (off_t) strlen(message)) { warnx("FAILURE: seek to EOF got %ld (should be %d)", (long) pos, strlen(message)); goto out; } warnx("passed: seek past/to EOF"); out: close(fd); remove(TESTFILE); return; }
static void check_dir(const char *dire, const struct test *test) { char filename[4096]; int fd, reffd; if (test->nocommentref) { snprintf(filename, sizeof(filename), "%s/nocomment.html", dire); fd = open(filename, O_RDONLY); fail_unless(fd > 0,"unable to open: %s", filename); reffd = open_testfile(test->nocommentref); diff_files(fd, reffd); close(reffd); close(fd); } if (test->notagsref) { snprintf(filename, sizeof(filename), "%s/notags.html", dire); fd = open(filename, O_RDONLY); fail_unless(fd > 0,"unable to open: %s", filename); reffd = open_testfile(test->notagsref); diff_files(fd, reffd); close(reffd); close(fd); } if (test->jsref) { snprintf(filename, sizeof(filename), "%s/javascript", dire); fd = open(filename, O_RDONLY); fail_unless(fd > 0,"unable to open: %s", filename); reffd = open_testfile(test->jsref); diff_files(fd, reffd); close(reffd); close(fd); } }
END_TEST #endif START_TEST(test_screnc_nullterminate) { int fd = open_testfile("input/screnc_test"); fmap_t *map; fail_unless(mkdir(dir, 0700) == 0,"mkdir failed"); map = fmap(fd, 0, 0); fail_unless(!!map, "fmap failed"); fail_unless(html_screnc_decode(map, dir) == 1, "html_screnc_decode failed"); funmap(map); fail_unless(cli_rmdirs(dir) == 0, "rmdirs failed"); close(fd); }
static void readlink_file(void) { char buf[128]; int fd, rv; fd = open_testfile("the question contains an invalid assumption"); if (fd<0) { return; } close(fd); rv = readlink(TESTFILE, buf, sizeof(buf)); report_test(rv, errno, EINVAL, "readlink on file"); remove(TESTFILE); }
static void ftruncate_size_neg(void) { int rv, fd; fd = open_testfile(0); if (fd<0) { return; } rv = ftruncate(fd, -60); report_test(rv, errno, EINVAL, "ftruncate to negative size"); close(fd); remove(TESTFILE); }
static void lseek_loc_negative(void) { int fd, rv; fd = open_testfile(NULL); if (fd<0) { return; } rv = lseek(fd, -309, SEEK_SET); report_test(rv, errno, EINVAL, "lseek to negative offset"); close(fd); remove(TESTFILE); }
static void lseek_whence_inval(void) { int fd, rv; fd = open_testfile(NULL); if (fd<0) { return; } rv = lseek(fd, 0, 3594); report_test(rv, errno, EINVAL, "lseek with invalid whence code"); close(fd); remove(TESTFILE); }
int create_testfile(void) { int fd, rv; fd = open_testfile(NULL); if (fd<0) { return -1; } rv = close(fd); if (rv<0) { warn("UH-OH: closing %s failed", TESTFILE); return -1; } return 0; }
END_TEST START_TEST(phishing_fake_test_allscan) { char buf[4096]; FILE *f = fdopen(open_testfile("input/daily.pdb"),"r"); fail_unless(!!f,"fopen daily.pdb"); while(fgets(buf, sizeof(buf), f)) { struct rtest rtest; const char *pdb = strchr(buf,':'); fail_unless(!!pdb, "missing : in pdb"); rtest.realurl = pdb; rtest.displayurl = pdb; rtest.result = 2; do_phishing_test_allscan(&rtest); rtest.realurl = "http://fake.example.com"; rtest.result = 0; do_phishing_test_allscan(&rtest); } fclose(f); }
static int lseek_whence_inval(void) { int fd, rv; int result; report_begin("lseek with invalid whence code"); fd = open_testfile(NULL); if (fd<0) { report_aborted(&result); return result; } rv = lseek(fd, 0, 3594); result = report_check(rv, errno, EINVAL); close(fd); remove(TESTFILE); return result; }
static int lseek_loc_negative(void) { int fd, rv; int result; report_begin("lseek to negative offset"); fd = open_testfile(NULL); if (fd<0) { report_aborted(&result); return result; } rv = lseek(fd, -309, SEEK_SET); result = report_check(rv, errno, EINVAL); close(fd); remove(TESTFILE); return result; }
static int lseek_file_stdin(void) { int fd, fd2, rv, status; const char slogan[] = "There ain't no such thing as a free lunch"; size_t len = strlen(slogan); pid_t pid; int result = 0; report_begin("lseek stdin when open on file"); /* fork so we don't affect our own stdin */ pid = fork(); if (pid<0) { report_warn("fork failed"); report_aborted(&result); return result; } else if (pid!=0) { /* parent */ rv = waitpid(pid, &status, 0); if (rv<0) { report_warn("waitpid failed"); report_aborted(&result); } if (WIFSIGNALED(status)) { report_warnx("subprocess exited with signal %d", WTERMSIG(status)); report_aborted(&result); } else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { report_warnx("subprocess exited with code %d", WEXITSTATUS(status)); report_aborted(&result); } return result; } /* child */ fd = open_testfile(NULL); if (fd<0) { _exit(0); } /* * Move file to stdin. * Use stdin (rather than stdout or stderr) to maximize the * chances of detecting any special-case handling of fds 0-2. * (Writing to stdin is fine as long as it's open for write, * and it will be.) */ fd2 = dup2(fd, STDIN_FILENO); if (fd2<0) { report_warn("dup2 to stdin failed"); close(fd); remove(TESTFILE); _exit(1); } if (fd2 != STDIN_FILENO) { report_warn("dup2 returned wrong file handle"); close(fd); remove(TESTFILE); _exit(1); } close(fd); rv = write(STDIN_FILENO, slogan, len); if (rv<0) { report_warn("write to %s (via stdin) failed", TESTFILE); remove(TESTFILE); _exit(1); } if ((unsigned)rv != len) { report_warnx("write to %s (via stdin) got short count", TESTFILE); remove(TESTFILE); _exit(1); } /* blah */ report_skipped(&result); rv = lseek(STDIN_FILENO, 0, SEEK_SET); report_begin("try 1: SEEK_SET"); result = report_check(rv, errno, 0); rv = lseek(STDIN_FILENO, 0, SEEK_END); report_begin("try 2: SEEK_END"); result = report_check(rv, errno, 0); remove(TESTFILE); _exit(0); }
static int lseek_loc_pasteof(void) { const char *message = "blahblah"; int fd; off_t pos; int result; report_begin("seek past/to EOF"); fd = open_testfile(message); if (fd<0) { report_aborted(&result); return result; } pos = lseek(fd, 5340, SEEK_SET); if (pos == -1) { report_warn("lseek past EOF failed"); report_failure(&result); goto out; } if (pos != 5340) { report_warnx("lseek to 5340 got offset %lld", (long long) pos); report_failure(&result); goto out; } pos = lseek(fd, -50, SEEK_CUR); if (pos == -1) { report_warn("small seek beyond EOF failed"); report_failure(&result); goto out; } if (pos != 5290) { report_warnx("SEEK_CUR to 5290 got offset %lld", (long long) pos); report_failure(&result); goto out; } pos = lseek(fd, 0, SEEK_END); if (pos == -1) { report_warn("seek to EOF failed"); report_failure(&result); goto out; } if (pos != (off_t) strlen(message)) { report_warnx("seek to EOF got %lld (should be %zu)", (long long) pos, strlen(message)); report_failure(&result); goto out; } report_passed(&result); out: close(fd); remove(TESTFILE); return result; }
static int wait_siblings(void) { pid_t pids[2]; int rv, fd, semfd, x; int bad = 0; char semname[32]; int result; /* This test may also blow up if FS synchronization is substandard */ report_begin("siblings wait for each other"); report_hassubs(); snprintf(semname, sizeof(semname), "sem:badcall.%d", (int)getpid()); semfd = open(semname, O_WRONLY|O_CREAT|O_TRUNC, 0664); if (semfd < 0) { report_warn("can't make semaphore"); report_aborted(&result); return result; } fd = open_testfile(NULL); if (fd<0) { report_aborted(&result); close(semfd); remove(semname); return result; } pids[0] = fork(); if (pids[0]<0) { report_warn("can't fork"); report_aborted(&result); close(fd); close(semfd); remove(semname); return result; } if (pids[0]==0) { close(fd); close(semfd); wait_siblings_child(semname); _exit(0); } pids[1] = fork(); if (pids[1]<0) { report_warn("can't fork"); report_aborted(&result); /* abandon the other child process :( */ close(fd); close(semfd); remove(semname); return result; } if (pids[1]==0) { close(fd); close(semfd); wait_siblings_child(semname); _exit(0); } rv = write(fd, pids, sizeof(pids)); if (rv < 0) { report_warn("write error on %s", TESTFILE); report_aborted(&result); /* abandon child procs :( */ close(fd); close(semfd); remove(semname); return result; } if (rv != (int)sizeof(pids)) { report_warnx("write error on %s: short count", TESTFILE); report_aborted(&result); /* abandon child procs :( */ close(fd); close(semfd); remove(semname); return result; } /* gate the child procs */ rv = write(semfd, " ", 2); if (rv < 0) { report_warn("%s: write", semname); bad = 1; } report_beginsub("overall"); rv = waitpid(pids[0], &x, 0); if (rv<0) { report_warn("error waiting for child 0 (pid %d)", pids[0]); bad = 1; } rv = waitpid(pids[1], &x, 0); if (rv<0) { report_warn("error waiting for child 1 (pid %d)", pids[1]); bad = 1; } if (bad) { /* XXX: aborted, or failure, or what? */ report_aborted(&result); } else { report_passed(&result); } close(fd); close(semfd); remove(semname); remove(TESTFILE); return result; }
int random_write_test() { char buf[1024]; char *tally = (char *)calloc(MAX_FMTS,1); int i; memset(tally,0,sizeof(tally)); /* Create the AFF file */ sprintf(buf,fmt,0); // figure out how big fmt string is int fmt_size = strlen(buf); printf("Random write test...\n"); printf("Creating test file with %d byte records.\n", fmt_size); AFFILE *af = open_testfile("test_random",1); if(af_write(af,(unsigned char *)buf,fmt_size)!=fmt_size){ err(1,"af_write"); } for(i=0;i<MAX_FMTS;i++){ /* Find a random spot that's available */ int pos = rand() % MAX_FMTS; while(tally[pos]==1){ // if this one is used, find next pos = (pos + 1) % MAX_FMTS; } tally[pos] = 1; sprintf(buf,fmt,pos); assert((int)strlen(buf)==fmt_size); // make sure af_seek(af,fmt_size*pos,SEEK_SET); int wrote = af_write(af,(unsigned char *)buf,fmt_size); if(wrote !=fmt_size){ fprintf(stderr,"Attempt to write buffer #%d \n",pos); fprintf(stderr,"wrote %d bytes instead of %d bytes\n",wrote,fmt_size); exit(1); } if(i%250==0) printf("\r%d ...",i); fflush(stdout); } af_close(af); /* Now verify what was written */ printf("Verifying write test...\n"); af = open_testfile("test_random",0); for(i=0;i<MAX_FMTS;i++){ char should[256]; // what we should get sprintf(should,fmt,i); int got = af_read(af,(unsigned char *)buf,fmt_size); if(got != fmt_size){ fprintf(stderr,"Attempt to read %d bytes; got %d\n",fmt_size,got); exit(1); } if(i%250==24) printf("\r%d .. %d okay",i-24,i); } af_close(af); printf("\n"); printf("\nRandom write test passes.\n"); printf("======================\n"); return 0; }
int random_read_test(int total_bytes,int data_page_size) { printf("\n\n\nrandom read test. filesize=%d, page_size=%d\n", total_bytes,data_page_size); /* Create a regular file and an AFF file */ printf("Creating random_contents.img and random_contents.%s, " "both with %d bytes of user data...\n", opt_ext,total_bytes); int fd = open("test_random_contents.img", O_CREAT|O_RDWR|O_TRUNC|O_BINARY,0666); if(fd<0) err(1,"fopen"); AFFILE *af = open_testfile("test_random_contents",1); /* Just write it out as one big write */ unsigned char *buf = (unsigned char *)malloc(total_bytes); unsigned char *buf2 = (unsigned char *)malloc(total_bytes); /* First half is random */ #ifdef HAVE_RAND_PSEUDO_BYTES RAND_pseudo_bytes(buf,total_bytes/2); #else for(int i=0;i<total_bytes/2;i++){ buf[i] = random(); } #endif /* Second half is a bit more predictable */ for(int i=total_bytes/2;i<total_bytes;i++){ buf[i] = ((i % 256) + (i / 256)) % 256; } if(write(fd,buf,total_bytes)!=total_bytes) err(1,"fwrite"); if(af_write(af,buf,total_bytes)!=(int)total_bytes) err(1,"af_write"); /* Now try lots of seeks and reads */ for(int i=0;i<MAX_FMTS;i++){ uint32_t loc = rand() % total_bytes; uint32_t len = rand() % total_bytes; memset(buf,0,total_bytes); memset(buf2,0,total_bytes); if(i%250==0) printf("\r#%d reading %"PRIu32" bytes at %"PRIu32" ...",i,loc,len); fflush(stdout); uint32_t l1 = (uint32_t)lseek(fd,loc,SEEK_SET); uint32_t l2 = (uint32_t)af_seek(af,loc,SEEK_SET); if(l1!=l2){ err(1,"l1 (%"PRIu32") != l2 (%"PRIu32")",l1,l2); } int r1 = read(fd,buf,len); int r2 = af_read(af,buf2,len); if(r1!=r2){ err(1,"r1 (%d) != r2 (%d)",r1,r2); } } af_close(af); close(fd); printf("\nRandom read test passes\n"); return 0; }
static void runtests(GSList* task_list) { size_t i; for (i = 0; i < g_slist_length(task_list); i++){ struct task* task; task = g_slist_nth_data(task_list, i); switch(task->op){ case OPEN: open_testfile(); break; case MANOPEN: manopen_testfile(); break; case CLOSE: close_testfile(); break; case CLOSE_SAVEFD: close_savefd(); break; case WAIT: waitnotice(); break; case SEND: sendnotice(); break; case WRITE: write_testfile(task); break; case READ: read_testfile(task); break; case RUN: test( task->run_param.num, task->run_param.sec, task->run_param.func, task->run_param.offset, task->run_param.length, task->run_param.pass, FATAL); break; case FORK:{ int subpid = fork(); if (subpid < 0){ perror("can't fork off child"); exit(1); } if (subpid == 0){ test( task->run_param.num, task->run_param.sec, task->run_param.func, task->run_param.offset, task->run_param.length, task->run_param.pass, FATAL); while(1) { sleep(1); } }else{ gi.forkpid = subpid; } break; } case KILL: if(kill(gi.forkpid, SIGINT) == -1){ perror("kill"); exit(1); } break; case SLEEP: sleep(task->sleep_param.time); break; case PUSHFD: pushfd(); break; case POPFD: popfd(); break; case TRUNC: hb_trunc(); break; } } }