blob: ccbf9a512ff642a36d6c9ae80bc2797f940765dd [file] [log] [blame]
Alice Wang9d4df702023-05-25 14:14:12 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Integration tests of the library libfdt.
18
Alice Wang2422bdc2023-06-12 08:37:55 +000019use libfdt::{Fdt, FdtError};
Jaewan Kimaa638702023-09-19 13:34:01 +090020use std::ffi::CStr;
Alice Wang9d4df702023-05-25 14:14:12 +000021use std::fs;
22use std::ops::Range;
23
Jaewan Kim5b057772023-10-19 01:02:17 +090024macro_rules! cstr {
25 ($str:literal) => {{
26 CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
27 }};
28}
29
Alice Wang2422bdc2023-06-12 08:37:55 +000030const TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH: &str = "data/test_tree_one_memory_range.dtb";
31const TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH: &str =
32 "data/test_tree_multiple_memory_ranges.dtb";
33const TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH: &str = "data/test_tree_empty_memory_range.dtb";
34const TEST_TREE_WITH_NO_MEMORY_NODE_PATH: &str = "data/test_tree_no_memory_node.dtb";
Alice Wang9d4df702023-05-25 14:14:12 +000035
36#[test]
Alice Wang2422bdc2023-06-12 08:37:55 +000037fn retrieving_memory_from_fdt_with_one_memory_range_succeeds() {
38 let data = fs::read(TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH).unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000039 let fdt = Fdt::from_slice(&data).unwrap();
40
41 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
Alice Wang2422bdc2023-06-12 08:37:55 +000042 let mut memory = fdt.memory().unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000043 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
Alice Wang2422bdc2023-06-12 08:37:55 +000044 assert!(memory.next().is_none());
45 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
46}
47
48#[test]
49fn retrieving_memory_from_fdt_with_multiple_memory_ranges_succeeds() {
50 let data = fs::read(TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH).unwrap();
51 let fdt = Fdt::from_slice(&data).unwrap();
52
53 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
54 const EXPECTED_SECOND_MEMORY_RANGE: Range<usize> = 512..1024;
55 let mut memory = fdt.memory().unwrap();
56 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
57 assert_eq!(memory.next(), Some(EXPECTED_SECOND_MEMORY_RANGE));
58 assert!(memory.next().is_none());
59 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
60}
61
62#[test]
63fn retrieving_first_memory_from_fdt_with_empty_memory_range_fails() {
64 let data = fs::read(TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH).unwrap();
65 let fdt = Fdt::from_slice(&data).unwrap();
66
67 let mut memory = fdt.memory().unwrap();
68 assert!(memory.next().is_none());
69 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
70}
71
72#[test]
73fn retrieving_memory_from_fdt_with_no_memory_node_fails() {
74 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
75 let fdt = Fdt::from_slice(&data).unwrap();
76
77 assert_eq!(fdt.memory().unwrap_err(), FdtError::NotFound);
78 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
Alice Wang9d4df702023-05-25 14:14:12 +000079}
Jaewan Kimaa638702023-09-19 13:34:01 +090080
81#[test]
82fn node_name() {
83 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
84 let fdt = Fdt::from_slice(&data).unwrap();
85
86 let root = fdt.root().unwrap();
87 assert_eq!(root.name().unwrap().to_str().unwrap(), "");
88
89 let chosen = fdt.chosen().unwrap().unwrap();
90 assert_eq!(chosen.name().unwrap().to_str().unwrap(), "chosen");
91
Jaewan Kim5b057772023-10-19 01:02:17 +090092 let nested_node_path = cstr!("/cpus/PowerPC,970@0");
Jaewan Kimaa638702023-09-19 13:34:01 +090093 let nested_node = fdt.node(nested_node_path).unwrap().unwrap();
94 assert_eq!(nested_node.name().unwrap().to_str().unwrap(), "PowerPC,970@0");
95}
Jaewan Kimbc828d72023-09-19 15:52:08 +090096
97#[test]
98fn node_subnodes() {
99 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
100 let fdt = Fdt::from_slice(&data).unwrap();
101 let root = fdt.root().unwrap();
102 let expected: Vec<&str> = vec!["cpus", "randomnode", "chosen"];
103
104 for (node, name) in root.subnodes().unwrap().zip(expected) {
105 assert_eq!(node.name().unwrap().to_str().unwrap(), name);
106 }
107}
Jaewan Kim72d10902023-10-12 21:59:26 +0900108
109#[test]
110fn node_properties() {
111 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
112 let fdt = Fdt::from_slice(&data).unwrap();
113 let root = fdt.root().unwrap();
114 let one_be = 0x1_u32.to_be_bytes();
115 let expected: Vec<(&str, &[u8])> = vec![
116 ("model", b"MyBoardName\0"),
117 ("compatible", b"MyBoardName\0MyBoardFamilyName\0"),
118 ("#address-cells", &one_be),
119 ("#size-cells", &one_be),
120 ("empty_prop", b""),
121 ];
122
123 for (prop, (name, value)) in root.properties().unwrap().zip(expected) {
124 assert_eq!(prop.name().unwrap().to_str().unwrap(), name);
125 assert_eq!(prop.value().unwrap(), value);
126 }
127}
Jaewan Kim5b057772023-10-19 01:02:17 +0900128
129#[test]
130fn node_supernode_at_depth() {
131 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
132 let fdt = Fdt::from_slice(&data).unwrap();
133 let node = fdt.node(cstr!("/cpus/PowerPC,970@1")).unwrap().unwrap();
134 let expected = &["", "cpus", "PowerPC,970@1"];
135
136 for (depth, expect) in expected.iter().enumerate() {
137 let supernode = node.supernode_at_depth(depth).unwrap();
138 assert_eq!(supernode.name().unwrap().to_str().unwrap(), *expect);
139 }
140}