]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 22 Apr 2022 04:08:34 +0000 (21:08 -0700)
committeralex <[email protected]>
Fri, 22 Apr 2022 04:08:34 +0000 (21:08 -0700)
test/unit/watch.tests.c

index 804a44a5b66dd49707effbead586a9e49bdaa88f..6a0b8963ebc8b55244aa1ce79c7f64befdf03890 100644 (file)
@@ -25,7 +25,67 @@ int main() {
 }
 
 static void watch_functionality_basic_test() {
-       assert(0);
+       /* see `man inotify` for explanation of alignment modifiers */
+       char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
+       const struct inotify_event *event;
+       int fd, watch_fd;
+       ssize_t len;
+       char *p;
+
+       fd = inotify_init();
+       assert(fd>=0);
+
+       watch_fd = inotify_add_watch(fd,TEST_DIRECTORY,WATCH_FLAGS);
+       assert(watch_fd>=0);
+
+       system("echo hello > " TEST_DIRECTORY "/notarealfile.txt"); // IN_CREATE + IN_MODIFY + IN_CLOSE_WRITE
+       system("echo hello2 >> " TEST_DIRECTORY "/notarealfile.txt"); // IN_MODIFY + IN_CLOSE_WRITE
+       system("mv " TEST_DIRECTORY "/notarealfile.txt " TEST_DIRECTORY "/notarealfile2.txt"); // IN_MOVED_TO
+       system("rm " TEST_DIRECTORY "/notarealfile2.txt"); // IN_DELETE
+
+       len = read(fd,buf,sizeof(buf));
+       assert(len==336);
+
+       p = buf;
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CREATE);
+       assert(strcmp(event->name,"notarealfile.txt")==0);
+
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_MODIFY);
+       assert(strcmp(event->name,"notarealfile.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CLOSE_WRITE);
+       assert(strcmp(event->name,"notarealfile.txt")==0);
+
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_MODIFY);
+       assert(strcmp(event->name,"notarealfile.txt")==0);
+
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CLOSE_WRITE);
+       assert(strcmp(event->name,"notarealfile.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_MOVED_TO);
+       assert(strcmp(event->name,"notarealfile2.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_DELETE);
+       assert(strcmp(event->name,"notarealfile2.txt")==0);
+
+       assert((p+(sizeof(struct inotify_event)+event->len)-buf)==336);
+
+       close(fd);
+
+       reset_env();
 }
 
 static void watch_spawn_all_basic_test() {
@@ -47,4 +107,6 @@ static void watch_spawn_all_basic_test() {
        close(fd);
 
        torrent_free(p);
+
+       reset_env();
 }