--- /dev/null
+CPPFLAGS = -DDEBUG -DLOG_LEVEL=LOG_DEBUG -I. -I.. -Iinclude
+#CPPFLAGS = -I. -I.. -I
+CFLAGS = -Wall -g
+
+.PHONY: all clean
+
+all: test
+
+test: test.o test_socket.o test_bind.o test_getsockname.o \
+ test_sendto.o test_recvfrom.o \
+ test_sendmsg.o test_recvmsg.o \
+ test_setsockopt.o test_getsockopt.o \
+ test_close.o test_dummy.o test_common.o
+
+
+clean:
+ -rm -f *~ *.o
+ -rm -f test
--- /dev/null
+/*
+ * swift data structures
+ *
+ * swift struct sockaddr is dubbed struct sockaddr_sw.
+ * swhdr is swift packet header (as delivered on the network).
+ *
+ * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+ */
+
+#ifndef SWIFT_TYPES_
+#define SWIFT_TYPES_ 1
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * 143 is a free IP protocol number (as shown in /etc/protocols
+ * and <netinet/in.h>).
+ */
+
+#define IPPROTO_SWIFT 137
+
+/*
+ * swift address
+ * - IP address (Network Layer)
+ * - file hash (or hash for part of a file)
+ * - a seeder (sender) publishes that hash
+ * - a leecher (receiver) requests that hash
+ * - stands as port number both for sender and receiver
+ */
+
+#define SWIFT_HASH_SIZE 8
+struct sw_state {
+ unsigned int state:4;
+ unsigned int info:4;
+};
+
+struct sw_hash {
+ u_int8_t h_array[SWIFT_HASH_SIZE];
+};
+
+struct sockaddr_sw {
+ __SOCKADDR_COMMON(sin_);
+ struct in_addr sin_addr;
+ struct sw_hash sw_hash;
+
+ /* Pad to size of `struct sockaddr'. */
+ unsigned char sw_zero[sizeof(struct sockaddr) -
+ __SOCKADDR_COMMON_SIZE -
+ sizeof(struct sw_hash) -
+ sizeof(struct in_addr)];
+};
+
+/*
+ * swift header (work in progress)
+ */
+
+struct swhdr {
+ /* file hash (to be seeded or requested) */
+ struct sw_hash base_hash;
+ u_int8_t piece_hash;
+ struct sw_state sock_state;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* SWIFT_TYPES_ */
--- /dev/null
+/*
+ * generic test suite
+ *
+ * test macros and headers
+ */
+
+#ifndef TEST_H_
+#define TEST_H_ 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+#include <string.h>
+
+/* Test function type. */
+typedef void (*test_fn)(void);
+
+/* Run test function f in another process. */
+void run_as_child_process(test_fn f);
+
+/*
+ * uncommend EXIT_IF_FAIL macro in order to stop test execution
+ * at first failed test
+ */
+
+/*#define EXIT_IF_FAIL 1*/
+
+#if defined (EXIT_IF_FAIL)
+#define test_do_fail() \
+ do { \
+ printf("failed\n"); \
+ exit(EXIT_FAILURE); \
+ } while (0)
+#else
+#define test_do_fail() \
+ printf("failed\n")
+#endif
+
+#define test_do_pass() \
+ printf("passed\n")
+
+#define test(test) \
+ do { \
+ size_t i; \
+ int t = (test); \
+ \
+ printf("%s", __FUNCTION__); \
+ fflush(stdout); \
+ \
+ for (i = 0; i < 60 - strlen(__FUNCTION__); i++) \
+ putchar('.'); \
+ \
+ if (!t) \
+ test_do_fail(); \
+ else \
+ test_do_pass(); \
+ \
+ fflush(stdout); \
+ } while (0)
+
+#define start_suite() \
+ do { \
+ printf("\n==== Starting %s ====\n", __FUNCTION__); \
+ } while (0)
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+/*
+ * Header for all swift test functions.
+ */
+
+#ifndef TEST_SW_H_
+#define TEST_SW_H_ 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void test_dummy(void);
+void socket_test_suite(void);
+void bind_test_suite(void);
+void getsockname_test_suite(void);
+void getsockopt_test_suite(void);
+void sendto_test_suite(void);
+void recvfrom_test_suite(void);
+void sendmsg_test_suite(void);
+void recvmsg_test_suite(void);
+void close_test_suite(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+/*
+ * useful structures/macros
+ *
+ * 2011, Operating Systems
+ */
+
+#ifndef UTIL_H_
+#define UTIL_H_ 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined (_WIN32)
+
+#include <windows.h>
+
+static VOID PrintLastError(const PCHAR message)
+{
+ CHAR errBuff[1024];
+
+ FormatMessage(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
+ NULL,
+ GetLastError(),
+ 0,
+ errBuff,
+ sizeof(errBuff) - 1,
+ NULL);
+
+ fprintf(stderr, "%s: %s\n", message, errBuff);
+}
+
+#define ERR(call_description) \
+ do { \
+ fprintf(stderr, "(%s, %d): ", \
+ __FILE__, __LINE__); \
+ PrintLastError(call_description); \
+ } while (0)
+
+#elif defined (__linux__)
+
+/* error printing macro */
+#define ERR(call_description) \
+ do { \
+ fprintf(stderr, "(%s, %d): ", \
+ __FILE__, __LINE__); \
+ perror(call_description); \
+ } while (0)
+
+#else
+ #error "Unknown platform"
+#endif
+
+/* print error (call ERR) and exit */
+#define DIE(assertion, call_description) \
+ do { \
+ if (assertion) { \
+ ERR(call_description); \
+ exit(EXIT_FAILURE); \
+ } \
+ } while(0)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+/*
+ * Test swift. Imports functions from test_sw_* files and runs tests.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+
+static void (*test_fun_array[])(void) = {
+ NULL,
+ test_dummy,
+ socket_test_suite,
+ bind_test_suite,
+ getsockname_test_suite,
+ getsockopt_test_suite,
+ sendto_test_suite,
+ recvfrom_test_suite,
+ sendmsg_test_suite,
+ recvmsg_test_suite,
+ close_test_suite,
+};
+
+static void usage(const char *argv0)
+{
+ fprintf(stderr, "Usage: %s [test_no]\n\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+/*
+ * In case of no arguments call all functions defined in test_fun_array.
+ */
+
+int main(int argc, char **argv)
+{
+ int test_idx;
+
+ /* No arguments: call all test functions. */
+ if (argc == 1) {
+ int i;
+ for (i = 1; i < sizeof(test_fun_array)/sizeof(test_fun_array[0]); i++)
+ test_fun_array[i]();
+ return 0;
+ }
+
+ if (argc != 2)
+ usage(argv[0]);
+
+ test_idx = atoi(argv[1]);
+
+ if (test_idx < 1 || test_idx >= sizeof(test_fun_array)/sizeof(test_fun_array[0])) {
+ fprintf(stderr, "Error: test index %d is out of bounds\n", test_idx);
+ exit(EXIT_FAILURE);
+ }
+
+ test_fun_array[test_idx]();
+
+ return 0;
+}
--- /dev/null
+/*
+ * Test bind "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void bind_dummy(void);
+static void bind_invalid_descriptor(void);
+static void bind_descriptor_not_a_socket(void);
+static void bind_invalid_ip_address(void);
+static void bind_ok(void);
+static void bind_address_in_use(void);
+static void bind_socket_already_bound(void);
+
+void bind_test_suite(void)
+{
+ start_suite();
+ bind_dummy();
+ bind_invalid_descriptor();
+ bind_descriptor_not_a_socket();
+ bind_invalid_ip_address();
+ bind_ok();
+ bind_address_in_use();
+ bind_socket_already_bound();
+}
+
+static void bind_dummy(void)
+{
+ test(1 == 1);
+}
+
+static void bind_invalid_descriptor(void)
+{
+ struct sockaddr_sw addr;
+ int rc;
+
+ rc = bind(-2, (struct sockaddr *) &addr, sizeof(addr));
+
+ test(rc < 0 && errno == EBADF);
+}
+
+static void bind_descriptor_not_a_socket(void)
+{
+ struct sockaddr_sw addr;
+ int fd = dup(STDOUT_FILENO);
+ int rc;
+
+ rc = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
+
+ /*
+ * We are unable to properly handle checking whether the file
+ * descriptor is a socket in the raw socket based implementation.
+ * To be updated when porting to the kernel.
+ */
+ test(rc < 0 && errno == ENOTSOCK);
+}
+
+static void bind_invalid_ip_address(void)
+{
+ struct sockaddr_sw addr;
+ int sockfd;
+ int rc;
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ inet_pton(AF_INET, "254.254.254.254", &addr.sin_addr.s_addr);
+ rc = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
+
+ /*
+ * We are unable to properly handle address validation in the raw
+ * socket based implementation.
+ * To be updated when porting to the kernel.
+ */
+ test(rc < 0 && errno == EADDRNOTAVAIL);
+
+ close(sockfd);
+}
+
+static void bind_ok(void)
+{
+ struct sockaddr_sw addr;
+ int sockfd;
+ int rc;
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = INADDR_ANY;
+ rc = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
+
+ test(rc == 0);
+
+ close(sockfd);
+}
+
+static void bind_address_in_use(void)
+{
+ struct sockaddr_sw addr1, addr2;
+ int sockfd1, sockfd2;
+ int rc;
+
+ sockfd1 = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ memset(&addr1, 0, sizeof(addr1));
+ addr1.sin_family = AF_INET;
+ addr1.sin_addr.s_addr = INADDR_ANY;
+ rc = bind(sockfd1, (struct sockaddr *) &addr1, sizeof(addr1));
+ dprintf("after first bind rc = %d\n", rc);
+
+ sockfd2 = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ memset(&addr2, 0, sizeof(addr2));
+ addr2.sin_family = AF_INET;
+ addr2.sin_addr.s_addr = INADDR_ANY;
+ rc = bind(sockfd2, (struct sockaddr *) &addr2, sizeof(addr2));
+ dprintf("after second bind rc = %d\n", rc);
+
+ test(rc < 0 && errno == EADDRINUSE);
+
+ close(sockfd1);
+ close(sockfd2);
+}
+
+static void bind_socket_already_bound(void)
+{
+ struct sockaddr_sw addr1, addr2;
+ int sockfd;
+ int rc;
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ memset(&addr1, 0, sizeof(addr1));
+ addr1.sin_family = AF_INET;
+ addr1.sin_addr.s_addr = INADDR_ANY;
+ rc = bind(sockfd, (struct sockaddr *) &addr1, sizeof(addr1));
+
+ memset(&addr2, 0, sizeof(addr2));
+ addr2.sin_family = AF_INET;
+ addr2.sin_addr.s_addr = INADDR_ANY;
+ addr2.sw_hash.h_array[0] = 0xFF; /* chage hash ("port") */
+ rc = bind(sockfd, (struct sockaddr *) &addr2, sizeof(addr2));
+
+ test(rc < 0 && errno == EINVAL);
+
+ close(sockfd);
+}
--- /dev/null
+/*
+ * Test close "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void close_dummy(void);
+static void close_invalid_descriptor(void);
+static void close_descriptor_is_not_a_socket(void);
+static void close_ok_descriptor_is_not_bound(void);
+static void close_ok_descriptor_is_bound(void);
+
+void close_test_suite(void)
+{
+ start_suite();
+ close_dummy();
+ close_invalid_descriptor();
+ close_descriptor_is_not_a_socket();
+ close_ok_descriptor_is_not_bound();
+ close_ok_descriptor_is_bound();
+}
+
+static void close_dummy(void)
+{
+ test(1 == 1);
+}
+
+/* Pass invalid file descriptor to close. */
+static void close_invalid_descriptor(void)
+{
+ int rc;
+
+ rc = close(-1);
+
+ test(rc < 0 && errno == EBADF);
+}
+
+/* Pass a duplicate of standard output to close. */
+static void close_descriptor_is_not_a_socket(void)
+{
+ int fd;
+ int rc;
+
+ fd = dup(STDOUT_FILENO);
+ DIE(fd < 0, "dup");
+
+ rc = close(fd);
+
+ test(rc < 0 && errno == EBADF);
+
+ close(fd);
+}
+
+/* Pass a non-bound socket. */
+static void close_ok_descriptor_is_not_bound(void)
+{
+ int s;
+ int rc;
+
+ s = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ DIE(s < 0, "socket");
+
+ rc = close(s);
+
+ test(rc == 0);
+}
+
+/* Pass a bound socket. */
+static void close_ok_descriptor_is_bound(void)
+{
+ int s;
+ int rc;
+ struct sockaddr_sw addr;
+
+ s = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ DIE(s < 0, "socket");
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = INADDR_ANY;
+ rc = bind(s, (struct sockaddr *) &addr, sizeof(addr));
+ DIE(rc < 0, "bind");
+
+ rc = close(s);
+
+ test(rc == 0);
+}
--- /dev/null
+/*
+ * Test suite functions.
+ *
+ * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#include "util.h"
+#include "test.h"
+
+/* Run test function f in another process. */
+void run_as_child_process(test_fn f)
+{
+ pid_t pid;
+ int status;
+ int rc;
+
+ pid = fork();
+ switch (pid) {
+ case -1: /* error */
+ ERR("fork");
+ exit(EXIT_FAILURE);
+
+ case 0: /* child process */
+ /* Run test function. */
+ f();
+ exit(EXIT_SUCCESS);
+ break;
+
+ default: /* parent process */
+ break;
+ }
+
+ /* Wait for child process. */
+ rc = waitpid(pid, &status, 0);
+ DIE(rc < 0, "waitpid");
+}
--- /dev/null
+/*
+ * Dummy test functions.
+ */
+
+#include "test.h"
+
+static void dummy_1_eq_1(void);
+static void dummy_1_neq_0(void);
+
+void test_dummy(void)
+{
+ start_suite();
+ dummy_1_eq_1();
+ dummy_1_neq_0();
+}
+
+static void dummy_1_eq_1(void)
+{
+ test(1 == 1);
+}
+
+static void dummy_1_neq_0(void)
+{
+ test(1 != 0);
+}
--- /dev/null
+/*
+ * Test getsockname "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void getsockname_dummy(void);
+static void getsockname_invalid_descriptor(void);
+static void getsockname_descriptor_not_a_socket(void);
+static void getsockname_invalid_len(void);
+static void getsockname_ok(void);
+
+void getsockname_test_suite(void)
+{
+ start_suite();
+ getsockname_dummy();
+}
+
+static void getsockname_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test getsockopt "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void getsockopt_dummy(void);
+
+void getsockopt_test_suite(void)
+{
+ start_suite();
+ getsockopt_dummy();
+}
+
+static void getsockopt_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test recvfrom "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void recvfrom_dummy(void);
+static void recvfrom_invalid_descriptor(void);
+static void recvfrom_descriptor_is_not_a_socket(void);
+static void recvfrom_socket_is_not_bound(void);
+static void recvfrom_after_sendto_ok(void);
+static void recvfrom_after_sendmsg_ok(void);
+
+// Additional function
+static void fill_sockaddr_sw(struct sockaddr_sw *local_addr, struct sockaddr_sw *remote_addr, char * local_address, char * hash, char * dest_address);
+
+void recvfrom_test_suite(void)
+{
+ start_suite();
+ recvfrom_dummy();
+ recvfrom_invalid_descriptor();
+ recvfrom_descriptor_is_not_a_socket();
+ recvfrom_socket_is_not_bound();
+ recvfrom_after_sendto_ok();
+ recvfrom_after_sendmsg_ok();
+}
+
+static void recvfrom_dummy(void)
+{
+ test(1 == 1);
+}
+
+static void recvfrom_invalid_descriptor(void)
+{
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_recv;
+ char buffer[BUFSIZ];
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_recv = recvfrom(-1, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test(bytes_recv < 0 && errno == EBADF);
+}
+
+static void recvfrom_descriptor_is_not_a_socket(void)
+{
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_recv;
+ char buffer[BUFSIZ];
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_recv = recvfrom(1, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test(bytes_recv < 0 && errno == ENOTSOCK);
+}
+
+static void recvfrom_socket_is_not_bound(void)
+{
+ int sockfd;
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_recv;
+ char buffer[BUFSIZ];
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ DIE(sockfd < 0, "socket");
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_recv = recvfrom(sockfd, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test( bytes_recv < 0 && errno == EAFNOSUPPORT );
+}
+
+static void recvfrom_after_sendto_ok(void)
+{
+ test ( 0 == 1 );
+}
+
+static void recvfrom_after_sendmsg_ok(void)
+{
+ test ( 0 == 1 );
+}
+
+static void fill_sockaddr_sw(struct sockaddr_sw *local_addr, struct sockaddr_sw *remote_addr, char * local_address, char * hash, char * dest_address)
+{
+ local_addr->sin_addr.s_addr = htonl((int)local_address);
+ memcpy(&local_addr->sw_hash, hash, sizeof(struct sw_hash));
+
+ remote_addr->sin_addr.s_addr = htonl((int)dest_address);
+ memcpy(&remote_addr->sw_hash, hash, sizeof(struct sw_hash));
+}
--- /dev/null
+/*
+ * Test recvmsg "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void recvmsg_dummy(void);
+static void recvmsg_invalid_descriptor(void);
+static void recvmsg_descriptor_is_not_a_socket(void);
+static void recvmsg_socket_is_not_bound(void);
+static void recvmsg_after_sendto_ok(void);
+static void recvmsg_after_sendmsg_ok(void);
+
+void recvmsg_test_suite(void)
+{
+ start_suite();
+ recvmsg_dummy();
+
+ recvmsg_invalid_descriptor();
+ recvmsg_descriptor_is_not_a_socket();
+ recvmsg_socket_is_not_bound();
+ recvmsg_after_sendto_ok();
+ recvmsg_after_sendmsg_ok();
+}
+
+static void recvmsg_dummy(void)
+{
+ test(1 == 1);
+}
+
+static void recvmsg_invalid_descriptor(void)
+{
+ test (1 == 0);
+}
+static void recvmsg_descriptor_is_not_a_socket(void)
+{
+ test (1 == 0);
+}
+static void recvmsg_socket_is_not_bound(void)
+{
+ test (1 == 0);
+}
+static void recvmsg_after_sendto_ok(void)
+{
+ test (1 == 0);
+}
+static void recvmsg_after_sendmsg_ok(void)
+{
+ test (1 == 0);
+}
--- /dev/null
+/*
+ * Test sendmsg "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void sendmsg_dummy(void);
+static void sendmsg_invalid_descriptor(void);
+static void sendmsg_descriptor_is_not_socket(void);
+static void sendmsg_socket_is_not_bound(void);
+static void sendmsg_ok(void);
+
+void sendmsg_test_suite(void)
+{
+ start_suite();
+ sendmsg_dummy();
+ sendmsg_invalid_descriptor();
+ sendmsg_descriptor_is_not_socket();
+ sendmsg_socket_is_not_bound();
+ sendmsg_ok();
+}
+
+static void sendmsg_dummy(void)
+{
+ test (1 == 1);
+}
+
+static void sendmsg_invalid_descriptor(void)
+{
+ test (1 == 0);
+}
+
+static void sendmsg_descriptor_is_not_socket(void)
+{
+ test (1 == 0);
+}
+
+static void sendmsg_socket_is_not_bound(void)
+{
+ test (1 == 0);
+}
+
+static void sendmsg_ok(void)
+{
+ test (1 == 0);
+}
--- /dev/null
+/*
+ * Test sendto "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void sendto_dummy(void);
+static void sendto_invalid_descriptor(void);
+static void sendto_descriptor_is_not_socket(void);
+static void sendto_socket_is_not_bound(void);
+static void sendto_ok(void);
+
+// Additional function
+static void fill_sockaddr_sw(struct sockaddr_sw *local_addr, struct sockaddr_sw *remote_addr, char * local_address, char * hash, char * dest_address);
+
+void sendto_test_suite(void)
+{
+ start_suite();
+ sendto_dummy();
+
+ sendto_invalid_descriptor();
+ sendto_descriptor_is_not_socket();
+ sendto_socket_is_not_bound();
+ sendto_ok();
+}
+
+static void sendto_dummy(void)
+{
+ test(1 == 1);
+}
+
+static void sendto_invalid_descriptor()
+{
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_sent;
+ char buffer[BUFSIZ];
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_sent = sendto(-1, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test(bytes_sent < 0 && errno == EBADF);
+}
+
+static void sendto_descriptor_is_not_socket(void)
+{
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_sent;
+ char buffer[BUFSIZ];
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_sent = sendto(1, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test(bytes_sent < 0 && errno == ENOTSOCK);
+}
+
+static void sendto_socket_is_not_bound(void)
+{
+ int sockfd;
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_sent;
+ char buffer[BUFSIZ];
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ DIE(sockfd < 0, "socket");
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+ bytes_sent = sendto(sockfd, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ test( bytes_sent < 0 && errno == EAFNOSUPPORT );
+}
+
+static void sendto_ok(void)
+{
+ int sockfd;
+ struct sockaddr_sw local_addr;
+ struct sockaddr_sw remote_addr;
+ ssize_t bytes_sent;
+ char buffer[BUFSIZ];
+ int rc;
+
+ fill_sockaddr_sw(&local_addr, &remote_addr, "127.0.0.1", "myHash", "127.0.0.1");
+
+ sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ DIE(sockfd < 0, "socket");
+
+ rc = bind(sockfd, (struct sockaddr *) &local_addr, sizeof(local_addr));
+ DIE(rc < 0, "bind");
+
+ bytes_sent = sendto(sockfd, buffer, BUFSIZ, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
+
+ perror("sendto");
+ test( bytes_sent >= 0 );
+
+}
+
+static void fill_sockaddr_sw(struct sockaddr_sw *local_addr, struct sockaddr_sw *remote_addr, char * local_address, char * hash, char * dest_address)
+{
+ inet_pton(PF_INET, local_address, &(local_addr->sin_addr));
+ memcpy(&local_addr->sw_hash, hash, sizeof(struct sw_hash));
+
+ inet_pton(PF_INET, dest_address, &(remote_addr->sin_addr));
+ memcpy(&remote_addr->sw_hash, hash, sizeof(struct sw_hash));
+}
--- /dev/null
+/*
+ * Test sw_setsockopt "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+void setsockopt_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test socket "syscall".
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
+
+#include "swift_types.h"
+
+#include "debug.h"
+#include "util.h"
+
+#include "test_sw.h"
+#include "test.h"
+
+static void socket_dummy(void);
+static void socket_invalid_domain(void);
+static void socket_invalid_type(void);
+static void socket_invalid_protocol(void);
+static void socket_insufficient_file_descriptors(void);
+static void socket_ok(void);
+
+void socket_test_suite(void)
+{
+ start_suite();
+ socket_dummy();
+ socket_invalid_domain();
+ socket_invalid_type();
+ socket_invalid_protocol();
+ run_as_child_process(socket_insufficient_file_descriptors);
+ socket_ok();
+}
+
+/* Dummy function for testing purposes only. */
+static void socket_dummy(void)
+{
+ test(1 == 1);
+}
+
+/* Use invalid domain when calling socket. */
+static void socket_invalid_domain(void)
+{
+ int rc;
+
+ rc = socket(PF_UNIX, SOCK_DGRAM, IPPROTO_SWIFT);
+
+ test(rc < 0 && errno == EINVAL);
+}
+
+/* Use invalid type when calling socket. */
+static void socket_invalid_type(void)
+{
+ int rc;
+
+ rc = socket(PF_INET, SOCK_STREAM, IPPROTO_SWIFT);
+
+ test(rc < 0 && errno == EINVAL);
+}
+
+/* Use invalid protocol when calling socket. */
+static void socket_invalid_protocol(void)
+{
+ int rc;
+
+ rc = socket(PF_INET, SOCK_DGRAM, -1);
+
+ test(rc < 0 && errno == EINVAL);
+}
+
+/*
+ * Use dup to fill the number of file descriptors for current process.
+ * Calling socket must result in error.
+ *
+ * File descriptors are not closed. Test processes must be restarted.
+ */
+static void socket_insufficient_file_descriptors(void)
+{
+ int fd;
+ int rc;
+
+ while (1) {
+ /* Duplicate standard output. */
+ fd = dup(STDOUT_FILENO);
+ if (fd < 0)
+ break;
+ }
+
+ rc = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+
+ dprintf("errno = %d\n", errno);
+ test(rc < 0 && errno == EMFILE);
+}
+
+/* Valid call of socket. */
+static void socket_ok(void)
+{
+ int rc;
+
+ rc = socket(PF_INET, SOCK_DGRAM, IPPROTO_SWIFT);
+ dprintf("rc = %d, errno = %d\n", rc, errno);
+
+ test(rc > 0);
+}