blob: 513ffd8b9bf102a97180952d7df97defc2cf8d64 [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
Alice Wang2422bdc2023-06-12 08:37:55 +000024const TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH: &str = "data/test_tree_one_memory_range.dtb";
25const TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH: &str =
26 "data/test_tree_multiple_memory_ranges.dtb";
27const TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH: &str = "data/test_tree_empty_memory_range.dtb";
28const TEST_TREE_WITH_NO_MEMORY_NODE_PATH: &str = "data/test_tree_no_memory_node.dtb";
Alice Wang9d4df702023-05-25 14:14:12 +000029
30#[test]
Alice Wang2422bdc2023-06-12 08:37:55 +000031fn retrieving_memory_from_fdt_with_one_memory_range_succeeds() {
32 let data = fs::read(TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH).unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000033 let fdt = Fdt::from_slice(&data).unwrap();
34
35 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
Alice Wang2422bdc2023-06-12 08:37:55 +000036 let mut memory = fdt.memory().unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000037 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
Alice Wang2422bdc2023-06-12 08:37:55 +000038 assert!(memory.next().is_none());
39 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
40}
41
42#[test]
43fn retrieving_memory_from_fdt_with_multiple_memory_ranges_succeeds() {
44 let data = fs::read(TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH).unwrap();
45 let fdt = Fdt::from_slice(&data).unwrap();
46
47 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
48 const EXPECTED_SECOND_MEMORY_RANGE: Range<usize> = 512..1024;
49 let mut memory = fdt.memory().unwrap();
50 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
51 assert_eq!(memory.next(), Some(EXPECTED_SECOND_MEMORY_RANGE));
52 assert!(memory.next().is_none());
53 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
54}
55
56#[test]
57fn retrieving_first_memory_from_fdt_with_empty_memory_range_fails() {
58 let data = fs::read(TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH).unwrap();
59 let fdt = Fdt::from_slice(&data).unwrap();
60
61 let mut memory = fdt.memory().unwrap();
62 assert!(memory.next().is_none());
63 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
64}
65
66#[test]
67fn retrieving_memory_from_fdt_with_no_memory_node_fails() {
68 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
69 let fdt = Fdt::from_slice(&data).unwrap();
70
71 assert_eq!(fdt.memory().unwrap_err(), FdtError::NotFound);
72 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
Alice Wang9d4df702023-05-25 14:14:12 +000073}
Jaewan Kimaa638702023-09-19 13:34:01 +090074
75#[test]
76fn node_name() {
77 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
78 let fdt = Fdt::from_slice(&data).unwrap();
79
80 let root = fdt.root().unwrap();
81 assert_eq!(root.name().unwrap().to_str().unwrap(), "");
82
83 let chosen = fdt.chosen().unwrap().unwrap();
84 assert_eq!(chosen.name().unwrap().to_str().unwrap(), "chosen");
85
86 let nested_node_path = CStr::from_bytes_with_nul(b"/cpus/PowerPC,970@0\0").unwrap();
87 let nested_node = fdt.node(nested_node_path).unwrap().unwrap();
88 assert_eq!(nested_node.name().unwrap().to_str().unwrap(), "PowerPC,970@0");
89}
Jaewan Kimbc828d72023-09-19 15:52:08 +090090
91#[test]
92fn node_subnodes() {
93 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
94 let fdt = Fdt::from_slice(&data).unwrap();
95 let root = fdt.root().unwrap();
96 let expected: Vec<&str> = vec!["cpus", "randomnode", "chosen"];
97
98 for (node, name) in root.subnodes().unwrap().zip(expected) {
99 assert_eq!(node.name().unwrap().to_str().unwrap(), name);
100 }
101}