blob: 806e6c04a89df792864a0072289509040f8456b5 [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};
Alice Wang9d4df702023-05-25 14:14:12 +000020use std::fs;
21use std::ops::Range;
22
Alice Wang2422bdc2023-06-12 08:37:55 +000023const TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH: &str = "data/test_tree_one_memory_range.dtb";
24const TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH: &str =
25 "data/test_tree_multiple_memory_ranges.dtb";
26const TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH: &str = "data/test_tree_empty_memory_range.dtb";
27const TEST_TREE_WITH_NO_MEMORY_NODE_PATH: &str = "data/test_tree_no_memory_node.dtb";
Alice Wang9d4df702023-05-25 14:14:12 +000028
29#[test]
Alice Wang2422bdc2023-06-12 08:37:55 +000030fn retrieving_memory_from_fdt_with_one_memory_range_succeeds() {
31 let data = fs::read(TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH).unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000032 let fdt = Fdt::from_slice(&data).unwrap();
33
34 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
Alice Wang2422bdc2023-06-12 08:37:55 +000035 let mut memory = fdt.memory().unwrap();
Alice Wang9d4df702023-05-25 14:14:12 +000036 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
Alice Wang2422bdc2023-06-12 08:37:55 +000037 assert!(memory.next().is_none());
38 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
39}
40
41#[test]
42fn retrieving_memory_from_fdt_with_multiple_memory_ranges_succeeds() {
43 let data = fs::read(TEST_TREE_WITH_MULTIPLE_MEMORY_RANGES_PATH).unwrap();
44 let fdt = Fdt::from_slice(&data).unwrap();
45
46 const EXPECTED_FIRST_MEMORY_RANGE: Range<usize> = 0..256;
47 const EXPECTED_SECOND_MEMORY_RANGE: Range<usize> = 512..1024;
48 let mut memory = fdt.memory().unwrap();
49 assert_eq!(memory.next(), Some(EXPECTED_FIRST_MEMORY_RANGE));
50 assert_eq!(memory.next(), Some(EXPECTED_SECOND_MEMORY_RANGE));
51 assert!(memory.next().is_none());
52 assert_eq!(fdt.first_memory_range(), Ok(EXPECTED_FIRST_MEMORY_RANGE));
53}
54
55#[test]
56fn retrieving_first_memory_from_fdt_with_empty_memory_range_fails() {
57 let data = fs::read(TEST_TREE_WITH_EMPTY_MEMORY_RANGE_PATH).unwrap();
58 let fdt = Fdt::from_slice(&data).unwrap();
59
60 let mut memory = fdt.memory().unwrap();
61 assert!(memory.next().is_none());
62 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
63}
64
65#[test]
66fn retrieving_memory_from_fdt_with_no_memory_node_fails() {
67 let data = fs::read(TEST_TREE_WITH_NO_MEMORY_NODE_PATH).unwrap();
68 let fdt = Fdt::from_slice(&data).unwrap();
69
70 assert_eq!(fdt.memory().unwrap_err(), FdtError::NotFound);
71 assert_eq!(fdt.first_memory_range(), Err(FdtError::NotFound));
Alice Wang9d4df702023-05-25 14:14:12 +000072}