blob: 10846dfbee1e2dc97ad80711c2464dd9ede67e15 [file] [log] [blame]
Rob Herringc4ffc052019-06-20 15:19:41 -06001// SPDX-License-Identifier: LGPL-2.1-or-later
David Gibson7ba551f2006-12-01 16:59:43 +11002/*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_nop_node()
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
David Gibson7ba551f2006-12-01 16:59:43 +11006 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11#include <ctype.h>
David Gibson857f54e2007-03-23 15:16:54 +110012#include <stdint.h>
David Gibson7ba551f2006-12-01 16:59:43 +110013
David Gibson7ba551f2006-12-01 16:59:43 +110014#include <libfdt.h>
15
16#include "tests.h"
17#include "testdata.h"
18
19int main(int argc, char *argv[])
20{
David Gibson73d60922006-12-15 15:12:47 +110021 void *fdt;
David Gibson7ba551f2006-12-01 16:59:43 +110022 int subnode1_offset, subnode2_offset, subsubnode2_offset;
23 int err;
24 int oldsize, delsize, newsize;
25
26 test_init(argc, argv);
27 fdt = load_blob_arg(argc, argv);
28
David Gibsona041dcd2007-11-01 11:37:31 +110029 fdt = open_blob_rw(fdt);
30
David Gibson7ba551f2006-12-01 16:59:43 +110031 oldsize = fdt_totalsize(fdt);
32
David Gibsond2a9da02007-09-28 15:51:04 +100033 subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
David Gibson9a9fdf52006-12-15 15:12:51 +110034 if (subnode1_offset < 0)
David Gibsond2a9da02007-09-28 15:51:04 +100035 FAIL("Couldn't find \"/subnode@1\": %s",
David Gibson9a9fdf52006-12-15 15:12:51 +110036 fdt_strerror(subnode1_offset));
David Gibson9521dc52007-11-20 13:35:46 +110037 check_getprop_cell(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
David Gibson7ba551f2006-12-01 16:59:43 +110038
David Gibsond2a9da02007-09-28 15:51:04 +100039 subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
David Gibson9a9fdf52006-12-15 15:12:51 +110040 if (subnode2_offset < 0)
David Gibsond2a9da02007-09-28 15:51:04 +100041 FAIL("Couldn't find \"/subnode@2\": %s",
David Gibson9a9fdf52006-12-15 15:12:51 +110042 fdt_strerror(subnode2_offset));
David Gibson9521dc52007-11-20 13:35:46 +110043 check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
David Gibson7ba551f2006-12-01 16:59:43 +110044
David Gibsond2a9da02007-09-28 15:51:04 +100045 subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
David Gibson9a9fdf52006-12-15 15:12:51 +110046 if (subsubnode2_offset < 0)
David Gibsond2a9da02007-09-28 15:51:04 +100047 FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
David Gibson9a9fdf52006-12-15 15:12:51 +110048 fdt_strerror(subsubnode2_offset));
David Gibson9521dc52007-11-20 13:35:46 +110049 check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
David Gibson7ba551f2006-12-01 16:59:43 +110050
51 err = fdt_del_node(fdt, subnode1_offset);
52 if (err)
53 FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));
54
David Gibsond2a9da02007-09-28 15:51:04 +100055 subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
David Gibson9a9fdf52006-12-15 15:12:51 +110056 if (subnode1_offset != -FDT_ERR_NOTFOUND)
David Gibson7ba551f2006-12-01 16:59:43 +110057 FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
David Gibson9a9fdf52006-12-15 15:12:51 +110058 fdt_strerror(subnode1_offset),
59 fdt_strerror(-FDT_ERR_NOTFOUND));
David Gibson7ba551f2006-12-01 16:59:43 +110060
David Gibsond2a9da02007-09-28 15:51:04 +100061 subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
David Gibson9a9fdf52006-12-15 15:12:51 +110062 if (subnode2_offset < 0)
63 FAIL("Couldn't find \"/subnode2\": %s",
64 fdt_strerror(subnode2_offset));
David Gibson9521dc52007-11-20 13:35:46 +110065 check_getprop_cell(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
David Gibson7ba551f2006-12-01 16:59:43 +110066
David Gibsond2a9da02007-09-28 15:51:04 +100067 subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
David Gibson9a9fdf52006-12-15 15:12:51 +110068 if (subsubnode2_offset < 0)
David Gibsond2a9da02007-09-28 15:51:04 +100069 FAIL("Couldn't find \"/subnode@2/subsubnode\": %s",
David Gibson9a9fdf52006-12-15 15:12:51 +110070 fdt_strerror(subsubnode2_offset));
David Gibson9521dc52007-11-20 13:35:46 +110071 check_getprop_cell(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
David Gibson7ba551f2006-12-01 16:59:43 +110072
73 err = fdt_del_node(fdt, subnode2_offset);
74 if (err)
75 FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));
76
David Gibsond2a9da02007-09-28 15:51:04 +100077 subnode1_offset = fdt_path_offset(fdt, "/subnode@1");
David Gibson9a9fdf52006-12-15 15:12:51 +110078 if (subnode1_offset != -FDT_ERR_NOTFOUND)
David Gibson7ba551f2006-12-01 16:59:43 +110079 FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
David Gibson9a9fdf52006-12-15 15:12:51 +110080 fdt_strerror(subnode1_offset),
81 fdt_strerror(-FDT_ERR_NOTFOUND));
David Gibson7ba551f2006-12-01 16:59:43 +110082
David Gibsond2a9da02007-09-28 15:51:04 +100083 subnode2_offset = fdt_path_offset(fdt, "/subnode@2");
David Gibson9a9fdf52006-12-15 15:12:51 +110084 if (subnode2_offset != -FDT_ERR_NOTFOUND)
David Gibson7ba551f2006-12-01 16:59:43 +110085 FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
David Gibson9a9fdf52006-12-15 15:12:51 +110086 fdt_strerror(subnode2_offset),
87 fdt_strerror(-FDT_ERR_NOTFOUND));
David Gibson7ba551f2006-12-01 16:59:43 +110088
David Gibsond2a9da02007-09-28 15:51:04 +100089 subsubnode2_offset = fdt_path_offset(fdt, "/subnode@2/subsubnode");
David Gibson9a9fdf52006-12-15 15:12:51 +110090 if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
David Gibson7ba551f2006-12-01 16:59:43 +110091 FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
David Gibson9a9fdf52006-12-15 15:12:51 +110092 fdt_strerror(subsubnode2_offset),
93 fdt_strerror(-FDT_ERR_NOTFOUND));
David Gibson7ba551f2006-12-01 16:59:43 +110094
95 delsize = fdt_totalsize(fdt);
96
David Gibson73d60922006-12-15 15:12:47 +110097 err = fdt_pack(fdt);
98 if (err)
David Gibson7ba551f2006-12-01 16:59:43 +110099 FAIL("fdt_pack(): %s", fdt_strerror(err));
100
101 newsize = fdt_totalsize(fdt);
102
103 verbose_printf("oldsize = %d, delsize = %d, newsize = %d\n",
104 oldsize, delsize, newsize);
105
106 if (newsize >= oldsize)
107 FAIL("Tree failed to shrink after deletions");
108
109 PASS();
110}