xref: /aosp_15_r20/external/dtc/tests/truncated_string.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1*cd60bc56SAndroid Build Coastguard Worker // SPDX-License-Identifier: LGPL-2.1-or-later
2*cd60bc56SAndroid Build Coastguard Worker /*
3*cd60bc56SAndroid Build Coastguard Worker  * libfdt - Flat Device Tree manipulation
4*cd60bc56SAndroid Build Coastguard Worker  *	Testcase for misbehaviour on a truncated string
5*cd60bc56SAndroid Build Coastguard Worker  * Copyright (C) 2018 David Gibson, IBM Corporation.
6*cd60bc56SAndroid Build Coastguard Worker  */
7*cd60bc56SAndroid Build Coastguard Worker 
8*cd60bc56SAndroid Build Coastguard Worker #include <stdlib.h>
9*cd60bc56SAndroid Build Coastguard Worker #include <stdio.h>
10*cd60bc56SAndroid Build Coastguard Worker #include <string.h>
11*cd60bc56SAndroid Build Coastguard Worker #include <stdint.h>
12*cd60bc56SAndroid Build Coastguard Worker 
13*cd60bc56SAndroid Build Coastguard Worker #include <libfdt.h>
14*cd60bc56SAndroid Build Coastguard Worker 
15*cd60bc56SAndroid Build Coastguard Worker #include "tests.h"
16*cd60bc56SAndroid Build Coastguard Worker #include "testdata.h"
17*cd60bc56SAndroid Build Coastguard Worker 
main(int argc,char * argv[])18*cd60bc56SAndroid Build Coastguard Worker int main(int argc, char *argv[])
19*cd60bc56SAndroid Build Coastguard Worker {
20*cd60bc56SAndroid Build Coastguard Worker 	void *fdt = &truncated_string;
21*cd60bc56SAndroid Build Coastguard Worker 	const struct fdt_property *good, *bad;
22*cd60bc56SAndroid Build Coastguard Worker 	int off, len;
23*cd60bc56SAndroid Build Coastguard Worker 	const char *name;
24*cd60bc56SAndroid Build Coastguard Worker 
25*cd60bc56SAndroid Build Coastguard Worker 	test_init(argc, argv);
26*cd60bc56SAndroid Build Coastguard Worker 
27*cd60bc56SAndroid Build Coastguard Worker 	vg_prepare_blob(fdt, fdt_totalsize(fdt));
28*cd60bc56SAndroid Build Coastguard Worker 
29*cd60bc56SAndroid Build Coastguard Worker 	off = fdt_first_property_offset(fdt, 0);
30*cd60bc56SAndroid Build Coastguard Worker 	good = fdt_get_property_by_offset(fdt, off, NULL);
31*cd60bc56SAndroid Build Coastguard Worker 
32*cd60bc56SAndroid Build Coastguard Worker 	off = fdt_next_property_offset(fdt, off);
33*cd60bc56SAndroid Build Coastguard Worker 	bad = fdt_get_property_by_offset(fdt, off, NULL);
34*cd60bc56SAndroid Build Coastguard Worker 
35*cd60bc56SAndroid Build Coastguard Worker 	if (fdt32_to_cpu(good->len) != 0)
36*cd60bc56SAndroid Build Coastguard Worker 		FAIL("Unexpected length for good property");
37*cd60bc56SAndroid Build Coastguard Worker 	name = fdt_get_string(fdt, fdt32_to_cpu(good->nameoff), &len);
38*cd60bc56SAndroid Build Coastguard Worker 	if (!name)
39*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() failed on good property: %s",
40*cd60bc56SAndroid Build Coastguard Worker 		     fdt_strerror(len));
41*cd60bc56SAndroid Build Coastguard Worker 	if (len != 4)
42*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() returned length %d (not 4) on good property",
43*cd60bc56SAndroid Build Coastguard Worker 		     len);
44*cd60bc56SAndroid Build Coastguard Worker 	if (!streq(name, "good"))
45*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() returned \"%s\" (not \"good\") on good property",
46*cd60bc56SAndroid Build Coastguard Worker 		     name);
47*cd60bc56SAndroid Build Coastguard Worker 
48*cd60bc56SAndroid Build Coastguard Worker 	if (fdt32_to_cpu(bad->len) != 0)
49*cd60bc56SAndroid Build Coastguard Worker 		FAIL("Unexpected length for bad property\n");
50*cd60bc56SAndroid Build Coastguard Worker 	name = fdt_get_string(fdt, fdt32_to_cpu(bad->nameoff), &len);
51*cd60bc56SAndroid Build Coastguard Worker 	if (name)
52*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() succeeded incorrectly on bad property");
53*cd60bc56SAndroid Build Coastguard Worker 	else if (len != -FDT_ERR_TRUNCATED)
54*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() gave unexpected error on bad property: %s",
55*cd60bc56SAndroid Build Coastguard Worker 		     fdt_strerror(len));
56*cd60bc56SAndroid Build Coastguard Worker 
57*cd60bc56SAndroid Build Coastguard Worker 	/* Make sure the 'good' property breaks correctly if we
58*cd60bc56SAndroid Build Coastguard Worker 	 * truncate the strings section */
59*cd60bc56SAndroid Build Coastguard Worker 	fdt_set_size_dt_strings(fdt, fdt32_to_cpu(good->nameoff) + 4);
60*cd60bc56SAndroid Build Coastguard Worker 	name = fdt_get_string(fdt, fdt32_to_cpu(good->nameoff), &len);
61*cd60bc56SAndroid Build Coastguard Worker 	if (name)
62*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() succeeded incorrectly on mangled property");
63*cd60bc56SAndroid Build Coastguard Worker 	else if (len != -FDT_ERR_TRUNCATED)
64*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_get_string() gave unexpected error on mangled property: %s",
65*cd60bc56SAndroid Build Coastguard Worker 		     fdt_strerror(len));
66*cd60bc56SAndroid Build Coastguard Worker 
67*cd60bc56SAndroid Build Coastguard Worker 	PASS();
68*cd60bc56SAndroid Build Coastguard Worker }
69