static void test_imap_receive_multiple_lines(void **state) { struct imap_connection *imap = malloc(sizeof(struct imap_connection)); imap_init(imap); imap->mode = RECV_LINE; const char *buffer = "a001 FOOBAR\r\na002 FOOBAZ\r\n"; expect_string(__wrap_hashtable_get, key, "FOOBAR"); will_return(__wrap_hashtable_get, test_handler); expect_string(__wrap_hashtable_get, key, "FOOBAZ"); will_return(__wrap_hashtable_get, test_handler); set_ab_recv_result((void *)buffer, strlen(buffer)); will_return(__wrap_ab_recv, strlen(buffer)); will_return(__wrap_poll, 0); imap->poll[0].revents = POLLIN; imap_receive(imap); assert_int_equal(handler_called, 2); imap_close(imap); }
static void test_imap_receive_full_buffer(void **state) { struct imap_connection *imap = malloc(sizeof(struct imap_connection)); imap_init(imap); imap->mode = RECV_LINE; char buffer[4096]; memset(buffer, 'a', 4096); const char *cmd_1 = "a001 FOOBAR "; memcpy(buffer, cmd_1, strlen(cmd_1)); const char *cmd_2 = "\r\na002 FOOBAZ "; memcpy(buffer + 2048 + 128, cmd_2, strlen(cmd_2)); buffer[4094] = '\r'; buffer[4095] = '\n'; will_return(__wrap_poll, 0); will_return(__wrap_poll, 0); will_return(__wrap_poll, 0); will_return(__wrap_poll, 0); imap->poll[0].revents = POLLIN; set_ab_recv_result((void *)buffer, 1024); will_return(__wrap_ab_recv, 1024); imap_receive(imap); // First command (incomplete) set_ab_recv_result((void *)(buffer + 1024), 1024); will_return(__wrap_ab_recv, 1024); imap_receive(imap); // First command (incomplete) expect_string(__wrap_hashtable_get, key, "FOOBAR"); will_return(__wrap_hashtable_get, test_handler); set_ab_recv_result((void *)(buffer + 2048), 1024); will_return(__wrap_ab_recv, 1024); imap_receive(imap); // First command (complete), second command (incomplete) assert_int_equal(handler_called, 1); expect_string(__wrap_hashtable_get, key, "FOOBAZ"); will_return(__wrap_hashtable_get, test_handler); set_ab_recv_result((void *)(buffer + 2048), 1024); will_return(__wrap_ab_recv, 1024); imap_receive(imap); // Second command (complete) assert_int_equal(handler_called, 2); imap_close(imap); }
static void test_handle_line_known_handler(void **state) { int _; struct imap_connection *imap = malloc(sizeof(struct imap_connection)); imap_init(imap); imap_arg_t *arg = malloc(sizeof(imap_arg_t)); imap_parse_args("a001 FOOBAR", arg, &_); expect_string(__wrap_hashtable_get, key, "FOOBAR"); will_return(__wrap_hashtable_get, test_handler); handle_line(imap, arg); assert_int_equal(handler_called, 1); free(arg); imap_close(imap); }
/* * Close examined/selected mailbox. */ int request_close(const char *server, const char *port, const char *user) { int r; session *s; if (!(s = session_find(server, port, user))) return -1; if ((r = response_generic(s, imap_close(s))) == -1) goto fail; return r; fail: close_connection(s); session_destroy(s); return -1; }
//gcc -o imap imap.c -DIMAP_TEST -DTEST_INS -DHAVE_PTHREAD -lpthread && ./imap int main() { IMAP *imap = NULL; int i = 0, j = 0, n = 0, total = 0, no = 0, stat[MASK], stat2[MASK]; int32_t val = 0, from = 0, to = 0, *res = NULL; int32_t inputs[256], nos[256], last[256]; int32_t all = 0; time_t stime = 0, etime = 0; void *timer = NULL; if((imap = imap_init("/tmp/1.idx"))) { res = (int32_t *)calloc(60000000, sizeof(int32_t)); TIMER_INIT(timer); #ifdef TEST_INS //fprintf(stdout, "sizeof(stat):%d\n", sizeof(stat)); memset(stat, 0, sizeof(stat)); memset(stat2, 0, sizeof(stat2)); srand(time(NULL)); n = 256; for(i = 0; i < n; i++) { no = (rand()%MASK); nos[i] = no; if((i % 3) == 0) inputs[i] = no * -1; else inputs[i] = no; } TIMER_RESET(timer); for(i = 1; i < 20000000; i++) { j = (rand()%n); val = inputs[j]; no = nos[j]; stat[no]++; imap_set(imap, i, val); last[j] = i; } TIMER_SAMPLE(timer); fprintf(stdout, "set() 40000000 data, time used:%lld\n", PT_LU_USEC(timer)); TIMER_RESET(timer); for(i = 0; i < n; i++) { imap_del(imap, last[i]); } TIMER_SAMPLE(timer); fprintf(stdout, "del() time used:%lld\n", PT_LU_USEC(timer)); TIMER_RESET(timer); for(i = 0; i < n; i++) { val = inputs[i]; no = nos[i]; stat2[no] = imap_in(imap, val, res); } TIMER_SAMPLE(timer); fprintf(stdout, "in() time used:%lld\n", PT_LU_USEC(timer)); TIMER_RESET(timer); total = imap_ins(imap, inputs, n, NULL); TIMER_SAMPLE(timer); fprintf(stdout, "ins(keys, NULL) total:%d time used:%lld\n", total, PT_LU_USEC(timer)); TIMER_RESET(timer); total = imap_ins(imap, inputs, n, res); TIMER_SAMPLE(timer); fprintf(stdout, "ins(keys, res:%p) total:%d time used:%lld\n", res, total, PT_LU_USEC(timer)); for(i = 0; i < n; i++) { j = nos[i]; if(stat[j] != stat2[j]) fprintf(stdout, "%d:%d/%d::%d\n", j, stat[j], stat2[j], inputs[i]); } #ifdef OUT_ALL for(i = 0; i < total; i++) { fprintf(stdout, "%d:%d\n", i, res[i]); } #endif #endif /* for(i = 0; i < imap->state->count; i++) { fprintf(stdout, "%d:{min:%d max:%d}(%d)\n", i, imap->slots[i].min, imap->slots[i].max, imap->slots[i].count); } */ #ifdef TEST_RANGEFILTER imap_set(imap, 1, 1234567); imap_set(imap, 2, 1567890); fprintf(stdout, "rangefrom():%d\n", imap_rangefrom(imap, 1569000, NULL)); fprintf(stdout, "rangeto():%d\n", imap_rangeto(imap, 1111111, NULL)); fprintf(stdout, "range():%d\n", imap_range(imap, 1111111, 1600000, NULL)); #endif #ifdef TEST_RANGE srand(time(NULL)); TIMER_RESET(timer); for(i = 1; i < 40000000; i++) { val = 1356969600 + (rand()%31536000); imap_set(imap, i, val); } TIMER_SAMPLE(timer); fprintf(stdout, "set() 40000000 timestamps, time used:%lld\n", PT_LU_USEC(timer)); fflush(stdout); srand(time(NULL)); TIMER_RESET(timer); all = 0; for(i = 0; i < 1000; i++) { val = 1356969600 + (rand()%31536000); all += imap_rangefrom(imap, val, res); } TIMER_SAMPLE(timer); fprintf(stdout, "rangefrom() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer)); fflush(stdout); srand(time(NULL)); TIMER_RESET(timer); all = 0; for(i = 0; i < 1000; i++) { val = 1356969600 + (rand()%31536000); all += imap_rangeto(imap, val, res); } TIMER_SAMPLE(timer); fprintf(stdout, "rangeto() 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer)); fflush(stdout); srand(time(NULL)); TIMER_RESET(timer); all = 0; for(i = 0; i < 1000; i++) { from = 1356969600 + (rand()%31536000); to = from + rand()%31536000; all += imap_range(imap, from, to, res); } TIMER_SAMPLE(timer); fprintf(stdout, "range(%p) 1000 times total:%lld, time used:%lld\n", res, (long long int)all, PT_LU_USEC(timer)); fflush(stdout); srand(time(NULL)); TIMER_RESET(timer); all = 0; for(i = 0; i < 1000; i++) { from = 1356969600 + (rand()%31536000); to = from + rand()%31536000; all += imap_range(imap, from, to, NULL); } TIMER_SAMPLE(timer); fprintf(stdout, "range(null) 1000 times total:%lld, time used:%lld\n", (long long int)all, PT_LU_USEC(timer)); fflush(stdout); #endif imap_close(imap); TIMER_CLEAN(timer); free(res); } }