• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for fdt_delprop()
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdint.h>
13 
14 #include <libfdt.h>
15 
16 #include "tests.h"
17 #include "testdata.h"
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 	void *fdt;
22 	const uint32_t *intp;
23 	const char *strp;
24 	int err, lenerr;
25 	int oldsize, delsize, newsize;
26 
27 	test_init(argc, argv);
28 	fdt = load_blob_arg(argc, argv);
29 
30 	fdt = open_blob_rw(fdt);
31 
32 	oldsize = fdt_totalsize(fdt);
33 
34 	intp = check_getprop_cell(fdt, 0, "prop-int", TEST_VALUE_1);
35 	verbose_printf("int value was 0x%08x\n", *intp);
36 
37 	err = fdt_delprop(fdt, 0, "prop-int");
38 	if (err)
39 		FAIL("Failed to delete \"prop-int\": %s", fdt_strerror(err));
40 
41 	intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
42 	if (intp)
43 		FAIL("prop-int still present after deletion");
44 	if (lenerr != -FDT_ERR_NOTFOUND)
45 		FAIL("Unexpected error on second getprop: %s",
46 		     fdt_strerror(lenerr));
47 
48 	strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
49 			     TEST_STRING_1);
50 	verbose_printf("string value was \"%s\"\n", strp);
51 	err = fdt_delprop(fdt, 0, "prop-str");
52 	if (err)
53 		FAIL("Failed to delete \"prop-str\": %s", fdt_strerror(err));
54 
55 	strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
56 	if (strp)
57 		FAIL("prop-str still present after deletion");
58 	if (lenerr != -FDT_ERR_NOTFOUND)
59 		FAIL("Unexpected error on second getprop: %s",
60 		     fdt_strerror(lenerr));
61 
62 	delsize = fdt_totalsize(fdt);
63 
64 	err = fdt_pack(fdt);
65 	if (err)
66 		FAIL("fdt_pack(): %s\n", fdt_strerror(err));
67 
68 	newsize = fdt_totalsize(fdt);
69 
70 	verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
71 		       oldsize, delsize, newsize);
72 
73 	if (newsize >= oldsize)
74 		FAIL("Tree failed to shrink after deletions");
75 
76 	PASS();
77 }
78