Blame tests/path_offset.c

Packit 2ad57b
/*
Packit 2ad57b
 * libfdt - Flat Device Tree manipulation
Packit 2ad57b
 *	Testcase for fdt_path_offset()
Packit 2ad57b
 * Copyright (C) 2006 David Gibson, IBM Corporation.
Packit 2ad57b
 *
Packit 2ad57b
 * This library is free software; you can redistribute it and/or
Packit 2ad57b
 * modify it under the terms of the GNU Lesser General Public License
Packit 2ad57b
 * as published by the Free Software Foundation; either version 2.1 of
Packit 2ad57b
 * the License, or (at your option) any later version.
Packit 2ad57b
 *
Packit 2ad57b
 * This library is distributed in the hope that it will be useful, but
Packit 2ad57b
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2ad57b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 2ad57b
 * Lesser General Public License for more details.
Packit 2ad57b
 *
Packit 2ad57b
 * You should have received a copy of the GNU Lesser General Public
Packit 2ad57b
 * License along with this library; if not, write to the Free Software
Packit 2ad57b
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Packit 2ad57b
 */
Packit 2ad57b
#include <stdlib.h>
Packit 2ad57b
#include <stdio.h>
Packit 2ad57b
#include <string.h>
Packit 2ad57b
#include <stdint.h>
Packit 2ad57b
Packit 2ad57b
#include <libfdt.h>
Packit 2ad57b
Packit 2ad57b
#include "tests.h"
Packit 2ad57b
#include "testdata.h"
Packit 2ad57b
Packit 2ad57b
static int check_subnode(void *fdt, int parent, const char *name)
Packit 2ad57b
{
Packit 2ad57b
	int offset;
Packit 2ad57b
	const struct fdt_node_header *nh;
Packit 2ad57b
	uint32_t tag;
Packit 2ad57b
Packit 2ad57b
	verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
Packit 2ad57b
	offset = fdt_subnode_offset(fdt, parent, name);
Packit 2ad57b
	verbose_printf("offset %d...", offset);
Packit 2ad57b
	if (offset < 0)
Packit 2ad57b
		FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
Packit 2ad57b
	nh = fdt_offset_ptr(fdt, offset, sizeof(*nh));
Packit 2ad57b
	verbose_printf("pointer %p\n", nh);
Packit 2ad57b
	if (! nh)
Packit 2ad57b
		FAIL("NULL retrieving subnode \"%s\"", name);
Packit 2ad57b
Packit 2ad57b
	tag = fdt32_to_cpu(nh->tag);
Packit 2ad57b
Packit 2ad57b
	if (tag != FDT_BEGIN_NODE)
Packit 2ad57b
		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
Packit 2ad57b
	if (!nodename_eq(nh->name, name))
Packit 2ad57b
		FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
Packit 2ad57b
		     nh->name, name);
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
static void check_path_offset(void *fdt, char *path, int offset)
Packit 2ad57b
{
Packit 2ad57b
	int rc;
Packit 2ad57b
Packit 2ad57b
	verbose_printf("Checking offset of \"%s\" is %d...\n", path, offset);
Packit 2ad57b
Packit 2ad57b
	rc = fdt_path_offset(fdt, path);
Packit 2ad57b
	if (rc < 0)
Packit 2ad57b
		FAIL("fdt_path_offset(\"%s\") failed: %s",
Packit 2ad57b
		     path,  fdt_strerror(rc));
Packit 2ad57b
	if (rc != offset)
Packit 2ad57b
		FAIL("fdt_path_offset(\"%s\") returned incorrect offset"
Packit 2ad57b
		     " %d instead of %d", path, rc, offset);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
static void check_path_offset_namelen(void *fdt, char *path, int namelen,
Packit 2ad57b
				      int offset)
Packit 2ad57b
{
Packit 2ad57b
	int rc;
Packit 2ad57b
Packit 2ad57b
	verbose_printf("Checking offset of \"%s\" [first %d characters]"
Packit 2ad57b
		       " is %d...\n", path, namelen, offset);
Packit 2ad57b
Packit 2ad57b
	rc = fdt_path_offset_namelen(fdt, path, namelen);
Packit 2ad57b
	if (rc == offset)
Packit 2ad57b
		return;
Packit 2ad57b
Packit 2ad57b
	if (rc < 0)
Packit 2ad57b
		FAIL("fdt_path_offset_namelen(\"%s\", %d) failed: %s",
Packit 2ad57b
		     path, namelen, fdt_strerror(rc));
Packit 2ad57b
	else
Packit 2ad57b
		FAIL("fdt_path_offset_namelen(\"%s\", %d) returned incorrect"
Packit 2ad57b
		     " offset %d instead of %d", path, namelen, rc, offset);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int main(int argc, char *argv[])
Packit 2ad57b
{
Packit 2ad57b
	void *fdt;
Packit 2ad57b
	int subnode1_offset, subnode2_offset;
Packit 2ad57b
	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
Packit 2ad57b
Packit 2ad57b
	test_init(argc, argv);
Packit 2ad57b
	fdt = load_blob_arg(argc, argv);
Packit 2ad57b
Packit 2ad57b
	check_path_offset(fdt, "/", 0);
Packit 2ad57b
Packit 2ad57b
	subnode1_offset = check_subnode(fdt, 0, "subnode@1");
Packit 2ad57b
	subnode2_offset = check_subnode(fdt, 0, "subnode@2");
Packit 2ad57b
Packit 2ad57b
	check_path_offset(fdt, "/subnode@1", subnode1_offset);
Packit 2ad57b
	check_path_offset(fdt, "/subnode@2", subnode2_offset);
Packit 2ad57b
Packit 2ad57b
	subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
Packit 2ad57b
	subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode@0");
Packit 2ad57b
	subsubnode2_offset2 = check_subnode(fdt, subnode2_offset, "subsubnode");
Packit 2ad57b
Packit 2ad57b
	check_path_offset(fdt, "/subnode@1/subsubnode", subsubnode1_offset);
Packit 2ad57b
	check_path_offset(fdt, "/subnode@2/subsubnode@0", subsubnode2_offset);
Packit 2ad57b
	check_path_offset(fdt, "/subnode@2/subsubnode", subsubnode2_offset2);
Packit 2ad57b
Packit 2ad57b
	/* Test paths with extraneous separators */
Packit 2ad57b
	check_path_offset(fdt, "//", 0);
Packit 2ad57b
	check_path_offset(fdt, "///", 0);
Packit 2ad57b
	check_path_offset(fdt, "//subnode@1", subnode1_offset);
Packit 2ad57b
	check_path_offset(fdt, "/subnode@1/", subnode1_offset);
Packit 2ad57b
	check_path_offset(fdt, "//subnode@1///", subnode1_offset);
Packit 2ad57b
	check_path_offset(fdt, "/subnode@2////subsubnode", subsubnode2_offset2);
Packit 2ad57b
Packit 2ad57b
	/* Test fdt_path_offset_namelen() */
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@1", 1, 0);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 10, subnode1_offset);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@1/subsubnode", 11, subnode1_offset);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 10, subnode2_offset);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2TRAILINGGARBAGE", 11, -FDT_ERR_NOTFOUND);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 23, subsubnode2_offset2);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 22, -FDT_ERR_NOTFOUND);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 24, subsubnode2_offset2);
Packit 2ad57b
	check_path_offset_namelen(fdt, "/subnode@2/subsubnode@0/more", 25, -FDT_ERR_NOTFOUND);
Packit 2ad57b
Packit 2ad57b
	PASS();
Packit 2ad57b
}