1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include "TestSupport.h"
#include "FileDescriptor.h"
#include <cerrno>
using namespace Passenger;
namespace tut {
struct FileDescriptorTest {
int pipes[2];
FileDescriptorTest() {
pipe(pipes);
}
~FileDescriptorTest() {
if (pipes[0] != -1) {
close(pipes[0]);
}
if (pipes[1] != -1) {
close(pipes[1]);
}
}
};
DEFINE_TEST_GROUP(FileDescriptorTest);
TEST_METHOD(1) {
// Test constructors.
FileDescriptor f;
ensure_equals("An empty FileDescriptor has value -1",
f, -1);
int fd = pipes[0];
pipes[0] = -1;
f = FileDescriptor(fd);
ensure_equals("FileDescriptor takes the value of its constructor argument",
f, fd);
}
TEST_METHOD(2) {
// It closes the underlying file descriptor when the last
// instance is destroyed.
int reader = pipes[0];
pipes[0] = -1;
{
FileDescriptor f(reader);
{
FileDescriptor f2(f);
}
ensure("File descriptor is not closed if there are still live copies",
write(pipes[1], "x", 1) != -1);
}
ensure("File descriptor is closed if the last live copy is dead",
write(pipes[1], "x", 1) == -1);
}
TEST_METHOD(3) {
// Calling close() will close the underlying file descriptor for all instances.
int reader = pipes[0];
pipes[0] = -1;
FileDescriptor f(reader);
FileDescriptor f2(f);
f.close();
ensure_equals(f, -1);
ensure_equals(f2, -1);
ensure(write(pipes[1], "x", 1) == -1);
}
}
|