Skip to content

Commit

Permalink
completely broken mingw code
Browse files Browse the repository at this point in the history
Clueless I am, and not having a machine to test this one does not help.
  • Loading branch information
pks-t committed Sep 6, 2024
1 parent c3406d5 commit 7046f4a
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion test/clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef __MINGW__
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <sys/wait.h>
#endif

#include "clar.h"

Expand Down Expand Up @@ -59,6 +64,93 @@ static char *read_file(const char *path)

static void run(const char *expected_output_file, int expected_error_code, ...)
{
#ifdef __MINGW32__
SECURITY_ATTRIBUTES sec_attributes = { 0 };
PROCESS_INFORMATION process_info = { 0 };
STARTUPINFO startup_info = { 0 };
const char cmdline[4096] = { 0 };
HANDLE pipe_write = NULL;
HANDLE pipe_read = NULL;
size_t output_size = 0;
char *output = NULL;
int exit_code;
va_list ap;
BOOL ret;

/*
* Assemble command line arguments. In theory we'd have to properly
* quote them. In practice none of our tests actually care.
*/
va_start(ap, expected_error_code);
snprintf(cmdline, sizeof(cmdline), "%s", SELFTEST_BINARY_PATH);
while (1) {
size_t cmdline_len = strlen(cmdline);
const char *arg;

arg = va_arg(ap, const char *);
if (!argv[i])
break;

cl_assert(cmdline_len + strlen(arg) < sizeof(cmdline));
snprintf(cmdline + cmdline_len, " %s", arg);
}
va_end(ap);

/*
* Create a pipe that we will use to read data from the child process.
* The writing side needs to be inheritable such that the child can use
* it as stdout and stderr. The reading side should only be used by the
* parent.
*/
sec_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
sec_attributes.bInheritHandle = TRUE;
CreatePipe(&pipe_read, &pipe_write, &sec_attributes, 0);
SetHandleInformation(pipe_read, HANDLE_FLAG_INHERIT, 0);

/*
* Create the child process with our pipe.
*/
startup_info.cb = sizeof(STARTUPINFO);
startup_info.hStdError = pipe_write;
startup_info.hStdOutput = pipe_write;
startup_info.dwFlags |= STARTF_USESTDHANDLES;
ret = CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0 NULL, NULL,
&startup_info, &process_info);
cl_assert_equal_b(ret, 1);

/* This handle is no longer needed now and is owned by the child. */
CloseHandle(pipe_write);

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

ret = ReadFile(pipe_read, buf, sizeof(buf), &bytes_read, NULL);
cl_assert_equal_b(ret, 1);
if (!bytes_read)
break;

output = realloc(output, output_size + bytes_read);
cl_assert(output);

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

CloseHandle(pipe_read);

ret = GetExitCodeProcess(process_info.hProcess, &exit_code);
cl_assert_equal_b(ret, 1);

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

expected_output = read_file(cl_fixture(expected_output_file));

cl_assert_equal_s(output, expected_output);
cl_assert_equal_i(exit_code, expected_error_code);
#else
const char *argv[16];
int pipe_fds[2];
va_list ap;
Expand Down Expand Up @@ -111,6 +203,7 @@ static void run(const char *expected_output_file, int expected_error_code, ...)
} else {
cl_fail("Fork failed.");
}
#endif
}

void test_clar__help(void)
Expand Down

0 comments on commit 7046f4a

Please sign in to comment.