Browse Source

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 <david@gibson.dropbear.id.au>
main
David Gibson 6 years ago
parent
commit
fd06c54d47
  1. 2
      tests/.gitignore
  2. 6
      tests/Makefile.tests
  3. 168
      tests/fs_tree1.c
  4. 11
      tests/run_tests.sh

2
tests/.gitignore vendored

@ -2,6 +2,7 @@
*.dts.test.s *.dts.test.s
*.test.dts *.test.dts
tmp.* tmp.*
/fs/
/add_subnode_with_nops /add_subnode_with_nops
/addr_size_cells /addr_size_cells
/addr_size_cells2 /addr_size_cells2
@ -28,6 +29,7 @@ tmp.*
/getprop /getprop
/incbin /incbin
/integer-expressions /integer-expressions
/fs_tree1
/mangle-layout /mangle-layout
/move_and_save /move_and_save
/node_check_compatible /node_check_compatible

6
tests/Makefile.tests

@ -27,7 +27,8 @@ LIB_TESTS_L = get_mem_rsv \
property_iterate \ property_iterate \
subnode_iterate \ subnode_iterate \
overlay overlay_bad_fixup \ 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)%) LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%)


LIBTREE_TESTS_L = truncated_property truncated_string truncated_memrsv 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 = *.output vglog.* vgcore.* *.dtb *.test.dts *.dtsv1 tmp.*
TESTS_CLEANFILES_L += dumptrees TESTS_CLEANFILES_L += dumptrees
TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%) TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%)
TESTS_CLEANDIRS_L = fs
TESTS_CLEANDIRS = $(TESTS_CLEANDIRS_L:%=$(TESTS_PREFIX)%)


.PHONY: tests .PHONY: tests
tests: $(TESTS) $(TESTS_TREES) tests: $(TESTS) $(TESTS_TREES)
@ -75,6 +78,7 @@ tests_clean:
@$(VECHO) CLEAN "(tests)" @$(VECHO) CLEAN "(tests)"
rm -f $(STD_CLEANFILES:%=$(TESTS_PREFIX)%) rm -f $(STD_CLEANFILES:%=$(TESTS_PREFIX)%)
rm -f $(TESTS_CLEANFILES) rm -f $(TESTS_CLEANFILES)
rm -rf $(TESTS_CLEANDIRS)


check: tests ${TESTS_BIN} $(TESTS_PYLIBFDT) check: tests ${TESTS_BIN} $(TESTS_PYLIBFDT)
cd $(TESTS_PREFIX); ./run_tests.sh cd $(TESTS_PREFIX); ./run_tests.sh

168
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <libfdt.h>

#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 <path>", 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();
}

11
tests/run_tests.sh

@ -448,10 +448,13 @@ libfdt_tests () {


run_test check_header test_tree1.dtb run_test check_header test_tree1.dtb


rm -rf fstree FSBASE=fs
mkdir fstree rm -rf $FSBASE
echo -n "foo" > fstree/non_empty_prop mkdir -p $FSBASE
run_dtc_test -I fs -O dts fstree 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 # check full tests
for good in test_tree1.dtb; do for good in test_tree1.dtb; do

Loading…
Cancel
Save