[fdt][test] Add integration test for libfdt

Bug: 282928116
Test: atest liblibfdt.integration_test
Change-Id: Ic9a59bf4ee4d925e028e7d2043d648fad93aa85d
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 3217ee1..cc413b4 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -70,6 +70,9 @@
       "path": "packages/modules/Virtualization/libs/devicemapper"
     },
     {
+      "path": "packages/modules/Virtualization/libs/libfdt"
+    },
+    {
       "path": "packages/modules/Virtualization/libs/vbmeta"
     },
     {
diff --git a/libs/libfdt/Android.bp b/libs/libfdt/Android.bp
index 5a729f1..55cb01b 100644
--- a/libs/libfdt/Android.bp
+++ b/libs/libfdt/Android.bp
@@ -44,3 +44,25 @@
     ],
     apex_available: ["com.android.virt"],
 }
+
+rust_test {
+    name: "liblibfdt.integration_test",
+    crate_name: "libfdt_test",
+    srcs: ["tests/*.rs"],
+    test_suites: ["general-tests"],
+    data: [
+        ":fdt_data_test_tree1_dtb",
+    ],
+    prefer_rlib: true,
+    rustlibs: [
+        "liblibfdt",
+    ],
+}
+
+genrule {
+    name: "fdt_data_test_tree1_dtb",
+    tools: ["dtc"],
+    srcs: ["tests/data/test_tree1.dts"],
+    out: ["data/test_tree1.dtb"],
+    cmd: "$(location dtc) -O dtb -I dts -o $(out) $(in)",
+}
diff --git a/libs/libfdt/TEST_MAPPING b/libs/libfdt/TEST_MAPPING
new file mode 100644
index 0000000..596f84b
--- /dev/null
+++ b/libs/libfdt/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "avf-presubmit": [
+    {
+      "name": "liblibfdt.integration_test"
+    }
+  ]
+}
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index df1058e..91214b3 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -191,7 +191,7 @@
 }
 
 /// DT node.
-#[derive(Clone, Copy)]
+#[derive(Clone, Copy, Debug)]
 pub struct FdtNode<'a> {
     fdt: &'a Fdt,
     offset: c_int,
@@ -599,6 +599,7 @@
 }
 
 /// Wrapper around low-level libfdt functions.
+#[derive(Debug)]
 #[repr(transparent)]
 pub struct Fdt {
     buffer: [u8],
diff --git a/libs/libfdt/tests/api_test.rs b/libs/libfdt/tests/api_test.rs
new file mode 100644
index 0000000..d0feb98
--- /dev/null
+++ b/libs/libfdt/tests/api_test.rs
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//! Integration tests of the library libfdt.
+
+use libfdt::Fdt;
+use std::fs;
+use std::ops::Range;
+
+const TEST_TREE1_PATH: &str = "data/test_tree1.dtb";
+
+#[test]
+fn parse_well_formed_fdt_successfully() {
+    let data = fs::read(TEST_TREE1_PATH).unwrap();
+    let fdt = Fdt::from_slice(&data).unwrap();
+
+    const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
+    let mut memory = fdt.memory().unwrap().unwrap();
+    assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
+}
diff --git a/libs/libfdt/tests/data/test_tree1.dts b/libs/libfdt/tests/data/test_tree1.dts
new file mode 100644
index 0000000..222b0b1
--- /dev/null
+++ b/libs/libfdt/tests/data/test_tree1.dts
@@ -0,0 +1,43 @@
+/dts-v1/;
+
+/memreserve/	0x0000000000000000 0x000000000000000e;
+/ {
+	model = "MyBoardName";
+	compatible = "MyBoardName", "MyBoardFamilyName";
+	#address-cells = <0x1>;
+	#size-cells = <0x1>;
+
+	cpus {
+		linux,phandle = <0x1>;
+		#address-cells = <0x1>;
+		#size-cells = <0x0>;
+
+		PowerPC,970@0 {
+			device_type = "cpu";
+			reg = <0x0>;
+			linux,boot-cpu;
+		};
+
+		PowerPC,970@1 {
+			device_type = "cpu";
+			reg = <0x1>;
+		};
+	};
+
+	randomnode {
+		string = "foo", "stuff";
+		bytes = [61 62 63 64 65];
+		nbytes = [80 ff];
+
+		child {
+		};
+	};
+
+	memory {
+		device_type = "memory";
+		reg = <0x0 0x100>;
+	};
+
+	chosen {
+	};
+};