stratovirt/0015-util-fdt-add-some-UT-test-cases.patch
Fei Xu a4a59d2b07 StratoVirt: Add fdt patches for musl compilation
Signed-off-by: Fei Xu <xufei30@huawei.com>
2021-07-27 14:38:08 +08:00

221 lines
11 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

From 1081b4f57b1bc959fa5e26a1e6520f1cfbf999b0 Mon Sep 17 00:00:00 2001
From: Jiajie Li <lijiajie11@huawei.com>
Date: Thu, 24 Jun 2021 16:15:48 +0800
Subject: [PATCH 5/5] util/fdt: add some UT test cases
Test the basic function of mod fdt, like adding all kinds
of properties for fdt node, adding nested node, getting the
final result. What's more, add some boundary test cases.
Signed-off-by: Jiajie Li <lijiajie11@huawei.com>
---
util/src/device_tree.rs | 195 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 195 insertions(+)
diff --git a/util/src/device_tree.rs b/util/src/device_tree.rs
index 2fdab44..d47b7f5 100644
--- a/util/src/device_tree.rs
+++ b/util/src/device_tree.rs
@@ -298,3 +298,198 @@ pub fn dump_dtb(fdt: &[u8], file_path: &str) {
let mut f = File::create(file_path).unwrap();
f.write_all(fdt).expect("Unable to write data");
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_add_all_properties() {
+ let mut fdt_builder = FdtBuilder::new();
+ let root_node = fdt_builder.begin_node("").unwrap();
+ fdt_builder.set_property("null", &[]).unwrap();
+ fdt_builder.set_property_u32("u32", 0x01234567).unwrap();
+ fdt_builder
+ .set_property_u64("u64", 0x0123456789abcdef)
+ .unwrap();
+ fdt_builder
+ .set_property_array_u32(
+ "u32_array",
+ &vec![0x11223344, 0x55667788, 0x99aabbcc, 0xddeeff00],
+ )
+ .unwrap();
+ fdt_builder
+ .set_property_array_u64("u64_array", &vec![0x0011223344556677, 0x8899aabbccddeeff])
+ .unwrap();
+ fdt_builder
+ .set_property_string("string", "hello fdt")
+ .unwrap();
+ fdt_builder.end_node(root_node).unwrap();
+ let right_fdt: Vec<u8> = vec![
+ 0xd0, 0x0d, 0xfe, 0xed, // 00: magic (0xd00dfeed)
+ 0x00, 0x00, 0x00, 0xf0, // 04: totalsize (0xf0)
+ 0x00, 0x00, 0x00, 0x38, // 08: off_dt_struct (0x38)
+ 0x00, 0x00, 0x00, 0xc8, // 0c: off_dt_strings (0xc8)
+ 0x00, 0x00, 0x00, 0x28, // 10: off_mem_rsvmap (0x28)
+ 0x00, 0x00, 0x00, 0x11, // 14: version (17)
+ 0x00, 0x00, 0x00, 0x10, // 18: last_comp_version (16)
+ 0x00, 0x00, 0x00, 0x00, // 1c: boot_cpuid_phys (0)
+ 0x00, 0x00, 0x00, 0x28, // 20: size_dt_strings (0x28)
+ 0x00, 0x00, 0x00, 0x90, // 24: size_dt_struct (0x90)
+ 0x00, 0x00, 0x00, 0x00, // 28: high address of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 2c: low address of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 30: high size of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 34: low size of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x01, // 38: FDT_BEGIN_NODE
+ 0x00, 0x00, 0x00, 0x00, // 3c: node name ("") with 4-byte alignment
+ 0x00, 0x00, 0x00, 0x03, // 40: FDT_PROP ("null")
+ 0x00, 0x00, 0x00, 0x00, // 44: property len (0)
+ 0x00, 0x00, 0x00, 0x00, // 48: property nameoff (0x00)
+ 0x00, 0x00, 0x00, 0x03, // 4c: FDT_PROP ("u32")
+ 0x00, 0x00, 0x00, 0x04, // 50: property len (4)
+ 0x00, 0x00, 0x00, 0x05, // 54: property nameoff (0x05)
+ 0x01, 0x23, 0x45, 0x67, // 58: propertys value (0x01234567)
+ 0x00, 0x00, 0x00, 0x03, // 5c: FDT_PROP ("u64")
+ 0x00, 0x00, 0x00, 0x08, // 60: property len (8)
+ 0x00, 0x00, 0x00, 0x09, // 64: property nameoff (0x09)
+ 0x01, 0x23, 0x45, 0x67, // 68: propertys value (0x01234567)
+ 0x89, 0xab, 0xcd, 0xef, // 6c: propertys value (0x89abcdef)
+ 0x00, 0x00, 0x00, 0x03, // 70: FDT_PROP ("u32_array")
+ 0x00, 0x00, 0x00, 0x10, // 74: property len (16)
+ 0x00, 0x00, 0x00, 0x0d, // 78: property nameoff (0x0d)
+ 0x11, 0x22, 0x33, 0x44, // 7c: propertys value (0x11223344)
+ 0x55, 0x66, 0x77, 0x88, // 80: propertys value (0x55667788)
+ 0x99, 0xaa, 0xbb, 0xcc, // 84: propertys value (0x99aabbcc)
+ 0xdd, 0xee, 0xff, 0x00, // 88: propertys value (0xddeeff00)
+ 0x00, 0x00, 0x00, 0x03, // 8c: FDT_PROP ("u64_array")
+ 0x00, 0x00, 0x00, 0x10, // 90: property len (16)
+ 0x00, 0x00, 0x00, 0x17, // 94: property nameoff (0x17)
+ 0x00, 0x11, 0x22, 0x33, // 98: propertys value (0x00112233)
+ 0x44, 0x55, 0x66, 0x77, // 9c: propertys value (0x44556677)
+ 0x88, 0x99, 0xaa, 0xbb, // a0: propertys value (0x8899aabb)
+ 0xcc, 0xdd, 0xee, 0xff, // a4: propertys value (0xccddeeff)
+ 0x00, 0x00, 0x00, 0x03, // a8: FDT_PROP ("string")
+ 0x00, 0x00, 0x00, 0x0a, // ac: property len (10)
+ 0x00, 0x00, 0x00, 0x21, // b0: property nameoff (0x21)
+ b'h', b'e', b'l', b'l', // b4: propertys value ("hell")
+ b'o', b' ', b'f', b'd', // b8: propertys value ("o fd")
+ b't', b'\0', 0x00, 0x00, // bc: propertys value ("t\0" with padding)
+ 0x00, 0x00, 0x00, 0x02, // c0: FDT_END_NODE
+ 0x00, 0x00, 0x00, 0x09, // c4: FDT_END
+ b'n', b'u', b'l', b'l', b'\0', // c8: "null" with offset 0x00
+ b'u', b'3', b'2', b'\0', // cd: "u32" with offset 0x05
+ b'u', b'6', b'4', b'\0', // d1: "u64" with offset 0x09
+ b'u', b'3', b'2', b'_', b'a', b'r', b'r', b'a', b'y',
+ b'\0', // d5: "u32_array" with offset 0x0d
+ b'u', b'6', b'4', b'_', b'a', b'r', b'r', b'a', b'y',
+ b'\0', // df: "u64_array" with offset 0x17
+ b's', b't', b'r', b'i', b'n', b'g', b'\0', // e9: "string" with offset 0x21
+ ];
+ let sample_fdt = fdt_builder.finish().unwrap();
+ assert_eq!(right_fdt, sample_fdt);
+ }
+
+ #[test]
+ fn test_nested_node() {
+ let mut fdt_builder = FdtBuilder::new();
+ let root_node = fdt_builder.begin_node("").unwrap();
+
+ let cpus_node = fdt_builder.begin_node("cpus").unwrap();
+ fdt_builder.set_property_u32("addrcells", 0x02).unwrap();
+ fdt_builder.set_property_u32("sizecells", 0x0).unwrap();
+
+ let cpu_map_node = fdt_builder.begin_node("cpu-map").unwrap();
+ fdt_builder.set_property_u32("cpu", 10).unwrap();
+
+ fdt_builder.end_node(cpu_map_node).unwrap();
+ fdt_builder.end_node(cpus_node).unwrap();
+ fdt_builder.end_node(root_node).unwrap();
+
+ fdt_builder.set_boot_cpuid_phys(1);
+
+ let right_fdt: Vec<u8> = vec![
+ 0xd0, 0x0d, 0xfe, 0xed, // 00: magic (0xd00dfeed)
+ 0x00, 0x00, 0x00, 0xb0, // 04: totalsize (0xb0)
+ 0x00, 0x00, 0x00, 0x38, // 08: off_dt_struct (0x38)
+ 0x00, 0x00, 0x00, 0x98, // 0c: off_dt_strings (0x98)
+ 0x00, 0x00, 0x00, 0x28, // 10: off_mem_rsvmap (0x28)
+ 0x00, 0x00, 0x00, 0x11, // 14: version (17)
+ 0x00, 0x00, 0x00, 0x10, // 18: last_comp_version (16)
+ 0x00, 0x00, 0x00, 0x01, // 1c: boot_cpuid_phys (1)
+ 0x00, 0x00, 0x00, 0x18, // 20: size_dt_strings (0x18)
+ 0x00, 0x00, 0x00, 0x60, // 24: size_dt_struct (0x60)
+ 0x00, 0x00, 0x00, 0x00, // 28: high address of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 2c: low address of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 30: high size of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x00, // 34: low size of memory reservation block terminator
+ 0x00, 0x00, 0x00, 0x01, // 38: FDT_BEGIN_NODE
+ 0x00, 0x00, 0x00, 0x00, // 3c: node name ("")
+ 0x00, 0x00, 0x00, 0x01, // 40: FDT_BEGIN_NODE
+ b'c', b'p', b'u', b's', // 44: node name ("cpus") with 4-byte alignment
+ b'\0', 0x00, 0x00, 0x00, // 48: padding
+ 0x00, 0x00, 0x00, 0x03, // 4c: FDT_PROP ("addrcells")
+ 0x00, 0x00, 0x00, 0x04, // 50: property len (4)
+ 0x00, 0x00, 0x00, 0x00, // 54: property nameoff (0x00)
+ 0x00, 0x00, 0x00, 0x02, // 58: propertys value (0x02)
+ 0x00, 0x00, 0x00, 0x03, // 5c: FDT_PROP ("sizecells")
+ 0x00, 0x00, 0x00, 0x04, // 60: property len (4)
+ 0x00, 0x00, 0x00, 0x0a, // 64: property nameoff (0xa)
+ 0x00, 0x00, 0x00, 0x00, // 68: propertys value (0x0)
+ 0x00, 0x00, 0x00, 0x01, // 6c: FDT_BEGIN_NODE
+ b'c', b'p', b'u', b'-', // 70: node name ("cpu-map")
+ b'm', b'a', b'p', b'\0', // 74: node name ("cpu-map")
+ 0x00, 0x00, 0x00, 0x03, // 78: FDT_PROP ("cpu")
+ 0x00, 0x00, 0x00, 0x04, // 7c: property len (4)
+ 0x00, 0x00, 0x00, 0x14, // 80: property nameoff (0x14)
+ 0x00, 0x00, 0x00, 0x0a, // 84: propertys value (10)
+ 0x00, 0x00, 0x00, 0x02, // 88: FDT_END_NODE
+ 0x00, 0x00, 0x00, 0x02, // 8c: FDT_END_NODE
+ 0x00, 0x00, 0x00, 0x02, // 90: FDT_END_NODE
+ 0x00, 0x00, 0x00, 0x09, // 94: FDT_END
+ b'a', b'd', b'd', b'r', b'c', b'e', b'l', b'l', b's',
+ b'\0', // 98: "addrcells" with offset 0x00
+ b's', b'i', b'z', b'e', b'c', b'e', b'l', b'l', b's',
+ b'\0', // a2: "sizecells" with offset 0x0a
+ b'c', b'p', b'u', b'\0', // ac: "cpu" with offset 0x14
+ ];
+ let sample_fdt = fdt_builder.finish().unwrap();
+ assert_eq!(right_fdt, sample_fdt);
+ }
+
+ #[test]
+ fn test_illegeal_string() {
+ let mut fdt_builder = FdtBuilder::new();
+ assert!(fdt_builder.begin_node("bad\0string").is_err());
+
+ fdt_builder.begin_node("good string").unwrap();
+ assert!(fdt_builder.set_property("bad property\0name", &[]).is_err());
+ assert!(fdt_builder
+ .set_property_string("good property name", "test\0val")
+ .is_ok());
+ }
+
+ #[test]
+ fn test_unclose_nested_node() {
+ let mut fdt_builder = FdtBuilder::new();
+ let root_node = fdt_builder.begin_node("").unwrap();
+ fdt_builder.begin_node("nested node").unwrap();
+ assert!(fdt_builder.end_node(root_node).is_err());
+ assert!(fdt_builder.finish().is_err());
+ }
+
+ #[test]
+ fn test_mem_reserve_overlap() {
+ let mut fdt_builder = FdtBuilder::new();
+ let mem_reservations = [
+ FdtReserveEntry {
+ address: 0x100,
+ size: 0x100,
+ },
+ FdtReserveEntry {
+ address: 0x150,
+ size: 0x100,
+ },
+ ];
+ assert!(fdt_builder.add_mem_reserve(&mem_reservations).is_err());
+ }
+}
--
2.31.1