summaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/linux
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/linux
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/linux')
-rw-r--r--lib/mlibc/tests/linux/cpuset.c27
-rw-r--r--lib/mlibc/tests/linux/malloc-usable-size.c13
-rw-r--r--lib/mlibc/tests/linux/pthread_attr.c90
-rw-r--r--lib/mlibc/tests/linux/pthread_setname_np.c33
-rw-r--r--lib/mlibc/tests/linux/xattr.c80
5 files changed, 243 insertions, 0 deletions
diff --git a/lib/mlibc/tests/linux/cpuset.c b/lib/mlibc/tests/linux/cpuset.c
new file mode 100644
index 0000000..3f55732
--- /dev/null
+++ b/lib/mlibc/tests/linux/cpuset.c
@@ -0,0 +1,27 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <sched.h>
+
+#define SET_SIZE 15
+
+int main() {
+ cpu_set_t *set = CPU_ALLOC(SET_SIZE);
+ size_t setsize = CPU_ALLOC_SIZE(SET_SIZE);
+
+ CPU_ZERO_S(setsize, set);
+
+ assert(!CPU_ISSET_S(11, setsize, set));
+
+ CPU_SET_S(11, setsize, set);
+ assert(CPU_ISSET_S(11, setsize, set));
+ assert(CPU_COUNT_S(setsize, set) == 1);
+
+ assert(!CPU_ISSET_S(CPU_SETSIZE - 1, setsize, set));
+
+ CPU_CLR_S(11, setsize, set);
+ assert(!CPU_ISSET_S(11, setsize, set));
+
+ CPU_FREE(set);
+
+ return 0;
+}
diff --git a/lib/mlibc/tests/linux/malloc-usable-size.c b/lib/mlibc/tests/linux/malloc-usable-size.c
new file mode 100644
index 0000000..3bb0394
--- /dev/null
+++ b/lib/mlibc/tests/linux/malloc-usable-size.c
@@ -0,0 +1,13 @@
+#include <assert.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+ void *p1 = malloc(1023);
+ fprintf(stderr, "size: %zu\n", malloc_usable_size(p1));
+ assert(malloc_usable_size(p1) >= 1023);
+ free(p1);
+
+ return 0;
+}
diff --git a/lib/mlibc/tests/linux/pthread_attr.c b/lib/mlibc/tests/linux/pthread_attr.c
new file mode 100644
index 0000000..4c1907c
--- /dev/null
+++ b/lib/mlibc/tests/linux/pthread_attr.c
@@ -0,0 +1,90 @@
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+
+static void test_affinity() {
+ pthread_attr_t attr;
+ cpu_set_t set = {0};
+ assert(!pthread_attr_init(&attr));
+ assert(!pthread_attr_setaffinity_np(&attr, 1, &set));
+
+ cpu_set_t other_set = {0};
+ assert(!pthread_attr_getaffinity_np(&attr, 1, &set));
+
+ assert(!memcmp(&set, &other_set, sizeof(cpu_set_t)));
+
+ pthread_attr_destroy(&attr);
+}
+
+#if !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
+static void test_sigmask() {
+ pthread_attr_t attr;
+ sigset_t set;
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+#ifndef USE_HOST_LIBC
+ sigaddset(&set, SIGCANCEL);
+#endif
+
+ assert(!pthread_attr_init(&attr));
+ assert(!pthread_attr_setsigmask_np(&attr, &set));
+
+ sigset_t other_set;
+ sigemptyset(&other_set);
+
+ assert(!pthread_attr_getsigmask_np(&attr, &other_set));
+
+ assert(sigismember(&other_set, SIGUSR1));
+#ifndef USE_HOST_LIBC
+ // Test whether internal signals get filtered properly.
+ assert(!sigismember(&other_set, SIGCANCEL));
+#endif
+
+ pthread_attr_destroy(&attr);
+}
+
+static void *getattr_worker(void *arg) {
+ (void)arg;
+ return NULL;
+}
+
+static void test_getattrnp() {
+ pthread_attr_t attr;
+ size_t stacksize = PTHREAD_STACK_MIN;
+ assert(!pthread_attr_init(&attr));
+ assert(!pthread_attr_setstacksize(&attr, stacksize));
+
+ pthread_t thread;
+ assert(!pthread_create(&thread, &attr, getattr_worker, NULL));
+ assert(!pthread_getattr_np(thread, &attr));
+ size_t other_stacksize;
+ assert(!pthread_attr_getstacksize(&attr, &other_stacksize));
+ assert(other_stacksize == stacksize);
+ assert(!pthread_join(thread, NULL));
+
+ pthread_t own_thread = pthread_self();
+ void *stack;
+ assert(!pthread_getattr_np(own_thread, &attr));
+ assert(!pthread_attr_getstack(&attr, &stack, &other_stacksize));
+ assert(stack);
+ assert(other_stacksize);
+ // Check that we can read from the highest byte returned.
+ // pthread_getattr_np() should return the lowest byte
+ // of the stack.
+ printf("highest byte: %hhu\n", *(char *)(stack + other_stacksize - 1));
+
+ pthread_attr_destroy(&attr);
+}
+#endif // !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
+
+int main() {
+ test_affinity();
+#if !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
+ test_sigmask();
+ test_getattrnp();
+#endif
+
+ return 0;
+}
diff --git a/lib/mlibc/tests/linux/pthread_setname_np.c b/lib/mlibc/tests/linux/pthread_setname_np.c
new file mode 100644
index 0000000..95c95c9
--- /dev/null
+++ b/lib/mlibc/tests/linux/pthread_setname_np.c
@@ -0,0 +1,33 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <pthread.h>
+
+int main() {
+ int ret = pthread_setname_np(pthread_self(), "mlibc-test-123");
+ assert(!ret);
+
+ char buf[16];
+ ret = pthread_getname_np(pthread_self(), buf, 16);
+ assert(!ret);
+ assert(!strcmp("mlibc-test-123", buf));
+
+ ret = pthread_setname_np(pthread_self(), "mlibc-test-123-too-long");
+ assert(ret == ERANGE);
+
+ ret = pthread_getname_np(pthread_self(), buf, 1);
+ assert(ret == ERANGE);
+
+ ret = pthread_getname_np(pthread_self(), buf, 15);
+ assert(ret == ERANGE);
+
+ ret = pthread_getname_np(pthread_self(), buf, 16);
+ assert(!ret);
+ assert(!strcmp("mlibc-test-123", buf));
+
+ return 0;
+}
diff --git a/lib/mlibc/tests/linux/xattr.c b/lib/mlibc/tests/linux/xattr.c
new file mode 100644
index 0000000..c6dd152
--- /dev/null
+++ b/lib/mlibc/tests/linux/xattr.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/xattr.h>
+
+#define assert_perror(x) \
+ ({ \
+ int res = (x); \
+ if (res < 0) { \
+ perror(#x); \
+ return 1; \
+ } \
+ res; \
+ })
+
+#define soft_assert(x, m) \
+ ({ \
+ if (!(x)) { \
+ fprintf(stderr, "cond \"%s\" failed: %s\n", #x, m); \
+ return 1; \
+ } \
+ })
+
+void remove_tmp(const char (*fname)[]) {
+ unlink(&(*fname)[0]);
+}
+#define _cleanup_file_ __attribute__((cleanup(remove_tmp)))
+
+int main(void) {
+ int ret;
+ char buf[32] = { 0 };
+ _cleanup_file_ char filename[] = "xattr_test.XXXXXX";
+ _cleanup_file_ char filename2[] = "xattr_test.XXXXXX.2";
+
+ int tmpfile = assert_perror(mkstemp(filename));
+ memcpy(filename2, filename, sizeof(filename) - 1);
+ assert_perror(symlink(filename, filename2));
+
+ assert_perror(setxattr(filename, "user.T1", "ABC", 3, XATTR_CREATE));
+ assert_perror(fsetxattr(tmpfile, "user.T2", "DEF", 3, XATTR_CREATE));
+
+ // for testing remove
+ assert_perror(fsetxattr(tmpfile, "user.T3", "DEF", 3, XATTR_CREATE));
+
+ if ((ret = getxattr(filename, "user.T1", buf, 3)) != 3) {
+ if (ret < 0) {
+ perror("getxattr");
+ return 1;
+ }
+
+ soft_assert(memcmp(buf, "ABC", 3) == 0, "xattr read wrong");
+ }
+
+ ret = lgetxattr(filename2, "user.T1", buf, 3);
+ soft_assert(ret < 0 && errno == ENODATA, "lgetxattr deref'd");
+
+ if ((ret = fgetxattr(tmpfile, "user.T3", buf, 3)) != 3) {
+ if (ret < 0) {
+ perror("fgetxattr");
+ return 1;
+ }
+
+ soft_assert(memcmp(buf, "DEF", 3) == 0, "xattr read wrong");
+ }
+
+ assert_perror(removexattr(filename, "user.T2"));
+ assert_perror(fremovexattr(tmpfile, "user.T3"));
+
+ ret = assert_perror(listxattr(filename, buf, sizeof(buf) - 1));
+ soft_assert(memmem(buf, ret, "user.T1", 7), "user.T1 not found");
+ soft_assert(!memmem(buf, ret, "user.T2", 7), "user.T2 found");
+ soft_assert(!memmem(buf, ret, "user.T3", 7), "user.T3 found");
+
+ ret = assert_perror(flistxattr(tmpfile, buf, sizeof(buf) - 1));
+ soft_assert(memmem(buf, ret, "user.T1", 7), "user.T1 not found");
+ soft_assert(!memmem(buf, ret, "user.T2", 7), "user.T2 found");
+ soft_assert(!memmem(buf, ret, "user.T3", 7), "user.T3 found");
+}