Merge libfdt into dtc.

Having pulled the libfdt repository into dtc, merge the makefiles and
testsuites so that they build together usefully.
This commit is contained in:
David Gibson 2007-06-14 15:05:55 +10:00
parent 400bd98a3a
commit 12578976fe
31 changed files with 95 additions and 105 deletions

View file

@ -1,12 +1,67 @@
DTC = ../dtc
VG_DTC = valgrind --tool=memcheck ../dtc
check: all
./run_all_tests.sh
LIB_TESTS = root_node find_property subnode_offset path_offset getprop \
notfound \
setprop_inplace nop_property nop_node \
sw_tree1 \
move_and_save \
open_pack rw_tree1 setprop del_property del_node
LIBTREE_TESTS = truncated_property
TESTS = $(LIB_TESTS) $(LIBTREE_TESTS)
UTILS = dumptrees
all:
TREES = test_tree1.dtb
CFLAGS = -Wall -g
CPPFLAGS = -I../libfdt
LDFLAGS = -L../libfdt
LIBFDT = ../libfdt/libfdt.a
ifdef V
VECHO = :
else
VECHO = echo " "
.SILENT:
endif
DEPFILES = $(TESTS:%=%.d) testutils.d
check: all
./run_tests.sh
all: $(TESTS) $(TREES)
%.o: %.c
@$(VECHO) CC $@
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
%.o: %.S
@$(VECHO) AS $@
$(CC) -D__ASSEMBLY__ $(CPPFLAGS) -o $@ -c $<
%: %.o
@$(VECHO) LD $@
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(LIB_TESTS): %: testutils.o $(LIBFDT)
$(LIBTREE_TESTS): %: testutils.o trees.o $(LIBFDT)
dumptrees: %: trees.o
$(TREES): dumptrees
@$(VECHO) DUMPTREES
./dumptrees >/dev/null
clean:
rm -f *~
rm -f $(TESTS)
rm -f *.dtb dumptrees
rm -f *~ *.d *.o a.out core
rm -f *.i *.output vgcore.*
%.d: %.c
@$(CC) $(CPPFLAGS) -MM -MT "$*.o $@" $< > $@
-include $(DEPFILES)

122
tests/del_node.c Normal file
View file

@ -0,0 +1,122 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
int oldsize, delsize, newsize;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
oldsize = fdt_totalsize(fdt);
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_del_node(fdt, subnode1_offset);
if (err)
FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_del_node(fdt, subnode2_offset);
if (err)
FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
delsize = fdt_totalsize(fdt);
err = fdt_pack(fdt);
if (err)
FAIL("fdt_pack(): %s", fdt_strerror(err));
newsize = fdt_totalsize(fdt);
verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
oldsize, delsize, newsize);
if (newsize >= oldsize)
FAIL("Tree failed to shrink after deletions");
PASS();
}

89
tests/del_property.c Normal file
View file

@ -0,0 +1,89 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_delprop()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
int err, lenerr;
int oldsize, delsize, newsize;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
oldsize = fdt_totalsize(fdt);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("int value was 0x%08x\n", *intp);
err = fdt_delprop(fdt, 0, "prop-int");
if (err)
FAIL("Failed to delete \"prop-int\": %s", fdt_strerror(err));
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after deletion");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("string value was \"%s\"\n", strp);
err = fdt_delprop(fdt, 0, "prop-str");
if (err)
FAIL("Failed to delete \"prop-str\": %s", fdt_strerror(err));
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after deletion");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));
delsize = fdt_totalsize(fdt);
err = fdt_pack(fdt);
if (err)
FAIL("fdt_pack(): %s\n", fdt_strerror(err));
newsize = fdt_totalsize(fdt);
verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
oldsize, delsize, newsize);
if (newsize >= oldsize)
FAIL("Tree failed to shrink after deletions");
PASS();
}

