1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-02 06:23:18 +02:00

flashrom_tester: Use path types for things that are paths

Use Path and PathBuf for things that are paths.

BUG=b:243460685
BRANCH=None
TEST=/usr/bin/flashrom_tester --flashrom_binary /usr/sbin/flashrom host
TEST=/usr/bin/flashrom_tester --libflashrom host

Change-Id: I69531bec5436a60430eae975eeab02c8835962bf
Signed-off-by: Evan Benn <evanbenn@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/69064
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
This commit is contained in:
Evan Benn
2022-11-01 11:43:10 +11:00
committed by Edward O'Callaghan
parent 8170c89568
commit c726a693d6
6 changed files with 79 additions and 69 deletions

View File

@ -36,10 +36,11 @@
use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;
use std::path::Path;
use rand::prelude::*;
pub fn gen_rand_testdata(path: &str, size: usize) -> std::io::Result<()> {
pub fn gen_rand_testdata(path: &Path, size: usize) -> std::io::Result<()> {
let mut buf = BufWriter::new(File::create(path)?);
let mut a: Vec<u8> = vec![0; size];
@ -58,8 +59,8 @@ mod tests {
fn gen_rand_testdata() {
use super::gen_rand_testdata;
let path0 = "/tmp/idk_test00";
let path1 = "/tmp/idk_test01";
let path0 = Path::new("/tmp/idk_test00");
let path1 = Path::new("/tmp/idk_test01");
let sz = 1024;
gen_rand_testdata(path0, sz).unwrap();

View File

@ -42,6 +42,8 @@ use serde_json::json;
use std::fs::File;
use std::io::Write;
use std::mem::MaybeUninit;
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
@ -60,13 +62,11 @@ pub struct TestEnv<'a> {
pub wp: WriteProtectState<'a, 'static>,
/// The path to a file containing the flash contents at test start.
// TODO(pmarheine) migrate this to a PathBuf for clarity
original_flash_contents: String,
original_flash_contents: PathBuf,
/// The path to a file containing flash-sized random data
// TODO(pmarheine) make this a PathBuf too
random_data: String,
random_data: PathBuf,
/// The path to a file containing layout data.
pub layout_file: String,
pub layout_file: PathBuf,
}
impl<'a> TestEnv<'a> {
@ -83,7 +83,7 @@ impl<'a> TestEnv<'a> {
wp: WriteProtectState::from_hardware(cmd, chip_type)?,
original_flash_contents: "/tmp/flashrom_tester_golden.bin".into(),
random_data: "/tmp/random_content.bin".into(),
layout_file: create_layout_file(rom_sz, "/tmp/", print_layout),
layout_file: create_layout_file(rom_sz, Path::new("/tmp/"), print_layout),
};
info!("Stashing golden image for verification/recovery on completion");
@ -122,7 +122,7 @@ impl<'a> TestEnv<'a> {
/// Return the path to a file that contains random data and is the same size
/// as the flash chip.
pub fn random_data_file(&self) -> &str {
pub fn random_data_file(&self) -> &Path {
&self.random_data
}
@ -156,7 +156,7 @@ impl<'a> TestEnv<'a> {
/// path.
///
/// Returns Err if they are not the same.
pub fn verify(&self, contents_path: &str) -> Result<(), FlashromError> {
pub fn verify(&self, contents_path: &Path) -> Result<(), FlashromError> {
self.cmd.verify_from_file(contents_path)?;
Ok(())
}
@ -496,12 +496,11 @@ fn decode_test_result(res: TestResult, con: TestConclusion) -> (TestConclusion,
}
}
fn create_layout_file(rom_sz: i64, tmp_dir: &str, print_layout: bool) -> String {
fn create_layout_file(rom_sz: i64, tmp_dir: &Path, print_layout: bool) -> PathBuf {
info!("Calculate ROM partition sizes & Create the layout file.");
let layout_sizes = utils::get_layout_sizes(rom_sz).expect("Could not partition rom");
let mut layout_file = tmp_dir.to_string();
layout_file.push_str("/layout.file");
let layout_file = tmp_dir.join("layout.file");
let mut f = File::create(&layout_file).expect("Could not create layout file");
let mut buf: Vec<u8> = vec![];
utils::construct_layout_file(&mut buf, &layout_sizes).expect("Could not construct layout file");

View File

@ -224,7 +224,7 @@ fn elog_sanity_test(env: &mut TestEnv) -> TestResult {
const ELOG_RW_REGION_NAME: &str = "RW_ELOG";
env.cmd
.read_region_into_file(ELOG_FILE, ELOG_RW_REGION_NAME)?;
.read_region_into_file(ELOG_FILE.as_ref(), ELOG_RW_REGION_NAME)?;
// Just checking for the magic numer
// TODO: improve this test to read the events