diff --git a/tests/Makefile b/tests/Makefile index 7a517c7..db27d07 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,10 +1,12 @@ PREFIX = /usr/local -LIB_TESTS = -LIBTREE_TESTS = root_node property_offset subnode_offset path_offset getprop \ +LIB_TESTS = root_node property_offset subnode_offset path_offset getprop \ notfound \ setprop_inplace nop_property nop_node -TESTS = $(LIB_TESTS) $(LIBTREE_TESTS) +TESTS = $(LIB_TESTS) +UTILS = dumptrees + +TREES = test_tree1.dtb CFLAGS = -Wall -g CPPFLAGS = -I.. @@ -21,7 +23,7 @@ endif DEPFILES = $(TESTS:%=%.d) testutils.d -all: $(TESTS) +all: $(TESTS) $(TREES) %.o: %.c @$(VECHO) CC $@ @@ -31,18 +33,22 @@ all: $(TESTS) @$(VECHO) AS $@ $(CC) -D__ASSEMBLY__ $(CPPFLAGS) -o $@ -c $< -$(LIB_TESTS): %: %.o testutils.o $(LIBFDT) - @$(VECHO) LD "(testcase)" $@ - $(CC) $(LDFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) +$(TREES): trees.o dumptrees + @$(VECHO) DUMPTREES + ./dumptrees >/dev/null -$(LIBTREE_TESTS): %: %.o testutils.o trees.o $(LIBFDT) - @$(VECHO) LD "(testcase + trees)" $@ - $(CC) $(LDFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) +%: %.o + @$(VECHO) LD $@ + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +dumptrees: trees.o + +$(LIB_TESTS): %: testutils.o $(LIBFDT) clean: @$(VECHO) CLEAN "(tests)" rm -f *~ *.o *.so *.a *.d *.s core a.out - rm -f $(TESTS) + rm -f $(TESTS) $(UTILS) *.dtb %.d: %.c @$(CC) $(CPPFLAGS) -MM -MT "$*.o $@" $< > $@ diff --git a/tests/dumptrees.c b/tests/dumptrees.c new file mode 100644 index 0000000..d7e110d --- /dev/null +++ b/tests/dumptrees.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include + +#include "tests.h" +#include "testdata.h" + +struct { + struct fdt_header *fdt; + 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++) { + struct fdt_header *fdt = trees[i].fdt; + const char *filename = trees[i].filename; + int size; + int fd; + int ret; + + size = fdt32_to_cpu(fdt->totalsize); + + 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, fdt, size); + if (ret != size) + perror("write()"); + + close(fd); + } + exit(0); +} diff --git a/tests/getprop.c b/tests/getprop.c index cfdd2ce..962b912 100644 --- a/tests/getprop.c +++ b/tests/getprop.c @@ -30,9 +30,10 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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); diff --git a/tests/nop_node.c b/tests/nop_node.c index 86d5d63..df421c0 100644 --- a/tests/nop_node.c +++ b/tests/nop_node.c @@ -31,11 +31,12 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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 ((err = fdt_offset_error(subnode1_offset))) diff --git a/tests/nop_property.c b/tests/nop_property.c index af488d2..a8bed5b 100644 --- a/tests/nop_property.c +++ b/tests/nop_property.c @@ -31,12 +31,13 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *fdt; uint32_t *intp; char *strp; 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("int value was 0x%08x\n", *intp); diff --git a/tests/notfound.c b/tests/notfound.c index 106830e..1dd8bc7 100644 --- a/tests/notfound.c +++ b/tests/notfound.c @@ -36,13 +36,14 @@ void check_error(const char *s, int err) int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *fdt; int offset; int subnode1_offset; void *val; int err; test_init(argc, argv); + fdt = load_blob_arg(argc, argv); offset = fdt_property_offset(fdt, 0, "nonexistant-property"); check_error("fdt_property_offset(\"nonexistant-property\")", diff --git a/tests/path_offset.c b/tests/path_offset.c index fd7b76b..32222be 100644 --- a/tests/path_offset.c +++ b/tests/path_offset.c @@ -57,13 +57,14 @@ int check_subnode(struct fdt_header *fdt, int parent, const char *name) int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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"); diff --git a/tests/property_offset.c b/tests/property_offset.c index a106159..1745258 100644 --- a/tests/property_offset.c +++ b/tests/property_offset.c @@ -29,9 +29,10 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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); diff --git a/tests/root_node.c b/tests/root_node.c index 906359b..ecd1cb1 100644 --- a/tests/root_node.c +++ b/tests/root_node.c @@ -30,11 +30,12 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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) diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 11cc691..0ffdc93 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -9,19 +9,25 @@ run_test () { PATH=".:$PATH" $ENV "$@" } -functional_tests () { +tree1_tests () { + TREE=$1 + # Read-only tests - run_test root_node - run_test property_offset - run_test subnode_offset - run_test path_offset - run_test getprop - run_test notfound + run_test root_node $TREE + run_test property_offset $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 - run_test nop_property - run_test nop_node + run_test setprop_inplace $TREE + run_test nop_property $TREE + run_test nop_node $TREE +} + +functional_tests () { + tree1_tests test_tree1.dtb } stress_tests () { diff --git a/tests/setprop_inplace.c b/tests/setprop_inplace.c index ce19bcc..61de789 100644 --- a/tests/setprop_inplace.c +++ b/tests/setprop_inplace.c @@ -31,13 +31,14 @@ int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *fdt; uint32_t *intp; char *strp, *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); diff --git a/tests/subnode_offset.c b/tests/subnode_offset.c index d64257a..0552b05 100644 --- a/tests/subnode_offset.c +++ b/tests/subnode_offset.c @@ -57,11 +57,12 @@ int check_subnode(struct fdt_header *fdt, int parent, const char *name) int main(int argc, char *argv[]) { - struct fdt_header *fdt = &_test_tree1; + struct fdt_header *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"); diff --git a/tests/tests.h b/tests/tests.h index 9ad07c8..b5fd475 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -104,6 +104,22 @@ void cleanup(void); 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; +} + const char *fdt_strerror(int errval); void check_property(struct fdt_header *fdt, int nodeoffset, const char *name, int len, const void *val); @@ -121,6 +137,7 @@ void *check_getprop(struct fdt_header *fdt, int nodeoffset, const char *name, typeof(val) x = val; \ check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \ }) - +//void *load_blob(const char *filename); +void *load_blob_arg(int argc, char *argv[]); #endif /* _TESTS_H */ diff --git a/tests/testutils.c b/tests/testutils.c index 30acb35..af8c2ad 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -161,3 +162,43 @@ void *check_getprop(struct fdt_header *fdt, int nodeoffset, const char *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 ", argv[0]); + return load_blob(argv[1]); +}