summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--socket.c40
-rw-r--r--testsuite.c1
2 files changed, 41 insertions, 0 deletions
diff --git a/socket.c b/socket.c
new file mode 100644
index 0000000..5cce69e
--- /dev/null
+++ b/socket.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <sys/time.h>
+
+#define TEST2(c, l, ...) ((c) ? 1 : \
+(err++,printf(__FILE__":"#l": "#c" failed: " __VA_ARGS__),putchar('\n'),0))
+#define TEST1(c, l, ...) TEST2(c, l, __VA_ARGS__)
+#define TEST(c, ...) TEST1(c, __LINE__, __VA_ARGS__)
+
+#define TESTE(c) TEST(c, "errno = %s", strerror(errno))
+
+int test_socket(void)
+{
+ int err;
+ struct sockaddr_in sa = { .sin_family = AF_INET };
+ int s, c;
+ char buf[100];
+
+ TESTE((s=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
+ TESTE(bind(s, (void *)&sa, sizeof sa)==0);
+ TESTE(getsockname(s, (void *)&sa, (socklen_t[]){sizeof sa})==0);
+
+ TESTE(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
+ &(struct timeval){.tv_usec=1}, sizeof(struct timeval))==0);
+
+ TESTE((c=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP))>=0);
+ sa.sin_addr.s_addr = htonl(0x7f000001);
+ TESTE(sendto(c, "x", 1, 0, (void *)&sa, sizeof sa)==1);
+ TESTE(recvfrom(s, buf, sizeof buf, 0, (void *)&sa, (socklen_t[]){sizeof sa})==1);
+ TEST(buf[0]=='x', "'%c'", buf[0]);
+
+ close(c);
+ close(s);
+
+ return err;
+}
diff --git a/testsuite.c b/testsuite.c
index c353735..82b2e3e 100644
--- a/testsuite.c
+++ b/testsuite.c
@@ -17,6 +17,7 @@ int main()
RUN_TEST(fnmatch);
RUN_TEST(fscanf);
RUN_TEST(popen);
+ RUN_TEST(socket);
RUN_TEST(spawn);
RUN_TEST(qsort);
RUN_TEST(time);