bug: Fixing runtime issue with test_util_file.c in Windows

Windows was failing tests due to how the Windows API works with some of the calls used.
When opening and reading a file O_BINARY is needed otherwise it fails for size checks. This is due to how Windows handles newlines and counts between text and binary mode file reads.

Also fixed is the test for /dev/null.
In Windows, this fails, but crashes due to a missing return statement when it cannot open this file.
I also tried telling windows to open the special file NUL, however that leads to a CRT crash later in the test that cannot be stopped and will always fail. Rather than fail a test that Windows will always fail, it has been disabled in that specific case.

Signed-off-by: Tyler Erickson <tyler.erickson@seagate.com>
This commit is contained in:
Tyler Erickson
2025-10-20 12:10:57 -06:00
parent e3a33ae8ae
commit 3723b182db

View File

@@ -27,7 +27,9 @@
static void test_read_valid_with_fd(const char *testdir);
static void test_read_valid_nested_with_fd(const char *testdir);
static void test_read_nonexistant(void);
#ifndef _WIN32
static void test_read_closed(void);
#endif
static void test_write_to_file(void);
static void stat_and_cat(const char *file);
@@ -92,7 +94,12 @@ static void test_write_to_file(void)
static void stat_and_cat(const char *file)
{
struct stat sb;
int d = open(file, O_RDONLY);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(file, flags);
if (d < 0)
{
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
@@ -159,7 +166,10 @@ int main(int argc, char **argv)
test_read_valid_with_fd(testdir);
test_read_valid_nested_with_fd(testdir);
test_read_nonexistant();
#ifndef _WIN32
// Disabled because the Windows CRT causes a crash during this test that cannot be disabled/stopped/worked around
test_read_closed();
#endif
test_write_to_file();
test_read_fd_equal(testdir);
return EXIT_SUCCESS;
@@ -169,8 +179,12 @@ static void test_read_valid_with_fd(const char *testdir)
{
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid.json", testdir);
int d = open(filename, O_RDONLY);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
@@ -194,8 +208,12 @@ static void test_read_valid_nested_with_fd(const char *testdir)
{
char filename[PATH_MAX];
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
int d = open(filename, O_RDONLY);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
@@ -250,7 +268,7 @@ static void test_read_nonexistant(void)
json_util_get_last_err());
}
}
#ifndef _WIN32
static void test_read_closed(void)
{
// Test reading from a closed fd
@@ -258,6 +276,7 @@ static void test_read_closed(void)
if (d < 0)
{
puts("FAIL: unable to open");
return;
}
// Copy over to a fixed fd number so test output is consistent.
int fixed_d = 10;
@@ -281,6 +300,7 @@ static void test_read_closed(void)
"expecting NULL, EBADF, got:NULL, %s\n",
json_util_get_last_err());
}
#endif
static void test_read_fd_equal(const char *testdir)
{
@@ -288,8 +308,12 @@ static void test_read_fd_equal(const char *testdir)
(void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
json_object *jso = json_object_from_file(filename);
int d = open(filename, O_RDONLY);
int flags = O_RDONLY;
#ifdef O_BINARY
// This fixes Windows which otherwise opens this in text mode and returns different counts
flags |= O_BINARY;
#endif
int d = open(filename, flags);
if (d < 0)
{
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));