Module Name:    src
Committed By:   kamil
Date:           Sun Nov 20 18:02:46 UTC 2016

Modified Files:
        src/tests/lib/libpthread_dbg: t_threads.c

Log Message:
Add threads5 in tests/lib/libpthread_dbg/t_threads

Asserts that td_thr_getname() handles shorter buffer parameter and the
result is properly truncated.

Currently this test fails due to bug in pthread_dbg.

Sponsored by <The NetBSD Foundation>


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/lib/libpthread_dbg/t_threads.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libpthread_dbg/t_threads.c
diff -u src/tests/lib/libpthread_dbg/t_threads.c:1.4 src/tests/lib/libpthread_dbg/t_threads.c:1.5
--- src/tests/lib/libpthread_dbg/t_threads.c:1.4	Sun Nov 20 17:42:56 2016
+++ src/tests/lib/libpthread_dbg/t_threads.c	Sun Nov 20 18:02:46 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_threads.c,v 1.4 2016/11/20 17:42:56 kamil Exp $	*/
+/*	$NetBSD: t_threads.c,v 1.5 2016/11/20 18:02:46 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -28,16 +28,14 @@
 
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_threads.c,v 1.4 2016/11/20 17:42:56 kamil Exp $");
+__RCSID("$NetBSD: t_threads.c,v 1.5 2016/11/20 18:02:46 kamil Exp $");
 
 #include <dlfcn.h>
 #include <pthread.h>
 #include <pthread_dbg.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <stdlib.h>
 
 #include <atf-c.h>
 
@@ -319,6 +317,88 @@ ATF_TC_BODY(threads4, tc)
 	    count, max_threads + 1);
 }
 
+ATF_TC(threads5);
+ATF_TC_HEAD(threads5, tc)
+{
+
+	atf_tc_set_md_var(tc, "descr",
+	    "Asserts that td_thr_getname() handles shorter buffer parameter "
+	    "and the result is properly truncated");
+}
+
+static volatile int exiting5;
+
+static void *
+busyFunction5(void *arg)
+{
+
+	while (exiting5 == 0)
+		usleep(50000);
+
+	return NULL;
+}
+
+static int
+iterateThreads5(td_thread_t *thread, void *arg)
+{
+	int *counter = (int *)arg;
+	/* Arbitrarily short string buffer */
+	char name[3];
+
+	ATF_REQUIRE(td_thr_getname(thread, name, sizeof(name)) == TD_ERR_OK);
+
+	printf("Thread name: %s\n", name);
+
+	/* strlen(3) does not count including a '\0' character */
+	ATF_REQUIRE(strlen(name) < sizeof(name));
+
+	++(*counter);
+
+	return TD_ERR_OK;
+}
+
+ATF_TC_BODY(threads5, tc)
+{
+	struct td_proc_callbacks_t dummy_callbacks;
+	td_proc_t *main_ta;
+	const size_t max_threads = 10;
+	size_t i;
+	pthread_t threads[max_threads];
+	int count = 0;
+
+	dummy_callbacks.proc_read	= basic_proc_read;
+	dummy_callbacks.proc_write	= basic_proc_write;
+	dummy_callbacks.proc_lookup	= basic_proc_lookup;
+	dummy_callbacks.proc_regsize	= dummy_proc_regsize;
+	dummy_callbacks.proc_getregs	= dummy_proc_getregs;
+	dummy_callbacks.proc_setregs	= dummy_proc_setregs;
+
+	for (i = 0; i < max_threads; i++) {
+		printf("Creating thread %zu\n", i);
+		PTHREAD_REQUIRE
+		    (pthread_create(&threads[i], NULL, busyFunction5, NULL));
+	}
+
+	for (i = 0; i < max_threads; i++) {
+		PTHREAD_REQUIRE
+		    (pthread_setname_np(threads[i], "test_%d", (void*)i));
+	}
+
+	printf("Calling td_open(3)\n");
+	ATF_REQUIRE(td_open(&dummy_callbacks, NULL, &main_ta) == TD_ERR_OK);
+
+	ATF_REQUIRE(td_thr_iter(main_ta, iterateThreads5, &count) == TD_ERR_OK);
+
+	exiting5 = 1;
+
+	printf("Calling td_close(3)\n");
+	ATF_REQUIRE(td_close(main_ta) == TD_ERR_OK);
+
+	ATF_REQUIRE_EQ_MSG(count, max_threads + 1,
+	    "counted threads (%d) != expected threads (%d)",
+	    count, max_threads + 1);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
@@ -326,6 +406,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, threads2);
 	ATF_TP_ADD_TC(tp, threads3);
 	ATF_TP_ADD_TC(tp, threads4);
+	ATF_TP_ADD_TC(tp, threads5);
 
 	return atf_no_error();
 }

Reply via email to