From fd06c54d4711f20d16bb1e18cba4d7bed09e5ad2 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Mon, 10 Sep 2018 12:59:53 +1000 Subject: tests: Better testing of dtc -I fs mode Greg Kurz added a trivial test of the -I fs mode recently, which was previously basically untested. This is an oversight, since we recently had a bug which completely broke it. This replaces Greg's test with a more thorough test of -I fs mode. We use a test helper to create the familiar test_tree1 in "fs" form, then use dtc -I fs to process it, and check that the results match what they should. We only check the content in -I fs -O dtb mode, since that's simplest, but we do run -I fs -O dts mode as well to make sure it doesn't blow up (the aforementioned bug caused just such a blow up, specific to -O dts mode, for example). Signed-off-by: David Gibson --- tests/.gitignore | 2 + tests/Makefile.tests | 6 +- tests/fs_tree1.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/run_tests.sh | 11 ++-- 4 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 tests/fs_tree1.c (limited to 'tests') diff --git a/tests/.gitignore b/tests/.gitignore index 7a99f5a..70a8c95 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -2,6 +2,7 @@ *.dts.test.s *.test.dts tmp.* +/fs/ /add_subnode_with_nops /addr_size_cells /addr_size_cells2 @@ -28,6 +29,7 @@ tmp.* /getprop /incbin /integer-expressions +/fs_tree1 /mangle-layout /move_and_save /node_check_compatible diff --git a/tests/Makefile.tests b/tests/Makefile.tests index e7cf6bc..6903333 100644 --- a/tests/Makefile.tests +++ b/tests/Makefile.tests @@ -27,7 +27,8 @@ LIB_TESTS_L = get_mem_rsv \ property_iterate \ subnode_iterate \ overlay overlay_bad_fixup \ - check_path check_header check_full + check_path check_header check_full \ + fs_tree1 LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv @@ -49,6 +50,8 @@ TESTS_DEPFILES = $(TESTS:%=%.d) \ TESTS_CLEANFILES_L = *.output vglog.* vgcore.* *.dtb *.test.dts *.dtsv1 tmp.* TESTS_CLEANFILES_L += dumptrees TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%) +TESTS_CLEANDIRS_L = fs +TESTS_CLEANDIRS = $(TESTS_CLEANDIRS_L:%=$(TESTS_PREFIX)%) .PHONY: tests tests: $(TESTS) $(TESTS_TREES) @@ -75,6 +78,7 @@ tests_clean: @$(VECHO) CLEAN "(tests)" rm -f $(STD_CLEANFILES:%=$(TESTS_PREFIX)%) rm -f $(TESTS_CLEANFILES) + rm -rf $(TESTS_CLEANDIRS) check: tests ${TESTS_BIN} $(TESTS_PYLIBFDT) cd $(TESTS_PREFIX); ./run_tests.sh diff --git a/tests/fs_tree1.c b/tests/fs_tree1.c new file mode 100644 index 0000000..5762465 --- /dev/null +++ b/tests/fs_tree1.c @@ -0,0 +1,168 @@ +/* + * libfdt - Flat Device Tree manipulation + * Testcase/tool constructing an fs tree for further test + * Copyright (C) 2018 David Gibson, Red Hat Inc. + * + * 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 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "tests.h" +#include "testdata.h" + +static void start_dir(const char *name) +{ + int rc; + + rc = mkdir(name, 0777); + if (rc != 0) + FAIL("mkdir(\"%s\"): %s", name, strerror(errno)); + + rc = chdir(name); + if (rc != 0) + FAIL("chdir(\"%s\"): %s", name, strerror(errno)); +} + +static void end_dir(void) +{ + int rc; + + rc = chdir(".."); + if (rc != 0) + FAIL("chdir(..): %s", strerror(errno)); +} + +static void mkfile(const char *name, void *data, size_t len) +{ + int fd; + int rc; + + fd = open(name, O_WRONLY|O_CREAT, 0666); + if (fd < 0) + FAIL("open(\"%s\"): %s", name, strerror(errno)); + + rc = write(fd, data, len); + if (rc < 0) + FAIL("write(\"%s\"): %s", name, strerror(errno)); + if (rc != len) + FAIL("write(\"%s\"): short write", name); + + rc = close(fd); + if (rc != 0) + FAIL("close(\"%s\"): %s", name, strerror(errno)); +} + +#define mkfile_str(name, s) \ + do { \ + char str[] = s; \ + mkfile((name), str, sizeof(str)); \ + } while (0) + +static void mkfile_u32(const char *name, uint32_t val) +{ + val = cpu_to_fdt32(val); + mkfile(name, &val, sizeof(val)); +} + +static void mkfile_u64(const char *name, uint64_t val) +{ + val = cpu_to_fdt64(val); + mkfile(name, &val, sizeof(val)); +} + +int main(int argc, char *argv[]) +{ + const char *base; + + test_init(argc, argv); + if (argc != 2) + CONFIG("Usage: %s ", argv[0]); + + base = argv[1]; + + start_dir(base); + mkfile_str("compatible", "test_tree1"); + mkfile_u32("prop-int", TEST_VALUE_1); + mkfile_u64("prop-int64", 0xdeadbeef01abcdefULL); + mkfile_str("prop-str", "hello world"); + mkfile_u32("#address-cells", 1); + mkfile_u32("#size-cells", 0); + + { + start_dir("subnode@1"); + + mkfile_str("compatible", "subnode1"); + mkfile_u32("reg", 1); + mkfile_u32("prop-int", TEST_VALUE_1); + + { + start_dir("subsubnode"); + + mkfile_str("compatible", "subsubnode1\0subsubnode"); + mkfile_str("placeholder", "this is a placeholder string\0string2"); + mkfile_u32("prop-int", TEST_VALUE_1); + + end_dir(); + } + + { + start_dir("ss1"); + end_dir(); + } + + end_dir(); + } + + { + start_dir("subnode@2"); + + mkfile_u32("reg", 2); + mkfile_u32("linux,phandle", 0x2000); + mkfile_u32("prop-int", TEST_VALUE_2); + mkfile_u32("#address-cells", 1); + mkfile_u32("#size-cells", 0); + + { + start_dir("subsubnode@0"); + + mkfile_u32("reg", 0); + mkfile_u32("phandle", 0x2001); + mkfile_str("compatible", "subsubnode2\0subsubnode"); + mkfile_u32("prop-int", TEST_VALUE_2); + + end_dir(); + } + + { + start_dir("ss2"); + end_dir(); + } + + end_dir(); + } + + PASS(); +} diff --git a/tests/run_tests.sh b/tests/run_tests.sh index e240356..bbdc5c8 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -448,10 +448,13 @@ libfdt_tests () { run_test check_header test_tree1.dtb - rm -rf fstree - mkdir fstree - echo -n "foo" > fstree/non_empty_prop - run_dtc_test -I fs -O dts fstree + FSBASE=fs + rm -rf $FSBASE + mkdir -p $FSBASE + run_test fs_tree1 $FSBASE/test_tree1 + run_dtc_test -I fs -O dts -o fs.test_tree1.test.dts $FSBASE/test_tree1 + run_dtc_test -I fs -O dtb -o fs.test_tree1.test.dtb $FSBASE/test_tree1 + run_test dtbs_equal_unordered -m fs.test_tree1.test.dtb test_tree1.dtb # check full tests for good in test_tree1.dtb; do -- cgit v1.1