49
tests/dumptrees.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include <libfdt_env.h>
#include "testdata.h"
struct {
void *blob;
const char *filename;
} trees[] = {
#define TREE(name) { &_##name, #name ".dtb" }
TREE(test_tree1),
};
#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < NUM_TREES; i++) {
void *blob = trees[i].blob;
const char *filename = trees[i].filename;
int size;
int fd;
int ret;
size = fdt_totalsize(blob);
printf("Tree \"%s\", %d bytes\n", filename, size);
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
perror("open()");
ret = write(fd, blob, size);
if (ret != size)
perror("write()");
close(fd);
}
exit(0);
}

42
tests/find_property.c Normal file
View file

@ -0,0 +1,42 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_property_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
check_property_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_property(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

43
tests/getprop.c Normal file
View file

@ -0,0 +1,43 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_getprop()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

76
tests/move_and_save.c Normal file
View file

@ -0,0 +1,76 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt, *fdt1, *fdt2, *fdt3;
void *buf;
int shuntsize;
int bufsize;
int err;
const char *inname;
char outname[PATH_MAX];
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
inname = argv[1];
shuntsize = ALIGN(fdt_totalsize(fdt) / 2, sizeof(uint64_t));
bufsize = fdt_totalsize(fdt) + shuntsize;
buf = xmalloc(bufsize);
fdt1 = buf;
err = fdt_move(fdt, fdt1, bufsize);
if (err)
FAIL("Failed to move tree into new buffer: %s",
fdt_strerror(err));
sprintf(outname, "moved.%s", inname);
save_blob(outname, fdt1);
fdt2 = buf + shuntsize;
err = fdt_move(fdt1, fdt2, bufsize-shuntsize);
if (err)
FAIL("Failed to shunt tree %d bytes: %s",
shuntsize, fdt_strerror(err));
sprintf(outname, "shunted.%s", inname);
save_blob(outname, fdt2);
fdt3 = buf;
err = fdt_move(fdt2, fdt3, bufsize);
if (err)
FAIL("Failed to deshunt tree %d bytes: %s",
shuntsize, fdt_strerror(err));
sprintf(outname, "deshunted.%s", inname);
save_blob(outname, fdt3);
PASS();
}

105
tests/nop_node.c Normal file
View file

@ -0,0 +1,105 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode1_offset);
if (err)
FAIL("fdt_nop_node(subnode1): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode2_offset);
if (err)
FAIL("fdt_nop_node(subnode2): %s", fdt_strerror(err));
subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));
PASS();
}

71
tests/nop_property.c Normal file
View file

@ -0,0 +1,71 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_property()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
int err;
int lenerr;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("int value was 0x%08x\n", *intp);
err = fdt_nop_property(fdt, 0, "prop-int");
if (err)
FAIL("Failed to nop \"prop-int\": %s", fdt_strerror(err));
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after nopping");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("string value was \"%s\"\n", strp);
err = fdt_nop_property(fdt, 0, "prop-str");
if (err)
FAIL("Failed to nop \"prop-str\": %s", fdt_strerror(err));
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after nopping");
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
PASS();
}

73
tests/notfound.c Normal file
View file

@ -0,0 +1,73 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for behaviour on searching for a non-existent node
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
void check_error(const char *s, int err)
{
if (err != -FDT_ERR_NOTFOUND)
FAIL("%s return error %s instead of -FDT_ERR_NOTFOUND", s,
fdt_strerror(err));
}
int main(int argc, char *argv[])
{
const struct fdt_property *prop;
void *fdt;
int offset;
int subnode1_offset;
const void *val;
int lenerr;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
prop = fdt_get_property(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_get_property(\"nonexistant-property\")", lenerr);
val = fdt_getprop(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_getprop(\"nonexistant-property\"", lenerr);
subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode1");
if (subnode1_offset < 0)
FAIL("Couldn't find subnode1: %s", fdt_strerror(subnode1_offset));
val = fdt_getprop(fdt, subnode1_offset, "prop-str", &lenerr);
check_error("fdt_getprop(\"prop-str\")", lenerr);
offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
check_error("fdt_subnode_offset(\"nonexistant-subnode\")", offset);
offset = fdt_subnode_offset(fdt, 0, "subsubnode");
check_error("fdt_subnode_offset(\"subsubnode\")", offset);
offset = fdt_path_offset(fdt, "/nonexistant-subnode");
check_error("fdt_path_offset(\"/nonexistant-subnode\")", offset);
PASS();
}

70
tests/open_pack.c Normal file
View file

@ -0,0 +1,70 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt, *fdt1;
void *buf;
int oldsize, bufsize, packsize;
int err;
const char *inname;
char outname[PATH_MAX];
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
inname = argv[1];
oldsize = fdt_totalsize(fdt);
bufsize = oldsize * 2;
buf = xmalloc(bufsize);
fdt1 = buf;
err = fdt_open_into(fdt, fdt1, bufsize);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));
sprintf(outname, "opened.%s", inname);
save_blob(outname, fdt1);
err = fdt_pack(fdt1);
if (err)
FAIL("fdt_pack(): %s", fdt_strerror(err));
sprintf(outname, "repacked.%s", inname);
save_blob(outname, fdt1);
packsize = fdt_totalsize(fdt1);
verbose_printf("oldsize = %d, bufsize = %d, packsize = %d\n",
oldsize, bufsize, packsize);
PASS();
}

