Have tests read example tree from a generated file, rather than link it in.
This makes the tests more flexible to re-use for testing the output from the write tests.
This commit is contained in:
parent
3da0f9a10d
commit
4e6221c171
14 changed files with 158 additions and 32 deletions
|
@ -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 $@" $< > $@
|
||||
|
|
47
tests/dumptrees.c
Normal file
47
tests/dumptrees.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <fdt.h>
|
||||
|
||||
#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);
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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\")",
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 () {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <libfdt.h>
|
||||
|
||||
|
@ -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 <dtb file>", argv[0]);
|
||||
return load_blob(argv[1]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue