tests: update libgpiosim tests and add a uAPI test case for valid lines
cover tests: update libgpiosim tests and add a uAPI test case for valid lines
This series fixes a couple minor bugs in tests, reworks the libgpiosim selftests a bit, adds support for using the new feature of the gpio-sim module that is marking GPIO lines as valid/invalid and finally adds a test-case for validating the correct kernel behavior with invalid lines.
---
Bartosz Golaszewski (7):
tests: harness: use correct type to capture a boolean retval
tests: gpiosim: don't allow clearing hogs on active devices
tests: gpiosim: selftests: shrink the self-test code
tests: gpiosim: selftests: add more test cases
tests: gpiosim: provide gpiosim_bank_set_line_valid()
tests: harness: support setting invalid lines
tests: add a test case for checking invalid lines
tests/gpiosim-glib/gpiosim-glib.c | 48 ++++++++++++++- tests/gpiosim/gpiosim-selftest.c | 126 ++++++++++++++++---------------------- tests/gpiosim/gpiosim.c | 23 ++++++- tests/gpiosim/gpiosim.h | 2 + tests/harness/gpiod-test.c | 4 +- tests/tests-kernel-uapi.c | 29 +++++++++ 6 files changed, 155 insertions(+), 77 deletions(-)
---
base-commit: 727d07ee53493219a339ffe7c65512eb85548e6f
change-id: 20250904-gpiosim-valid-lines-87bf86d5d91b
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH 1/7 tests: harness: use correct type to capture a boolean retval
From: Bartosz Golaszewski <[email protected]>
Unlike the libgpiosim functions used in g_gpiosim_glib_apply_properties(), the return value of g_gpiosim_chip_apply_line_names() is a gboolean so add a second local variable of the correct type to capture it.
--- tests/gpiosim-glib/gpiosim-glib.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tests/gpiosim-glib/gpiosim-glib.c +3 -2
@@ -132,6 +132,7 @@ static gboolean g_gpiosim_chip_apply_hogs(GPIOSimChip *self)static gboolean g_gpiosim_chip_apply_properties (GPIOSimChip *self) { +gboolean err;int ret; ret =gpiosim_bank_set_num_lines (self->bank, self->num_lines);
@@ -154,8 +155,8 @@ static gboolean g_gpiosim_chip_apply_properties(GPIOSimChip *self) } } - ret =g_gpiosim_chip_apply_line_names (self); -if (!ret) + err =g_gpiosim_chip_apply_line_names (self); +if (!err)return FALSE;return g_gpiosim_chip_apply_hogs (self);
Signature · 1 line
2.48.1
PATCH 2/7 tests: gpiosim: don't allow clearing hogs on active devices
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Check if the GPIO device is pending in gpiosim_bank_clear_hog() before actually trying to remove the hog directory.
--- tests/gpiosim/gpiosim.c | 3 +++ 1 file changed, 3 insertions(+)
tests/gpiosim/gpiosim.c +3 -0
@@ -1116,6 +1116,9 @@ GPIOSIM_API int gpiosim_bank_clear_hog(struct gpiosim_bank *bank, unsigned int offset) {char buf[64 ]; + +if (!dev_check_pending (bank->dev)) +return -1 ;snprintf (buf,sizeof (buf),"line%u/hog" , offset);
Signature · 1 line
2.48.1
PATCH 3/7 tests: gpiosim: selftests: shrink the self-test code
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Most tests failures end with a perror() print and an exit(1) so wrap it in a helper function and a macro and shrink the code.
--- tests/gpiosim/gpiosim-selftest.c | 112 ++++++++++++++------------------------- 1 file changed, 39 insertions(+), 73 deletions(-)
tests/gpiosim/gpiosim-selftest.c +39 -73
@@ -6,7 +6,8 @@#include "gpiosim.h" -#define UNUSED __attribute__((unused)) +#define UNUSED __attribute__((unused)) +#define NORETURN __attribute__((noreturn))static const char *const line_names[] = {"foo" ,
@@ -16,111 +17,89 @@ static const char *const line_names[] = { "barfoo", }; +static NORETURN void die(const char *msg) +{ +perror (msg); +exit (EXIT_FAILURE ); +} + +#define expect_or_die (_cond, _msg) \ + do { \ + if (!(_cond)) \ + die(_msg); \ + } while (0) + int main(int argc UNUSED, char **argv UNUSED) {struct gpiosim_bank *bank0, *bank1;struct gpiosim_dev *dev;struct gpiosim_ctx *ctx; +enum gpiosim_pull pull; +enum gpiosim_value val;int ret, i;printf ("Creating gpiosim context\n" ); ctx =gpiosim_ctx_new (); -if (!ctx) { -perror ("unable to create the gpios-sim context" ); -return EXIT_FAILURE ; - } +expect_or_die (ctx,"unable to create the gpios-sim context" );printf ("Creating a chip\n" ); dev =gpiosim_dev_new (ctx); -if (!dev) { -perror ("Unable to create a chip" ); -return EXIT_FAILURE ; - } +expect_or_die (dev,"Unable to create a chip" );printf ("Creating a bank\n" ); bank0 =gpiosim_bank_new (dev); -if (!bank0) { -perror ("Unable to create a bank" ); -return EXIT_FAILURE ; - } +expect_or_die (bank0,"Unable to create a bank" );printf ("Creating a second bank\n" ); bank1 =gpiosim_bank_new (dev); -if (!bank1) { -perror ("Unable to create a bank" ); -return EXIT_FAILURE ; - } +expect_or_die (bank1,"Unable to create a bank" );printf ("Setting the label of bank #2 to foobar\n" ); ret =gpiosim_bank_set_label (bank1,"foobar" ); -if (ret) { -perror ("Unable to set the label of bank #2" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to set the label of bank #2" );printf ("Setting the number of lines in bank #1 to 16\n" ); ret =gpiosim_bank_set_num_lines (bank0,16 ); -if (ret) { -perror ("Unable to set the number of lines" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to set the number of lines" );printf ("Setting the number of lines in bank #2 to 8\n" ); ret =gpiosim_bank_set_num_lines (bank1,8 ); -if (ret) { -perror ("Unable to set the number of lines" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to set the number of lines" );printf ("Setting names for some lines in bank #1\n" );for (i =0 ; i <5 ; i++) { ret =gpiosim_bank_set_line_name (bank0, i, line_names[i]); -if (ret) { -perror ("Unable to set line names" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to set line names" ); }printf ("Hog a line on bank #2\n" ); ret =gpiosim_bank_hog_line (bank1,3 ,"xyz" ,GPIOSIM_DIRECTION_OUTPUT_HIGH ); -if (ret) { -perror ("Unable to hog a line" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to hog a line" );printf ("Enabling the GPIO device\n" ); ret =gpiosim_dev_enable (dev); -if (ret) { -perror ("Unable to enable the device" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to enable the device" );printf ("Setting the pull of a single line to pull-up\n" ); ret =gpiosim_bank_set_pull (bank0,6 ,GPIOSIM_PULL_UP ); -if (ret) { -perror ("Unable to set the pull" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to set the pull" );printf ("Reading the pull back\n" ); ret =gpiosim_bank_get_pull (bank0,6 ); -if (ret <0 ) { -perror ("Unable to read the pull" ); -return EXIT_FAILURE ; - } +expect_or_die (ret >=0 ,"Unable to read the pull" );if (ret !=GPIOSIM_PULL_UP ) {fprintf (stderr,"Invalid pull value read\n" );
@@ -129,13 +108,10 @@ int main(int argc UNUSED, char **argv UNUSED)printf ("Reading the value\n" ); - ret =gpiosim_bank_get_value (bank0,6 ); -if (ret <0 ) { -perror ("Unable to read the value" ); -return EXIT_FAILURE ; - } + val =gpiosim_bank_get_value (bank0,6 ); +expect_or_die (val !=GPIOSIM_VALUE_ERROR ,"Unable to read the value" ); -if (ret !=GPIOSIM_VALUE_ACTIVE ) { +if (val !=GPIOSIM_VALUE_ACTIVE ) {fprintf (stderr,"Invalid value read\n" );return EXIT_FAILURE ; }
@@ -143,28 +119,19 @@ int main(int argc UNUSED, char **argv UNUSED)printf ("Disabling the GPIO device\n" ); ret =gpiosim_dev_disable (dev); -if (ret) { -perror ("Error while disabling the device" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Error while disabling the device" );printf ("Re-enabling the GPIO device\n" ); ret =gpiosim_dev_enable (dev); -if (ret) { -perror ("Unable to re-enable the device" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Unable to re-enable the device" );printf ("Checking the pull has been reset\n" ); - ret =gpiosim_bank_get_pull (bank0,6 ); -if (ret <0 ) { -perror ("Unable to read the pull" ); -return EXIT_FAILURE ; - } + pull =gpiosim_bank_get_pull (bank0,6 ); +expect_or_die (pull !=GPIOSIM_PULL_ERROR ,"Unable to read the pull" ); -if (ret !=GPIOSIM_PULL_DOWN ) { +if (pull !=GPIOSIM_PULL_DOWN ) {fprintf (stderr,"Invalid pull value read\n" );return EXIT_FAILURE ; }
@@ -172,15 +139,14 @@ int main(int argc UNUSED, char **argv UNUSED)printf ("Re-disabling the device\n" ); ret =gpiosim_dev_disable (dev); -if (ret) { -perror ("Error while re-disabling the device" ); -return EXIT_FAILURE ; - } +expect_or_die (ret ==0 ,"Error while re-disabling the device" );gpiosim_bank_unref (bank1);gpiosim_bank_unref (bank0);gpiosim_dev_unref (dev);gpiosim_ctx_unref (ctx); +printf ("ALL TESTS OK\n" ); +return EXIT_SUCCESS ; }
Signature · 1 line
2.48.1
PATCH 4/7 tests: gpiosim: selftests: add more test cases
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Add simple self tests for libgpiosim functions that were not tested before.
--- tests/gpiosim/gpiosim-selftest.c | 9 +++++++++ 1 file changed, 9 insertions(+)
tests/gpiosim/gpiosim-selftest.c +9 -0
@@ -91,6 +91,8 @@ int main(int argc UNUSED, char **argv UNUSED) ret =gpiosim_dev_enable (dev);expect_or_die (ret ==0 ,"Unable to enable the device" ); +expect_or_die (gpiosim_dev_is_live (dev),"Failed to enable the device" ); +printf ("Setting the pull of a single line to pull-up\n" ); ret =gpiosim_bank_set_pull (bank0,6 ,GPIOSIM_PULL_UP );
@@ -118,9 +120,16 @@ int main(int argc UNUSED, char **argv UNUSED)printf ("Disabling the GPIO device\n" ); +expect_or_die (gpiosim_dev_is_live (dev),"Failed to disable the device" ); + ret =gpiosim_dev_disable (dev);expect_or_die (ret ==0 ,"Error while disabling the device" ); +printf ("Clearing the GPIO hog on bank #2\n" ); + + ret =gpiosim_bank_clear_hog (bank1,3 ); +expect_or_die (ret ==0 ,"Error while clearing the hog" ); +printf ("Re-enabling the GPIO device\n" ); ret =gpiosim_dev_enable (dev);
Signature · 1 line
2.48.1
PATCH 5/7 tests: gpiosim: provide gpiosim_bank_set_line_valid()
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Provide a function allowing to mark a GPIO at given offset as valid or invalid (all lines are valid by default). This will make it impossible for any user to request it once the simulator is up.
--- tests/gpiosim/gpiosim-selftest.c | 5 +++++ tests/gpiosim/gpiosim.c | 20 +++++++++++++++++++- tests/gpiosim/gpiosim.h | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-)
tests/gpiosim/gpiosim-selftest.c +5 -0
@@ -130,6 +130,11 @@ int main(int argc UNUSED, char **argv UNUSED) ret =gpiosim_bank_clear_hog (bank1,3 );expect_or_die (ret ==0 ,"Error while clearing the hog" ); +printf ("Mark one line as invalid\n" ); + + ret =gpiosim_bank_set_line_valid (bank0,1 , false); +expect_or_die (ret ==0 ,"Unable to mark line as invalid" ); +printf ("Re-enabling the GPIO device\n" ); ret =gpiosim_dev_enable (dev);
tests/gpiosim/gpiosim.c +19 -1
@@ -24,7 +24,7 @@#define GPIOSIM_API __attribute__((visibility("default")))#define UNUSED __attribute__((unused)) -#define MIN_KERNEL_VERSION KERNEL_VERSION(5, 17, 4) +#define MIN_KERNEL_VERSION KERNEL_VERSION(6, 17, 0)static pthread_mutex_t id_lock =PTHREAD_MUTEX_INITIALIZER ;static pthread_once_t id_init_once =PTHREAD_ONCE_INIT ;
@@ -1125,6 +1125,24 @@ GPIOSIM_API int gpiosim_bank_clear_hog(struct gpiosim_bank *bank,return unlinkat (bank->cfs_dir_fd, buf,AT_REMOVEDIR ); } +GPIOSIM_API int gpiosim_bank_set_line_valid(struct gpiosim_bank *bank, +unsigned int offset,bool valid) +{ +char buf[32 ]; +int ret; + +if (!dev_check_pending (bank->dev)) +return -1 ; + + ret =bank_make_line_dir (bank, offset); +if (ret) +return -1 ; + +snprintf (buf,sizeof (buf),"line%u/valid" , offset); + +return open_write_close (bank->cfs_dir_fd, buf, valid ?"1" :"0" ); +} +static int sysfs_read_bank_attr (struct gpiosim_bank *bank,unsigned int offset,const char *attr,char *buf,size_t bufsize) {
tests/gpiosim/gpiosim.h +2 -0
@@ -61,6 +61,8 @@ int gpiosim_bank_set_line_name(struct gpiosim_bank *bank,int gpiosim_bank_hog_line (struct gpiosim_bank *bank,unsigned int offset,const char *name,enum gpiosim_direction direction);int gpiosim_bank_clear_hog (struct gpiosim_bank *bank,unsigned int offset); +int gpiosim_bank_set_line_valid (struct gpiosim_bank *bank,unsigned int offset, +bool valid);enum gpiosim_value gpiosim_bank_get_value (struct gpiosim_bank *bank,unsigned int offset);
Signature · 1 line
2.48.1
PATCH 6/7 tests: harness: support setting invalid lines
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Wrap the new gpiosim_bank_set_line_valid() symbol exported by libgpiosim in a GLib property and allow libgpiod tests to mark simulated lines as invalid.
--- tests/gpiosim-glib/gpiosim-glib.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
tests/gpiosim-glib/gpiosim-glib.c +45 -0
@@ -3,6 +3,7 @@#include <errno.h> #include <gpiosim.h> +#include <stdbool.h> #include <stdlib.h> #include <unistd.h>
@@ -18,6 +19,7 @@ struct _GPIOSimChip {gchar *label;GVariant *line_names;GVariant *hogs; +GVariant *invalid_lines; };enum {
@@ -27,6 +29,7 @@ enum {G_GPIOSIM_CHIP_PROP_LABEL ,G_GPIOSIM_CHIP_PROP_LINE_NAMES ,G_GPIOSIM_CHIP_PROP_HOGS , + G_GPIOSIM_CHIP_PROP_INVALID_LINES, };static struct gpiosim_ctx *sim_ctx;
@@ -130,6 +133,31 @@ static gboolean g_gpiosim_chip_apply_hogs(GPIOSimChip *self)return TRUE; } +static gboolean g_gpiosim_chip_apply_invalid_lines (GPIOSimChip *self) +{ +g_autoptr (GVariantIter) iter =NULL ; +gboolean ret; +guint offset; + +if (!self->invalid_lines) +return TRUE; + + iter =g_variant_iter_new (self->invalid_lines); + +while (g_variant_iter_loop (iter,"u" , &offset)) { + ret =gpiosim_bank_set_line_valid (self->bank, offset, false); +if (ret) { +g_set_error (&self->construct_err,G_GPIOSIM_ERROR , +G_GPIOSIM_ERR_CHIP_INIT_FAILED , +"Unable to set simulated GPIO line as invalid: %s" , +g_strerror (errno)); +return FALSE; + } + } + +return TRUE; +} +static gboolean g_gpiosim_chip_apply_properties (GPIOSimChip *self) {gboolean err;
@@ -159,6 +187,10 @@ static gboolean g_gpiosim_chip_apply_properties(GPIOSimChip *self)if (!err)return FALSE; + ret =g_gpiosim_chip_apply_invalid_lines (self); +if (!ret) +return FALSE; +return g_gpiosim_chip_apply_hogs (self); }
@@ -289,6 +321,9 @@ static void g_gpiosim_chip_set_property(GObject *obj, guint prop_id,case G_GPIOSIM_CHIP_PROP_HOGS :set_variant_prop (&self->hogs, val);break ; +case G_GPIOSIM_CHIP_PROP_INVALID_LINES : +set_variant_prop (&self->invalid_lines, val); +break ;default :G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);break ;
@@ -301,6 +336,7 @@ static void g_gpiosim_chip_dispose(GObject *obj)g_clear_pointer (&self->line_names, g_variant_unref);g_clear_pointer (&self->hogs, g_variant_unref); +g_clear_pointer (&self->invalid_lines, g_variant_unref);G_OBJECT_CLASS (g_gpiosim_chip_parent_class)->dispose (obj); }
@@ -385,6 +421,14 @@ static void g_gpiosim_chip_class_init(GPIOSimChipClass *chip_class)"List of hogged lines and their directions." , (GVariantType *)"a(usi)" ,NULL , G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + +g_object_class_install_property ( + class,G_GPIOSIM_CHIP_PROP_INVALID_LINES , +g_param_spec_variant ( +"invalid-lines" ,"Invalid lines" , +"List of offsets of lines not valid as GPIOs." , + (GVariantType *)"au" ,NULL , +G_PARAM_WRITABLE |G_PARAM_CONSTRUCT_ONLY )); }static void g_gpiosim_chip_init (GPIOSimChip *self)
@@ -394,6 +438,7 @@ static void g_gpiosim_chip_init(GPIOSimChip *self) self->label =NULL ; self->line_names =NULL ; self->hogs =NULL ; + self->invalid_lines =NULL ; }const gchar *g_gpiosim_chip_get_dev_path (GPIOSimChip *self)
Signature · 1 line
2.48.1
PATCH 7/7 tests: add a test case for checking invalid lines
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Add a test case for validating kernel's behavior when user-space tries to request an invalid GPIO line.
--- tests/harness/gpiod-test.c | 4 ++-- tests/tests-kernel-uapi.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-)
tests/harness/gpiod-test.c +2 -2
@@ -10,8 +10,8 @@#include "gpiod-test.h" -#define MIN_KERNEL_MAJOR 5 -#define MIN_KERNEL_MINOR 19 +#define MIN_KERNEL_MAJOR 6 +#define MIN_KERNEL_MINOR 17#define MIN_KERNEL_RELEASE 0#define MIN_KERNEL_VERSION KERNEL_VERSION(MIN_KERNEL_MAJOR, \ MIN_KERNEL_MINOR, \
tests/tests-kernel-uapi.c +29 -0
@@ -197,3 +197,32 @@ GPIOD_TEST_CASE(open_source_emulation) g_assert_cmpint(gpiod_line_info_get_drive(info), ==,GPIOD_LINE_DRIVE_OPEN_SOURCE ); } + +GPIOD_TEST_CASE (valid_lines) +{ +static const guint invalid_lines[] = {2 ,4 }; + +g_autoptr (GPIOSimChip) sim =NULL ; +g_autoptr (struct_gpiod_chip) chip =NULL ; +g_autoptr (struct_gpiod_line_info) info0 =NULL ; +g_autoptr (struct_gpiod_line_info) info1 =NULL ; +g_autoptr (GVariant) vinval =NULL ; + + vinval =g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32 , + invalid_lines,2 ,sizeof (guint)); +g_variant_ref_sink (vinval); + + sim =g_gpiosim_chip_new ("num-lines" ,8 , +"invalid-lines" , vinval, +NULL ); + + chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); + info0 =gpiod_chip_get_line_info (chip,0 ); + info1 =gpiod_chip_get_line_info (chip,2 ); +g_assert_nonnull (info0); +g_assert_nonnull (info1); +gpiod_test_return_if_failed (); + +g_assert_false (gpiod_line_info_is_used (info0)); +g_assert_true (gpiod_line_info_is_used (info1)); +}
Signature · 1 line
2.48.1
Bartosz Golaszewski
AppliedFrom: Bartosz Golaszewski <[email protected]>
Quoted 6 lines · Bartosz Golaszewski
This series fixes a couple minor bugs in tests, reworks the libgpiosim selftests a bit, adds support for using the new feature of the gpio-sim module that is marking GPIO lines as valid/invalid and finally adds a test-case for validating the correct kernel behavior with invalid lines.
Applied, thanks!
[1/7] tests: harness: use correct type to capture a boolean retval
https://git.kernel.org/brgl/libgpiod/c/347adbd698537a32986b2a590fb6e91034d1d479 [2/7] tests: gpiosim: don't allow clearing hogs on active devices https://git.kernel.org/brgl/libgpiod/c/1aca992f8b8fc5f3d5a76043ada2a01f740f0066 [3/7] tests: gpiosim: selftests: shrink the self-test code https://git.kernel.org/brgl/libgpiod/c/43d100a49c437a842a0585d67ff09861b72567a1 [4/7] tests: gpiosim: selftests: add more test cases https://git.kernel.org/brgl/libgpiod/c/8379438130ca12424ef794cae7471736f519f18b [5/7] tests: gpiosim: provide gpiosim_bank_set_line_valid() https://git.kernel.org/brgl/libgpiod/c/b4009e2e6715564ead10424ccba73eb1ecdd8a33 [6/7] tests: harness: support setting invalid lines https://git.kernel.org/brgl/libgpiod/c/d6fd0a065a17a98e5761421587fdfd5abaadecb1 [7/7] tests: add a test case for checking invalid lines https://git.kernel.org/brgl/libgpiod/c/f4fe71e9405c3dbeab41dc8ea772735fd64562f8
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>