diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 2a4ceef..03ba3a4 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -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));