int main(void) { test_package_begin("hash", "Externally linked hash table data structure with operations"); hash_init(); test_begin("Hash allocation"); { Hash *a; a = hash_new(int_hash, int_key_eq); test_result(a != NULL); hash_destroy(a); } test_end(); test_begin("Multiple allocs"); { Hash *a, *b; a = hash_new(int_hash, int_key_eq); b = hash_new(int_hash, int_key_eq); test_result(a != NULL && b != NULL && a != b); hash_destroy(b); hash_destroy(a); } test_end(); test_begin("Insert/lookup"); { Hash *a; const char *t; a = hash_new(int_hash, int_key_eq); hash_insert(a, (const void *) 17, "foo"); test_result((t = hash_lookup(a, (const void *) 17)) != NULL && strcmp(t, "foo") == 0); hash_destroy(a); } test_end(); test_begin("Resize"); { Hash *a; const char *t; unsigned int i; a = hash_new(int_hash, int_key_eq); hash_insert(a, (const void *) 4711, "foo"); for(i = 0; i < 100; i++) hash_insert(a, (const void *) (3519 + i), "test-data"); test_result((t = hash_lookup(a, (const void *) 4711)) != NULL && strcmp(t, "foo") == 0); hash_destroy(a); } test_end(); return test_package_end(); }
static void test_ds_buffers(void) { test_begin("data-stack buffer growth"); T_BEGIN { size_t i; unsigned char *p; size_t left = t_get_bytes_available(); while (left < 10000) { t_malloc_no0(left); /* force a new block */ left = t_get_bytes_available(); } left -= 64; /* make room for the sentry if DEBUG */ p = t_buffer_get(1); p[0] = 1; for (i = 2; i <= left; i++) { /* grow it */ unsigned char *p2 = t_buffer_get(i); test_assert_idx(p == p2, i); p[i-1] = i; test_assert_idx(p[i-2] == (unsigned char)(i-1), i); } /* now fix it permanently */ t_buffer_alloc_last_full(); test_assert(t_get_bytes_available() < 64 + MEM_ALIGN(1)); } T_END; test_end(); test_begin("data-stack buffer interruption"); T_BEGIN { void *b = t_buffer_get(1000); void *a = t_malloc_no0(1); void *b2 = t_buffer_get(1001); test_assert(a == b); /* expected, not guaranteed */ test_assert(b2 != b); } T_END; test_end(); test_begin("data-stack buffer with reallocs"); T_BEGIN { size_t bigleft = t_get_bytes_available(); size_t i; for (i = 1; i < bigleft-64; i += i_rand()%32) T_BEGIN { unsigned char *p, *p2; size_t left; t_malloc_no0(i); left = t_get_bytes_available(); /* The most useful idx for the assert is 'left' */ test_assert_idx(left <= bigleft-i, left); p = t_buffer_get(left/2); p[0] = 'Z'; p[left/2 - 1] = 'Z'; p2 = t_buffer_get(left + left/2); test_assert_idx(p != p2, left); test_assert_idx(p[0] == 'Z', left); test_assert_idx(p[left/2 -1] == 'Z', left); } T_END; } T_END; test_end(); }
void test_unichar(void) { static const char overlong_utf8[] = "\xf8\x80\x95\x81\xa1"; static const char collate_in[] = "\xc3\xbc \xc2\xb3"; static const char collate_exp[] = "U\xcc\x88 3"; buffer_t *collate_out; unichar_t chr, chr2; string_t *str = t_str_new(16); test_begin("unichars encode/decode"); for (chr = 0; chr <= 0x10ffff; chr++) { /* The bottom 6 bits should be irrelevant to code coverage, only test 000000, 111111, and something in between. */ if ((chr & 63) == 1) chr += rand() % 62; /* After 0, somewhere between 1 and 62 */ else if ((chr & 63) > 0 && (chr & 63) < 63) chr |= 63; /* After random, straight to 63 */ str_truncate(str, 0); uni_ucs4_to_utf8_c(chr, str); test_assert(uni_utf8_str_is_valid(str_c(str))); test_assert(uni_utf8_get_char(str_c(str), &chr2) == (int)uni_utf8_char_bytes(*str_data(str))); test_assert(chr2 == chr); if ((chr & 0x63) == 0) { unsigned int utf8len = uni_utf8_char_bytes(*str_c(str)); /* virtually truncate the byte string */ while (--utf8len > 0) test_assert(uni_utf8_get_char_n(str_c(str), utf8len, &chr2) == 0); utf8len = uni_utf8_char_bytes(*str_c(str)); /* actually truncate the byte stream */ while (--utf8len > 0) { str_truncate(str, utf8len); test_assert(!uni_utf8_str_is_valid(str_c(str))); test_assert(uni_utf8_get_char(str_c(str), &chr2) == 0); } } } test_end(); test_begin("unichar collation"); collate_out = buffer_create_dynamic(default_pool, 32); uni_utf8_to_decomposed_titlecase(collate_in, sizeof(collate_in), collate_out); test_assert(!strcmp(collate_out->data, collate_exp)); buffer_free(&collate_out); test_assert(!uni_utf8_str_is_valid(overlong_utf8)); test_assert(uni_utf8_get_char(overlong_utf8, &chr2) < 0); test_end(); test_unichar_uni_utf8_strlen(); test_unichar_uni_utf8_partial_strlen_n(); }
int main(int argc, char **argv) { TDSRESULTINFO *info; char mymsg[256]; fprintf(stdout, "%s: Testing flags from server\n", __FILE__); if (try_tds_login(&login, &tds, __FILE__, 0) != TDS_SUCCESS) { fprintf(stderr, "try_tds_login() failed\n"); return 1; } if (run_query(tds, "create table #tmp1 (i numeric(10,0) identity primary key, b varchar(20) null, c int not null)") != TDS_SUCCESS) fatal_error("creating table error"); /* TDS 4.2 without FOR BROWSE clause seem to forget flags... */ if (!IS_TDS42(tds->conn)) { /* check select of all fields */ test_begin("select * from #tmp1"); info = tds->current_results; if (info->num_cols != 3) { sprintf(mymsg,"wrong number of columns returned expected 3 got %d", info->num_cols); fatal_error(mymsg); } check_flags(info->columns[0], 0, "identity"); check_flags(info->columns[1], 1, "nullable writable"); check_flags(info->columns[2], 2, "writable"); test_end(); } /* check select of 2 field */ test_begin("select c, b from #tmp1 for browse"); info = tds->current_results; if (info->num_cols != 3) fatal_error("wrong number of columns returned"); check_flags(info->columns[0], 0, "writable"); if (!IS_TDS42(tds->conn)) { check_flags(info->columns[1], 1, "nullable writable"); } else { check_flags(info->columns[1], 1, "writable-nullable writable"); } /* TDS5 return not identity information altough documented.. */ check_flags(info->columns[2], 2, "writable identity key hidden-writable key hidden"); test_end(); try_tds_logout(login, tds, 0); return 0; }
void test_str_sanitize(void) { static struct str_sanitize_test tests[] = { { NULL, 2, NULL }, { "", 2, NULL }, { "a", 2, NULL }, { "ab", 2, NULL }, { "abc", 2, "..." }, { "abcd", 3, "..." }, { "abcde", 4, "a..." }, { "\xD1\x81", 1, "..." }, { "\xD1\x81", 2, "\xD1\x81" }, { "\xD1\x81", 3, NULL }, { "\xC3\xA4\xC3\xA4zyxa", 1, "..." }, { "\xC3\xA4\xC3\xA4zyxa", 2, "..." }, { "\xC3\xA4\xC3\xA4zyxa", 3, "..." }, { "\xC3\xA4\xC3\xA4zyxa", 4, "..." }, { "\xC3\xA4\xC3\xA4zyxa", 5, "\xC3\xA4..." }, { "\xC3\xA4\xC3\xA4zyxa", 6, "\xC3\xA4..." }, { "\xC3\xA4\xC3\xA4zyxa", 7, "\xC3\xA4\xC3\xA4..." }, { "\xC3\xA4\xC3\xA4zyxa", 8, "\xC3\xA4\xC3\xA4zyxa" }, { "\001x\x1fy\x81", 10, "?x?y?" } }; const char *str; string_t *str2; unsigned int i; test_begin("str_sanitize"); for (i = 0; i < N_ELEMENTS(tests); i++) { str = str_sanitize(tests[i].str, tests[i].max_len); if (tests[i].sanitized != NULL) test_assert_idx(null_strcmp(str, tests[i].sanitized) == 0, i); else test_assert_idx(str == tests[i].str, i); } test_end(); test_begin("str_sanitize_append"); str2 = t_str_new(128); for (i = 0; i < N_ELEMENTS(tests); i++) { if (tests[i].str == NULL) continue; str_truncate(str2, 0); str_append(str2, "1234567890"); str_sanitize_append(str2, tests[i].str, tests[i].max_len); test_assert_idx(strncmp(str_c(str2), "1234567890", 10) == 0, i); if (tests[i].sanitized != NULL) test_assert_idx(strcmp(str_c(str2)+10, tests[i].sanitized) == 0, i); else test_assert_idx(strcmp(str_c(str2)+10, tests[i].str) == 0, i); } test_end(); }
void test_istream_base64_encoder(void) { unsigned int i; for (i = 0; i < N_ELEMENTS(tests); i++) { test_begin(t_strdup_printf("istream base64 decoder %u", i+1)); encode_test(tests[i].input, tests[i].chars_per_line, tests[i].crlf, tests[i].output); test_end(); } test_begin("istream base64 encoder seek"); test_istream_base64_encoder_seek(hello, "aGVs\r\nbG8g\r\nd29y\r\nbGQ="); test_end(); }
static void test_unichar_uni_utf8_strlen(void) { static const char input[] = "\xC3\xA4\xC3\xA4\0a"; test_begin("uni_utf8_strlen()"); test_assert(uni_utf8_strlen(input) == 2); test_end(); test_begin("uni_utf8_strlen_n()"); test_assert(uni_utf8_strlen_n(input, 1) == 0); test_assert(uni_utf8_strlen_n(input, 2) == 1); test_assert(uni_utf8_strlen_n(input, 3) == 1); test_assert(uni_utf8_strlen_n(input, 4) == 2); test_end(); }
static void disable_and_enable(void) { const char *test_id = "kevent(EVFILT_USER, EV_DISABLE and EV_ENABLE)"; struct kevent kev; test_begin(test_id); test_no_kevents(); kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL); kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_DISABLE, 0, 0, NULL); /* Trigger the event, but since it is disabled, nothing will happen. */ kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL); test_no_kevents(); kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ENABLE, 0, 0, NULL); kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL); kev.flags = EV_CLEAR; kev.fflags &= ~NOTE_FFCTRLMASK; kev.fflags &= ~NOTE_TRIGGER; kevent_cmp(&kev, kevent_get(kqfd)); success(); }
void test_kevent_socket_disable_and_enable(void) { const char *test_id = "kevent(EVFILT_READ, EV_DISABLE)"; struct kevent kev; test_begin(test_id); /* Add an event, then disable it. */ EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_DISABLE, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); kevent_socket_fill(); test_no_kevents(); /* Re-enable the knote, then see if an event is generated */ kev.flags = EV_ENABLE; if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); kev.flags = EV_ADD; kev.data = 1; kevent_cmp(&kev, kevent_get(kqfd)); kevent_socket_drain(); kev.flags = EV_DELETE; if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); success(); }
static void test_message_header_encode_q(void) { string_t *input = t_str_new(100); string_t *str = t_str_new(512); unsigned int i, j, skip; test_begin("message header encode q"); str_append_c(input, 'a'); for (i = 0; i < 40; i++) str_append(input, "\xC3\xA4"); for (i = 0; i < 80; i++) { for (skip = 0; skip < 2; skip++) { str_truncate(str, 0); for (j = 1; j < i; j++) str_append_c(str, 'X'); if (i != 0) str_append_c(str, ' '); message_header_encode_q(str_data(input) + skip, str_len(input) - skip, str, i == 0 ? 0 : i+1); test_assert(verify_q(str_c(str), i, skip == 0)); } } test_end(); }
static void test_message_id_get_next(void) { const char *input[] = { "<foo@bar>", "<foo@bar>,skipped,<foo2@bar2>", "(c) < (c) foo (c) @ (c) bar (c) > (c)", }; const char *output[] = { "foo@bar", NULL, "foo@bar", "foo2@bar2", NULL, "foo@bar", NULL }; const char *msgid, *next_msgid; unsigned int i, j; test_begin("message id parser"); for (i = 0, j = 0; i < N_ELEMENTS(input); i++) { msgid = input[i]; while ((next_msgid = message_id_get_next(&msgid)) != NULL) { if (output[j] == NULL) break; test_assert(strcmp(output[j++], next_msgid) == 0); } test_assert(output[j++] == NULL && next_msgid == NULL); } test_assert(j == N_ELEMENTS(output)); test_end(); }
void test_kevent_socket_get(void) { const char *test_id = "kevent(EVFILT_READ) wait"; struct kevent kev; test_begin(test_id); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); kevent_socket_fill(); kev.data = 1; kevent_cmp(&kev, kevent_get(kqfd)); kevent_socket_drain(); test_no_kevents(); kev.flags = EV_DELETE; if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); success(); }
static void event_trigger(void) { struct kevent kev; pid_t pid; test_begin("kevent(EVFILT_PROC, wait)"); /* Create a child that waits to be killed and then exits */ pid = fork(); if (pid == 0) { pause(); printf(" -- child caught signal, exiting\n"); exit(2); } printf(" -- child created (pid %d)\n", (int) pid); test_no_kevents(); kevent_add(kqfd, &kev, pid, EVFILT_PROC, EV_ADD, 0, 0, NULL); /* Cause the child to exit, then retrieve the event */ printf(" -- killing process %d\n", (int) pid); if (kill(pid, SIGUSR1) < 0) err(1, "kill"); kevent_cmp(&kev, kevent_get(kqfd)); test_no_kevents(); success(); }
static void test_static_v1_input_short(void) { ssize_t siz; const struct hash_method *hash = hash_method_lookup("sha256"); unsigned char hash_ctx[hash->context_size]; unsigned char hash_dgst[hash->digest_size]; hash->init(hash_ctx); test_begin("test_static_v1_input_short"); struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc", IO_BLOCK_SIZE); struct istream *is_2 = i_stream_create_base64_decoder(is_1); i_stream_unref(&is_1); struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv); i_stream_unref(&is_2); struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx); i_stream_unref(&is_3); while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); } if (is_4->stream_errno != 0) i_debug("error: %s", i_stream_get_error(is_4)); test_assert(is_4->stream_errno == 0); i_stream_unref(&is_4); hash->result(hash_ctx, hash_dgst); test_assert(strcmp(test_sample_v1_short_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0); test_end(); }
void test_kevent_socket_lowat(void) { const char *test_id = "kevent(EVFILT_READ, NOTE_LOWAT)"; struct kevent kev; test_begin(test_id); /* Re-add the watch and make sure no events are pending */ puts("-- re-adding knote, setting low watermark to 2 bytes"); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD | EV_ONESHOT, NOTE_LOWAT, 2, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); test_no_kevents(); puts("-- checking that one byte does not trigger an event.."); kevent_socket_fill(); test_no_kevents(); puts("-- checking that two bytes triggers an event.."); kevent_socket_fill(); if (kevent(kqfd, NULL, 0, &kev, 1, NULL) != 1) err(1, "%s", test_id); KEV_CMP(kev, sockfd[0], EVFILT_READ, 0); test_no_kevents(); kevent_socket_drain(); kevent_socket_drain(); success(); }
/* Detect French */ static void test_fts_language_detect_french(void) { struct fts_language_list *lp = NULL; const struct fts_language *lang_r = NULL; const unsigned char french[] = "D\xC3\xA9""claration universelle des droits de l\xE2\x80\x99" "homme Pr\xC3\xA9""ambule Consid\xC3\xA9rant que la "\ "reconnaissance de la dignit\xC3\xA9 inh\xC3\xA9rente \xC3\xA0"\ " tous les membres de la famille humaine et de leurs droits "\ "\xC3\xA9gaux et inali\xC3\xA9nables constitue le fondement de"\ " la libert\xC3\xA9, de la justice et de la paix dans le monde,"\ " Consid\xC3\xA9rant que la m\xC3\xA9""connaissance et le "\ "m\xC3\xA9pris des droits de l\xE2\x80\x99homme ont conduit "\ "\xC3\xA0 des actes de barbarie qui r\xC3\xA9voltent la "\ "conscience de l\xE2\x80\x99humanit\xC3\xA9 et que "\ "l\xE2\x80\x99""av\xC3\xA8nement d\xE2\x80\x99un monde o\xC3\xB9"\ " les \xC3\xAAtres humains seront libres de parler et de "\ "croire, lib\xC3\xA9r\xC3\xA9s de la terreur et de la "\ "mis\xC3\xA8re, a \xC3\xA9t\xC3\xA9 proclam\xC3\xA9 comme la "\ "plus haute aspiration de l\xE2\x80\x99homme,"; const char names[] = "de, fi, fr, en"; const char *unknown, *error; test_begin("fts language detect French"); test_assert(fts_language_list_init(settings, &lp, &error) == 0); test_assert(fts_language_list_add_names(lp, names, &unknown) == TRUE); test_assert(fts_language_detect(lp, french, sizeof(french)-1, &lang_r) == FTS_LANGUAGE_RESULT_OK); test_assert(strcmp(lang_r->name, "fr") == 0); fts_language_list_deinit(&lp); test_end(); }
static void test_var_expand_builtin(void) { static struct var_expand_test tests[] = { { "%{hostname}", NULL }, { "%{pid}", NULL }, { "a%{env:FOO}b", "abaRb" } }; static struct var_expand_table table[] = { { '\0', NULL, NULL } }; string_t *str = t_str_new(128); unsigned int i; tests[0].out = my_hostname; tests[1].out = my_pid; env_put("FOO=baR"); test_begin("var_expand"); for (i = 0; i < N_ELEMENTS(tests); i++) { str_truncate(str, 0); var_expand(str, tests[i].in, table); test_assert(strcmp(tests[i].out, str_c(str)) == 0); } test_end(); }
/* Detect English */ static void test_fts_language_detect_english(void) { struct fts_language_list *lp = NULL; const struct fts_language *lang_r = NULL; const unsigned char english[] = "Whereas recognition of the inherent dignity and"\ " of the equal and inalienable rights of all members of the human"\ "family is the foundation of freedom, justice and peace in the "\ "world,\n Whereas disregard and contempt for human rights have "\ "resulted in barbarous acts which have outraged the conscience"\ "of mankind, and the advent of a world in which human beings"\ "shall enjoy freedom of speech and belief and freedom from "\ "fear and want has been proclaimed as the highest aspiration"\ "of the common people, "; const char names[] = "fi, de, fr, en"; const char *unknown, *error; test_begin("fts language detect English"); test_assert(fts_language_list_init(settings, &lp, &error) == 0); test_assert(fts_language_list_add_names(lp, names, &unknown) == TRUE); test_assert(fts_language_detect(lp, english, sizeof(english)-1, &lang_r) == FTS_LANGUAGE_RESULT_OK); test_assert(strcmp(lang_r->name, "en") == 0); fts_language_list_deinit(&lp); test_end(); }
static void test_fts_filter_lowercase(void) { struct { const char *input; const char *output; } tests[] = { { "foo", "foo" }, { "FOO", "foo" }, { "fOo", "foo" } }; struct fts_filter *filter; const char *error; const char *token; unsigned int i; test_begin("fts filter lowercase"); test_assert(fts_filter_create(fts_filter_lowercase, NULL, &english_language, NULL, &filter, &error) == 0); for (i = 0; i < N_ELEMENTS(tests); i++) { token = tests[i].input; test_assert_idx(fts_filter_filter(filter, &token, &error) > 0 && strcmp(token, tests[i].output) == 0, 0); } fts_filter_unref(&filter); test_end(); }
static void test_fs_async_copy(const char *test_name, struct fs *fs) { struct fs_file *src, *dest; struct test_fs_file *test_file; test_begin(t_strdup_printf("%s: async copy", test_name)); src = fs_file_init(fs, "foo", FS_OPEN_MODE_REPLACE); test_assert(fs_write(src, "source", 6) == 0); dest = fs_file_init(fs, "bar", FS_OPEN_MODE_REPLACE | FS_OPEN_FLAG_ASYNC); test_assert(fs_copy(src, dest) == -1 && errno == EAGAIN); test_file = test_fs_file_get(fs, "bar"); test_file->wait_async = FALSE; test_assert(fs_copy_finish_async(dest) == 0); test_assert(test_file->contents->used > 0); fs_file_deinit(&dest); fs_file_deinit(&src); test_end(); }
void test_kevent_socket_oneshot(void) { const char *test_id = "kevent(EVFILT_READ, EV_ONESHOT)"; struct kevent kev; test_begin(test_id); /* Re-add the watch and make sure no events are pending */ puts("-- re-adding knote"); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); test_no_kevents(); puts("-- getting one event"); kevent_socket_fill(); kev.data = 1; kevent_cmp(&kev, kevent_get(kqfd)); puts("-- checking knote disabled"); test_no_kevents(); /* Try to delete the knote, it should already be deleted */ EV_SET(&kev, sockfd[0], EVFILT_READ, EV_DELETE, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) == 0) err(1, "%s", test_id); kevent_socket_drain(); success(); }
static void test_fts_icu_translate_resize(void) { const char *translit_id = "Any-Hex"; const char *src_utf8 = "FOO"; buffer_t *dest, *src_utf16; UTransliterator *translit; const char *error; unsigned int i; test_begin("fts_icu_translate_resize resize"); src_utf16 = buffer_create_dynamic(pool_datastack_create(), 16); translit = get_translit(translit_id); for (i = 2; i <= 20; i++) { buffer_set_used_size(src_utf16, 0); fts_icu_utf8_to_utf16(src_utf16, src_utf8); dest = buffer_create_dynamic(pool_datastack_create(), i); test_assert(buffer_get_size(dest) == i); test_assert(fts_icu_translate(dest, src_utf16->data, src_utf16->used/sizeof(UChar), translit, &error) == 0); } utrans_close(translit); test_end(); }
void test_kevent_socket_dispatch(void) { const char *test_id = "kevent(EVFILT_READ, EV_DISPATCH)"; test_begin(test_id); struct kevent kev; /* Re-add the watch and make sure no events are pending */ puts("-- re-adding knote"); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD | EV_DISPATCH, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); test_no_kevents(); /* The event will occur only once, even though EV_CLEAR is not specified. */ kevent_socket_fill(); kev.data = 1; kevent_cmp(&kev, kevent_get(kqfd)); test_no_kevents(); /* Since the knote is disabled, the EV_DELETE operation succeeds. */ EV_SET(&kev, sockfd[0], EVFILT_READ, EV_DELETE, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); kevent_socket_drain(); success(); }
void test_kevent_signal_del(void) { const char *test_id = "kevent(EVFILT_SIGNAL, EV_DELETE)"; struct kevent kev; test_begin(test_id); /* Delete the kevent */ EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_DELETE, 0, 0, NULL); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); /* Block SIGUSR1, then send it to ourselves */ sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGUSR1); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) err(1, "sigprocmask"); if (kill(getpid(), SIGUSR1) < 0) err(1, "kill"); test_no_kevents(); success(); }
void test_kevent_socket_eof(void) { const char *test_id = "kevent(EVFILT_READ, EV_EOF)"; struct kevent kev; test_begin(test_id); /* Re-add the watch and make sure no events are pending */ EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); test_no_kevents(); if (close(sockfd[1]) < 0) err(1, "close(2)"); kev.flags |= EV_EOF; kevent_cmp(&kev, kevent_get(kqfd)); /* Delete the watch */ EV_SET(&kev, sockfd[0], EVFILT_READ, EV_DELETE, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); success(); }
static void test_message_parser_small_blocks(void) { struct message_parser_ctx *parser; struct istream *input; struct message_part *parts, *parts2; struct message_block block; unsigned int i, end_of_headers_idx; pool_t pool; int ret; test_begin("message parser in small blocks"); pool = pool_alloconly_create("message parser", 10240); input = test_istream_create(test_msg); /* full parsing */ parser = message_parser_init(pool, input, 0, 0); while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ; test_assert(ret < 0); test_assert(message_parser_deinit(&parser, &parts) == 0); /* parsing in small blocks */ i_stream_seek(input, 0); test_istream_set_allow_eof(input, FALSE); parser = message_parser_init(pool, input, 0, 0); for (i = 1; i <= TEST_MSG_LEN*2+1; i++) { test_istream_set_size(input, i/2); if (i > TEST_MSG_LEN*2) test_istream_set_allow_eof(input, TRUE); while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ; test_assert((ret == 0 && i <= TEST_MSG_LEN*2) || (ret < 0 && i > TEST_MSG_LEN*2)); } test_assert(message_parser_deinit(&parser, &parts2) == 0); test_assert(msg_parts_cmp(parts, parts2)); /* parsing in small blocks from preparsed parts */ i_stream_seek(input, 0); test_istream_set_allow_eof(input, FALSE); end_of_headers_idx = (strstr(test_msg, "\n-----") - test_msg); parser = message_parser_init_from_parts(parts, input, 0, MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK); for (i = 1; i <= TEST_MSG_LEN*2+1; i++) { test_istream_set_size(input, i/2); if (i > TEST_MSG_LEN*2) test_istream_set_allow_eof(input, TRUE); while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ; test_assert((ret == 0 && i/2 <= end_of_headers_idx) || (ret < 0 && i/2 > end_of_headers_idx)); } test_assert(message_parser_deinit(&parser, &parts2) == 0); test_assert(msg_parts_cmp(parts, parts2)); i_stream_unref(&input); pool_unref(&pool); test_end(); }
void test_kevent_socket_clear(void) { const char *test_id = "kevent(EVFILT_READ, EV_CLEAR)"; struct kevent kev; test_begin(test_id); test_no_kevents(); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); kevent_socket_fill(); kevent_socket_fill(); kev.data = 2; kevent_cmp(&kev, kevent_get(kqfd)); /* We filled twice, but drain once. Edge-triggered would not generate additional events. */ kevent_socket_drain(); test_no_kevents(); kevent_socket_drain(); EV_SET(&kev, sockfd[0], EVFILT_READ, EV_DELETE, 0, 0, &sockfd[0]); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); success(); }
void test_kevent_signal_oneshot(void) { const char *test_id = "kevent(EVFILT_SIGNAL, EV_ONESHOT)"; struct kevent kev; test_begin(test_id); EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD | EV_ONESHOT, 0, 0, NULL); if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) err(1, "%s", test_id); /* Block SIGUSR1, then send it to ourselves */ sigset_t mask; sigemptyset(&mask); sigaddset(&mask, SIGUSR1); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) err(1, "sigprocmask"); if (kill(getpid(), SIGUSR1) < 0) err(1, "kill"); kev.flags |= EV_CLEAR; kev.data = 1; kevent_cmp(&kev, kevent_get(kqfd)); /* Send another one and make sure we get no events */ if (kill(getpid(), SIGUSR1) < 0) err(1, "kill"); test_no_kevents(); success(); }
void test_istream_unix(void) { int fd[2]; test_begin("istream unix"); if ((send_fd = open("/dev/null", O_RDONLY)) == -1) i_fatal("open(/dev/null) failed: %m"); if ((send_fd2 = open("/dev/zero", O_RDONLY)) == -1) i_fatal("open(/dev/zero) failed: %m"); if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) i_fatal("socketpair() failed: %m"); switch (fork()) { case -1: i_fatal("fork() failed: %m"); case 0: i_close_fd(&fd[0]); test_istream_unix_client(fd[1]); _exit(0); default: i_close_fd(&fd[1]); test_istream_unix_server(fd[0]); break; } i_close_fd(&send_fd); i_close_fd(&send_fd2); test_end(); }
void menu_print(void) { bool begin_test = false; while (!begin_test) { test_params_print(); NRF_LOG_INFO("Throughput example:\r\n"); NRF_LOG_INFO(" 1) Run test.\r\n"); NRF_LOG_INFO(" 2) Adjust test parameters.\r\n"); NRF_LOG_FLUSH(); switch (NRF_LOG_GETCHAR()) { case ONE: default: begin_test = true; test_begin(); break; case TWO: test_param_adjust(); break; } } m_print_menu = false; }