aboutsummaryrefslogtreecommitdiff
path: root/tests/fs_tree1.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/fs_tree1.c')
-rw-r--r--tests/fs_tree1.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/tests/fs_tree1.c b/tests/fs_tree1.c
new file mode 100644
index 0000000..5762465
--- /dev/null
+++ b/tests/fs_tree1.c
@@ -0,0 +1,168 @@
+/*
+ * libfdt - Flat Device Tree manipulation
+ * Testcase/tool constructing an fs tree for further test
+ * Copyright (C) 2018 David Gibson, Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+#include <stdint.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <libfdt.h>
+
+#include "tests.h"
+#include "testdata.h"
+
+static void start_dir(const char *name)
+{
+ int rc;
+
+ rc = mkdir(name, 0777);
+ if (rc != 0)
+ FAIL("mkdir(\"%s\"): %s", name, strerror(errno));
+
+ rc = chdir(name);
+ if (rc != 0)
+ FAIL("chdir(\"%s\"): %s", name, strerror(errno));
+}
+
+static void end_dir(void)
+{
+ int rc;
+
+ rc = chdir("..");
+ if (rc != 0)
+ FAIL("chdir(..): %s", strerror(errno));
+}
+
+static void mkfile(const char *name, void *data, size_t len)
+{
+ int fd;
+ int rc;
+
+ fd = open(name, O_WRONLY|O_CREAT, 0666);
+ if (fd < 0)
+ FAIL("open(\"%s\"): %s", name, strerror(errno));
+
+ rc = write(fd, data, len);
+ if (rc < 0)
+ FAIL("write(\"%s\"): %s", name, strerror(errno));
+ if (rc != len)
+ FAIL("write(\"%s\"): short write", name);
+
+ rc = close(fd);
+ if (rc != 0)
+ FAIL("close(\"%s\"): %s", name, strerror(errno));
+}
+
+#define mkfile_str(name, s) \
+ do { \
+ char str[] = s; \
+ mkfile((name), str, sizeof(str)); \
+ } while (0)
+
+static void mkfile_u32(const char *name, uint32_t val)
+{
+ val = cpu_to_fdt32(val);
+ mkfile(name, &val, sizeof(val));
+}
+
+static void mkfile_u64(const char *name, uint64_t val)
+{
+ val = cpu_to_fdt64(val);
+ mkfile(name, &val, sizeof(val));
+}
+
+int main(int argc, char *argv[])
+{
+ const char *base;
+
+ test_init(argc, argv);
+ if (argc != 2)
+ CONFIG("Usage: %s <path>", argv[0]);
+
+ base = argv[1];
+
+ start_dir(base);
+ mkfile_str("compatible", "test_tree1");
+ mkfile_u32("prop-int", TEST_VALUE_1);
+ mkfile_u64("prop-int64", 0xdeadbeef01abcdefULL);
+ mkfile_str("prop-str", "hello world");
+ mkfile_u32("#address-cells", 1);
+ mkfile_u32("#size-cells", 0);
+
+ {
+ start_dir("subnode@1");
+
+ mkfile_str("compatible", "subnode1");
+ mkfile_u32("reg", 1);
+ mkfile_u32("prop-int", TEST_VALUE_1);
+
+ {
+ start_dir("subsubnode");
+
+ mkfile_str("compatible", "subsubnode1\0subsubnode");
+ mkfile_str("placeholder", "this is a placeholder string\0string2");
+ mkfile_u32("prop-int", TEST_VALUE_1);
+
+ end_dir();
+ }
+
+ {
+ start_dir("ss1");
+ end_dir();
+ }
+
+ end_dir();
+ }
+
+ {
+ start_dir("subnode@2");
+
+ mkfile_u32("reg", 2);
+ mkfile_u32("linux,phandle", 0x2000);
+ mkfile_u32("prop-int", TEST_VALUE_2);
+ mkfile_u32("#address-cells", 1);
+ mkfile_u32("#size-cells", 0);
+
+ {
+ start_dir("subsubnode@0");
+
+ mkfile_u32("reg", 0);
+ mkfile_u32("phandle", 0x2001);
+ mkfile_str("compatible", "subsubnode2\0subsubnode");
+ mkfile_u32("prop-int", TEST_VALUE_2);
+
+ end_dir();
+ }
+
+ {
+ start_dir("ss2");
+ end_dir();
+ }
+
+ end_dir();
+ }
+
+ PASS();
+}