/** * test_server_loop() 関数テスト * * @return なし */ void test_server_loop(void) { pid_t cpid = 0; /* 子プロセスID */ pid_t w = 0; /* wait戻り値 */ int status = 0; /* wait引数 */ int retval = 0; /* 戻り値 */ int count = 1; /* ループカウント */ if (set_port_string(port) < 0) cut_error("set_port_string"); ssock = server_sock(); cpid = fork(); if (cpid < 0) { cut_error("fork(%d)", errno); return; } if (cpid == 0) { dbglog("child"); count = 2; g_sig_handled = 1; while (count--) server_loop(ssock); exit(EXIT_SUCCESS); } else { dbglog("parent: cpid=%d", (int)cpid); csock = inet_sock_client(); if (csock < 0) return; /* 送信 */ retval = send_client(csock, expr, sizeof(expr)); if (retval < 0) { cut_error("send_client: csock=%d(%d)", csock, errno); return; } /* 受信 */ retval = recv_client(csock, readbuf); if (retval < 0) { cut_error("recv_client: csock=%d(%d)", csock, errno); return; } cut_assert_equal_string((char *)expected, (char *)readbuf); w = wait(&status); if (w < 0) cut_notify("wait(%d)", errno); dbglog("w=%d", (int)w); } }
/** * print_timer() 関数テスト * * @return なし */ void test_print_timer(void) { unsigned int t = 0, time = 0; /* タイマ用変数 */ int fd = -1; /* ファイルディスクリプタ */ int retval = 0; /* 戻り値 */ char actual[BUF_SIZE] = {0}; /* 実際の文字列 */ const char expected[] = /* 期待する文字列 */ "time of time: [0-9]+\\.[0-9]+\\[msec\\]"; start_timer(&t); time = stop_timer(&t); fd = pipe_fd(STDERR_FILENO); if (fd < 0) { cut_error("pipe_fd=%d(%d)", fd, errno); return; } print_timer(time); retval = read(fd, actual, sizeof(actual)); if (retval < 0) { cut_fail("read=%d(%d)", fd, errno); goto error_handler; } dbglog("actual=%s", actual); cut_assert_match(expected, actual, cut_message("expected=%s actual=%s", expected, actual)); error_handler: close_fd(&fd, NULL); }
/** * test_server_sock() 関数テスト * * @return なし */ void test_server_sock(void) { dbglog("start"); if (set_port_string(port) < 0) cut_error("set_port_string"); ssock = server_sock(); dbglog("server_sock=%d", ssock); cut_assert_not_equal_int(EX_NG, ssock); csock = inet_sock_client(); if (csock < 0) cut_error("inet_sock_client"); }
static void stub_error_iterated_test (gconstpointer data) { cut_assert_true(TRUE, cut_message("always pass")); if (GPOINTER_TO_INT(data) == 2) MARK_FAIL(cut_error("ERROR!")); cut_assert_true(TRUE, cut_message("always pass if come here")); }
static void stub_error_in_data_setup_iterated_data (void) { cut_add_data("First", GINT_TO_POINTER(1), NULL, NULL); cut_add_data("Second", GINT_TO_POINTER(2), NULL, NULL); cut_error("error in data setup"); cut_add_data("Third", GINT_TO_POINTER(3), NULL, NULL); }
static void not_uint32_size_key_increment(grn_test_data *test_data) { gchar *string = test_data->key; gint i; for (i = 0; i < not_uint32_key_size; i++) { if (string[i] < '~') { string[i]++; return; } } cut_error("can't increment more!: %s", string); }
void test_aton(gconstpointer data) { const gchar *input, *input_end, *rest; grn_builtin_type type; grn_rc rc; type = gcut_data_get_int(data, "type"); input = gcut_data_get_string(data, "input"); input_end = strchr(input, '\0'); rc = grn_aton(&context, input, input_end, &rest, &buffer); grn_test_assert(rc); cut_assert_equal_string(input_end, rest); cut_assert_equal_int(type, buffer.header.domain); switch (type) { case GRN_DB_INT32 : cut_assert_equal_int(gcut_data_get_int(data, "expected"), GRN_INT32_VALUE(&buffer)); break; case GRN_DB_UINT32 : cut_assert_equal_uint(gcut_data_get_uint(data, "expected"), GRN_UINT32_VALUE(&buffer)); break; case GRN_DB_INT64 : gcut_assert_equal_int64(gcut_data_get_int64(data, "expected"), GRN_INT64_VALUE(&buffer)); break; case GRN_DB_UINT64 : gcut_assert_equal_uint64(gcut_data_get_uint64(data, "expected"), GRN_UINT64_VALUE(&buffer)); break; case GRN_DB_FLOAT : cut_assert_equal_double(gcut_data_get_double(data, "expected"), 0.000001, GRN_FLOAT_VALUE(&buffer)); break; default : cut_error("unknown type: %d", type); break; } }
/** * test_server_proc() 関数テスト * * @return なし */ void test_server_proc(void) { pid_t cpid = 0; /* 子プロセスID */ pid_t w = 0; /* wait戻り値 */ int status = 0; /* wait引数 */ int retval = 0; /* 戻り値 */ thread_data *dt = NULL; /* ソケット情報構造体 */ void *servret = NULL; /* テスト関数戻り値 */ if (set_port_string(port) < 0) cut_error("set_port_string"); ssock = server_sock(); cpid = fork(); if (cpid < 0) { cut_error("fork(%d)", errno); return; } if (cpid == 0) { dbglog("child"); dt = (thread_data *)malloc(sizeof(thread_data)); if (!dt) { outlog("malloc: size=%zu", sizeof(thread_data)); exit(EXIT_FAILURE); } (void)memset(dt, 0, sizeof(thread_data)); dt->len = (socklen_t)sizeof(dt->addr); dt->sock = accept(ssock, (struct sockaddr *)&dt->addr, &dt->len); if (dt->sock < 0) { outlog("accept: ssock=%d", ssock); memfree((void **)&dt, NULL); exit(EXIT_FAILURE); } g_sig_handled = 1; /* テスト関数実行 */ servret = server.server_proc(dt); if (servret) { outlog("server_proc"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { dbglog("parent: cpid=%d", (int)cpid); csock = inet_sock_client(); if (csock < 0) return; /* 送信 */ retval = send_client(csock, expr, sizeof(expr)); if (retval < 0) { cut_error("send_client: csock=%d(%d)", csock, errno); return; } /* 受信 */ retval = recv_client(csock, readbuf); if (retval < 0) { cut_error("recv_client: csock=%d(%d)", csock, errno); return; } cut_assert_equal_string((char *)expected, (char *)readbuf); w = wait(&status); if (w < 0) cut_notify("wait(%d)", errno); dbglog("w=%d", (int)w); if (WEXITSTATUS(status)) cut_error("child failed"); } }
static void stub_error_test (void) { MARK_FAIL(cut_error("This test should error")); }
/** * readline() 実行 * * @param[in] data テストデータ * @param[in] length バイト数 * @return 結果文字列 */ static unsigned char * exec_readline(char *data, size_t length) { FILE *fp = NULL; /* ファイルポインタ */ int retval = 0; /* 戻り値 */ pid_t cpid = 0; /* プロセスID */ pid_t w = 0; /* wait戻り値 */ int status = 0; /* ステイタス */ ssize_t len = 0; /* writen 戻り値 */ retval = pipe(pfd); if (retval < 0) { cut_error("pipe=%d", retval); return NULL; } fp = fdopen(pfd[PIPE_R], "r"); if (!fp) { cut_error("fdopen=%p", fp); return NULL; } cpid = fork(); if (cpid < 0) { cut_error("fork(%d)", errno); return NULL; } if (cpid == 0) { /* 子プロセス */ dbglog("child"); close_fd(&pfd[PIPE_R], NULL); /* 送信 */ len = writen(pfd[PIPE_W], data, length); if (len < 0) { outlog("writen"); close_fd(&pfd[PIPE_W], NULL); exit(EXIT_FAILURE); } close_fd(&pfd[PIPE_W], NULL); exit(EXIT_SUCCESS); } else { /* 親プロセス */ dbglog("parent: cpid=%d", (int)cpid); close_fd(&pfd[PIPE_W], NULL); /* テスト関数の実行 */ /* 受信待ち */ result = _readline(fp); dbglog("result=%s", result); close_fd(&pfd[PIPE_R], NULL); w = waitpid(-1, &status, WNOHANG); if (w < 0) cut_notify("wait: status=%d(%d)", status, errno); dbglog("w=%d", (int)w); if (WEXITSTATUS(status)) { cut_notify("child error"); return NULL; } } return result; }