From: alex Date: Fri, 22 Apr 2022 20:13:54 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=64b2cdcba4d63c7e9951a827e313768b5f9a8e2b;p=seeder ... --- diff --git a/test/unit/watch.tests.c b/test/unit/watch.tests.c index 6a0b896..1935e4d 100644 --- a/test/unit/watch.tests.c +++ b/test/unit/watch.tests.c @@ -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;