blob: 65ebc4d2c7b06e52affb5c7aacbad9fb8bbaee47 [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
Jaewan Kim17ba7a32023-10-19 13:25:15 +090019use libfdt::{Fdt, FdtError, Phandle};
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";
Jaewan Kim17ba7a32023-10-19 13:25:15 +090035const TEST_TREE_PHANDLE_PATH: &str = "data/test_tree_phandle.dtb";
Alice Wang9d4df702023-05-25 14:14:12 +000036
37#[test]
Alice Wang2422bdc2023-06-12 08:37:55 +000038fn retrieving_memory_from_fdt_with_one_memory_range_succeeds() {
39 let data = fs::read(TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH).unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000040 let fdt = Fdt::from_slice(&data).unwrap();
41
42 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
Alice Wang2422bdc2023-06-12 08:37:55 +000043 let mut memory = fdt.memory().unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000044 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
Alice Wang2422bdc2023-06-12 08:37:55 +000045 assert!(memory.next().is_none());
46 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
47}
48
49#[test]
50fn retrieving_memory_from_fdt_with_multiple_memory_ranges_succeeds() {
51 let data = fs::read(TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH).unwrap();
52 let fdt = Fdt::from_slice(&data).unwrap();
53
54 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
55 const EXPECTED_SECOND_MEMORY_RANGE: Range<usize> = 512..1024;
56 let mut memory = fdt.memory().unwrap();
57 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
58 assert_eq!(memory.next(), Some(EXPECTED_SECOND_MEMORY_RANGE));
59 assert!(memory.next().is_none());
60 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
61}
62
63#[test]
64fn retrieving_first_memory_from_fdt_with_empty_memory_range_fails() {
65 let data = fs::read(TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH).unwrap();
66 let fdt = Fdt::from_slice(&data).unwrap();
67
68 let mut memory = fdt.memory().unwrap();
69 assert!(memory.next().is_none());
70 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
71}
72
73#[test]
74fn retrieving_memory_from_fdt_with_no_memory_node_fails() {
75 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
76 let fdt = Fdt::from_slice(&data).unwrap();
77
78 assert_eq!(fdt.memory().unwrap_err(), FdtError::NotFound);
79 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
Alice Wang9d4df702023-05-25 14:14:12 +000080}
Jaewan Kimaa638702023-09-19 13:34:01 +090081
82#[test]
83fn node_name() {
84 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
85 let fdt = Fdt::from_slice(&data).unwrap();
86
87 let root = fdt.root().unwrap();
88 assert_eq!(root.name().unwrap().to_str().unwrap(), "");
89
90 let chosen = fdt.chosen().unwrap().unwrap();
91 assert_eq!(chosen.name().unwrap().to_str().unwrap(), "chosen");
92
Jaewan Kim5b057772023-10-19 01:02:17 +090093 let nested_node_path = cstr!("/cpus/PowerPC,970@0");
Jaewan Kimaa638702023-09-19 13:34:01 +090094 let nested_node = fdt.node(nested_node_path).unwrap().unwrap();
95 assert_eq!(nested_node.name().unwrap().to_str().unwrap(), "PowerPC,970@0");
96}
Jaewan Kimbc828d72023-09-19 15:52:08 +090097
98#[test]
99fn node_subnodes() {
100 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
101 let fdt = Fdt::from_slice(&data).unwrap();
102 let root = fdt.root().unwrap();
103 let expected: Vec<&str> = vec!["cpus", "randomnode", "chosen"];
104
105 for (node, name) in root.subnodes().unwrap().zip(expected) {
106 assert_eq!(node.name().unwrap().to_str().unwrap(), name);
107 }
108}
Jaewan Kim72d10902023-10-12 21:59:26 +0900109
110#[test]
111fn node_properties() {
112 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
113 let fdt = Fdt::from_slice(&data).unwrap();
114 let root = fdt.root().unwrap();
115 let one_be = 0x1_u32.to_be_bytes();
116 let expected: Vec<(&str, &[u8])> = vec![
117 ("model", b"MyBoardName\0"),
118 ("compatible", b"MyBoardName\0MyBoardFamilyName\0"),
119 ("#address-cells", &one_be),
120 ("#size-cells", &one_be),
121 ("empty_prop", b""),
122 ];
123
124 for (prop, (name, value)) in root.properties().unwrap().zip(expected) {
125 assert_eq!(prop.name().unwrap().to_str().unwrap(), name);
126 assert_eq!(prop.value().unwrap(), value);
127 }
128}
Jaewan Kim5b057772023-10-19 01:02:17 +0900129
130#[test]
131fn node_supernode_at_depth() {
132 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
133 let fdt = Fdt::from_slice(&data).unwrap();
134 let node = fdt.node(cstr!("/cpus/PowerPC,970@1")).unwrap().unwrap();
135 let expected = &["", "cpus", "PowerPC,970@1"];
136
137 for (depth, expect) in expected.iter().enumerate() {
138 let supernode = node.supernode_at_depth(depth).unwrap();
139 assert_eq!(supernode.name().unwrap().to_str().unwrap(), *expect);
140 }
141}
Jaewan Kim17ba7a32023-10-19 13:25:15 +0900142
143#[test]
144fn phandle_new() {
145 let phandle_u32 = 0x55;
146 let phandle = Phandle::new(phandle_u32).unwrap();
147
148 assert_eq!(u32::from(phandle), phandle_u32);
149}
150
151#[test]
152fn max_phandle() {
153 let data = fs::read(TEST_TREE_PHANDLE_PATH).unwrap();
154 let fdt = Fdt::from_slice(&data).unwrap();
155
156 assert_eq!(fdt.max_phandle().unwrap(), Phandle::new(0xFF).unwrap());
157}
158
159#[test]
160fn node_with_phandle() {
161 let data = fs::read(TEST_TREE_PHANDLE_PATH).unwrap();
162 let fdt = Fdt::from_slice(&data).unwrap();
163
164 // Test linux,phandle
165 let node = fdt.node_with_phandle(Phandle::new(0xFF).unwrap()).unwrap().unwrap();
166 assert_eq!(node.name().unwrap().to_str().unwrap(), "node_zz");
167
168 // Test phandle
169 let node = fdt.node_with_phandle(Phandle::new(0x22).unwrap()).unwrap().unwrap();
170 assert_eq!(node.name().unwrap().to_str().unwrap(), "node_abc");
171}
Jaewan Kim4ae0e712023-10-19 14:16:17 +0900172
173#[test]
174fn node_nop() {
175 let mut data = fs::read(TEST_TREE_PHANDLE_PATH).unwrap();
176 let fdt = Fdt::from_mut_slice(&mut data).unwrap();
177
178 fdt.node_with_phandle(Phandle::new(0xFF).unwrap()).unwrap().unwrap();
179 let node = fdt.node_mut(cstr!("/node_z/node_zz")).unwrap().unwrap();
180
181 node.nop().unwrap();
182
183 assert!(fdt.node_with_phandle(Phandle::new(0xFF).unwrap()).unwrap().is_none());
184 assert!(fdt.node(cstr!("/node_z/node_zz")).unwrap().is_none());
185
186 fdt.unpack().unwrap();
187 fdt.pack().unwrap();
188
189 assert!(fdt.node_with_phandle(Phandle::new(0xFF).unwrap()).unwrap().is_none());
190 assert!(fdt.node(cstr!("/node_z/node_zz")).unwrap().is_none());
191}