Merge pull request #108 from nix-community/make-test-functions-reusable
tool: make some utility test functions reusable
This commit is contained in:
commit
ceed92460f
|
@ -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::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::os::unix::prelude::MetadataExt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Output;
|
use std::process::Output;
|
||||||
|
|
||||||
|
@ -9,6 +15,7 @@ use assert_cmd::Command;
|
||||||
use rand::distributions::Alphanumeric;
|
use rand::distributions::Alphanumeric;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
/// Create a mock generation link.
|
/// Create a mock generation link.
|
||||||
///
|
///
|
||||||
|
@ -150,3 +157,41 @@ fn systemd_location_from_env() -> Result<String> {
|
||||||
On a system with Nix installed, you can set it with: export TEST_SYSTEMD=$(nix-build '<nixpkgs>' -A systemd)";
|
On a system with Nix installed, you can set it with: export TEST_SYSTEMD=$(nix-build '<nixpkgs>' -A systemd)";
|
||||||
std::env::var("TEST_SYSTEMD").context(error_msg)
|
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> {
|
||||||
|
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<bool> {
|
||||||
|
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<usize> {
|
||||||
|
Ok(fs::read_dir(path)?.count())
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
|
use common::count_files;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn keep_only_configured_number_of_generations() -> Result<()> {
|
fn keep_only_configured_number_of_generations() -> Result<()> {
|
||||||
let esp_mountpoint = tempdir()?;
|
let esp_mountpoint = tempdir()?;
|
||||||
|
@ -81,7 +83,3 @@ fn keep_unrelated_files_on_esp() -> Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_files(path: &Path) -> Result<usize> {
|
|
||||||
Ok(fs::read_dir(path)?.count())
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use sha2::{Digest, Sha256};
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
|
||||||
use std::{fs, os::unix::prelude::MetadataExt};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
|
use common::{hash_file, mtime, remove_signature, verify_signature};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn keep_systemd_boot_binaries() -> Result<()> {
|
fn keep_systemd_boot_binaries() -> Result<()> {
|
||||||
let esp = tempdir()?;
|
let esp = tempdir()?;
|
||||||
|
@ -119,37 +119,3 @@ fn systemd_boot_path(esp: &tempfile::TempDir) -> PathBuf {
|
||||||
fn systemd_boot_fallback_path(esp: &tempfile::TempDir) -> PathBuf {
|
fn systemd_boot_fallback_path(esp: &tempfile::TempDir) -> PathBuf {
|
||||||
esp.path().join("EFI/BOOT/BOOTX64.EFI")
|
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> {
|
|
||||||
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<bool> {
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue