test: store the list of open file descriptors in the litest context
authorPeter Hutterer <peter.hutterer@who-t.net>
Fri, 3 Jul 2020 02:29:16 +0000 (12:29 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 7 Jul 2020 08:51:33 +0000 (18:51 +1000)
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
test/litest-int.h
test/litest.c
test/test-misc.c
test/test-tablet.c

index 36be7a5b2217b3de0bd3a7b9791aa3ed16a11150..c5b05e68864346bb7809aa8e512d622205a5c45b 100644 (file)
@@ -131,6 +131,16 @@ struct litest_device_interface {
        int max[2]; /* x/y axis maximum */
 };
 
+struct path {
+       struct list link;
+       char *path;
+       int fd;
+};
+
+struct litest_context {
+       struct list paths;
+};
+
 void litest_set_current_device(struct litest_device *device);
 int litest_scale(const struct litest_device *d, unsigned int axis, double val);
 void litest_generic_device_teardown(void);
index 9965b72f0cfddd5f2d3ead3074ca3f8ca97349e0..da634e0379f3858f61a741b8a783fdd3b369b98f 100644 (file)
@@ -769,13 +769,41 @@ litest_init_all_device_udev_rules(struct list *created_files)
 static int
 open_restricted(const char *path, int flags, void *userdata)
 {
-       int fd = open(path, flags);
-       return fd < 0 ? -errno : fd;
+       const char prefix[] = "/dev/input/event";
+       struct litest_context *ctx = userdata;
+       struct path *p;
+       int fd;
+
+       litest_assert_ptr_notnull(ctx);
+
+       fd = open(path, flags);
+       if (fd < 0)
+               return -errno;
+
+       if (strneq(path, prefix, strlen(prefix))) {
+               p = zalloc(sizeof *p);
+               p->path = safe_strdup(path);
+               p->fd = fd;
+               list_append(&ctx->paths, &p->link);
+       }
+
+       return fd;
 }
 
 static void
 close_restricted(int fd, void *userdata)
 {
+       struct litest_context *ctx = userdata;
+       struct path *p, *tmp;
+
+       list_for_each_safe(p, tmp, &ctx->paths, link) {
+               if (p->fd != fd)
+                       continue;
+               list_remove(&p->link);
+               free(p->path);
+               free(p);
+       }
+
        close(fd);
 }
 
@@ -1620,8 +1648,13 @@ litest_create(enum litest_device_type which,
 struct libinput *
 litest_create_context(void)
 {
-       struct libinput *libinput =
-               libinput_path_create_context(&interface, NULL);
+       struct libinput *libinput;
+       struct litest_context *ctx;
+
+       ctx = zalloc(sizeof *ctx);
+       list_init(&ctx->paths);
+
+       libinput = libinput_path_create_context(&interface, ctx);
        litest_assert_notnull(libinput);
 
        libinput_log_set_handler(libinput, litest_log_handler);
@@ -1634,7 +1667,18 @@ litest_create_context(void)
 void
 litest_destroy_context(struct libinput *li)
 {
+       struct path *p, *tmp;
+       struct litest_context *ctx;
+
+
+       ctx = libinput_get_user_data(li);
+       litest_assert_ptr_notnull(ctx);
        libinput_unref(li);
+
+       list_for_each_safe(p, tmp, &ctx->paths, link) {
+               litest_abort_msg("Device paths should be removed by now");
+       }
+       free(ctx);
 }
 
 void
index 8030e198a9112a4f00503470d8ff1e40e0a80f81..b72a77ec191a528292b6e340e86b9af9da97831e 100644 (file)
@@ -669,6 +669,7 @@ START_TEST(timer_offset_bug_warning)
        struct litest_device *dev = litest_current_device();
        struct libinput *li = dev->libinput;
        int warning_triggered = 0;
+       void *old_user_data;
 
        litest_enable_tap(dev->libinput_device);
        litest_drain_events(li);
@@ -678,6 +679,7 @@ START_TEST(timer_offset_bug_warning)
 
        litest_timeout_tap();
 
+       old_user_data = libinput_get_user_data(li);
        libinput_set_user_data(li, &warning_triggered);
        libinput_log_set_handler(li, timer_offset_warning);
        libinput_dispatch(li);
@@ -685,6 +687,8 @@ START_TEST(timer_offset_bug_warning)
        /* triggered for touch down and touch up */
        ck_assert_int_eq(warning_triggered, 2);
        litest_restore_log_handler(li);
+
+       libinput_set_user_data(li, old_user_data);
 }
 END_TEST
 
@@ -706,7 +710,9 @@ START_TEST(timer_delay_bug_warning)
        struct litest_device *dev = litest_current_device();
        struct libinput *li = dev->libinput;
        int warning_triggered = 0;
+       void *old_user_data;
 
+       old_user_data = libinput_get_user_data(li);
        litest_drain_events(li);
 
        for (int i = 0; i < 10; i++) {
@@ -720,8 +726,10 @@ START_TEST(timer_delay_bug_warning)
                libinput_dispatch(li);
        }
 
+
        ck_assert_int_ge(warning_triggered, 1);
        litest_restore_log_handler(li);
+       libinput_set_user_data(li, old_user_data);
 }
 END_TEST
 
index a722deed264ba4bedb4cfe706d9767272599f3f2..a3060f1c5cf1a66e7af837ac61c3f71b165a3922 100644 (file)
@@ -4073,9 +4073,11 @@ START_TEST(tablet_pressure_offset_exceed_threshold)
        };
        double pressure;
        int warning_triggered = 0;
+       void *old_user_data;
 
        litest_drain_events(li);
 
+       old_user_data = libinput_get_user_data(li);
        libinput_set_user_data(li, &warning_triggered);
 
        libinput_log_set_handler(li, pressure_threshold_warning);
@@ -4090,6 +4092,8 @@ START_TEST(tablet_pressure_offset_exceed_threshold)
 
        ck_assert_int_eq(warning_triggered, 1);
        litest_restore_log_handler(li);
+
+       libinput_set_user_data(li, old_user_data);
 }
 END_TEST