From: Bartosz Golaszewski Date: Wed, 15 Mar 2023 19:54:52 +0000 (+0100) Subject: tests: add a test case for incorrect retrieving of edge events from buffer X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=8bf7b0c46f019136dea39f078737de2361ca2e95;p=qemu-gpiodev%2Flibgpiod.git tests: add a test case for incorrect retrieving of edge events from buffer Add a test case that makes sure we don't crash when trying to access an out-of-bounds edge event in an edge event buffer. Signed-off-by: Bartosz Golaszewski --- diff --git a/tests/tests-edge-event.c b/tests/tests-edge-event.c index 0ff7dc8..e79f457 100644 --- a/tests/tests-edge-event.c +++ b/tests/tests-edge-event.c @@ -639,3 +639,45 @@ GPIOD_TEST_CASE(null_buffer) g_assert_cmpint(ret, ==, -1); gpiod_test_expect_errno(EINVAL); } + +GPIOD_TEST_CASE(get_edge_event_index_out_of_bounds) +{ + static const guint offset = 2; + + g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); + g_autoptr(struct_gpiod_chip) chip = NULL; + g_autoptr(struct_gpiod_line_settings) settings = NULL; + g_autoptr(struct_gpiod_line_config) line_cfg = NULL; + g_autoptr(struct_gpiod_line_request) request = NULL; + g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL; + struct gpiod_edge_event *event; + gint ret; + + chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); + settings = gpiod_test_create_line_settings_or_fail(); + line_cfg = gpiod_test_create_line_config_or_fail(); + buffer = gpiod_test_create_edge_event_buffer_or_fail(64); + + gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); + gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH); + + gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, + settings); + + request = gpiod_test_request_lines_or_fail(chip, NULL, line_cfg); + + g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP); + g_usleep(500); + g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN); + g_usleep(500); + g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP); + g_usleep(500); + + ret = gpiod_line_request_read_edge_events(request, buffer, 3); + g_assert_cmpint(ret, ==, 3); + gpiod_test_return_if_failed(); + + event = gpiod_edge_event_buffer_get_event(buffer, 5); + g_assert_null(event); + gpiod_test_expect_errno(EINVAL); +}