8000 minix/test/test56.c at R3.1.8 · gezidan/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2581 lines (2036 loc) · 53.9 KB

test56.c

File metadata and controls

2581 lines (2036 loc) · 53.9 KB
debug("leaving test_socketpair()");
}
void test_ucred(void)
{
struct ucred credentials;
socklen_t ucred_length;
uid_t euid = geteuid();
gid_t egid = getegid();
int sv[2];
int rc;
debug("Test credentials passing");
ucred_length = sizeof(struct ucred);
rc = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
if (rc == -1) {
test_fail("socketpair(PF_UNIX, SOCK_STREAM, 0, sv) failed");
}
memset(&credentials, '\0', ucred_length);
rc = getsockopt(sv[0], SOL_SOCKET, SO_PEERCRED, &credentials,
&ucred_length);
if (rc == -1) {
test_fail("getsockopt(SO_PEERCRED) failed");
} else if (credentials.pid != getpid() ||
credentials.uid != geteuid() ||
credentials.gid != getegid()) {
/* printf("%d=%d %d=%d %d=%d",credentials.pid, getpid(),
credentials.uid, geteuid(), credentials.gid, getegid()); */
test_fail("Credential passing gave us the wrong cred");
}
rc = getpeereid(sv[0], &euid, &egid);
if (rc == -1) {
test_fail("getpeereid(sv[0], &euid, &egid) failed");
} else if (credentials.uid != euid || credentials.gid != egid) {
test_fail("getpeereid() didn't give the correct euid/egid");
}
CLOSE(sv[0]);
CLOSE(sv[1]);
}
void test_getsockname(void)
{
int sd;
int rc;
struct sockaddr_un addr, sock_addr;
socklen_t sock_addr_len;
memset(&addr, '\0', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
debug("Test bind() success");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(struct sockaddr_un));
sock_addr_len = sizeof(struct sockaddr_un);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
if (!(sock_addr.sun_family == AF_UNIX && strncmp(sock_addr.sun_path,
fullpath(TEST_SUN_PATH),
sizeof(sock_addr.sun_path) - 1) == 0)) {
test_fail("getsockname() did return the right address");
fprintf(stderr, "exp: '%s' | got: '%s'\n", addr.sun_path,
sock_addr.sun_path);
}
CLOSE(sd);
}
void test_bind(void)
{
struct sockaddr_un addr;
struct sockaddr_un sock_addr;
socklen_t sock_addr_len;
int sd;
int sd2;
int rc;
int on;
debug("entering test_bind()");
on = 1;
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
debug("Test bind() success");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc == -1) {
test_fail("bind() should have worked");
}
debug("Test getsockname() success");
memset(&sock_addr, '\0', sizeof(struct sockaddr_un));
sock_addr_len = sizeof(struct sockaddr_un);
rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
if (rc == -1) {
test_fail("getsockname() should have worked");
}
if (!(sock_addr.sun_family == AF_UNIX &&
strncmp(sock_addr.sun_path,
fullpath(TEST_SUN_PATH),
sizeof(sock_addr.sun_path) - 1) == 0)) {
test_fail("getsockname() didn't return the right addr");
fprintf(stderr, "exp: '%s' | got: '%s'\n", addr.sun_path,
sock_addr.sun_path);
}
debug("Test bind() with a address that has already been bind()'d");
SOCKET(sd2, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = bind(sd2, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == EADDRINUSE))) {
test_fail("bind() should have failed with EADDRINUSE");
}
CLOSE(sd2);
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
debug("Test bind() with an empty sun_path");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
memset(addr.sun_path, '\0', sizeof(addr.sun_path));
errno = 0;
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!(rc == -1 && errno == ENOENT)) {
test_fail("bind() should have failed with ENOENT");
}
CLOSE(sd);
debug("Test bind() with a NULL address");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = bind(sd, (struct sockaddr *) NULL, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == EFAULT))) {
test_fail("bind() should have failed with EFAULT");
}
CLOSE(sd);
debug("Test bind() using a symlink loop");
UNLINK(TEST_SUN_PATH);
UNLINK(TEST_SYM_A);
UNLINK(TEST_SYM_B);
SYMLINK(TEST_SYM_A, TEST_SYM_B);
SYMLINK(TEST_SYM_B, TEST_SYM_A);
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
strncpy(addr.sun_path, TEST_SYM_A, sizeof(addr.sun_path) - 1);
errno = 0;
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (!((rc == -1) && (errno == ELOOP))) {
test_fail("bind() should have failed with ELOOP");
}
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
UNLINK(TEST_SYM_A);
UNLINK(TEST_SYM_B);
debug("leaving test_bind()");
}
void test_listen(void)
{
int sd;
int rc;
debug("entering test_listen()");
debug("Test listen() with a bad file descriptor");
errno = 0;
rc = listen(-1, 0);
if (!(rc == -1 && errno == EBADF)) {
test_fail("listen(-1, 0) should have failed");
}
debug("Test listen() with a non-socket file descriptor");
errno = 0;
rc = listen(0, 0);
if (!(rc == -1 && errno == ENOTTY)) {
test_fail("listen(0, 0) should have failed");
}
debug("leaving test_listen()");
}
void test_shutdown(void)
{
int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
int sd;
int rc;
int i;
debug("entering test_shutdown()");
/* test for each direction (read, write, read-write) */
for (i = 0; i < 3; i++) {
debug("test shutdown() with an invalid descriptor");
errno = 0;
rc = shutdown(-1, how[i]);
if (!(rc == -1 && errno == EBADF)) {
test_fail("shutdown(-1, how[i]) should have failed");
}
debug("test shutdown() with a non-socket descriptor");
errno = 0;
rc = shutdown(0, how[i]);
if (!(rc == -1 && errno == ENOSYS)) {
test_fail("shutdown() should have failed with ENOSYS");
}
debug("test shutdown() with a socket that is not connected");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = shutdown(sd, how[i]);
if (!(rc == -1 && errno == ENOTCONN)) {
test_fail("shutdown() should have failed");
}
CLOSE(sd);
}
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
errno = 0;
rc = shutdown(sd, -1);
if (!(rc == -1 && errno == ENOTCONN)) {
test_fail("shutdown(sd, -1) should have failed with ENOTCONN");
}
CLOSE(sd);
debug("leaving test_shutdown()");
}
void test_close(void)
{
struct sockaddr_un addr;
int sd, sd2;
int rc, i;
debug("entering test_close()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
debug("Test close() success");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
CLOSE(sd);
debug("Close an already closed file descriptor");
errno = 0;
rc = close(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("close(sd) should have failed with EBADF");
}
UNLINK(TEST_SUN_PATH);
debug("dup()'ing a file descriptor and closing both should work");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
} else {
CLOSE(sd2);
CLOSE(sd);
}
UNLINK(TEST_SUN_PATH);
/* Create and close a socket a bunch of times.
* If the implementation doesn't properly free the
* socket during close(), eventually socket() will
* fail when the internal descriptor table is full.
*/
for (i = 0; i < 1024; i++) {
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
CLOSE(sd);
}
debug("leaving test_close()");
}
void test_sockopts(void)
{
int i;
int rc;
int sd;
int option_value;
int option_value_orig;
socklen_t option_len;
debug("entering test_sockopts()");
for (i = 0; i < 3; i++) {
SOCKET(sd, PF_UNIX, types[i], 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
&option_len);
if (rc != 0) {
test_fail("setsockopt() should have worked");
}
if (option_value != types[i]) {
test_fail("SO_TYPE didn't seem to work.");
}
CLOSE(sd);
}
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
if (rc != 0) {
test_fail("getsockopt() should have worked");
}
if (option_value != PIPE_BUF) {
test_fail("SO_SNDBUF didn't seem to work.");
}
CLOSE(sd);
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
debug("Test setsockopt() works");
option_value = 0;
option_len = sizeof(option_value);
errno = 0;
rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
if (rc != 0) {
test_fail("getsockopt() should have worked");
}
if (option_value != PIPE_BUF) {
test_fail("SO_RCVBUF didn't seem to work.");
}
CLOSE(sd);
debug("leaving test_sockopts()");
}
void test_read(void)
{
int rc;
int fd;
char buf[BUFSIZE];
debug("entering test_read()");
errno = 0;
rc = read(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("read() should have failed with EBADF");
}
fd = open("/tmp", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/tmp\", O_RDONLY) should have worked");
}
CLOSE(fd);
debug("leaving test_read()");
}
void test_write(void)
{
int rc;
char buf[BUFSIZE];
debug("entering test_write()");
errno = 0;
rc = write(-1, buf, sizeof(buf));
if (!(rc == -1 && errno == EBADF)) {
test_fail("write() should have failed with EBADF");
}
debug("leaving test_write()");
}
void test_dup(void)
{
struct stat info1;
struct stat info2;
struct sockaddr_un addr;
int sd, sd2;
int rc;
int i;
debug("entering test_dup()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
debug("Test dup()");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
errno = 0;
sd2 = dup(sd);
if (sd2 == -1) {
test_fail("dup(sd) should have worked");
}
rc = fstat(sd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd2, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (info1.st_ino != info2.st_ino) {
test_fail("dup() failed info1.st_ino != info2.st_ino");
}
CLOSE(sd);
CLOSE(sd2);
debug("Test dup() with a closed socket");
errno = 0;
rc = dup(sd);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(sd) on a closed socket shouldn't have worked");
}
debug("Test dup() with socket descriptor of -1");
errno = 0;
rc = dup(-1);
if (!(rc == -1 && errno == EBADF)) {
test_fail("dup(-1) shouldn't have worked");
}
debug("Test dup() when all of the file descriptors are taken");
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
for (i = 4; i < getdtablesize(); i++) {
rc = open("/dev/null", O_RDONLY);
if (rc == -1) {
test_fail("we couldn't open /dev/null for read");
}
}
errno = 0;
sd2 = dup(sd);
if (!(sd2 == -1 && errno == EMFILE)) {
test_fail("dup(sd) should have failed with errno = EMFILE");
}
for (i = 3; i < getdtablesize(); i++) {
CLOSE(i);
}
UNLINK(TEST_SUN_PATH);
debug("leaving test_dup()");
}
void test_dup2(void)
{
struct stat info1;
struct stat info2;
struct sockaddr_un addr;
int sd;
int fd;
int rc;
debug("entering test_dup2()");
UNLINK(TEST_SUN_PATH);
memset(&addr, '\0', sizeof(struct sockaddr_un));
strncpy(addr.sun_path, TEST_SUN_PATH, sizeof(addr.sun_path) - 1);
addr.sun_family = AF_UNIX;
SOCKET(sd, PF_UNIX, SOCK_STREAM, 0);
rc = bind(sd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
if (rc != 0) {
test_fail("bind() should have worked");
}
fd = open("/dev/null", O_RDONLY);
if (fd == -1) {
test_fail("open(\"/dev/null\", O_RDONLY) failed");
}
fd = dup2(sd, fd);
if (fd == -1) {
test_fail("dup2(sd, fd) failed.");
}
memset(&info1, '\0', sizeof(struct stat));
memset(&info2, '\0', sizeof(struct stat));
rc = fstat(fd, &info1);
if (rc == -1) {
test_fail("fstat(fd, &info1) failed");
}
rc = fstat(sd, &info2);
if (rc == -1) {
test_fail("fstat(sd, &info2) failed");
}
if (!(info1.st_ino == info2.st_ino &&
major(info1.st_dev) == major(info2.st_dev) &&
minor(info1.st_dev) == minor(info2.st_dev))) {
test_fail("dup2() failed");
}
CLOSE(fd);
CLOSE(sd);
UNLINK(TEST_SUN_PATH);
debug("leaving test_dup2()");
}
/*
* A toupper() server. This toy server converts a string to upper case.
*/
void test_xfer_server(pid_t pid)
{
struct ucred credentials;
socklen_t ucred_length;
int i;
int on;
struct timeval tv;
fd_set readfds;
int status;
int rc;
int sd;
char buf[BUFSIZE];
socklen_t client_addr_size;
int client_sd;
0