98
tests/path_offset.c Normal file
View file

@ -0,0 +1,98 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_path_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int check_subnode(void *fdt, int parent, const char *name)
{
int offset;
struct fdt_node_header *nh;
uint32_t tag;
verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset;
int subnode1_offset_p, subnode2_offset_p;
int subsubnode1_offset, subsubnode2_offset;
int subsubnode1_offset_p, subsubnode2_offset_p;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
subnode1_offset_p = fdt_path_offset(fdt, "/subnode1");
subnode2_offset_p = fdt_path_offset(fdt, "/subnode2");
if (subnode1_offset != subnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode1_offset, subnode1_offset_p);
if (subnode2_offset != subnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode2_offset, subnode2_offset_p);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
subsubnode1_offset_p = fdt_path_offset(fdt, "/subnode1/subsubnode");
subsubnode2_offset_p = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode1_offset != subsubnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode1_offset, subsubnode1_offset_p);
if (subsubnode2_offset != subsubnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode2_offset, subsubnode2_offset_p);
PASS();
}

53
tests/root_node.c Normal file
View file

@ -0,0 +1,53 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
struct fdt_node_header *nh;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
nh = fdt_offset_ptr_typed(fdt, 0, nh);
if (! nh)
FAIL("NULL retrieving root node");
if (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE)
FAIL("Wrong tag on root node");
if (strlen(nh->name) != 0)
FAIL("Wrong name for root node, \"%s\" instead of empty",
nh->name);
PASS();
}

View file

@ -1,2 +0,0 @@
#! /bin/sh

94
tests/run_tests.sh Executable file
View file

@ -0,0 +1,94 @@
#! /bin/bash
export QUIET_TEST=1
ENV=/usr/bin/env
run_test () {
echo -n "$@: "
PATH=".:$PATH" $ENV "$@"
}
tree1_tests () {
TREE=$1
# Read-only tests
run_test root_node $TREE
run_test find_property $TREE
run_test subnode_offset $TREE
run_test path_offset $TREE
run_test getprop $TREE
run_test notfound $TREE
# Write-in-place tests
run_test setprop_inplace $TREE
run_test nop_property $TREE
run_test nop_node $TREE
}
functional_tests () {
# Make sure we don't have stale blobs lying around
rm -f *.test.dtb
tree1_tests test_tree1.dtb
# Sequential write tests
run_test sw_tree1
tree1_tests sw_tree1.test.dtb
tree1_tests unfinished_tree1.test.dtb
# fdt_move tests
for tree in test_tree1.dtb sw_tree1.test.dtb unfinished_tree1.test.dtb; do
rm -f moved.$tree shunted.$tree deshunted.$tree
run_test move_and_save $tree
tree1_tests moved.$tree
tree1_tests shunted.$tree
tree1_tests deshunted.$tree
done
# Read-write tests
for tree in test_tree1.dtb sw_tree1.test.dtb; do
rm -f opened.$tree repacked.$tree
run_test open_pack $tree
tree1_tests opened.$tree
tree1_tests repacked.$tree
done
run_test setprop test_tree1.dtb
run_test del_property test_tree1.dtb
run_test del_node test_tree1.dtb
run_test rw_tree1
tree1_tests rw_tree1.test.dtb
# Tests for behaviour on various sorts of corrupted trees
run_test truncated_property
}
stress_tests () {
ITERATIONS=10 # Number of iterations for looping tests
}
while getopts "vdt:" ARG ; do
case $ARG in
"v")
unset QUIET_TEST
;;
"t")
TESTSETS=$OPTARG
;;
esac
done
if [ -z "$TESTSETS" ]; then
TESTSETS="func stress"
fi
for set in $TESTSETS; do
case $set in
"func")
functional_tests
;;
"stress")
stress_tests
;;
esac
done

