...
authoralex <[email protected]>
Fri, 22 Apr 2022 20:13:54 +0000 (13:13 -0700)
committeralex <[email protected]>
Fri, 22 Apr 2022 20:13:54 +0000 (13:13 -0700)
test/unit/watch.tests.c

index 6a0b8963ebc8b55244aa1ce79c7f64befdf03890..1935e4d038144ef06e768cc20ea8ceb4e027234d 100644 (file)
@@ -12,12 +12,14 @@ void log_message(enum log_level lvl, FILE *fp, const char *format,...) { return;
 int main();
 static void watch_spawn_all_basic_test();
 static void watch_functionality_basic_test();
+static void watch_functionality_multi_level_test();
 
 int main() {
        setup_env();
 
-       watch_spawn_all_basic_test();
        watch_functionality_basic_test();
+       watch_functionality_multi_level_test();
+       watch_spawn_all_basic_test();
 
        clean_env();
 
@@ -88,6 +90,84 @@ static void watch_functionality_basic_test() {
        reset_env();
 }
 
+static void watch_functionality_multi_level_test() {
+       /* 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, watch_fd2;
+       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("mkdir -p " TEST_DIRECTORY "/directory1/directory2"); // IN_CREATE
+       system("echo hello > " TEST_DIRECTORY "/directory1/test.txt"); // NO EVENT
+
+       watch_fd2 = inotify_add_watch(fd,TEST_DIRECTORY "/directory1/directory2",WATCH_FLAGS);
+       assert(watch_fd2>=0);
+
+       system("echo hello > " TEST_DIRECTORY "/directory1/directory2/test2.txt"); // IN_CREATE + IN_MODIFY + IN_CLOSE_WRITE
+       system("rm " TEST_DIRECTORY "/directory1/directory2/test2.txt"); // IN_DELETE
+       system("rmdir " TEST_DIRECTORY "/directory1/directory2"); // IN_IGNORED (since directory [subject of watch2] is removed)
+       system("rm " TEST_DIRECTORY "/directory1/test.txt"); // NO EVENT
+       system("rmdir " TEST_DIRECTORY "/directory1"); // IN_ISDIR
+       
+       len = read(fd,buf,sizeof(buf));
+       assert(len==208);
+       
+       p = buf;
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CREATE);
+       assert(event->wd==watch_fd);
+       assert(strcmp(event->name,"directory1")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CREATE);
+       assert(event->wd==watch_fd2);
+       assert(strcmp(event->name,"test2.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_MODIFY);
+       assert(event->wd==watch_fd2);
+       assert(strcmp(event->name,"test2.txt")==0);
+
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_CLOSE_WRITE);
+       assert(event->wd==watch_fd2);
+       assert(strcmp(event->name,"test2.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_DELETE);
+       assert(event->wd==watch_fd2);
+       assert(strcmp(event->name,"test2.txt")==0);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_IGNORED);
+       assert(event->wd==watch_fd2);
+       
+       p += (sizeof(struct inotify_event)+event->len);
+       event = (const struct inotify_event*) p;
+       assert(event->mask & IN_ISDIR);
+       assert(event->mask & IN_DELETE);
+       assert(event->wd==watch_fd);
+       assert(strcmp(event->name,"directory1")==0);
+       
+       assert((p+(sizeof(struct inotify_event)+event->len)-buf)==208);
+       
+       close(fd);
+
+       reset_env();
+}
+
 static void watch_spawn_all_basic_test() {
        struct torrent *p;
        int fd;