ATF_TC_BODY(compare_file__empty__not_match, tc) { atf_utils_create_file("test.txt", "%s", ""); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "foo")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", " ")); }
/** Waits for a subprocess and validates its exit condition. * * \param pid The process to be waited for. Must have been started by * testutils_fork(). * \param exitstatus Expected exit status. * \param expout Expected contents of stdout. * \param experr Expected contents of stderr. */ void atf_utils_wait(const pid_t pid, const int exitstatus, const char *expout, const char *experr) { int status; ATF_REQUIRE(waitpid(pid, &status, 0) != -1); atf_utils_cat_file("atf_utils_fork_out.txt", "subprocess stdout: "); atf_utils_cat_file("atf_utils_fork_err.txt", "subprocess stderr: "); ATF_REQUIRE(WIFEXITED(status)); ATF_REQUIRE_EQ(exitstatus, WEXITSTATUS(status)); const char *save_prefix = "save:"; const size_t save_prefix_length = strlen(save_prefix); if (strlen(expout) > save_prefix_length && strncmp(expout, save_prefix, save_prefix_length) == 0) { atf_utils_copy_file("atf_utils_fork_out.txt", expout + save_prefix_length); } else { ATF_REQUIRE(atf_utils_compare_file("atf_utils_fork_out.txt", expout)); } if (strlen(experr) > save_prefix_length && strncmp(experr, save_prefix, save_prefix_length) == 0) { atf_utils_copy_file("atf_utils_fork_err.txt", experr + save_prefix_length); } else { ATF_REQUIRE(atf_utils_compare_file("atf_utils_fork_err.txt", experr)); } ATF_REQUIRE(unlink("atf_utils_fork_out.txt") != -1); ATF_REQUIRE(unlink("atf_utils_fork_err.txt") != -1); }
ATF_TC_BODY(compare_file__short__not_match, tc) { atf_utils_create_file("test.txt", "this is a short file"); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a Short file")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short fil")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short file ")); }
ATF_TC_BODY(compare_file__long__not_match, tc) { char long_contents[3456]; size_t i = 0; for (; i < sizeof(long_contents) - 1; i++) long_contents[i] = '0' + (i % 10); long_contents[i] = '\0'; atf_utils_create_file("test.txt", "%s", long_contents); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n")); ATF_REQUIRE(!atf_utils_compare_file("test.txt", "0123456789")); long_contents[i - 1] = 'Z'; ATF_REQUIRE(!atf_utils_compare_file("test.txt", long_contents)); }
ATF_TC_BODY(test__config__builtin, tc) { char* helpers = helpers_path(tc); check(EXIT_SUCCESS, "", "", "test", helpers, "print_config", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n")); }
ATF_TC_BODY(test__timeout, tc) { char* helpers = select_helper(tc, "sleep"); check(EXIT_FAILURE, "", "Subprocess timed out; sending KILL signal...\n", "-t1", "test", helpers, "main", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Test case " "timed out\n")); }
ATF_TC_BODY(test__fail, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "First line to stdout\n", "First line to stderr\n", "test", helpers, "fail", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "failed: This is the failure message\n")); }
ATF_TC_CLEANUP(cleanup_check_work_directory, tc) { fprintf(stdout, "Cleanup stdout\n"); fprintf(stderr, "Cleanup stderr\n"); if (atf_utils_compare_file("cookie-in-work-directory", "the value\n")) { printf("Cleanup properly ran in the same directory as the body\n"); } else { printf("Cleanup did not run in the same directory as the body\n"); } }
ATF_TC_BODY(test__fail, tc) { char* helpers = select_helper(tc, "fail"); check(EXIT_FAILURE, "First line to stdout\n", "First line to stderr\n", "test", helpers, "main", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "failed: Returned non-success exit status 78\n")); }
ATF_TC_BODY(test__result_priority, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "Killing cleanup\n", "", "test", "-vhas.cleanup=true", helpers, "body_and_cleanup_fail", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "failed: Body fails\n")); }
ATF_TC_BODY(rewrite__missing_file, tc) { bool success; RE(kyua_atf_result_rewrite("in.txt", "out.txt", generate_wait_exitstatus(EXIT_SUCCESS), false, &success)); atf_utils_cat_file("out.txt", "OUTPUT: "); ATF_REQUIRE(atf_utils_compare_file("out.txt", "broken: Premature exit; test case exited with code 0\n")); ATF_REQUIRE(!success); }
ATF_TC_BODY(test__cleanup__timeout, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "", "Subprocess timed out; sending KILL signal...\n", "-t1", "test", "-vhas.cleanup=true", helpers, "cleanup_sleep", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Test case " "cleanup timed out\n")); }
ATF_TC_BODY(test__cleanup__fail, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "", "", "test", "-vhas.cleanup=true", helpers, "cleanup_fail", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Test case " "cleanup exited with code 1\n")); }
ATF_TC_BODY(test__pass, tc) { char* helpers = select_helper(tc, "pass"); check(EXIT_SUCCESS, "First line to stdout\nSecond line to stdout\n", "First line to stderr\nSecond line to stderr\n", "test", helpers, "main", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n")); }
ATF_TC_BODY(copy_file__empty, tc) { atf_utils_create_file("src.txt", "%s", ""); ATF_REQUIRE(chmod("src.txt", 0520) != -1); atf_utils_copy_file("src.txt", "dest.txt"); ATF_REQUIRE(atf_utils_compare_file("dest.txt", "")); struct stat sb; ATF_REQUIRE(stat("dest.txt", &sb) != -1); ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff); }
ATF_TC_BODY(test__invalid_test_case_name, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "", // TODO(jmmv): Should be EXIT_INTERNAL_ERROR. "atf_helpers: ERROR: Unknown test case `foo'\n" "atf_helpers: See atf-test-program(1) for usage details.\n", "test", "-vhas.cleanup=false", helpers, "foo", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Premature exit; test case exited with code 1\n")); }
ATF_TC_BODY(test__cleanup__ok, tc) { char* helpers = helpers_path(tc); check(EXIT_SUCCESS, "Body stdout\nCleanup stdout\n" "Cleanup properly ran in the same directory as the body\n", "Body stderr\nCleanup stderr\n", "test", "-vhas.cleanup=true", helpers, "cleanup_check_work_directory", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n")); }
ATF_TC_BODY(test__config_ignored, tc) { char* helpers = select_helper(tc, "pass"); check(EXIT_SUCCESS, "First line to stdout\nSecond line to stdout\n", "save:stderr.txt", "test", "-va=b", "-vfoo=a b c", helpers, "main", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_grep_file("ignoring 'a=b'", "stderr.txt")); ATF_REQUIRE(atf_utils_grep_file("ignoring 'foo=a b c'", "stderr.txt")); ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n")); }
ATF_TC_BODY(test__config__custom, tc) { char* helpers = helpers_path(tc); check(EXIT_SUCCESS, "body my-var1 value1\n" "body v2 a b c foo\n" "cleanup my-var1 value1\n" "cleanup v2 a b c foo\n", "", "test", "-vmy-var1=value1", "-vv2=a b c foo", helpers, "print_config", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n")); }
ATF_TC_BODY(test__crash, tc) { char* helpers = select_helper(tc, "signal"); check(EXIT_FAILURE, "", "save:crash.err", "test", helpers, "main", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Received signal 6\n")); ATF_REQUIRE(atf_utils_grep_file("About to die due to SIGABRT!", "crash.err")); ATF_REQUIRE(atf_utils_grep_file("attempting to gather stack trace", "crash.err")); }
ATF_TC_BODY(test__crash, tc) { char* helpers = helpers_path(tc); check(EXIT_FAILURE, "", "save:crash.err", "test", helpers, "signal", "test-result", NULL); free(helpers); ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Premature exit; test case received signal 6 (core dumped)\n")); ATF_REQUIRE(atf_utils_grep_file("About to die due to SIGABRT!", "crash.err")); ATF_REQUIRE(atf_utils_grep_file("attempting to gather stack trace", "crash.err")); }
ATF_TC_BODY(wait__save_stderr, tc) { const pid_t control = fork(); ATF_REQUIRE(control != -1); if (control == 0) fork_and_wait(123, "Some output\n", "save:my-output.txt"); else { int status; ATF_REQUIRE(waitpid(control, &status, 0) != -1); ATF_REQUIRE(WIFEXITED(status)); ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status)); ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some error\n")); } }
ATF_TC_BODY(fprintf__simple_string, tc) { const char *contents = "This is a message\n"; FILE *output = fopen("test.txt", "w"); ATF_REQUIRE(fprintf(output, "%s", contents) > 0); fclose(output); /* The ATF C library provides more than just macros to verify the * outcome of expressions. It also includes various helper functions * to work with files and processes. Here is just a simple * example. */ ATF_REQUIRE(atf_utils_compare_file("test.txt", contents)); /* Of special note here is that we are NOT deleting the * temporary files we created in this test. Kyua takes care of * this cleanup automatically and tests can (and should) rely on * this behavior. */ }
ATF_TC_BODY(rewrite__too_long_with_newlines, tc) { char input[1000]; fill_buffer("failed: ", "line\n", input, sizeof(input)); // This is quite awful but is the price we have to pay for using fixed-size // buffers in the code for simplicity and speed... char exp_output[1024 + 8 /* strlen("failed: ") */ + 1]; fill_buffer("failed: ", "line<<NEWLINE>>", exp_output, sizeof(exp_output)); exp_output[sizeof(exp_output) - 2] = '\n'; bool success; atf_utils_create_file("in.txt", "%s", input); RE(kyua_atf_result_rewrite("in.txt", "out.txt", generate_wait_exitstatus(EXIT_FAILURE), false, &success)); atf_utils_cat_file("out.txt", "OUTPUT: "); printf("EXPECTED: %s", exp_output); ATF_REQUIRE(atf_utils_compare_file("out.txt", exp_output)); ATF_REQUIRE_EQ(false, success); }
ATF_TC_BODY(copy_file__some_contents, tc) { atf_utils_create_file("src.txt", "This is a\ntest file\n"); atf_utils_copy_file("src.txt", "dest.txt"); ATF_REQUIRE(atf_utils_compare_file("dest.txt", "This is a\ntest file\n")); }
bool atf::utils::compare_file(const std::string& path, const std::string& contents) { return atf_utils_compare_file(path.c_str(), contents.c_str()); }