summaryrefslogtreecommitdiff
path: root/tests/sockbind.c
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2011-10-07 17:22:09 +0100
committerRichard Maw <richard.maw@codethink.co.uk>2011-10-10 10:33:12 +0100
commitcdb2882dc33b7bd1ee3fba2190cc17fef324050d (patch)
tree1f06d7dd2c762cf147a7eff3a87473b2968d6edd /tests/sockbind.c
parentf083ad60f671c0d058668c64ff2bb9ca8fad6fb2 (diff)
downloadtbdiff-cdb2882dc33b7bd1ee3fba2190cc17fef324050d.tar.gz
Added the ability to test the return code of create and deploy
So expected failures can be made by comparing the return code to the expected Also socket tests have been added, add and diff expect create to fail for now The only sensible operation on sockets is to remove them as they need a program to bind them and that program (or a child) needs to be a server So tbdiff should fail if it has to perform such an act. It may be worth having an option to ignore the change, but for now it's better to fail and let the user know why so they can fix it
Diffstat (limited to 'tests/sockbind.c')
-rw-r--r--tests/sockbind.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/sockbind.c b/tests/sockbind.c
new file mode 100644
index 0000000..f98b5ed
--- /dev/null
+++ b/tests/sockbind.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#define MAX_CLIENTS 1
+#ifdef DEBUG
+#define DEBUGPRINT(fmt, ...) fprintf(stderr, "DEBUG: %s %s %d: " fmt, \
+ __FILE__, __func__, __LINE__, __VA_ARGS__)
+#else
+#define DEBUGPRINT(...) (void)0
+#endif
+
+int main(int argc, char *argv[]){
+ struct sockaddr_un sock = {
+ .sun_family = AF_UNIX
+ };
+ int sfd;
+ if (argc < 1){
+ fprintf(stderr, "Usage: %s PATH\n", argv[0]);
+ return 1;
+ }
+ strncpy(sock.sun_path, argv[1], sizeof(sock) - offsetof(struct sockaddr_un, sun_path));
+ DEBUGPRINT("%s", "Constructed socket address\n");
+ if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1){
+ perror("socket");
+ return 2;
+ }
+ DEBUGPRINT("Created socket fd=%d\n", sfd);
+ if (bind(sfd, (struct sockaddr*)&sock, sizeof(sock)) == -1){
+ perror("bind");
+ return 3;
+ }
+ if (listen(sfd, MAX_CLIENTS) == -1){
+ perror("listen");
+ return 4;
+ }
+ DEBUGPRINT("Listening to %d clients\n", MAX_CLIENTS);
+ {
+ struct sockaddr_un client_address;
+ socklen_t client_size = sizeof(client_address);
+ int cfd;
+ while ((cfd = accept(sfd, (struct sockaddr*)&client_address,
+ &client_size)) != -1) {
+ char buf[BUFSIZ];
+ ssize_t rdcount = -1;
+ DEBUGPRINT("Listening to client fd=%d\n", cfd);
+ while ((rdcount = read(cfd, buf, sizeof(buf))) > 0) {
+ DEBUGPRINT("Read %zi bytes from client, "
+ "message was:\n%.*s", rdcount,
+ rdcount, buf);
+ write(STDOUT_FILENO, buf, rdcount);
+ }
+ assert(rdcount == 0 || rdcount == -1);
+ if (rdcount == -1) {
+ perror("read");
+ return 5;
+ }
+ DEBUGPRINT("Finished listening to fd=%d\n", cfd);
+ close(cfd);
+ }
+ }
+
+ close(sfd);
+ unlink(argv[1]);
+ return 0;
+}