Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
pks-t committed Sep 7, 2024
1 parent d0bfbe2 commit eb1eb99
Showing 1 changed file with 35 additions and 43 deletions.
78 changes: 35 additions & 43 deletions test/clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,53 @@
# define WIN32_LEAN_AND_MEAN
# include <windows.h>

static char *read_file(const char *path)
static char *read_full(HANDLE h, int is_pipe)
{
char *content = NULL;
size_t content_size = 0;
HANDLE file;

file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
cl_assert(file != INVALID_HANDLE_VALUE);
char *data = NULL;
size_t data_size = 0;

while (1) {
CHAR buf[4096];
DWORD bytes_read;

if (!ReadFile(file, buf, sizeof(buf), &bytes_read, NULL))
cl_fail("Failed reading file.");
if (!ReadFile(h, buf, sizeof(buf), &bytes_read, NULL)) {
if (!is_pipe)
cl_fail("Failed reading file handle.");
cl_assert_equal_i(GetLastError(), ERROR_BROKEN_PIPE);
break;
}
if (!bytes_read)
break;

content = realloc(content, content_size + bytes_read);
cl_assert(content);
memcpy(content + content_size, buf, bytes_read);
content_size += bytes_read;
data = realloc(data, data_size + bytes_read);
cl_assert(data);
memcpy(data + data_size, buf, bytes_read);
data_size += bytes_read;
}

content = realloc(content, content_size + 1);
cl_assert(content);
content[content_size] = '\0';
data = realloc(data, data_size + 1);
cl_assert(data);
data[data_size] = '\0';

while (strstr(data, "\r\n")) {
char *ptr = strstr(data, "\r\n");
memmove(ptr, ptr + 1, strlen(ptr));
}

return data;
}

static char *read_file(const char *path)
{
char *content;
HANDLE file;

file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
cl_assert(file != INVALID_HANDLE_VALUE);
content = read_full(file, 0);
cl_assert_equal_b(1, CloseHandle(file));

return content;
}

Expand All @@ -53,7 +70,6 @@ static void run(const char *expected_output_file, int expected_error_code, ...)
char cmdline[4096] = { 0 };
char *expected_output = NULL;
char *output = NULL;
size_t output_size = 0;
HANDLE stdout_write;
HANDLE stdout_read;
DWORD exit_code;
Expand Down Expand Up @@ -99,33 +115,9 @@ static void run(const char *expected_output_file, int expected_error_code, ...)
startup_info.dwFlags |= STARTF_USESTDHANDLES;
cl_assert_equal_b(1, CreateProcess(SELFTEST_BINARY_PATH, cmdline, NULL, NULL, TRUE,
0, NULL, NULL, &startup_info, &process_info));

cl_assert_equal_b(1, CloseHandle(stdout_write));

while (1) {
CHAR buf[4096];
DWORD bytes_read;

if (!ReadFile(stdout_read, buf, sizeof(buf), &bytes_read, NULL)) {
cl_assert_equal_i(GetLastError(), ERROR_BROKEN_PIPE);
break;
}

output = realloc(output, output_size + bytes_read);
cl_assert(output);
memcpy(output + output_size, buf, bytes_read);
output_size += bytes_read;
}

output = realloc(output, output_size + 1);
cl_assert(output);
output[output_size] = '\0';

while (strstr(output, "\r\n")) {
char *ptr = strstr(output, "\r\n");
memmove(ptr, ptr + 1, strlen(ptr));
}

output = read_full(stdout_read, 1);
cl_assert_equal_b(1, CloseHandle(stdout_read));
cl_assert_equal_b(1, GetExitCodeProcess(process_info.hProcess, &exit_code));

Expand Down

0 comments on commit eb1eb99

Please sign in to comment.