Skip to content

Commit

Permalink
Merge pull request #711 from ReFirmLabs/no_unwrap_in_examples
Browse files Browse the repository at this point in the history
Removed unwrap() from code examples
  • Loading branch information
devttys0 authored Oct 24, 2024
2 parents 3d67da7 + 6c80473 commit 8d3a998
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
28 changes: 25 additions & 3 deletions src/binwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::extractors;
use crate::magic;
use crate::signatures;

/// Returned on initialization error
#[derive(Debug, Default, Clone)]
pub struct BinwalkError;

Expand Down Expand Up @@ -100,12 +101,19 @@ impl Binwalk {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_102_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
/// use binwalk::Binwalk;
///
/// // Don't scan for these file signatures
/// let exclude_filters: Vec<String> = vec!["jpeg".to_string(), "png".to_string()];
///
/// let binwalker = Binwalk::configure(None, None, None, Some(exclude_filters), None).unwrap();
/// let binwalker = Binwalk::configure(None,
/// None,
/// None,
/// Some(exclude_filters),
/// None)?;
/// # Ok(binwalker)
/// # } _doctest_main_src_binwalk_rs_102_0(); }
/// ```
pub fn configure(
target_file_name: Option<String>,
Expand Down Expand Up @@ -525,13 +533,18 @@ impl Binwalk {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_529_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
/// use binwalk::Binwalk;
///
/// # std::fs::remove_dir_all("/tmp/foobar");
/// let target_path = "/usr/share/man/man2/accept.2.gz".to_string();
/// let extraction_directory = "/tmp/foobar/extractions".to_string();
///
/// let binwalker = Binwalk::configure(Some(target_path), Some(extraction_directory), None, None, None).unwrap();
/// let binwalker = Binwalk::configure(Some(target_path),
/// Some(extraction_directory),
/// None,
/// None,
/// None)?;
///
/// let file_data = std::fs::read(&binwalker.base_target_file).expect("Unable to read file");
///
Expand All @@ -542,6 +555,8 @@ impl Binwalk {
/// assert_eq!(extraction_results.len(), 1);
/// assert_eq!(std::path::Path::new("/tmp/foobar/extractions/accept.2.gz.extracted/0/decompressed.bin").exists(), true);
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(binwalker)
/// # } _doctest_main_src_binwalk_rs_529_0(); }
/// ```
pub fn extract(
&self,
Expand Down Expand Up @@ -619,20 +634,27 @@ impl Binwalk {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_624_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
/// use binwalk::Binwalk;
///
/// # std::fs::remove_dir_all("/tmp/foobar");
/// let target_path = "/usr/share/man/man2/accept.2.gz".to_string();
/// let extraction_directory = "/tmp/foobar/extractions".to_string();
///
/// let binwalker = Binwalk::configure(Some(target_path), Some(extraction_directory), None, None, None).unwrap();
/// let binwalker = Binwalk::configure(Some(target_path),
/// Some(extraction_directory),
/// None,
/// None,
/// None)?;
///
/// let analysis_results = binwalker.analyze(&binwalker.base_target_file, true);
///
/// assert_eq!(analysis_results.file_map.len(), 1);
/// assert_eq!(analysis_results.extractions.len(), 1);
/// assert_eq!(std::path::Path::new("/tmp/foobar/extractions/accept.2.gz.extracted/0/decompressed.bin").exists(), true);
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(binwalker)
/// # } _doctest_main_src_binwalk_rs_624_0(); }
/// ```
pub fn analyze(&self, target_file: &String, do_extraction: bool) -> AnalysisResults {
// Return value
Expand Down
6 changes: 4 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::io::Read;
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_common_rs_11_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::common::read_file;
///
/// let file_data = read_file("/etc/passwd").unwrap();
///
/// let file_data = read_file("/etc/passwd")?;
/// assert!(file_data.len() > 0);
/// # Ok(())
/// # } _doctest_main_src_common_rs_11_0(); }
/// ```
pub fn read_file(file: impl Into<String>) -> Result<Vec<u8>, std::io::Error> {
let mut file_data = Vec::new();
Expand Down
40 changes: 32 additions & 8 deletions src/extractors/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_213_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -220,8 +221,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_file("created_file.txt", file_data), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/created_file.txt").unwrap(), "foobar");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/created_file.txt")?, "foobar");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_213_0(); }
/// ```
pub fn create_file(&self, file_path: impl Into<String>, file_data: &[u8]) -> bool {
let safe_file_path: String = self.chrooted_path(file_path);
Expand Down Expand Up @@ -250,6 +253,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_255_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -259,8 +263,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.carve_file("carved_file.txt", file_data_with_trailing_junk, 0, 6), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/carved_file.txt").unwrap(), "foobar");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/carved_file.txt")?, "foobar");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// } _doctest_main_src_extractors_common_rs_255_0(); }
/// ```
pub fn carve_file(
&self,
Expand Down Expand Up @@ -304,6 +310,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_312_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -314,8 +321,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_character_device("char_device", dev_major, dev_minor), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/char_device").unwrap(), "c 1 2");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/char_device")?, "c 1 2");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_312_0(); }
/// ```
pub fn create_character_device(
&self,
Expand All @@ -333,6 +342,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_345_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -343,8 +353,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_block_device("block_device", dev_major, dev_minor), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/block_device").unwrap(), "b 1 2");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/block_device")?, "b 1 2");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_345_0(); }
/// ```
pub fn create_block_device(
&self,
Expand All @@ -362,6 +374,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_377_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -370,8 +383,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_fifo("fifo_file"), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/fifo_file").unwrap(), "fifo");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/fifo_file")?, "fifo");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_377_0(); }
/// ```
pub fn create_fifo(&self, file_path: impl Into<String>) -> bool {
self.create_file(file_path, b"fifo")
Expand All @@ -384,6 +399,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_401_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -392,8 +408,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_socket("socket_file"), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/socket_file").unwrap(), "socket");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/socket_file")?, "socket");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_401_0(); }
/// ```
pub fn create_socket(&self, file_path: impl Into<String>) -> bool {
self.create_file(file_path, b"socket")
Expand All @@ -406,6 +424,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_426_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -415,8 +434,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.append_to_file("append.txt", my_file_data), true);
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/append.txt").unwrap(), "foobar");
/// assert_eq!(std::fs::read_to_string("/tmp/foobar/append.txt")?, "foobar");
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_426_0(); }
/// ```
pub fn append_to_file(&self, file_path: impl Into<String>, data: &[u8]) -> bool {
let safe_file_path: String = self.chrooted_path(file_path);
Expand Down Expand Up @@ -548,6 +569,7 @@ impl Chroot {
/// ## Example
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_extractors_common_rs_571_0() -> Result<(), Box<dyn std::error::Error>> {
/// use binwalk::extractors::common::Chroot;
///
/// let chroot_dir = "/tmp/foobar".to_string();
Expand All @@ -556,8 +578,10 @@ impl Chroot {
/// let chroot = Chroot::new(Some(&chroot_dir));
///
/// assert_eq!(chroot.create_symlink("symlink", "/"), true);
/// assert_eq!(std::fs::canonicalize("/tmp/foobar/symlink").unwrap().to_str(), Some("/tmp/foobar"));
/// assert_eq!(std::fs::canonicalize("/tmp/foobar/symlink")?.to_str(), Some("/tmp/foobar"));
/// # std::fs::remove_dir_all("/tmp/foobar");
/// # Ok(())
/// # } _doctest_main_src_extractors_common_rs_571_0(); }
/// ```
pub fn create_symlink(
&self,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pub mod extractors;
mod magic;
pub mod signatures;
pub mod structures;
pub use binwalk::{AnalysisResults, Binwalk};
pub use binwalk::{AnalysisResults, Binwalk, BinwalkError};
5 changes: 4 additions & 1 deletion src/structures/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct StructureError;
/// ## Example:
///
/// ```
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_structures_common_rs_34_0() -> Result<bool, binwalk::structures::common::StructureError> {
/// use binwalk::structures;
///
/// let my_structure = vec![
Expand All @@ -43,10 +44,12 @@ pub struct StructureError;
/// ];
///
/// let some_data = b"AAAA\x01\x00\x00\x00\x00\x00\x00\x00\x08\x0A\x0B\x0C\x01\x02";
/// let header = structures::common::parse(some_data, &my_structure, "little").unwrap();
/// let header = structures::common::parse(some_data, &my_structure, "little")?;
///
/// assert_eq!(header["magic"], 0x41414141);
/// assert_eq!(header["checksum"], 0x0201);
/// # Ok(true)
/// # } _doctest_main_src_structures_common_rs_34_0(); }
/// ```
pub fn parse(
data: &[u8],
Expand Down

0 comments on commit 8d3a998

Please sign in to comment.