aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Kenzior <denkenz@gmail.com>2023-07-17 01:38:37 -0500
committerDenis Kenzior <denkenz@gmail.com>2023-07-17 22:50:23 -0500
commitc94667db15393a081d4a524e97f84cc61660ad24 (patch)
tree7258d6c41444183f1f5a2155fd6ec22d6bda5eb5
parentb54157513b89d3c6ee13ceb4985f95a0b18dad7d (diff)
unit: Add basic unit test for l_sysctl_get/set
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am5
-rw-r--r--unit/test-sysctl.c78
3 files changed, 83 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 1540012b..31ef6686 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,6 +62,7 @@ unit/test-ecdh
unit/test-time
unit/test-path
unit/test-net
+unit/test-sysctl
unit/cert-*.pem
unit/cert-*.csr
unit/cert-*.srl
diff --git a/Makefile.am b/Makefile.am
index 738aa75a..cc68b246 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -201,7 +201,8 @@ unit_tests = unit/test-unit \
unit/test-ecdh \
unit/test-time \
unit/test-path \
- unit/test-net
+ unit/test-net \
+ unit/test-sysctl
dbus_tests = unit/test-hwdb \
unit/test-dbus \
@@ -350,6 +351,8 @@ unit_test_path_LDADD = ell/libell-private.la
unit_test_net_LDADD = ell/libell-private.la
+unit_test_sysctl_LDADD = ell/libell-private.la
+
unit_test_data_files = unit/settings.test unit/dbus.conf
if EXAMPLES
diff --git a/unit/test-sysctl.c b/unit/test-sysctl.c
new file mode 100644
index 00000000..820c7079
--- /dev/null
+++ b/unit/test-sysctl.c
@@ -0,0 +1,78 @@
+/*
+ *
+ * Embedded Linux library
+ *
+ * Copyright (C) 2023 Cruise LLC. All rights reserved.
+ *
+ * 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; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * 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 St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include <ell/ell.h>
+
+static void test_sysctl_get_set(const void *data)
+{
+ FILE *f;
+ uint32_t n;
+ uint32_t expected;
+ int r;
+
+ /* Grab the setting via sysctl */
+ f = popen("sysctl -n net.core.somaxconn", "r");
+ assert(f);
+
+ assert(fscanf(f, "%u", &expected) == 1);
+ pclose(f);
+
+ /* Now check against what we get */
+ assert(l_sysctl_get_u32(&n, "/proc/sys/%s/%s/%s",
+ "net", "core", "somaxconn") == 0);
+ assert(n == expected);
+
+ /*
+ * Attempt to change the setting, if we get an -EPERM, then we're
+ * most likely not running as root, so succeed silently. Any other
+ * error is treated as a unit test failure
+ */
+
+ r = l_sysctl_set_u32(5000, "/proc/sys/net/core/%s", "somaxconn");
+ if (r == -EACCES)
+ return;
+
+ assert(!r);
+ assert(!l_sysctl_get_u32(&n, "/proc/sys/net/core/somaxconn"));
+ assert(n == 5000);
+
+ /* Set it back to original */
+ assert(!l_sysctl_set_u32(expected, "/proc/sys/net/core/somaxconn"));
+}
+
+int main(int argc, char *argv[])
+{
+ l_test_init(&argc, &argv);
+
+ l_test_add("sysctl/get_set", test_sysctl_get_set, NULL);
+
+ return l_test_run();
+}