Use stdio.h instead of cstdio for fdopen. Wrap all used POSIX functions for testing.

This commit is contained in:
Victor Zverovich
2014-05-15 08:58:10 -07:00
parent d758dbb33a
commit fb17316d77
5 changed files with 126 additions and 42 deletions
+63 -10
View File
@@ -27,8 +27,6 @@
#include "posix-test.h"
#undef open
#include <errno.h>
#include <fcntl.h>
@@ -36,29 +34,84 @@
namespace {
int open_count;
int close_count;
int dup_count;
int dup2_count;
int fdopen_count;
int fileno_count;
int read_count;
int write_count;
}
#define EMULATE_EINTR(func, error_result) \
if (func##_count != 0) { \
if (func##_count++ != 3) { \
errno = EINTR; \
return error_result; \
} \
}
#ifndef _WIN32
int test::open(const char *path, int oflag, int mode) {
if ((open_count++ % 3) == 2)
return ::open(path, oflag, mode);
errno = EINTR;
return -1;
EMULATE_EINTR(open, -1);
return ::open(path, oflag, mode);
}
#else
errno_t test::sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode) {
errno_t test::sopen_s(
int* pfh, const char *filename, int oflag, int shflag, int pmode) {
EMULATE_EINTR(open, EINTR);
return _sopen_s(pfh, filename, oflag, shflag, pmode);
}
#endif
int test::close(int fildes) {
// Close the file first because close shouldn't be retried.
int result = ::close(fildes);
EMULATE_EINTR(close, -1);
return result;
}
int test::dup(int fildes) {
EMULATE_EINTR(dup, -1);
return ::dup(fildes);
}
int test::dup2(int fildes, int fildes2) {
EMULATE_EINTR(dup2, -1);
return ::dup2(fildes, fildes2);
}
FILE *test::fdopen(int fildes, const char *mode) {
EMULATE_EINTR(fdopen, 0);
return ::fdopen(fildes, mode);
}
int test::fileno(FILE *stream) {
EMULATE_EINTR(fileno, -1);
return ::fileno(stream);
}
test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
EMULATE_EINTR(read, -1);
return ::read(fildes, buf, nbyte);
}
test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
EMULATE_EINTR(write, -1);
return ::write(fildes, buf, nbyte);
}
#ifndef _WIN32
# define EXPECT_RETRY(statement, func, message) \
func##_count = 0; \
func##_count = 1; \
statement; \
EXPECT_EQ(3, func##_count);
EXPECT_EQ(4, func##_count); \
func##_count = 0;
#else
# define EXPECT_RETRY(statement, func, message) \
EXPECT_SYSTEM_ERROR(statement, EINTR, message);
func##_count = 1; \
EXPECT_SYSTEM_ERROR(statement, EINTR, message); \
func##_count = 0;
#endif
TEST(FileTest, OpenRetry) {