[Pdbg] [PATCH 13/13] tests: Add test for libpdbg property api

Amitay Isaacs amitay at ozlabs.org
Wed Jan 15 16:19:01 AEDT 2020


Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
 Makefile.am                   |  10 ++-
 fake.dts.m4                   |   2 +
 src/tests/libpdbg_prop_test.c | 120 ++++++++++++++++++++++++++++++++++
 tests/test_prop.sh            |  55 ++++++++++++++++
 4 files changed, 186 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/libpdbg_prop_test.c
 create mode 100755 tests/test_prop.sh

diff --git a/Makefile.am b/Makefile.am
index db93faf..a4ca5f3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,8 @@ libpdbg_tests = libpdbg_target_test \
 bin_PROGRAMS = pdbg
 check_PROGRAMS = $(libpdbg_tests) libpdbg_dtree_test \
 		libpdbg_p9_fapi_translation_test \
-		optcmd_test hexdump_test cronus_proxy
+		optcmd_test hexdump_test cronus_proxy \
+		libpdbg_prop_test
 
 PDBG_TESTS = \
 	tests/test_selection.sh 	\
@@ -26,11 +27,13 @@ PDBG_TESTS = \
 	tests/test_hexdump.sh		\
 	tests/test_tree.sh		\
 	tests/test_tree2.sh		\
+	tests/test_prop.sh		\
 	tests/test_p9_fapi_translation.sh
 
 TESTS = $(libpdbg_tests) optcmd_test $(PDBG_TESTS)
 
 tests/test_tree2.sh: fake2.dtb
+tests/test_prop.sh: fake.dtb
 
 test: $(libpdbg_tests)
 
@@ -266,6 +269,11 @@ libpdbg_p9_fapi_translation_test_LDFLAGS = $(libpdbg_test_ldflags)
 libpdbg_p9_fapi_translation_test_LDADD = $(libpdbg_test_ldadd)
 EXTRA_libpdbg_p9_fapi_translation_test_DEPENDENCIES = p9.dtb p9-kernel.dtb
 
+libpdbg_prop_test_SOURCES = src/tests/libpdbg_prop_test.c
+libpdbg_prop_test_CFLAGS = $(libpdbg_test_cflags)
+libpdbg_prop_test_LDFLAGS = $(libpdbg_test_ldflags)
+libpdbg_prop_test_LDADD = $(libpdbg_test_ldadd)
+
 M4_V = $(M4_V_$(V))
 M4_V_ = $(M4_V_$(AM_DEFAULT_VERBOSITY))
 M4_V_0 = @echo "  M4      " $@;
