2016-09-15 16:28:52 -07:00
|
|
|
#include "greatest.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
2016-09-18 13:35:43 -07:00
|
|
|
#include <inttypes.h>
|
2016-09-15 16:28:52 -07:00
|
|
|
|
2016-09-18 13:35:43 -07:00
|
|
|
#include "io.h"
|
2016-09-15 16:28:52 -07:00
|
|
|
|
2016-09-18 13:35:43 -07:00
|
|
|
SUITE(io_tests);
|
2016-09-15 16:28:52 -07:00
|
|
|
|
|
|
|
TEST ipc_socket_test(void) {
|
2016-09-18 13:35:43 -07:00
|
|
|
const char *socket_pathname = "test-socket";
|
2016-09-15 16:28:52 -07:00
|
|
|
int socket_fd = bind_ipc_sock(socket_pathname);
|
|
|
|
ASSERT(socket_fd >= 0);
|
|
|
|
|
2016-09-18 13:35:43 -07:00
|
|
|
char *test_string = "hello world";
|
|
|
|
char *test_bytes = "another string";
|
2016-09-15 16:28:52 -07:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
close(socket_fd);
|
|
|
|
socket_fd = connect_ipc_sock(socket_pathname);
|
|
|
|
ASSERT(socket_fd >= 0);
|
2016-09-18 13:35:43 -07:00
|
|
|
write_string(socket_fd, test_string);
|
|
|
|
write_bytes(socket_fd, (uint8_t *) test_bytes, strlen(test_bytes));
|
2016-09-15 16:28:52 -07:00
|
|
|
close(socket_fd);
|
2016-09-17 15:15:18 -07:00
|
|
|
exit(0);
|
2016-09-15 16:28:52 -07:00
|
|
|
} else {
|
2016-09-17 15:15:18 -07:00
|
|
|
int client_fd = accept_client(socket_fd);
|
|
|
|
ASSERT(client_fd >= 0);
|
2016-09-18 13:35:43 -07:00
|
|
|
char *message = read_string(client_fd);
|
2016-09-15 16:28:52 -07:00
|
|
|
ASSERT(message != NULL);
|
|
|
|
ASSERT_STR_EQ(test_string, message);
|
|
|
|
free(message);
|
2016-09-18 13:35:43 -07:00
|
|
|
int64_t len;
|
|
|
|
uint8_t *bytes;
|
|
|
|
read_bytes(client_fd, &bytes, &len);
|
|
|
|
ASSERT(memcmp(test_bytes, bytes, len) == 0);
|
2016-09-17 15:15:18 -07:00
|
|
|
close(client_fd);
|
2016-09-15 16:28:52 -07:00
|
|
|
close(socket_fd);
|
|
|
|
unlink(socket_pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2016-09-18 13:35:43 -07:00
|
|
|
SUITE(io_tests) {
|
2016-09-15 16:28:52 -07:00
|
|
|
RUN_TEST(ipc_socket_test);
|
|
|
|
}
|
|
|
|
|
|
|
|
GREATEST_MAIN_DEFS();
|
|
|
|
|
2016-09-18 13:35:43 -07:00
|
|
|
int main(int argc, char **argv) {
|
2016-09-15 16:28:52 -07:00
|
|
|
GREATEST_MAIN_BEGIN();
|
2016-09-18 13:35:43 -07:00
|
|
|
RUN_SUITE(io_tests);
|
2016-09-15 16:28:52 -07:00
|
|
|
GREATEST_MAIN_END();
|
|
|
|
}
|