--- /dev/null
+#CPPFLAGS = -DDEBUG -DLOG_LEVEL=LOG_DEBUG
+CFLAGS = -Wall -g
+
+.PHONY: all clean
+
+all: test
+
+test: test.o test_sw_socket.o test_sw_bind.o test_sw_getsockname.o \
+ test_sw_sendto.o test_sw_recvfrom.o test_sw_sendmsg.o \
+ test_sw_recvmsg.o test_sw_setsockopt.o test_sw_getsockopt.o \
+ test_sw_shutdown.o test_sw_close.o test_dummy.o
+
+clean:
+ -rm -f *~ *.o
+ -rm -f test
--- /dev/null
+/*
+ * debugging macros
+ * heavily inspired by previous work and Internet resources
+ *
+ * uses C99 variadic macros
+ * uses non-standard usage of the token-paste operator (##) for
+ * removing the comma symbol (,) when not followed by a token
+ * uses non-standard __FUNCTION__ macro (MSVC doesn't support __func__)
+ * tested on gcc 4.4.5 and Visual Studio 2008 (9.0), compiler version 15.00
+ *
+ * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+ */
+
+#ifndef DEBUG_H_
+#define DEBUG_H_ 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+
+/* log levels */
+enum {
+ LOG_EMERG = 1,
+ LOG_ALERT,
+ LOG_CRIT,
+ LOG_ERR,
+ LOG_WARNING,
+ LOG_NOTICE,
+ LOG_INFO,
+ LOG_DEBUG
+};
+
+/*
+ * initialize default loglevel (for dlog)
+ * may be redefined in the including code
+ */
+
+#ifndef LOG_LEVEL
+#define LOG_LEVEL LOG_WARNING
+#endif
+
+/*
+ * define DEBUG macro as a compiler option:
+ * -DDEBUG for GCC
+ * /DDEBUG for MSVC
+ */
+
+#if defined DEBUG
+#define dprintf(format, ...) \
+ fprintf(stderr, " [%s(), %s:%u] " format, \
+ __FUNCTION__, __FILE__, __LINE__, \
+ ##__VA_ARGS__)
+#else
+#define dprintf(format, ...) \
+ do { \
+ } while (0)
+#endif
+
+#if defined DEBUG
+#define dlog(level, format, ...) \
+ do { \
+ if (level <= LOG_LEVEL) \
+ dprintf(format, ##__VA_ARGS__); \
+ } while (0)
+#else
+#define dlog(level, format, ...) \
+ do { \
+ } while (0)
+#endif
+
+#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,
+ dummy_1_eq_1,
+ dummy_1_neq_0,
+ socket_dummy,
+ bind_dummy,
+ getsockname_dummy,
+ sendto_dummy,
+ recvfrom_dummy,
+ sendmsg_dummy,
+ recvmsg_dummy,
+ shutdown_dummy,
+ close_dummy,
+};
+
+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
+/*
+ * 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>
+
+/*
+ * 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)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+/*
+ * Dummy test functions.
+ */
+
+#include "test.h"
+
+void dummy_1_eq_1(void)
+{
+ test(1 == 1);
+}
+
+void dummy_1_neq_0(void)
+{
+ test(1 != 0);
+}
--- /dev/null
+/*
+ * Header for all swift test functions.
+ */
+
+#ifndef TEST_SW_H_
+#define TEST_SW_H_ 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void dummy_1_eq_1(void);
+void dummy_1_neq_0(void);
+void socket_dummy(void);
+void bind_dummy(void);
+void getsockname_dummy(void);
+void sendto_dummy(void);
+void recvfrom_dummy(void);
+void sendmsg_dummy(void);
+void recvmsg_dummy(void);
+void shutdown_dummy(void);
+void close_dummy(void);
+
+/* TODO: fill with test function headers. */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--- /dev/null
+/*
+ * Test sw_bind "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void bind_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test sw_close "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void close_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test sw_getsockname "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void getsockname_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test sw_getsockopt "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void getsockopt_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test sw_recvfrom "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void recvfrom_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test sw_recvmsg "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void recvmsg_dummy(void)
+{
+ test(1 == 1);
+}
+
--- /dev/null
+/*
+ * Test sw_sendmsg "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void sendmsg_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test sw_sendto "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void sendto_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test sw_setsockopt "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void setsockopt_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test sw_shutdown "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void shutdown_dummy(void)
+{
+ test(1 == 1);
+}
--- /dev/null
+/*
+ * Test sw_socket "syscall".
+ */
+
+#include "test_sw.h"
+#include "test.h"
+
+void socket_dummy(void)
+{
+ test(1 == 1);
+}
--- /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