tests: add support for the gpio-sim kernel module
cover tests: add support for the gpio-sim kernel module
This series implements user-space support for the gpio-sim kernel module and ports the core C tests to using it. The goal is to entirely drop support for gpio-mockup from libgpiod.
The new library is designed in a way that allows multiple test suites to run at the same time as any process using the library only ever interacts with the simulated chips it instantiated itself. The new kernel driver also doesn't require the user to unload the module to change the chip setup.
The first patch removes leftover code. The second adds the new library and the last one does the porting of the core code.
In the future - once v2 for language bindings is done, we'll port other test-suites to the new library.
Bartosz Golaszewski (3):
tests: remove gpiod_test_chip_num()
libgpiosim: new library for controlling the gpio-sim module
tests: port C tests to libgpiosim
configure.ac | 5 +- tests/Makefile.am | 6 +- tests/gpiod-test.c | 134 ++++-- tests/gpiod-test.h | 3 +- tests/gpiosim/.gitignore | 4 + tests/gpiosim/Makefile.am | 16 + tests/gpiosim/gpiosim-selftest.c | 103 +++++ tests/gpiosim/gpiosim.c | 743 +++++++++++++++++++++++++++++++ tests/gpiosim/gpiosim.h | 42 ++ 9 files changed, 1012 insertions(+), 44 deletions(-) create mode 100644 tests/gpiosim/.gitignore create mode 100644 tests/gpiosim/Makefile.am create mode 100644 tests/gpiosim/gpiosim-selftest.c create mode 100644 tests/gpiosim/gpiosim.c create mode 100644 tests/gpiosim/gpiosim.h
Signature · 1 line
2.30.1
PATCH 1/3 tests: remove gpiod_test_chip_num()
This function is no longer used, remove it.
--- tests/gpiod-test.c | 12 ------------ tests/gpiod-test.h | 1 - 2 files changed, 13 deletions(-)
tests/gpiod-test.c +0 -12
@@ -149,18 +149,6 @@ const gchar *gpiod_test_chip_name(guint idx)return name; } -gint gpiod_test_chip_num (unsigned int idx) -{ -gint num; - - num =gpio_mockup_chip_num (globals.mockup, idx); -if (num <0 ) -g_error ("unable to retrieve the chip number: %s" , -g_strerror (errno)); - -return num; -} -gint gpiod_test_chip_get_value (guint chip_index,guint line_offset) {gint ret;
tests/gpiod-test.h +0 -1
@@ -80,7 +80,6 @@ enum {/* Wrappers around libgpiomockup helpers. */ const gchar *gpiod_test_chip_path (guint idx);const gchar *gpiod_test_chip_name (guint idx); -gint gpiod_test_chip_num (guint idx);gint gpiod_test_chip_get_value (guint chip_index,guint line_offset);void gpiod_test_chip_set_pull (guint chip_index,guint line_offset,gint pull);
Signature · 1 line
2.30.1
PATCH 2/3 libgpiosim: new library for controlling the gpio-sim module
In reply to Bartosz GolaszewskiAdd a C library for controlling the gpio-sim kernel module from various libgpiod test suites. This aims at replacing the old gpio-mockup module and its user-space library - libgpio-mockup - in the project's tree.
--- configure.ac | 5 +- tests/Makefile.am | 2 +- tests/gpiosim/.gitignore | 4 + tests/gpiosim/Makefile.am | 16 + tests/gpiosim/gpiosim-selftest.c | 103 +++++ tests/gpiosim/gpiosim.c | 743 +++++++++++++++++++++++++++++++ tests/gpiosim/gpiosim.h | 42 ++ 7 files changed, 913 insertions(+), 2 deletions(-) create mode 100644 tests/gpiosim/.gitignore create mode 100644 tests/gpiosim/Makefile.am create mode 100644 tests/gpiosim/gpiosim-selftest.c create mode 100644 tests/gpiosim/gpiosim.c create mode 100644 tests/gpiosim/gpiosim.h
configure.ac +4 -1
@@ -31,6 +31,7 @@ AC_SUBST(ABI_CXX_VERSION, [2.1.1]) # ABI version for libgpiomockup (we need this since it can be installed if we # enable install-tests). AC_SUBST(ABI_MOCKUP_VERSION, [0.1.0]) +AC_SUBST(ABI_GPIOSIM_VERSION, [0.1.0]) AC_CONFIG_AUX_DIR([autostuff]) AC_CONFIG_MACRO_DIRS([m4])
@@ -126,10 +127,11 @@ AC_DEFUN([FUNC_NOT_FOUND_TESTS], if test "x$with_tests" = xtrue then - # For libgpiomockup + # For libgpiomockup & libgpiosim AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])]) PKG_CHECK_MODULES([KMOD], [libkmod >= 18]) PKG_CHECK_MODULES([UDEV], [libudev >= 215]) + PKG_CHECK_MODULES([MOUNT], [mount >= 2.33.1]) # For core library tests PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50])
@@ -224,6 +226,7 @@ AC_CONFIG_FILES([Makefile tools/Makefile tests/Makefile tests/mockup/Makefile + tests/gpiosim/Makefile bindings/cxx/libgpiodcxx.pc bindings/Makefile bindings/cxx/Makefile
tests/Makefile.am +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> -SUBDIRS = mockup +SUBDIRS = mockup gpiosim AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/mockup/ AM_CFLAGS += -include $(top_builddir)/config.h
tests/gpiosim/.gitignore new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]> + +gpiosim-selftest
tests/gpiosim/Makefile.am new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> + +lib_LTLIBRARIES = libgpiosim.la +noinst_PROGRAMS = gpiosim-selftest + +AM_CFLAGS = -Wall -Wextra -g -fvisibility=hidden -std=gnu89 +AM_CFLAGS += -include $(top_builddir)/config.h + +libgpiosim_la_SOURCES = gpiosim.c gpiosim.h +libgpiosim_la_CFLAGS = $(AM_CFLAGS) $(KMOD_CFLAGS) $(UDEV_CFLAGS) $(MOUNT_CFLAGS) +libgpiosim_la_LDFLAGS = -version-info $(subst .,:,$(ABI_GPIOSIM_VERSION)) +libgpiosim_la_LDFLAGS += $(KMOD_LIBS) $(UDEV_LIBS) $(MOUNT_LIBS) + +gpiosim_selftest_SOURCES = gpiosim-selftest.c +gpiosim_selftest_LDADD = libgpiosim.la
tests/gpiosim/gpiosim-selftest.c new file +103 -0
103 lines not on this page · raw message
tests/gpiosim/gpiosim.c new file +743 -0
743 lines not on this page · raw message
tests/gpiosim/gpiosim.h new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.30.1
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 3 lines · Bartosz Golaszewski
Add a C library for controlling the gpio-sim kernel module from various libgpiod test suites. This aims at replacing the old gpio-mockup module and its user-space library - libgpio-mockup - in the project's tree.
...
tests/gpiosim/gpiosim.c line 54
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+ rv =uname (&un); +if (rv) +return -1 ;
Wondering why in all such cases instead of returning error code from upper layer we shadow it with -1.
...
tests/gpiosim/gpiosim.c line 267
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+GPIOSIM_API void gpiosim_ctx_unref(struct gpiosim_ctx *ctx) +{ +if (--ctx->refcnt ==0 ) {
if (--refcnt) return;
?
Quoted 12 lines
tests/gpiosim/gpiosim.c new file +12
-0+close (ctx->pending_dir_fd); +close (ctx->live_dir_fd); + +if (ctx->cfs_mnt_dir) { +umount (ctx->cfs_mnt_dir); +rmdir (ctx->cfs_mnt_dir); +free (ctx->cfs_mnt_dir); + } + +free (ctx); + } +}
...
Quoted 1 line
+/* We don't have
mkdtempat()... :( */
But we have tmpnam() / tmpnam_r(), why to reinvent it below?
Quoted 40 lines
tests/gpiosim/gpiosim.c new file +40
-0+static char *make_random_dir_at (int at) +{ +static const char chars[] ="abcdefghijklmnoprstquvwxyz" +"ABCDEFGHIJKLMNOPRSTQUVWXYZ" +"0123456789" ; +static const unsigned int namelen =16 ; + +unsigned int idx, i; +char *name; +int ret; + + name =malloc (namelen); +if (!name) +return NULL ; + +again: +for (i =0 ; i < namelen; i++) { + ret =getrandom (&idx,sizeof (idx),GRND_NONBLOCK ); +if (ret !=sizeof (idx)) { +if (ret >=0 ) + errno =EAGAIN ; + +free (name); +return NULL ; + } + + name[i] = chars[idx % (GPIOSIM_ARRAY_SIZE (chars) -1 )]; + } + + ret =mkdirat (at, name,0600 ); +if (ret) { +if (errno ==EEXIST ) + goto again; + +free (name); +return NULL ; + } + +return name; +}
...
tests/gpiosim/gpiosim.c line 505
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+for (i =0 ; i < num_names; i++) { + len = names[i] ?strlen (names[i]) :0 ; +/* Length of the name + '"'x2 + ', '. */
This x2 is kinda hard to get on the first glance, perhaps:
/* Length of the '"' + name + '"' + ', '. */
?
tests/gpiosim/gpiosim.c line 513
Quoted 8 lines
tests/gpiosim/gpiosim.c new file +8
-0+ size += len +4 ; + } + + buf =malloc (size); +if (!buf) +return -1 ; + +memset (buf,0 , size);
calloc() ?
tests/gpiosim/gpiosim.c line 518
Quoted 4 lines
tests/gpiosim/gpiosim.c new file +4
-0+for (i =0 ; i < num_names; i++) + written +=snprintf (buf + written, size - written, +"\"%s\", " , names[i] ?:"" ); + buf[size -2 ] ='\0' ;
Dunno if you can use asprintf() and actually replace NULL by "" in the original array. Ah, see you already using it somewhere else, why not here?
...
tests/gpiosim/gpiosim.c line 598
Quoted 1 line
tests/gpiosim/gpiosim.c new file +1
-0+while (--tries) {
I consider such loops better in a form of
do {
...
} while (--tries);Quoted 1 line
(quoted diff) +1
-0+ }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Add a C library for controlling the gpio-sim kernel module from various libgpiod test suites. This aims at replacing the old gpio-mockup module and its user-space library - libgpio-mockup - in the project's tree.
Quoted 9 lines
...
tests/gpiosim/gpiosim.c line 54
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+ rv =uname (&un); +if (rv) +return -1 ;Wondering why in all such cases instead of returning error code from upper layer we shadow it with -1.
Unlike the linux kernel - the error codes in libc are not returned as return values of functions. Instead standard routines return -1 in almost all error cases and set errno to indicate the error code. This is why it's not necessary to propagate error numbers.
Quoted 11 lines
...
tests/gpiosim/gpiosim.c line 267
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+GPIOSIM_API void gpiosim_ctx_unref(struct gpiosim_ctx *ctx) +{ +if (--ctx->refcnt ==0 ) {if (--refcnt) return;?
Matter of taste I suppose but whatever, I can change it.
Quoted 16 lines
Quoted 13 lines
tests/gpiosim/gpiosim.c new file +12
-0+close (ctx->pending_dir_fd); +close (ctx->live_dir_fd); + +if (ctx->cfs_mnt_dir) { +umount (ctx->cfs_mnt_dir); +rmdir (ctx->cfs_mnt_dir); +free (ctx->cfs_mnt_dir); + } + +free (ctx); + } +}...
Quoted 4 lines
Quoted 1 line
+/* We don't have
mkdtempat()... :( */But we have
tmpnam()/tmpnam_r(), why to reinvent it below?
Because of this:
$man tmpnam_r
...
The created pathname has a directory prefix P_tmpdir.
...
And this:
./stdio.h:120:# define P_tmpdir "/tmp"
Quoted 53 lines
Quoted 40 lines
tests/gpiosim/gpiosim.c new file +40
-0+static char *make_random_dir_at (int at) +{ +static const char chars[] ="abcdefghijklmnoprstquvwxyz" +"ABCDEFGHIJKLMNOPRSTQUVWXYZ" +"0123456789" ; +static const unsigned int namelen =16 ; + +unsigned int idx, i; +char *name; +int ret; + + name =malloc (namelen); +if (!name) +return NULL ; + +again: +for (i =0 ; i < namelen; i++) { + ret =getrandom (&idx,sizeof (idx),GRND_NONBLOCK ); +if (ret !=sizeof (idx)) { +if (ret >=0 ) + errno =EAGAIN ; + +free (name); +return NULL ; + } + + name[i] = chars[idx % (GPIOSIM_ARRAY_SIZE (chars) -1 )]; + } + + ret =mkdirat (at, name,0600 ); +if (ret) { +if (errno ==EEXIST ) + goto again; + +free (name); +return NULL ; + } + +return name; +}...
tests/gpiosim/gpiosim.c line 505
Quoted 3 lines
tests/gpiosim/gpiosim.c new file +3
-0+for (i =0 ; i < num_names; i++) { + len = names[i] ?strlen (names[i]) :0 ; +/* Length of the name + '"'x2 + ', '. */ This x2 is kinda hard to get on the first glance, perhaps:
/* Length of the '"' + name + '"' + ', '. */?
Quoted 11 lines
tests/gpiosim/gpiosim.c line 513
Quoted 8 lines
tests/gpiosim/gpiosim.c new file +8
-0+ size += len +4 ; + } + + buf =malloc (size); +if (!buf) +return -1 ; + +memset (buf,0 , size);
calloc()?
Nah, I prefer malloc() for single block allocs even if it needs a memset().
Quoted 9 lines
tests/gpiosim/gpiosim.c line 518
Quoted 5 lines
tests/gpiosim/gpiosim.c new file +4
-0+for (i =0 ; i < num_names; i++) + written +=snprintf (buf + written, size - written, +"\"%s\", " , names[i] ?:"" ); + buf[size -2 ] ='\0' ;Dunno if you can use
asprintf()and actually replace NULL by "" in the original array. Ah, see you already using it somewhere else, why not here?
Not sure what you mean, we can't use asprintf() to create a composite string like what is needed here. Can you give me an example?
Quoted 10 lines
...
Quoted 1 line
+ while (--tries) {
I consider such loops better in a form of
do { ... } while (--tries);
Sure.
Bart
Quoted 8 lines
Quoted 2 lines
+ }
--
With Best Regards,
Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 11:47:33AM +0200, Bartosz Golaszewski wrote:
...
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+/* We don't have
mkdtempat()... :( */But we have
tmpnam()/tmpnam_r(), why to reinvent it below?Because of this:
Quoted 8 lines
$man
tmpnam_r
...
The created pathname has a directory prefixP_tmpdir.
...And this:
./stdio.h:120:# define
P_tmpdir"/tmp"
Still you may advance the pointer by the length of P_tmpdir + 1.
...
Quoted 11 lines
Quoted 8 lines
tests/gpiosim/gpiosim.c line 518
Quoted 4 lines
tests/gpiosim/gpiosim.c new file +4
-0+for (i =0 ; i < num_names; i++) + written +=snprintf (buf + written, size - written, +"\"%s\", " , names[i] ?:"" ); + buf[size -2 ] ='\0' ;Dunno if you can use
asprintf()and actually replace NULL by "" in the original array. Ah, see you already using it somewhere else, why not here?Not sure what you mean, we can't use
asprintf()to create a composite string like what is needed here. Can you give me an example?
I have got this after sending. Either you need to create a format string with va_args, or do it manually.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 11:47:33AM +0200, Bartosz Golaszewski wrote:
...
Quoted 12 lines
Quoted 12 lines
Quoted 4 lines
Quoted 1 line
+/* We don't have
mkdtempat()... :( */But we have
tmpnam()/tmpnam_r(), why to reinvent it below?Because of this:
$man
tmpnam_r
...
The created pathname has a directory prefixP_tmpdir.
...
Quoted 5 lines
Quoted 3 lines
And this:
./stdio.h:120:# define
P_tmpdir"/tmp"Still you may advance the pointer by the length of
P_tmpdir+ 1.
There is also tempnam().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 03:07:49PM +0200, Bartosz Golaszewski wrote:
...
Quoted 3 lines
Quoted 1 line
Still you may advance the pointer by the length of
P_tmpdir+ 1.There is also
tempnam().
Scratch it. It seems legacy one that actually tries to create file...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 9 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 03:07:49PM +0200, Bartosz Golaszewski wrote:
...
Quoted 2 lines
Quoted 1 line
Still you may advance the pointer by the length of
P_tmpdir+ 1.
Quoted 9 lines
Quoted 1 line
There is also
tempnam().Scratch it. It seems legacy one that actually tries to create file...
--
With Best Regards,
Andy Shevchenko
And also this:
libgpiod/tests/gpiosim/gpiosim.c:331: warning: the use of tmpnam_r' is dangerous, better use mkstemp'
So I'll go with my solution.
Bart
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
Quoted 13 lines · Bartosz Golaszewski
Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 03:07:49PM +0200, Bartosz Golaszewski wrote:
...
Quoted 3 lines
Quoted 1 line
Still you may advance the pointer by the length of
P_tmpdir+ 1.There is also
tempnam().
Quoted 8 lines
Quoted 1 line
Scratch it. It seems legacy one that actually tries to create file...
And also this:
libgpiod/tests/gpiosim/gpiosim.c:331: warning: the use oftmpnam_r' is dangerous, better usemkstemp'So I'll go with my solution.
My gosh, we so much legacy stuff here and there...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [libgpiod][PATCH 2/3] libgpiosim: new library for controlling the gpio-sim module
In reply to Andy ShevchenkoQuoted 37 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko
On Thu, Apr 29, 2021 at 11:47:33AM +0200, Bartosz Golaszewski wrote:
...
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+/* We don't have
mkdtempat()... :( */But we have
tmpnam()/tmpnam_r(), why to reinvent it below?Because of this:
Quoted 8 lines
$man
tmpnam_r
...
The created pathname has a directory prefixP_tmpdir.
...And this:
./stdio.h:120:# define
P_tmpdir"/tmp"Still you may advance the pointer by the length of
P_tmpdir+ 1....
Quoted 9 lines
Quoted 8 lines
tests/gpiosim/gpiosim.c line 518
Quoted 4 lines
tests/gpiosim/gpiosim.c new file +4
-0+for (i =0 ; i < num_names; i++) + written +=snprintf (buf + written, size - written, +"\"%s\", " , names[i] ?:"" ); + buf[size -2 ] ='\0' ;Dunno if you can use
asprintf()and actually replace NULL by "" in the original array. Ah, see you already using it somewhere else, why not here?
Quoted 7 lines
Quoted 2 lines
Not sure what you mean, we can't use
asprintf()to create a composite string like what is needed here. Can you give me an example?I have got this after sending. Either you need to create a format string with
va_args, or do it manually.
No can do, you can't create a va_list out of thin air.
Bart
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
PATCH 3/3 tests: port C tests to libgpiosim
In reply to Bartosz GolaszewskiThis converts the core library tests to using libgpiosim instead of libgpiod-mockup while keeping the same interface for tests.
--- tests/Makefile.am | 4 +- tests/gpiod-test.c | 122 +++++++++++++++++++++++++++++++++++---------- tests/gpiod-test.h | 2 +- 3 files changed, 99 insertions(+), 29 deletions(-)
tests/Makefile.am +2 -2
@@ -3,13 +3,13 @@ SUBDIRS = mockup gpiosim -AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/mockup/ +AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/gpiosim/ AM_CFLAGS += -include $(top_builddir)/config.h AM_CFLAGS += -Wall -Wextra -g -std=gnu89 $(GLIB_CFLAGS) AM_CFLAGS += -DG_LOG_DOMAIN=\"gpiod-test\" AM_LDFLAGS = -pthread LDADD = $(top_builddir)/lib/libgpiod.la -LDADD += $(top_builddir)/tests/mockup/libgpiomockup.la +LDADD += $(top_builddir)/tests/gpiosim/libgpiosim.la LDADD += $(GLIB_LIBS) bin_PROGRAMS = gpiod-test
tests/gpiod-test.c +96 -26
195 lines not on this page · raw message
tests/gpiod-test.h +1 -1
@@ -50,7 +50,7 @@ enum {/* * Register a test case function. The last argument is the array of numbers -* of lines per mockup chip. +* of lines per simulated chip. */ #define GPIOD_TEST_CASE (_name, _flags, ...) \ static void _name(void); \
Signature · 1 line
2.30.1