diff --git a/fake.dts.m4 b/fake.dts.m4
index a099154..3d49bef 100644
--- a/fake.dts.m4
+++ b/fake.dts.m4
@@ -91,6 +91,8 @@ define(`dump_backend',dnl
         system-path = "/proc$1/pib";
         reg = <CONCAT(0x,pib_addr) 0x0>;
         index = <0x$1>;
+	ATTR1 = <0xc0ffee>;
+	ATTR2 = "processor$1";
       };
     };
 
diff --git a/src/tests/libpdbg_prop_test.c b/src/tests/libpdbg_prop_test.c
new file mode 100644
index 0000000..c916dcc
--- /dev/null
+++ b/src/tests/libpdbg_prop_test.c
@@ -0,0 +1,120 @@
+/* Copyright 2020 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <endian.h>
+#include <string.h>
+#include <assert.h>
+
+#include <libpdbg.h>
+
+static void usage(void)
+{
+	fprintf(stderr, "Usage: libpdbg_fdt_test <path> read <prop> char|int\n");
+	fprintf(stderr, "       libpdbg_fdt_test <path> write <prop> char|int <value>\n");
+	exit(1);
+}
+
+int main(int argc, const char **argv)
+{
+	struct pdbg_target *target;
+	const char *path, *prop, *prop_value = NULL;
+	bool do_read = false, do_write = false;
+	bool is_int;
+
+	if (argc < 5)
+		usage();
+
+	path = argv[1];
+
+	if (strcmp(argv[2], "read") == 0)
+		do_read = true;
+	else if (strcmp(argv[2], "write") == 0)
+		do_write = true;
+	else
+		usage();
+
+	prop = argv[3];
+
+	if (strcmp(argv[4], "int") == 0)
+		is_int = true;
+	else if (strcmp(argv[4], "char") == 0)
+		is_int = false;
+	else
+		usage();
+
+	if (do_read && argc != 5)
+		usage();
+
+	if (do_write && argc != 6)
+		usage();
+	else
+		prop_value = argv[5];
+
+
+	pdbg_set_backend(PDBG_BACKEND_FAKE, NULL);
+	assert(pdbg_targets_init(NULL));
+
+	target = pdbg_target_from_path(NULL, path);
+	if (!target)
+		exit(1);
+
+	if (do_read) {
+		if (is_int) {
+			uint32_t value;
+			int ret;
+
+			ret = pdbg_target_u32_property(target, prop, &value);
+			if (ret)
+				exit(88);
+
+			printf("0x%08x\n", value);
+		} else {
+			const void *buf;
+			size_t buflen;
+
+			buf = pdbg_target_property(target, prop, &buflen);
+			if (!buf)
+				exit(88);
+
+			printf("%s<%zu>\n", (const char *)buf, buflen);
+		}
+
+	}
+
+	if (do_write) {
+		bool ok;
+
+		if (is_int) {
+			uint32_t value;
+
+			value = strtoul(prop_value, NULL, 0);
+			value = htobe32(value);
+
+			ok = pdbg_target_set_property(target, prop, &value, 4);
+		} else {
+			size_t len = strlen(prop_value) + 1;
+
+			ok = pdbg_target_set_property(target, prop, prop_value, len);
+		}
+
+		if (!ok)
+			exit(99);
+	}
+
+	return 0;
+}
diff --git a/tests/test_prop.sh b/tests/test_prop.sh
new file mode 100755
index 0000000..e2e511f
--- /dev/null
+++ b/tests/test_prop.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+. $(dirname "$0")/driver.sh
+
+test_group "libpdbg property tests"
+
+test_result 88 --
+test_run libpdbg_prop_test /proc0/pib read ATTR0 int
+
+test_result 88 --
+test_run libpdbg_prop_test /proc1/pib read ATTR0 char
+
+test_result 0 <<EOF
+0x00c0ffee
+EOF
+test_run libpdbg_prop_test /proc0/pib read ATTR1 int
+
+test_result 0 <<EOF
+0x00c0ffee
+EOF
+test_run libpdbg_prop_test /proc1/pib read ATTR1 int
+
+test_result 0 <<EOF
+processor0<11>
+EOF
+test_run libpdbg_prop_test /proc0/pib read ATTR2 char
+
+test_result 0 <<EOF
+processor1<11>
+EOF
+test_run libpdbg_prop_test /proc1/pib read ATTR2 char
+
+test_result 99 --
+test_run libpdbg_prop_test /proc0/pib write ATTR1 int 0xdeadbeef
+
+test_result 99 --
+test_run libpdbg_prop_test /proc0/pib write ATTR2 char PROCESSOR0
+
+export PDBG_DTB=fake.dtb
+
+test_result 0 --
+test_run libpdbg_prop_test /proc1/pib write ATTR1 int 0xdeadbeef
+
+test_result 0 --
+test_run libpdbg_prop_test /proc1/pib write ATTR2 char PROCESSOR0
+
+test_result 0 <<EOF
+0xdeadbeef
+EOF
+test_run libpdbg_prop_test /proc1/pib read ATTR1 int
+
+test_result 0 <<EOF
+PROCESSOR0<11>
+EOF
+test_run libpdbg_prop_test /proc1/pib read ATTR2 char
-- 
2.21.1



More information about the Pdbg mailing list