From 31a8996ea59c23ad0c8915bea1ad38a769bb42d6 Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Fri, 26 Jun 2020 11:05:45 +0200 Subject: [PATCH] Add tests for heapviz tool. --- .github/workflows/cargo_fmt.yml | 7 +++ .github/workflows/heapviz_test.yml | 32 ++++++++++++ run_desktop_tests.sh | 5 ++ tools/heapviz/Cargo.toml | 1 + tools/heapviz/src/main.rs | 82 ++++++++++++++++++++++++------ 5 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/heapviz_test.yml diff --git a/.github/workflows/cargo_fmt.yml b/.github/workflows/cargo_fmt.yml index df466b3..72dec08 100644 --- a/.github/workflows/cargo_fmt.yml +++ b/.github/workflows/cargo_fmt.yml @@ -5,6 +5,7 @@ on: - 'examples/*.rs' - 'libraries/**/*.rs' - 'src/**/*.rs' + - 'tools/**/*.rs' - 'patches/**' - '**/Cargo.toml' - '.cargo/config' @@ -46,3 +47,9 @@ jobs: with: command: fmt args: --manifest-path libraries/crypto/Cargo.toml --all -- --check + + - name: Cargo format tools/heapviz + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --manifest-path tools/heapviz/Cargo.toml --all -- --check diff --git a/.github/workflows/heapviz_test.yml b/.github/workflows/heapviz_test.yml new file mode 100644 index 0000000..56f9b8c --- /dev/null +++ b/.github/workflows/heapviz_test.yml @@ -0,0 +1,32 @@ +--- +name: Heapviz tool tests +on: + push: + paths: + - 'tools/heapviz/**' + pull_request: + types: [opened, synchronize, reopened] + +jobs: + cbor_test: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + + - name: Check heapviz tool + uses: actions-rs/cargo@v1 + with: + command: check + args: --manifest-path tool/heapviz/Cargo.toml + + - name: Unit testing of heapviz tool (debug mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path tool/heapviz/Cargo.toml + + - name: Unit testing of heapviz tool (release mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path tool/heapviz/Cargo.toml --release diff --git a/run_desktop_tests.sh b/run_desktop_tests.sh index 8bcd2d7..d812b22 100755 --- a/run_desktop_tests.sh +++ b/run_desktop_tests.sh @@ -23,11 +23,16 @@ cd ../.. cd libraries/crypto cargo fmt --all -- --check cd ../.. +cd tools/heapviz +cargo fmt --all -- --check +cd ../.. echo "Building sha256sum tool..." cargo build --manifest-path third_party/tock/tools/sha256sum/Cargo.toml echo "Checking that heapviz tool builds properly..." cargo build --manifest-path tools/heapviz/Cargo.toml +echo "Testing heapviz tool..." +cargo test --manifest-path tools/heapviz/Cargo.toml echo "Checking that CTAP2 builds properly..." cargo check --release --target=thumbv7em-none-eabi diff --git a/tools/heapviz/Cargo.toml b/tools/heapviz/Cargo.toml index 4ab1466..4300f7e 100644 --- a/tools/heapviz/Cargo.toml +++ b/tools/heapviz/Cargo.toml @@ -9,5 +9,6 @@ edition = "2018" [dependencies] clap = "2.33.1" +lazy_static = "1.4.0" ncurses = "5.99.0" regex = "1" diff --git a/tools/heapviz/src/main.rs b/tools/heapviz/src/main.rs index f129692..86dcb1e 100644 --- a/tools/heapviz/src/main.rs +++ b/tools/heapviz/src/main.rs @@ -13,6 +13,7 @@ // limitations under the License. use clap::{App, Arg}; +use lazy_static::lazy_static; use regex::Regex; use std::fs::File; use std::io::{BufRead, BufReader, Read, Write}; @@ -59,6 +60,7 @@ fn parse_cli() -> Config { } /// An allocation or deallocation event. +#[cfg_attr(test, derive(Debug, PartialEq))] struct Event { /// Whether this even is an allocation (true) or a deallocation (false). is_alloc: bool, @@ -68,16 +70,14 @@ struct Event { len: usize, } -fn main() { - let config = parse_cli(); - +fn parse_event(line: &str) -> Option { // The following regex matches lines looking like the following from OpenSK's output. Such lines // are printed to the console when the `--debug-allocations` feature is enabled in the deploy // script. // // ``` - // alloc[256, 1] = 0x2002410c (2 ptrs, 384 bytes) - // dealloc[256, 1] = 0x2002410c (1 ptrs, 512 bytes) + // alloc[256, 1] = 0x2002401c (2 ptrs, 384 bytes) + // dealloc[64, 1] = 0x2002410c (1 ptrs, 512 bytes) // ``` // // The two integers between square brackets after the (de)alloc keywords represent the length @@ -90,20 +90,31 @@ fn main() { // - The keyword to know whether this operation is an allocation or a deallocation. // - The length of the allocated block. // - The starting address of the allocated block. - let re = Regex::new(r"^(alloc|dealloc)\[(\d+), \d+\] = 0x([0-9a-f]+) \(\d+ ptrs, \d+ bytes\)$") - .unwrap(); + lazy_static! { + static ref RE: Regex = + Regex::new(r"^(alloc|dealloc)\[(\d+), \d+\] = 0x([0-9a-f]+) \(\d+ ptrs, \d+ bytes\)$") + .unwrap(); + } + + RE.captures(line).map(|caps| { + let typ = caps.get(1).unwrap().as_str(); + let len = caps.get(2).unwrap().as_str().parse::().unwrap(); + let start = usize::from_str_radix(&caps.get(3).unwrap().as_str(), 16).unwrap(); + Event { + is_alloc: typ == "alloc", + start, + len, + } + }) +} + +fn main() { + let config = parse_cli(); let mut events = Vec::new(); for line in BufReader::new(config.logfile).lines() { - if let Some(caps) = re.captures(&line.unwrap()) { - let typ = caps.get(1).unwrap().as_str(); - let len = caps.get(2).unwrap().as_str().parse::().unwrap(); - let start = usize::from_str_radix(&caps.get(3).unwrap().as_str(), 16).unwrap(); - events.push(Event { - is_alloc: typ == "alloc", - start, - len, - }); + if let Some(event) = parse_event(&line.unwrap()) { + events.push(event); } } @@ -165,3 +176,42 @@ fn main() { ncurses::endwin(); } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_parse_event_alloc() { + assert_eq!( + parse_event("alloc[256, 1] = 0x2002401c (2 ptrs, 384 bytes)"), + Some(Event { + is_alloc: true, + start: 0x2002401c, + len: 256, + }) + ); + } + + #[test] + fn test_parse_event_dealloc() { + assert_eq!( + parse_event("dealloc[64, 1] = 0x2002410c (1 ptrs, 512 bytes)"), + Some(Event { + is_alloc: false, + start: 0x2002410c, + len: 64, + }) + ); + } + + #[test] + fn test_parse_event_none() { + assert_eq!( + parse_event( + "NRF52 HW INFO: Variant: AAD0, Part: N52840, Package: QI, Ram: K256, Flash: K1024" + ), + None + ); + } +}