Skip to content

Commit

Permalink
Merge pull request #7 from woshiluo/test
Browse files Browse the repository at this point in the history
Add tests & a GitHub workflow to automatically run tests & fix alignment issues
  • Loading branch information
luojia65 authored Oct 27, 2024
2 parents 730c317 + 0a34ca5 commit 7c88139
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 8 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Cargo

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
# By default, RUSTFLAGS with “-D warnings” turns “asm_const” warnings into errors.
RUSTFLAGS:

jobs:
fmt:
name: Rustfmt all packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: rustfmt
- name: Rustfmt Check
uses: actions-rust-lang/rustfmt@v1

test:
name: Test
needs: fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: nightly
- name: Run tests
run: cargo test
2 changes: 1 addition & 1 deletion examples/hifive-unmatched-a00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Cpu<'a> {
const RAW_DEVICE_TREE: &[u8] = include_bytes!("hifive-unmatched-a00.dtb");
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

#[repr(align(4))]
#[repr(align(8))]
struct AlignedBuffer {
pub data: [u8; RAW_DEVICE_TREE.len()],
}
Expand Down
2 changes: 1 addition & 1 deletion examples/qemu-virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde_device_tree::{
const RAW_DEVICE_TREE: &[u8] = include_bytes!("qemu-virt.dtb");
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

#[repr(align(4))]
#[repr(align(8))]
struct AlignedBuffer {
pub data: [u8; RAW_DEVICE_TREE.len()],
}
Expand Down
28 changes: 23 additions & 5 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ use serde::de;
/// # Example
///
/// ```
/// # static DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
/// # let dtb_pa = DEVICE_TREE.as_ptr() as usize;
/// # static RAW_DEVICE_TREE: &'static [u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
/// # const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();
/// # #[repr(align(4))]
/// # struct AlignedBuffer {
/// # pub data: [u8; RAW_DEVICE_TREE.len()],
/// # }
/// # let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
/// # data: [0; BUFFER_SIZE],
/// # });
/// # aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
/// # let fdt_ptr = aligned_data.data.as_ptr();
/// use serde_derive::Deserialize;
///
/// #[derive(Debug, Deserialize)]
Expand All @@ -47,7 +56,7 @@ use serde::de;
/// stdout_path: Option<&'a str>,
/// }
///
/// let tree: Tree = unsafe { serde_device_tree::from_raw(dtb_pa as *const u8) }
/// let tree: Tree = unsafe { serde_device_tree::from_raw(fdt_ptr as *const u8) }
/// .expect("parse device tree");
/// if let Some(chosen) = tree.chosen {
/// if let Some(stdout_path) = chosen.stdout_path {
Expand All @@ -68,7 +77,7 @@ where

let total_size = u32::from_be(header.total_size);
let raw_data_len = (total_size - HEADER_LEN) as usize;
let ans_ptr = core::ptr::from_raw_parts(ptr as *const (), raw_data_len);
let ans_ptr = core::ptr::from_raw_parts(ptr as *const u8, raw_data_len);
let device_tree: &DeviceTree = &*ans_ptr;
let tags = device_tree.tags();
let mut d = Deserializer {
Expand Down Expand Up @@ -513,7 +522,16 @@ mod tests {
#[test]
fn error_invalid_magic() {
static DEVICE_TREE: &[u8] = &[0x11, 0x22, 0x33, 0x44]; // not device tree blob format
let ptr = DEVICE_TREE.as_ptr();
const DEVICE_TREE_LEN: usize = DEVICE_TREE.len();
#[repr(align(8))]
struct AlignedBuffer {
pub data: [u8; DEVICE_TREE_LEN],
}
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
data: [0; DEVICE_TREE_LEN],
});
aligned_data.data[..DEVICE_TREE_LEN].clone_from_slice(DEVICE_TREE);
let ptr = aligned_data.data.as_ptr();

#[derive(Debug, Deserialize)]
struct Tree {}
Expand Down
1 change: 0 additions & 1 deletion src/de_mut/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl<'de> Node<'de> {
ptr: *const &StructDeserializer<'de>,
) -> Self {
let struct_deseriallizer = &*(ptr);
println!("get node from {:?}", struct_deseriallizer.cursor);
let dtb = struct_deseriallizer.dtb;
let mut cursor = struct_deseriallizer.cursor;
let mut prop: Option<BodyCursor> = None;
Expand Down
59 changes: 59 additions & 0 deletions tests/hifive-unmatched-a00.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use serde_derive::Deserialize;
use serde_device_tree::Compatible;

#[derive(Debug, Deserialize)]
struct Tree<'a> {
#[serde(rename = "#address-cells")]
num_address_cells: u32,
#[serde(rename = "#size-cells")]
num_size_cells: u32,
model: &'a str,
#[allow(unused)]
compatible: Compatible<'a>,
chosen: Option<Chosen<'a>>,
cpus: Cpus,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Chosen<'a> {
stdout_path: Option<&'a str>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Cpus {
timebase_frequency: u32,
#[serde(rename = "u-boot,dm-spl")]
u_boot_dm_spl: bool,
}

const RAW_DEVICE_TREE: &[u8] = include_bytes!("../examples/hifive-unmatched-a00.dtb");
const BUFFER_SIZE: usize = RAW_DEVICE_TREE.len();

#[repr(align(4))]
struct AlignedBuffer {
pub data: [u8; RAW_DEVICE_TREE.len()],
}

#[test]
fn hifive_unmatched() {
let mut aligned_data: Box<AlignedBuffer> = Box::new(AlignedBuffer {
data: [0; BUFFER_SIZE],
});
aligned_data.data[..BUFFER_SIZE].clone_from_slice(RAW_DEVICE_TREE);
let ptr = aligned_data.data.as_ptr();
let t: Tree = unsafe { serde_device_tree::from_raw(ptr) }.unwrap();
assert_eq!(t.num_address_cells, 2);
assert_eq!(t.num_size_cells, 2);
assert_eq!(t.model, "SiFive HiFive Unmatched A00\0");
if let Some(chosen) = t.chosen {
if let Some(stdout_path) = chosen.stdout_path {
assert_eq!(stdout_path, "serial0\0");
} else {
panic!("Failed to find chosen/stdout_path");
}
}
assert_eq!(t.cpus.timebase_frequency, 1000000);
assert_eq!(t.cpus.u_boot_dm_spl, true);
}

0 comments on commit 7c88139

Please sign in to comment.