test.c

00001 #include <err.h>
00002 #include <signal.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <sys/types.h>
00007 #include <sys/socket.h>
00008 #include <unistd.h>
00009 
00010 #include "pnotify.h"
00011 #include "pnotify-internal.h"
00012 
00013 /* Global context variable */
00014 struct pnotify_ctx *ctx;
00015 
00016 /* Compare a pnotify_event against an expected set of values */
00017 int
00018 event_cmp(struct pnotify_event *ev, int wd, int mask, const char *name)
00019 {
00020         int i = (ev->watch->wd == wd) &&
00021                 (ev->mask == mask);
00022                 
00023         if (name) {
00024                 i = i && (strcmp(ev->name, name) == 0);
00025         }
00026 
00027         if (!i) {
00028                 printf(" *ERROR * mismatch: expecting (wd:mask:name) of '%d:%d:%s' but got '%d:%d:%s'\n",
00029                           wd, mask, name,
00030                           ev->watch->wd, ev->mask, ev->name);
00031                 pnotify_print_event(ev);
00032         }
00033         return i;
00034 }
00035 
00036 #define test(x) do { \
00037    printf(" * " #x ": ");                               \
00038    fflush(stdout);                                      \
00039    printf("%s\n", ((x) >= 0) ? "passed" : "failed");    \
00040 } while (0)
00041 
00042 static void
00043 test_signals()
00044 {
00045         struct pnotify_event    evt;
00046 
00047         printf("signal tests\n");
00048         test (pnotify_trap_signal(SIGUSR1, NULL, NULL));
00049         test (kill(getpid(), SIGUSR1));
00050         test (pnotify_get_event(&evt, ctx));
00051         if (!event_cmp(&evt, SIGUSR1, PN_SIGNAL, NULL)) 
00052                 err(1, "unexpected event value");
00053         printf("signal tests complete\n");
00054 }
00055 
00056 static void
00057 test_fd()
00058 {
00059         struct pnotify_event evt;
00060         int wd, fildes[2];
00061 
00062         printf("fd tests\n");
00063         test (pipe(fildes));
00064         test ((wd = pnotify_watch_fd(fildes[0], PN_READ, NULL, NULL)));
00065         if (write(fildes[1], "a", 1) != 1)
00066                 err(1, "write(2)");
00067         test (pnotify_get_event(&evt, ctx));
00068         if (!event_cmp(&evt, wd, PN_READ, NULL)) 
00069                 err(1, "unexpected event value");
00070         printf("fd tests complete\n");
00071 }
00072 
00073 static void
00074 test_timer()
00075 {
00076         struct pnotify_event evt;
00077         int wd;
00078 
00079         printf("timer tests\n");
00080         test ((wd = pnotify_set_timer(1, PN_DEFAULT, NULL, NULL)));
00081         test (pnotify_get_event(&evt, ctx));
00082         printf("timer tests complete\n");
00083 }
00084 
00085 static void 
00086 test_callback(int signum)
00087 {
00088         printf("signum %d\n");
00089 }
00090 
00091 static void
00092 test_dispatch()
00093 {
00094         test (pnotify_set_timer(1, PN_DEFAULT, test_callback, NULL));
00095         test (pnotify_dispatch());
00096 }
00097 
00098 int
00099 main(int argc, char **argv)
00100 {
00101         struct pnotify_event    evt;
00102         int wd;
00103 
00104         /* Create a test directory */
00105         (void) system("rm -rf .check");
00106         if (system("mkdir .check") < 0)
00107                 err(1, "mkdir failed");
00108         if (system("mkdir .check/dir") < 0)
00109                 err(1, "mkdir failed");
00110 
00111         /* Initialize the queue */
00112         test(ctx = pnotify_init());
00113 
00114         test_signals();
00115         test_fd();
00116         test_timer();
00117         /* Disabled: test_dispatch(); */
00118 
00119         /* Watch for events in the test directory */
00120         test((wd = pnotify_watch_vnode(".check", PN_CREATE | PN_DELETE | PN_MODIFY, NULL, NULL))); 
00121 
00122         /* Create a new file */
00123         test (system("touch .check/foo"));
00124 
00125         /* Read the event */
00126         test (pnotify_get_event(&evt, ctx)); 
00127         if (!event_cmp(&evt, 1, PN_CREATE, "foo")) 
00128                 err(1, "unexpected event value");
00129 
00130         /* Create a new file #2 */
00131         test (system("touch .check/bar"));
00132 
00133         /* Read the event */
00134         test (pnotify_get_event(&evt, ctx));
00135         if (!event_cmp(&evt, 1, PN_CREATE, "bar")) 
00136                 err(1, "unexpected event value");
00137 
00138         /* Delete the new file */
00139         test (system("rm .check/foo"));
00140 
00141         /* Read the delete event */
00142         test (pnotify_get_event(&evt, ctx));
00143         if (!event_cmp(&evt, 1, PN_DELETE, "foo")) 
00144                 err(1, "unexpected event value");
00145 
00146         /* Modify file #2 */
00147         test (system("echo hi >> .check/bar"));
00148 
00149         /* Read the modify event */
00150         test (pnotify_get_event(&evt, ctx));
00151         if (!event_cmp(&evt, 1, PN_MODIFY, "bar")) 
00152                 err(1, "unexpected event value");
00153 
00154         /* Remove the watch */
00155         test (pnotify_rm_watch(wd));
00156                 
00157 #if DEADWOOD
00158         if ((fd = inotify_init()) < 0) 
00159                 err(1, "%s", "inotify_init()");
00160 
00161         if ((wd = inotify_add_watch(fd, ".check", IN_ALL_EVENTS)) <= 0) 
00162                 err(1, "%s - %d", "inotify_add_watch()", wd);
00163 
00164         if ((wd = inotify_rm_watch(fd, wd)) < 0) 
00165                 err(1, "%s", "inotify_rm_watch()");
00166 
00167         if ((wd = inotify_rm_watch(fd, 666)) == 0) 
00168                 err(1, "%s", "inotify_rm_watch() - false positive");
00169 
00170         /* Add it back so we can monitor events */
00171         if ((wd = inotify_add_watch(fd, ".check", IN_ALL_EVENTS)) <= 0) 
00172                 err(1, "%s - %d", "inotify_add_watch()", wd);
00173 
00174         printf("touching foo\n");
00175         if (system("touch .check/foo") < 0)
00176                 err(1, "touch failed");
00177         get_event(fd, &evt, &name, sizeof(name));
00178 
00179 #if TODO
00180         if ((wd = inotify_add_watch(fd, ".check/foo", IN_ALL_EVENTS)) <= 0) 
00181                 err(1, "%s - %d", "inotify_add_watch()", wd);
00182 
00183         printf("touching bar\n");
00184         if (system("touch .check/bar") < 0)
00185                 err(1, "touch failed");
00186 
00187         printf("unlinking foo\n");
00188         if (system("rm .check/foo") < 0)
00189                 err(1, "rm failed");
00190 
00191         printf("unlinking bar\n");
00192         if (system("rm .check/bar") < 0)
00193                 err(1, "rm failed");
00194 
00195 #endif
00196 #endif
00197 
00198         printf("all tests passed.\n");
00199         exit(EXIT_SUCCESS);
00200 }

Generated on Wed Aug 22 23:15:42 2007 for pnotify by  doxygen 1.5.1