1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_set_name()
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11
12 #include <libfdt.h>
13
14 #include "tests.h"
15 #include "testdata.h"
16
check_set_name(void * fdt,const char * path,const char * newname)17 static void check_set_name(void *fdt, const char *path, const char *newname)
18 {
19 int offset;
20 const char *getname, *oldname;
21 int len, err;
22
23 oldname = strrchr(path, '/');
24 if (!oldname)
25 TEST_BUG();
26 oldname += 1;
27
28 offset = fdt_path_offset(fdt, path);
29 if (offset < 0)
30 FAIL("Couldn't find %s", path);
31
32 getname = fdt_get_name(fdt, offset, &len);
33 verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
34 offset, getname, len);
35 if (!getname)
36 FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
37
38 if (strcmp(getname, oldname) != 0)
39 FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
40 path, getname, oldname);
41
42 if (len < 0)
43 FAIL("fdt_get_name(%s) returned negative length: %d",
44 path, len);
45
46 if ((unsigned)len != strlen(getname))
47 FAIL("fdt_get_name(%s) returned length %d instead of %zd",
48 path, len, strlen(getname));
49
50 err = fdt_set_name(fdt, offset, newname);
51 if (err)
52 FAIL("fdt_set_name(%d, \"%s\"): %s", offset, newname,
53 fdt_strerror(err));
54
55 getname = fdt_get_name(fdt, offset, &len);
56 if (!getname)
57 FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
58 if (len < 0)
59 FAIL("negative name length (%d) for returned node name\n", len);
60
61 if (strcmp(getname, newname) != 0)
62 FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
63 path, getname, newname);
64
65 if ((unsigned)len != strlen(getname))
66 FAIL("fdt_get_name(%s) returned length %d instead of %zd",
67 path, len, strlen(getname));
68 }
69
main(int argc,char * argv[])70 int main(int argc, char *argv[])
71 {
72 void *fdt;
73
74 test_init(argc, argv);
75 fdt = load_blob_arg(argc, argv);
76 fdt = open_blob_rw(fdt);
77
78 check_set_name(fdt, "/subnode@1", "subnode@17");
79 check_set_name(fdt, "/subnode@2/subsubnode@0", "fred@0");
80 check_set_name(fdt, "/subnode@17/subsubnode", "something@0");
81
82 PASS();
83 }
84