]> git.ozlabs.org Git - ccan-lca-2011.git/commitdiff
lca2011: simpler testing by not having oserver_serve exit.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 21 Jan 2011 03:40:45 +0000 (14:10 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 21 Jan 2011 03:40:45 +0000 (14:10 +1030)
This makes it easier to test error paths, too.

ccan/oserver/_info
ccan/oserver/oserver.c
ccan/oserver/oserver.h
ccan/oserver/test/run.c

index a05fa035f169a7a11923c79d0be639fcc18e10f8..b906502beba1d4579f1fdac53ab6ed3a226fb1ae 100644 (file)
@@ -25,7 +25,9 @@
  *             if (fd < 0)
  *                     err(1, "Accepting connection on TCP socket");
  *
- *             oserver_serve(fd);
+ *             if (!oserver_serve(fd))
+ *                     err(1, "Serving client");
+ *             exit(0);
  *     }
  */
 int main(int argc, char *argv[])
@@ -35,7 +37,6 @@ int main(int argc, char *argv[])
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
-               printf("ccan/compiler\n");
                printf("ccan/read_write_all\n");
                printf("ccan/str\n");
                printf("ccan/foreach\n");
index 5a40ff98cce90c8c96908da83ff497d1bed921d1..5f120cbf2bd4342a3ae34cc7ee8a57e74ebc16fa 100644 (file)
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
-void oserver_serve(int fd)
+bool oserver_serve(int fd)
 {
        char buf[1024];
        unsigned int i;
 
        for (i = 0; i < sizeof(buf)-1; i++) {
-               if (read(fd, &buf[i], 1) != 1)
-                       errx(1, "Client disconnected");
+               if (read(fd, &buf[i], 1) != 1) {
+                       errno = EIO;
+                       return false;
+               }
                if (buf[i] == '\n' || buf[i] == '\r')
                        break;
                buf[i] = toupper(buf[i]);
@@ -29,8 +32,8 @@ void oserver_serve(int fd)
                       strlen("Louder, like this: '"))
            || !write_all(fd, buf, i)
            || !write_all(fd, "'\r\n", strlen("'\r\n")))
-               err(1, "Write failed");
-       exit(0);
+               return false;
+       return true;
 }
 
 int oserver_setup(void)
index bf2b0ead5b7c362f09f2688ea89ab82bb492fb14..d727560cd76455d092acd3db9a6e06adbd16add7 100644 (file)
@@ -1,6 +1,6 @@
 #ifndef CCAN_OSERVER_H
 #define CCAN_OSERVER_H
-#include <ccan/compiler/compiler.h>
+#include <stdbool.h>
 
 /**
  * oserver_setup - get a listening filedescriptor for an oserver
@@ -21,7 +21,7 @@ int oserver_setup(void);
  * oserver_serve - serve an oserver client via a file descriptor
  * @fd: the file descriptor (usually a connected socket)
  *
- * This does not return, but exits with status 0 if served OK.
+ * This returns false (with errno set) on failure.
  *
  * Example:
  *      #include <sys/types.h>
@@ -33,11 +33,11 @@ int oserver_setup(void);
  *             if (clientfd < 0)
  *                     err(1, "Accepting connection from client");
  *             if (fork() == 0)
- *                     oserver_serve(clientfd);
+ *                     exit(oserver_serve(clientfd) ? 0 : 1);
  *             else
  *                     close(clientfd);
  */
-void NORETURN oserver_serve(int fd);
+bool oserver_serve(int fd);
 
 #define OSERVER_PORT 2727
 #endif /* CCAN_OSERVER_H */
index dc6031b9de547e715a4e6f54c80389afcc62fc4c..ee3959ec10ce888b5281bad6e61b6f326240d9a4 100644 (file)
 int main(void)
 {
        int fd;
-       int status;
        char buf[200];
        const char *input;
 
        /* This is how many tests you plan to run */
-       plan_tests(4 * 6);
+       plan_tests(3 * 6);
 
        foreach_ptr(input,
                    "This is a test\n",
@@ -31,13 +30,7 @@ int main(void)
                write(fd, input, strlen(input));
                lseek(fd, 0, SEEK_SET);
 
-               if (fork() == 0)
-                       oserver_serve(fd);
-
-               wait(&status);
-
-               ok1(WIFEXITED(status));
-               ok1(WEXITSTATUS(status) == 0);
+               ok1(oserver_serve(fd));
 
                lseek(fd, 0, SEEK_SET);
                buf[read(fd, buf, sizeof(buf)-1)] = '\0';