| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <string.h> |
| #include <unistd.h> |
| #include <fcntl.h> |
| #include <errno.h> |
| #include <sys/types.h> |
| #include <sys/stat.h> |
| #include <stdbool.h> |
| #include <stdio.h> |
|
|
| |
| |
| |
| |
| |
| |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| |
| |
| |
| static int run_simple_cat_capture(const char *input, size_t input_len, |
| size_t bufsize, |
| char **out, size_t *out_len, bool *func_ret) |
| { |
| int inpipe[2] = {-1, -1}; |
| int outpipe[2] = {-1, -1}; |
| int saved_stdout = -1; |
| char *capture = NULL; |
| size_t cap = 0, len = 0; |
| int rc = 0; |
|
|
| if (pipe(inpipe) != 0) |
| return -1; |
|
|
| |
| size_t written = 0; |
| while (written < input_len) { |
| ssize_t n = write(inpipe[1], input + written, input_len - written); |
| if (n < 0) { rc = -2; goto cleanup; } |
| written += (size_t)n; |
| } |
| if (close(inpipe[1]) != 0) { inpipe[1] = -1; rc = -3; goto cleanup; } |
| inpipe[1] = -1; |
|
|
| input_desc = inpipe[0]; |
| infile = "test-input"; |
|
|
| if (pipe(outpipe) != 0) { rc = -4; goto cleanup; } |
|
|
| saved_stdout = dup(STDOUT_FILENO); |
| if (saved_stdout < 0) { rc = -5; goto cleanup; } |
|
|
| if (dup2(outpipe[1], STDOUT_FILENO) < 0) { rc = -6; goto cleanup; } |
| |
| if (close(outpipe[1]) != 0) { outpipe[1] = -1; rc = -7; goto cleanup; } |
| outpipe[1] = -1; |
|
|
| |
| char *buf = (char *)malloc(bufsize > 0 ? bufsize : 1); |
| if (!buf) { rc = -8; goto restore_stdout; } |
|
|
| bool ret = simple_cat(buf, bufsize); |
| free(buf); |
|
|
| restore_stdout: |
| |
| { |
| int tmp = dup2(saved_stdout, STDOUT_FILENO); |
| (void)tmp; |
| close(saved_stdout); |
| saved_stdout = -1; |
| } |
|
|
| |
| if (inpipe[0] >= 0) { close(inpipe[0]); inpipe[0] = -1; } |
|
|
| |
| { |
| char tmpbuf[4096]; |
| ssize_t n; |
| for (;;) { |
| n = read(outpipe[0], tmpbuf, sizeof tmpbuf); |
| if (n < 0) { rc = -9; goto cleanup; } |
| if (n == 0) break; |
| if (len + (size_t)n > cap) { |
| size_t newcap = cap ? cap * 2 : 1024; |
| while (newcap < len + (size_t)n) newcap *= 2; |
| char *newp = (char *)realloc(capture, newcap); |
| if (!newp) { rc = -10; goto cleanup; } |
| capture = newp; |
| cap = newcap; |
| } |
| memcpy(capture + len, tmpbuf, (size_t)n); |
| len += (size_t)n; |
| } |
| } |
| if (outpipe[0] >= 0) { close(outpipe[0]); outpipe[0] = -1; } |
|
|
| *out = capture; |
| *out_len = len; |
| *func_ret = ret; |
| return 0; |
|
|
| cleanup: |
| if (saved_stdout >= 0) { |
| |
| dup2(saved_stdout, STDOUT_FILENO); |
| close(saved_stdout); |
| saved_stdout = -1; |
| } |
| if (inpipe[0] >= 0) close(inpipe[0]); |
| if (inpipe[1] >= 0) close(inpipe[1]); |
| if (outpipe[0] >= 0) close(outpipe[0]); |
| if (outpipe[1] >= 0) close(outpipe[1]); |
| free(capture); |
| return rc; |
| } |
|
|
| |
| void test_simple_cat_basic(void) |
| { |
| const char *input = "Hello, world!\nSecond line.\nNo newline at end"; |
| char *out = NULL; |
| size_t out_len = 0; |
| bool func_ret = false; |
|
|
| int rc = run_simple_cat_capture(input, strlen(input), 64, &out, &out_len, &func_ret); |
| TEST_ASSERT_EQUAL_INT(0, rc); |
| TEST_ASSERT_TRUE(func_ret); |
| TEST_ASSERT_EQUAL_INT((int)strlen(input), (int)out_len); |
| TEST_ASSERT_EQUAL_MEMORY(input, out, out_len); |
|
|
| free(out); |
| } |
|
|
| |
| void test_simple_cat_empty_input(void) |
| { |
| const char *input = ""; |
| char *out = NULL; |
| size_t out_len = 0; |
| bool func_ret = false; |
|
|
| int rc = run_simple_cat_capture(input, 0, 32, &out, &out_len, &func_ret); |
| TEST_ASSERT_EQUAL_INT(0, rc); |
| TEST_ASSERT_TRUE(func_ret); |
| TEST_ASSERT_EQUAL_INT(0, (int)out_len); |
|
|
| free(out); |
| } |
|
|
| |
| void test_simple_cat_large_input_small_buf(void) |
| { |
| const size_t N = 131072 + 123; |
| char *input = (char *)malloc(N); |
| TEST_ASSERT_NOT_NULL(input); |
| for (size_t i = 0; i < N; i++) { |
| input[i] = (char)('A' + (int)(i % 26)); |
| } |
|
|
| char *out = NULL; |
| size_t out_len = 0; |
| bool func_ret = false; |
|
|
| int rc = run_simple_cat_capture(input, N, 128, &out, &out_len, &func_ret); |
| TEST_ASSERT_EQUAL_INT(0, rc); |
| TEST_ASSERT_TRUE(func_ret); |
| TEST_ASSERT_EQUAL_INT((int)N, (int)out_len); |
| TEST_ASSERT_EQUAL_MEMORY(input, out, N); |
|
|
| free(out); |
| free(input); |
| } |
|
|
| |
| void test_simple_cat_read_error_returns_false(void) |
| { |
| |
| input_desc = -1; |
| infile = "invalid-fd-for-test"; |
|
|
| char dummy_buf[16]; |
| bool ret = simple_cat(dummy_buf, sizeof dummy_buf); |
| TEST_ASSERT_FALSE(ret); |
| } |
|
|
| int main(void) |
| { |
| UNITY_BEGIN(); |
| RUN_TEST(test_simple_cat_basic); |
| RUN_TEST(test_simple_cat_empty_input); |
| RUN_TEST(test_simple_cat_large_input_small_buf); |
| RUN_TEST(test_simple_cat_read_error_returns_false); |
| return UNITY_END(); |
| } |