Skip to content

Commit 7c88139

Browse files
authored
Merge pull request #7 from woshiluo/test
Add tests & a GitHub workflow to automatically run tests & fix alignment issues
2 parents 730c317 + 0a34ca5 commit 7c88139

File tree

6 files changed

+116
-8
lines changed

6 files changed

+116
-8
lines changed

.github/workflows/test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Cargo
2+
3+
on: [push, pull_request]
4+
5+
env:
6+
CARGO_TERM_COLOR: always
7+
# By default, RUSTFLAGS with “-D warnings” turns “asm_const” warnings into errors.
8+
RUSTFLAGS:
9+
10+
jobs:
11+
fmt:
12+
name: Rustfmt all packages
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: actions-rust-lang/setup-rust-toolchain@v1
17+
with:
18+
components: rustfmt
19+
- name: Rustfmt Check
20+
uses: actions-rust-lang/rustfmt@v1
21+
22+
test:
23+
name: Test
24+
needs: fmt
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
- uses: actions-rust-lang/setup-rust-toolchain@v1
29+
with:
30+
toolchain: nightly
31+
- name: Run tests
32+
run: cargo test

examples/hifive-unmatched-a00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct Cpu<'a> {
4949
const RAW_DEVICE_TREE: &[u8] = include_bytes!("hifive-unmatched-a00.dtb");
5050
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
5151

52-
#[repr(align(4))]
52+
#[repr(align(8))]
5353
struct AlignedBuffer {
5454
pub data: [u8; RAW_DEVICE_TREE.len()],
5555
}

examples/qemu-virt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde_device_tree::{
1919
const RAW_DEVICE_TREE: &[u8] = include_bytes!("qemu-virt.dtb");
2020
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
2121

22-
#[repr(align(4))]
22+
#[repr(align(8))]
2323
struct AlignedBuffer {
2424
pub data: [u8; RAW_DEVICE_TREE.len()],
2525
}

src/de.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@ use serde::de;
3131
/// # Example
3232
///
3333
/// ```
34-
/// # static DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
35-
/// # let dtb_pa = DEVICE_TREE.as_ptr() as usize;
34+
/// # static RAW_DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
35+
/// # const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
36+
/// # #[repr(align(4))]
37+
/// # struct AlignedBuffer {
38+
/// # pub data: [u8; RAW_DEVICE_TREE.len()],
39+
/// # }
40+
/// # let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
41+
/// # data: [0; BUFFER_SIZE],
42+
/// # });
43+
/// # aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
44+
/// # let fdt_ptr = aligned_data.data.as_ptr();
3645
/// use serde_derive::Deserialize;
3746
///
3847
/// #[derive(Debug, Deserialize)]
@@ -47,7 +56,7 @@ use serde::de;
4756
/// stdout_path: Option<&'a str>,
4857
/// }
4958
///
50-
/// let tree: Tree = unsafe { serde_device_tree::from_raw(dtb_pa as *const u8) }
59+
/// let tree: Tree = unsafe { serde_device_tree::from_raw(fdt_ptr as *const u8) }
5160
/// .expect("parse device tree");
5261
/// if let Some(chosen) = tree.chosen {
5362
/// if let Some(stdout_path) = chosen.stdout_path {
@@ -68,7 +77,7 @@ where
6877

6978
let total_size = u32::from_be(header.total_size);
7079
let raw_data_len = (total_size - HEADER_LEN) as usize;
71-
let ans_ptr = core::ptr::from_raw_parts(ptr as *const (), raw_data_len);
80+
let ans_ptr = core::ptr::from_raw_parts(ptr as *const u8, raw_data_len);
7281
let device_tree: &DeviceTree = &*ans_ptr;
7382
let tags = device_tree.tags();
7483
let mut d = Deserializer {
@@ -513,7 +522,16 @@ mod tests {
513522
#[test]
514523
fn error_invalid_magic() {
515524
static DEVICE_TREE: &[u8] = &[0x11, 0x22, 0x33, 0x44]; // not device tree blob format
516-
let ptr = DEVICE_TREE.as_ptr();
525+
const DEVICE_TREE_LEN: usize = DEVICE_TREE.len();
526+
#[repr(align(8))]
527+
struct AlignedBuffer {
528+
pub data: [u8; DEVICE_TREE_LEN],
529+
}
530+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
531+
data: [0; DEVICE_TREE_LEN],
532+
});
533+
aligned_data.data[..DEVICE_TREE_LEN].clone_from_slice(DEVICE_TREE);
534+
let ptr = aligned_data.data.as_ptr();
517535

518536
#[derive(Debug, Deserialize)]
519537
struct Tree {}

src/de_mut/node.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ impl<'de> Node<'de> {
5252
ptr: *const &StructDeserializer<'de>,
5353
) -> Self {
5454
let struct_deseriallizer = &*(ptr);
55-
println!("get node from {:?}", struct_deseriallizer.cursor);
5655
let dtb = struct_deseriallizer.dtb;
5756
let mut cursor = struct_deseriallizer.cursor;
5857
let mut prop: Option<BodyCursor> = None;

tests/hifive-unmatched-a00.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use serde_derive::Deserialize;
2+
use serde_device_tree::Compatible;
3+
4+
#[derive(Debug, Deserialize)]
5+
struct Tree<'a> {
6+
#[serde(rename = "#address-cells")]
7+
num_address_cells: u32,
8+
#[serde(rename = "#size-cells")]
9+
num_size_cells: u32,
10+
model: &'a str,
11+
#[allow(unused)]
12+
compatible: Compatible<'a>,
13+
chosen: Option<Chosen<'a>>,
14+
cpus: Cpus,
15+
}
16+
17+
#[derive(Debug, Deserialize)]
18+
#[serde(rename_all = "kebab-case")]
19+
struct Chosen<'a> {
20+
stdout_path: Option<&'a str>,
21+
}
22+
23+
#[derive(Debug, Deserialize)]
24+
#[serde(rename_all = "kebab-case")]
25+
struct Cpus {
26+
timebase_frequency: u32,
27+
#[serde(rename = "u-boot,dm-spl")]
28+
u_boot_dm_spl: bool,
29+
}
30+
31+
const RAW_DEVICE_TREE: &[u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
32+
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
33+
34+
#[repr(align(4))]
35+
struct AlignedBuffer {
36+
pub data: [u8; RAW_DEVICE_TREE.len()],
37+
}
38+
39+
#[test]
40+
fn hifive_unmatched() {
41+
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
42+
data: [0; BUFFER_SIZE],
43+
});
44+
aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
45+
let ptr = aligned_data.data.as_ptr();
46+
let t: Tree = unsafe { serde_device_tree::from_raw(ptr) }.unwrap();
47+
assert_eq!(t.num_address_cells, 2);
48+
assert_eq!(t.num_size_cells, 2);
49+
assert_eq!(t.model, "SiFive HiFive Unmatched A00\0");
50+
if let Some(chosen) = t.chosen {
51+
if let Some(stdout_path) = chosen.stdout_path {
52+
assert_eq!(stdout_path, "serial0\0");
53+
} else {
54+
panic!("Failed to find chosen/stdout_path");
55+
}
56+
}
57+
assert_eq!(t.cpus.timebase_frequency, 1000000);
58+
assert_eq!(t.cpus.u_boot_dm_spl, true);
59+
}

0 commit comments

Comments
 (0)