91
tests/rw_tree1.c Normal file
View file

@ -0,0 +1,91 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define CHECK(code) \
{ \
err = (code); \
if (err) \
FAIL(#code ": %s", fdt_strerror(err)); \
}
#define OFF_CHECK(off, code) \
{ \
(off) = (code); \
if (off < 0) \
FAIL(#code ": %s", fdt_strerror(off)); \
}
int main(int argc, char *argv[])
{
void *fdt;
int err;
int offset;
test_init(argc, argv);
fdt = xmalloc(SPACE);
/* First create empty tree with SW */
CHECK(fdt_create(fdt, SPACE));
CHECK(fdt_finish_reservemap(fdt));
CHECK(fdt_begin_node(fdt, ""));
CHECK(fdt_end_node(fdt));
CHECK(fdt_finish(fdt));
verbose_printf("Built empty tree, totalsize = %d\n",
fdt_totalsize(fdt));
CHECK(fdt_open_into(fdt, fdt, SPACE));
CHECK(fdt_setprop_typed(fdt, 0, "prop-int", TEST_VALUE_1));
CHECK(fdt_setprop_string(fdt, 0, "prop-str", TEST_STRING_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode1"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode2"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_2));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode"));
CHECK(fdt_setprop_typed(fdt, offset, "prop-int", TEST_VALUE_2));
CHECK(fdt_pack(fdt));
save_blob("rw_tree1.test.dtb", fdt);
PASS();
}

78
tests/setprop.c Normal file
View file

@ -0,0 +1,78 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_setprop()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define NEW_STRING "here is quite a long test string, blah blah blah"
int main(int argc, char *argv[])
{
void *fdt;
void *buf;
const uint32_t *intp;
const char *strp;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
buf = xmalloc(SPACE);
err = fdt_open_into(fdt, buf, SPACE);
if (err)
FAIL("fdt_open_into(): %s", fdt_strerror(err));
fdt = buf;
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("Old int value was 0x%08x\n", *intp);
err = fdt_setprop_string(fdt, 0, "prop-int", NEW_STRING);
if (err)
FAIL("Failed to set \"prop-int\" to \"%s\": %s",
NEW_STRING, fdt_strerror(err));
strp = check_getprop_string(fdt, 0, "prop-int", NEW_STRING);
verbose_printf("New value is \"%s\"\n", strp);
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("Old string value was \"%s\"\n", strp);
err = fdt_setprop(fdt, 0, "prop-str", NULL, 0);
if (err)
FAIL("Failed to empty \"prop-str\": %s",
fdt_strerror(err));
check_getprop(fdt, 0, "prop-str", 0, NULL);
PASS();
}

72
tests/setprop_inplace.c Normal file
View file

@ -0,0 +1,72 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_setprop_inplace()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt;
const uint32_t *intp;
const char *strp;
char *xstr;
int xlen, i;
int err;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("Old int value was 0x%08x\n", *intp);
err = fdt_setprop_inplace_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
if (err)
FAIL("Failed to set \"prop-int\" to 0x08%x: %s",
~TEST_VALUE_1, fdt_strerror(err));
intp = check_getprop_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
verbose_printf("New int value is 0x%08x\n", *intp);
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("Old string value was \"%s\"\n", strp);
xstr = strdup(strp);
xlen = strlen(xstr);
for (i = 0; i < xlen; i++)
xstr[i] = toupper(xstr[i]);
err = fdt_setprop_inplace(fdt, 0, "prop-str", xstr, xlen+1);
if (err)
FAIL("Failed to set \"prop-str\" to \"%s\": %s",
xstr, fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", xlen+1, xstr);
verbose_printf("New string value is \"%s\"\n", strp);
PASS();
}

83
tests/subnode_offset.c Normal file
View file

@ -0,0 +1,83 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_subnode_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int check_subnode(struct fdt_header *fdt, int parent, const char *name)
{
int offset;
struct fdt_node_header *nh;
uint32_t tag;
verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
void *fdt;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset;
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
if (subnode1_offset == subnode2_offset)
FAIL("Different subnodes have same offset");
check_property_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
check_property_typed(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
PASS();
}

83
tests/sw_tree1.c Normal file
View file

@ -0,0 +1,83 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <ctype.h>
#include <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
#define SPACE 65536
#define CHECK(code) \
{ \
err = (code); \
if (err) \
FAIL(#code ": %s", fdt_strerror(err)); \
}
int main(int argc, char *argv[])
{
void *fdt;
int err;
test_init(argc, argv);
fdt = xmalloc(SPACE);
CHECK(fdt_create(fdt, SPACE));
CHECK(fdt_finish_reservemap(fdt));
CHECK(fdt_begin_node(fdt, ""));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_property_string(fdt, "prop-str", TEST_STRING_1));
CHECK(fdt_begin_node(fdt, "subnode1"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_begin_node(fdt, "subsubnode"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
CHECK(fdt_begin_node(fdt, "subnode2"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_2));
CHECK(fdt_begin_node(fdt, "subsubnode"));
CHECK(fdt_property_typed(fdt, "prop-int", TEST_VALUE_2));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt));
save_blob("unfinished_tree1.test.dtb", fdt);
CHECK(fdt_finish(fdt));
verbose_printf("Completed tree, totalsize = %d\n",
fdt_totalsize(fdt));
save_blob("sw_tree1.test.dtb", fdt);
PASS();
}

9
tests/testdata.h Normal file
View file

@ -0,0 +1,9 @@
#define TEST_VALUE_1 0xdeadbeef
#define TEST_VALUE_2 0xabcd1234
#define TEST_STRING_1 "hello world"
#ifndef __ASSEMBLY__
extern struct fdt_header _test_tree1;
extern struct fdt_header _truncated_property;
#endif /* ! __ASSEMBLY */

133
tests/tests.h Normal file
View file

@ -0,0 +1,133 @@
#ifndef _TESTS_H
#define _TESTS_H
/*
* libfdt - Flat Device Tree manipulation
* Testcase definitions
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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
*/
#define DEBUG
/* Test return codes */
#define RC_PASS 0
#define RC_CONFIG 1
#define RC_FAIL 2
#define RC_BUG 99
extern int verbose_test;
extern char *test_name;
void test_init(int argc, char *argv[]);
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define PALIGN(p, a) ((void *)ALIGN((unsigned long)(p), (a)))
#define streq(s1, s2) (strcmp((s1),(s2)) == 0)
/* Each test case must define this function */
void cleanup(void);
#define verbose_printf(...) \
if (verbose_test) { \
printf(__VA_ARGS__); \
fflush(stdout); \
}
#define ERR "ERR: "
#define ERROR(fmt, args...) fprintf(stderr, ERR fmt, ## args)
#define PASS() \
do { \
cleanup(); \
printf("PASS\n"); \
exit(RC_PASS); \
} while (0)
#define PASS_INCONCLUSIVE() \
do { \
cleanup(); \
printf("PASS (inconclusive)\n"); \
exit(RC_PASS); \
} while (0)
#define IRRELEVANT() \
do { \
cleanup(); \
printf("PASS (irrelevant)\n"); \
exit(RC_PASS); \
} while (0)
/* Look out, gcc extension below... */
#define FAIL(fmt, ...) \
do { \
cleanup(); \
printf("FAIL\t" fmt "\n", ##__VA_ARGS__); \
exit(RC_FAIL); \
} while (0)
#define CONFIG(fmt, ...) \
do { \
cleanup(); \
printf("Bad configuration: " fmt "\n", ##__VA_ARGS__); \
exit(RC_CONFIG); \
} while (0)
#define TEST_BUG(fmt, ...) \
do { \
cleanup(); \
printf("BUG in testsuite: " fmt "\n", ##__VA_ARGS__); \
exit(RC_BUG); \
} while (0)
static inline void *xmalloc(size_t size)
{
void *p = malloc(size);
if (! p)
FAIL("malloc() failure");
return p;
}
static inline void *xrealloc(void *p, size_t size)
{
p = realloc(p, size);
if (! p)
FAIL("realloc() failure");
return p;
}
void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_property_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_property(fdt, nodeoffset, name, sizeof(x), &x); \
})
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_getprop_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
})
#define check_getprop_string(fdt, nodeoffset, name, s) \
check_getprop((fdt), (nodeoffset), (name), strlen(s)+1, (s))
//void *load_blob(const char *filename);
void *load_blob_arg(int argc, char *argv[]);
void save_blob(const char *filename, void *blob);
#endif /* _TESTS_H */

193
tests/testutils.c Normal file
View file

@ -0,0 +1,193 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase common utility functions
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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
*/
#define _GNU_SOURCE /* for strsignal() */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <libfdt.h>
#include "tests.h"
int verbose_test = 1;
char *test_name;
void __attribute__((weak)) cleanup(void)
{
}
static void sigint_handler(int signum, siginfo_t *si, void *uc)
{
cleanup();
fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
strsignal(signum), getpid());
exit(RC_BUG);
}
void test_init(int argc, char *argv[])
{
int err;
struct sigaction sa_int = {
.sa_sigaction = sigint_handler,
};
test_name = argv[0];
err = sigaction(SIGINT, &sa_int, NULL);
if (err)
FAIL("Can't install SIGINT handler");
if (getenv("QUIET_TEST"))
verbose_test = 0;
verbose_printf("Starting testcase \"%s\", pid %d\n",
test_name, getpid());
}
void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const struct fdt_property *prop;
int retlen;
uint32_t tag, nameoff, proplen;
const char *propname;
verbose_printf("Checking property \"%s\"...", name);
prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
verbose_printf("pointer %p\n", prop);
if (! prop)
FAIL("Error retreiving \"%s\" pointer: %s", name,
fdt_strerror(retlen));
tag = fdt32_to_cpu(prop->tag);
nameoff = fdt32_to_cpu(prop->nameoff);
proplen = fdt32_to_cpu(prop->len);
if (tag != FDT_PROP)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
propname = fdt_string(fdt, nameoff);
if (!propname || !streq(propname, name))
FAIL("Property name mismatch \"%s\" instead of \"%s\"",
propname, name);
if (proplen != retlen)
FAIL("Length retrieved for \"%s\" by fdt_get_property()"
" differs from stored length (%d != %d)",
name, retlen, proplen);
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, prop->data, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
}
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const void *propval;
int proplen;
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if (! propval)
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, propval, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
return propval;
}
#define CHUNKSIZE 128
void *load_blob(const char *filename)
{
int fd;
int offset = 0;
int bufsize = 1024;
char *p = NULL;
int ret;
fd = open(filename, O_RDONLY);
if (fd < 0)
CONFIG("Couldn't open blob from \"%s\": %s", filename,
strerror(errno));
p = xmalloc(bufsize);
do {
if (offset == bufsize) {
bufsize *= 2;
p = xrealloc(p, bufsize);
}
ret = read(fd, &p[offset], bufsize - offset);
if (ret < 0)
CONFIG("Couldn't read from \"%s\": %s", filename,
strerror(errno));
offset += ret;
} while (ret != 0);
return p;
}
void *load_blob_arg(int argc, char *argv[])
{
if (argc != 2)
CONFIG("Usage: %s <dtb file>", argv[0]);
return load_blob(argv[1]);
}
void save_blob(const char *filename, void *fdt)
{
int fd;
int totalsize;
int offset;
void *p;
int ret;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
CONFIG("Couldn't open \"%s\" to write blob: %s", filename,
strerror(errno));
totalsize = fdt_totalsize(fdt);
offset = 0;
p = fdt;
while (offset < totalsize) {
ret = write(fd, p + offset, totalsize - offset);
if (ret < 0)
CONFIG("Couldn't write to \"%s\": %s", filename,
strerror(errno));
offset += ret;
}
}

