From 8707de3510ac14bcdf0d39d53a64d47726f75d3b Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Thu, 5 Nov 2020 13:37:41 -0500 Subject: [PATCH] port: tests: Add a simple unit test for shm operations Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers Change-Id: Idf347eb2985cac2216da0bb13aee5b09884c8b48 --- .gitignore | 1 + configure.ac | 1 + tests/Makefile.am | 1 + tests/unit/Makefile.am | 1 + tests/unit/libringbuffer/Makefile.am | 9 ++++ tests/unit/libringbuffer/shm.c | 67 ++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+) create mode 100644 tests/unit/libringbuffer/Makefile.am create mode 100644 tests/unit/libringbuffer/shm.c diff --git a/.gitignore b/.gitignore index f34fd050..d96ab16a 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ tests/compile/hello-many/hello-many tests/compile/same_line_tracepoint/same_line_tracepoint tests/compile/test-app-ctx/hello tests/unit/gcc-weak-hidden/test_gcc_weak_hidden +tests/unit/libringbuffer/test_shm tests/unit/pthread_name/test_pthread_name tests/unit/snprintf/test_snprintf tests/unit/ust-elf/test_ust_elf diff --git a/configure.ac b/configure.ac index 1aec6d06..14f297de 100644 --- a/configure.ac +++ b/configure.ac @@ -540,6 +540,7 @@ AC_CONFIG_FILES([ tests/benchmark/Makefile tests/unit/gcc-weak-hidden/Makefile tests/unit/Makefile + tests/unit/libringbuffer/Makefile tests/unit/pthread_name/Makefile tests/unit/snprintf/Makefile tests/unit/ust-elf/Makefile diff --git a/tests/Makefile.am b/tests/Makefile.am index 156b8396..6792b0b9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,6 +5,7 @@ LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ $(srcdir)/utils/tap-driver.sh TESTS = \ + unit/libringbuffer/test_shm \ unit/gcc-weak-hidden/test_gcc_weak_hidden \ unit/pthread_name/test_pthread_name \ unit/snprintf/test_snprintf \ diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 8c14ffbb..b1de13a1 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -1,5 +1,6 @@ SUBDIRS = \ gcc-weak-hidden \ + libringbuffer \ pthread_name \ snprintf \ ust-elf diff --git a/tests/unit/libringbuffer/Makefile.am b/tests/unit/libringbuffer/Makefile.am new file mode 100644 index 00000000..7094cf66 --- /dev/null +++ b/tests/unit/libringbuffer/Makefile.am @@ -0,0 +1,9 @@ +AM_CPPFLAGS += -I$(top_srcdir)/include -I$(top_srcdir)/ -I$(top_srcdir)/tests/utils + +noinst_PROGRAMS = test_shm +test_shm_SOURCES = shm.c +test_shm_LDADD = \ + $(top_builddir)/libringbuffer/libringbuffer.la \ + $(top_builddir)/liblttng-ust-comm/liblttng-ust-comm.la \ + $(top_builddir)/snprintf/libustsnprintf.la \ + $(top_builddir)/tests/utils/libtap.a diff --git a/tests/unit/libringbuffer/shm.c b/tests/unit/libringbuffer/shm.c new file mode 100644 index 00000000..5ea13109 --- /dev/null +++ b/tests/unit/libringbuffer/shm.c @@ -0,0 +1,67 @@ +/* Copyright (C) 2020 Michael Jeanson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; only + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "libringbuffer/shm.h" + +#include "tap.h" + +#define SHM_PATH "/ust-shm-test" + +int main() +{ + int shmfd; + size_t shmsize = LTTNG_UST_PAGE_SIZE * 10; + struct shm_object_table *table; + struct shm_object *shmobj; + struct shm_ref shm_ref; + + plan_tests(5); + + /* Open a zero byte shm fd */ + shmfd = shm_open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + ok(shmfd > 0, "Open a POSIX shm fd"); + + /* Create a dummy shm object table to test the allocation function */ + table = shm_object_table_create(1); + ok(table, "Create a shm object table"); + assert(table); + + /* This function sets the initial size of the shm with ftruncate and zeros it */ + shmobj = shm_object_table_alloc(table, shmsize, SHM_OBJECT_SHM, shmfd, -1); + ok(shmobj, "Allocate the shm object table"); + assert(shmobj); + + shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 5); + ok(shm_ref.index != -1, "Allocate an object in the shm with sufficient space"); + + shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 6); + ok(shm_ref.index == -1, "Allocate an object in the shm with insufficient space"); + + /* Cleanup */ + shm_object_table_destroy(table, 1); + + return exit_status(); +} -- 2.34.1