Skip to content

Commit

Permalink
add unit test for Config::verify
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Oct 11, 2023
1 parent d32a733 commit 656417a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/bootstrap/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use crate::config::TomlConfig;
use super::{Config, Flags};
use clap::CommandFactory;
use serde::Deserialize;
use std::{env, path::Path};
use std::{
env,
fs::{remove_file, File},
io::Write,
path::Path,
};

fn parse(config: &str) -> Config {
Config::parse_inner(&["check".to_owned(), "--config=/does/not/exist".to_owned()], |&_| {
Expand Down Expand Up @@ -196,3 +201,22 @@ fn rust_optimize() {
fn invalid_rust_optimize() {
parse("rust.optimize = \"a\"");
}

#[test]
fn verify_file_integrity() {
let config = parse("");

let tempfile = config.tempdir().join(".tmp-test-file");

let mut file = File::create(&tempfile).unwrap();
assert!(tempfile.exists());

let file_content = b"dummy value";
file.write_all(file_content).unwrap();
assert!(
config
.verify(&tempfile, "7e255dd9542648a8779268a0f268b891a198e9828e860ed23f826440e786eae5")
);

remove_file(tempfile).unwrap();
}
2 changes: 1 addition & 1 deletion src/bootstrap/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl Config {
}

/// Returns whether the SHA256 checksum of `path` matches `expected`.
fn verify(&self, path: &Path, expected: &str) -> bool {
pub(crate) fn verify(&self, path: &Path, expected: &str) -> bool {
use sha2::Digest;

self.verbose(&format!("verifying {}", path.display()));
Expand Down

0 comments on commit 656417a

Please sign in to comment.