From 4d2e67f799eca523f053b889eba6bb7e9e2da2cd Mon Sep 17 00:00:00 2001 From: nikstur Date: Wed, 15 Feb 2023 23:09:24 +0100 Subject: [PATCH] tool: make some utility test functions reusable Make them reusable by moving them to the common module. --- rust/tool/tests/common/mod.rs | 45 +++++++++++++++++++++++++++++++++ rust/tool/tests/gc.rs | 8 +++--- rust/tool/tests/systemd_boot.rs | 42 +++--------------------------- 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/rust/tool/tests/common/mod.rs b/rust/tool/tests/common/mod.rs index 828f868..aac101d 100644 --- a/rust/tool/tests/common/mod.rs +++ b/rust/tool/tests/common/mod.rs @@ -1,6 +1,12 @@ +// Utility code in this module can become marked as dead code if it is not used in every single +// module in `tests/`. Thus we need to allow dead code here. See +// https://stackoverflow.com/a/67902444 +#![allow(dead_code)] + use std::ffi::OsStr; use std::fs; use std::io::Write; +use std::os::unix::prelude::MetadataExt; use std::path::{Path, PathBuf}; use std::process::Output; @@ -9,6 +15,7 @@ use assert_cmd::Command; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; use serde_json::json; +use sha2::{Digest, Sha256}; /// Create a mock generation link. /// @@ -150,3 +157,41 @@ fn systemd_location_from_env() -> Result { On a system with Nix installed, you can set it with: export TEST_SYSTEMD=$(nix-build '' -A systemd)"; std::env::var("TEST_SYSTEMD").context(error_msg) } + +/// Look up the modification time (mtime) of a file. +pub fn mtime(path: &Path) -> i64 { + fs::metadata(path) + .expect("Failed to read modification time.") + .mtime() +} + +pub fn hash_file(path: &Path) -> sha2::digest::Output { + Sha256::digest(fs::read(path).expect("Failed to read file to hash.")) +} + +/// Remove signature from a signed PE file. +pub fn remove_signature(path: &Path) -> Result<()> { + let output = Command::new("sbattach") + .arg("--remove") + .arg(path.as_os_str()) + .output()?; + print!("{}", String::from_utf8(output.stdout)?); + print!("{}", String::from_utf8(output.stderr)?); + Ok(()) +} + +/// Verify signature of PE file. +pub fn verify_signature(path: &Path) -> Result { + let output = Command::new("sbverify") + .arg(path.as_os_str()) + .arg("--cert") + .arg("tests/fixtures/uefi-keys/db.pem") + .output()?; + print!("{}", String::from_utf8(output.stdout)?); + print!("{}", String::from_utf8(output.stderr)?); + Ok(output.status.success()) +} + +pub fn count_files(path: &Path) -> Result { + Ok(fs::read_dir(path)?.count()) +} diff --git a/rust/tool/tests/gc.rs b/rust/tool/tests/gc.rs index e9dece6..e969cd6 100644 --- a/rust/tool/tests/gc.rs +++ b/rust/tool/tests/gc.rs @@ -1,11 +1,13 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use anyhow::Result; use tempfile::tempdir; mod common; +use common::count_files; + #[test] fn keep_only_configured_number_of_generations() -> Result<()> { let esp_mountpoint = tempdir()?; @@ -81,7 +83,3 @@ fn keep_unrelated_files_on_esp() -> Result<()> { Ok(()) } - -fn count_files(path: &Path) -> Result { - Ok(fs::read_dir(path)?.count()) -} diff --git a/rust/tool/tests/systemd_boot.rs b/rust/tool/tests/systemd_boot.rs index 89f1cfb..c6a07ed 100644 --- a/rust/tool/tests/systemd_boot.rs +++ b/rust/tool/tests/systemd_boot.rs @@ -1,13 +1,13 @@ -use sha2::{Digest, Sha256}; -use std::path::{Path, PathBuf}; -use std::process::Command; -use std::{fs, os::unix::prelude::MetadataExt}; +use std::fs; +use std::path::PathBuf; use anyhow::Result; use tempfile::tempdir; mod common; +use common::{hash_file, mtime, remove_signature, verify_signature}; + #[test] fn keep_systemd_boot_binaries() -> Result<()> { let esp = tempdir()?; @@ -119,37 +119,3 @@ fn systemd_boot_path(esp: &tempfile::TempDir) -> PathBuf { fn systemd_boot_fallback_path(esp: &tempfile::TempDir) -> PathBuf { esp.path().join("EFI/BOOT/BOOTX64.EFI") } - -/// Look up the modification time (mtime) of a file. -fn mtime(path: &Path) -> i64 { - fs::metadata(path) - .expect("Failed to read modification time.") - .mtime() -} - -fn hash_file(path: &Path) -> sha2::digest::Output { - Sha256::digest(fs::read(path).expect("Failed to read file to hash.")) -} - -/// Remove signature from a signed PE file. -pub fn remove_signature(path: &Path) -> Result<()> { - let output = Command::new("sbattach") - .arg("--remove") - .arg(path.as_os_str()) - .output()?; - print!("{}", String::from_utf8(output.stdout)?); - print!("{}", String::from_utf8(output.stderr)?); - Ok(()) -} - -/// Verify signature of PE file. -pub fn verify_signature(path: &Path) -> Result { - let output = Command::new("sbverify") - .arg(path.as_os_str()) - .arg("--cert") - .arg("tests/fixtures/uefi-keys/db.pem") - .output()?; - print!("{}", String::from_utf8(output.stdout)?); - print!("{}", String::from_utf8(output.stderr)?); - Ok(output.status.success()) -}