116
tests/trees.S Normal file
View file

@ -0,0 +1,116 @@
#include <fdt.h>
#include "testdata.h"
#define FDTLONG(val) \
.byte ((val) >> 24) & 0xff ; \
.byte ((val) >> 16) & 0xff ; \
.byte ((val) >> 8) & 0xff ; \
.byte (val) & 0xff
#define FDTQUAD(val) \
.byte ((val) >> 56) & 0xff ; \
.byte ((val) >> 48) & 0xff ; \
.byte ((val) >> 40) & 0xff ; \
.byte ((val) >> 32) & 0xff ; \
.byte ((val) >> 24) & 0xff ; \
.byte ((val) >> 16) & 0xff ; \
.byte ((val) >> 8) & 0xff ; \
.byte (val) & 0xff
#define TREE_HDR(tree) \
.balign 4 ; \
.globl _##tree ; \
_##tree: \
tree: \
FDTLONG(FDT_MAGIC) ; \
FDTLONG(tree##_end - tree) ; \
FDTLONG(tree##_struct - tree) ; \
FDTLONG(tree##_strings - tree) ; \
FDTLONG(tree##_rsvmap - tree) ; \
FDTLONG(0x11) ; \
FDTLONG(0x10) ; \
FDTLONG(0) ; \
FDTLONG(tree##_end - tree##_strings) ; \
FDTLONG(tree##_strings - tree##_struct) ;
#define RSVMAP_ENTRY(addr, len) \
FDTQUAD(addr) ; \
FDTQUAD(len) ;
#define PROPHDR(tree, name, len) \
FDTLONG(FDT_PROP) ; \
FDTLONG(len) ; \
FDTLONG(tree##_##name - tree##_strings) ;
#define PROP_INT(tree, name, val) \
PROPHDR(tree, name, 4) \
/* For ease of testing the property values go in native-endian */ \
.long val
#define PROP_STR(tree, name, str) \
PROPHDR(tree, name, 55f - 54f) \
54: \
.string str ; \
55: \
.balign 4
#define BEGIN_NODE(name) \
FDTLONG(FDT_BEGIN_NODE) ; \
.string name ; \
.balign 4
#define END_NODE \
FDTLONG(FDT_END_NODE) ;
#define STRING(tree, name, str) \
tree##_##name: \
.string str
.data
TREE_HDR(test_tree1)
test_tree1_rsvmap:
RSVMAP_ENTRY(0, 0)
test_tree1_struct:
BEGIN_NODE("")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
PROP_STR(test_tree1, prop_str, TEST_STRING_1)
BEGIN_NODE("subnode1")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
END_NODE
END_NODE
BEGIN_NODE("subnode2")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
END_NODE
END_NODE
END_NODE
FDTLONG(FDT_END)
test_tree1_strings:
STRING(test_tree1, prop_int, "prop-int")
STRING(test_tree1, prop_str, "prop-str")
test_tree1_end:
TREE_HDR(truncated_property)
truncated_property_rsvmap:
RSVMAP_ENTRY(0, 0)
truncated_property_struct:
BEGIN_NODE("")
PROPHDR(truncated_property, prop_truncated, 4)
/* Oops, no actual property data here */
truncated_property_strings:
STRING(truncated_property, prop_truncated, "truncated")
truncated_property_end:

View file

@ -0,0 +1,49 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for misbehaviour on a truncated property
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* 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 <stdint.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
void *fdt = &_truncated_property;
const void *prop;
int err;
int len;
test_init(argc, argv);
prop = fdt_getprop(fdt, 0, "truncated", &len);
if (prop)
FAIL("fdt_getprop() succeeded on truncated property");
if (len != -FDT_ERR_BADSTRUCTURE)
FAIL("fdt_getprop() failed with \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(-FDT_ERR_BADSTRUCTURE));
PASS();
}