|
| 1 | +//! Checksum builtins - md5sum, sha1sum, sha256sum |
| 2 | +
|
| 3 | +use async_trait::async_trait; |
| 4 | +use md5::Md5; |
| 5 | +use sha1::Sha1; |
| 6 | +use sha2::{Digest, Sha256}; |
| 7 | + |
| 8 | +use super::{Builtin, Context}; |
| 9 | +use crate::error::Result; |
| 10 | +use crate::interpreter::ExecResult; |
| 11 | + |
| 12 | +/// md5sum builtin - compute MD5 message digest |
| 13 | +pub struct Md5sum; |
| 14 | + |
| 15 | +/// sha1sum builtin - compute SHA-1 message digest |
| 16 | +pub struct Sha1sum; |
| 17 | + |
| 18 | +/// sha256sum builtin - compute SHA-256 message digest |
| 19 | +pub struct Sha256sum; |
| 20 | + |
| 21 | +#[async_trait] |
| 22 | +impl Builtin for Md5sum { |
| 23 | + async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> { |
| 24 | + checksum_execute::<Md5>(&ctx, "md5sum").await |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl Builtin for Sha1sum { |
| 30 | + async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> { |
| 31 | + checksum_execute::<Sha1>(&ctx, "sha1sum").await |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[async_trait] |
| 36 | +impl Builtin for Sha256sum { |
| 37 | + async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> { |
| 38 | + checksum_execute::<Sha256>(&ctx, "sha256sum").await |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async fn checksum_execute<D: Digest>(ctx: &Context<'_>, cmd: &str) -> Result<ExecResult> { |
| 43 | + let files: Vec<&String> = ctx.args.iter().filter(|a| !a.starts_with('-')).collect(); |
| 44 | + |
| 45 | + let mut output = String::new(); |
| 46 | + |
| 47 | + if files.is_empty() { |
| 48 | + // Read from stdin |
| 49 | + let input = ctx.stdin.unwrap_or(""); |
| 50 | + let hash = hex_digest::<D>(input.as_bytes()); |
| 51 | + output.push_str(&hash); |
| 52 | + output.push_str(" -\n"); |
| 53 | + } else { |
| 54 | + for file in &files { |
| 55 | + let path = if file.starts_with('/') { |
| 56 | + std::path::PathBuf::from(file) |
| 57 | + } else { |
| 58 | + ctx.cwd.join(file) |
| 59 | + }; |
| 60 | + |
| 61 | + match ctx.fs.read_file(&path).await { |
| 62 | + Ok(content) => { |
| 63 | + let hash = hex_digest::<D>(&content); |
| 64 | + output.push_str(&hash); |
| 65 | + output.push_str(" "); |
| 66 | + output.push_str(file); |
| 67 | + output.push('\n'); |
| 68 | + } |
| 69 | + Err(e) => { |
| 70 | + return Ok(ExecResult::err(format!("{}: {}: {}\n", cmd, file, e), 1)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Ok(ExecResult::ok(output)) |
| 77 | +} |
| 78 | + |
| 79 | +fn hex_digest<D: Digest>(data: &[u8]) -> String { |
| 80 | + let result = D::digest(data); |
| 81 | + result.iter().map(|b| format!("{:02x}", b)).collect() |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +#[allow(clippy::unwrap_used)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + use std::collections::HashMap; |
| 89 | + use std::path::PathBuf; |
| 90 | + use std::sync::Arc; |
| 91 | + |
| 92 | + use crate::fs::InMemoryFs; |
| 93 | + |
| 94 | + async fn run_checksum<B: Builtin>( |
| 95 | + builtin: &B, |
| 96 | + args: &[&str], |
| 97 | + stdin: Option<&str>, |
| 98 | + ) -> ExecResult { |
| 99 | + let fs = Arc::new(InMemoryFs::new()); |
| 100 | + let mut variables = HashMap::new(); |
| 101 | + let env = HashMap::new(); |
| 102 | + let mut cwd = PathBuf::from("/"); |
| 103 | + |
| 104 | + let args: Vec<String> = args.iter().map(|s| s.to_string()).collect(); |
| 105 | + let ctx = Context { |
| 106 | + args: &args, |
| 107 | + env: &env, |
| 108 | + variables: &mut variables, |
| 109 | + cwd: &mut cwd, |
| 110 | + fs, |
| 111 | + stdin, |
| 112 | + #[cfg(feature = "http_client")] |
| 113 | + http_client: None, |
| 114 | + #[cfg(feature = "git")] |
| 115 | + git_client: None, |
| 116 | + }; |
| 117 | + |
| 118 | + builtin.execute(ctx).await.unwrap() |
| 119 | + } |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn test_md5sum_stdin() { |
| 123 | + let result = run_checksum(&Md5sum, &[], Some("hello\n")).await; |
| 124 | + assert_eq!(result.exit_code, 0); |
| 125 | + // md5("hello\n") = b1946ac92492d2347c6235b4d2611184 |
| 126 | + assert!(result |
| 127 | + .stdout |
| 128 | + .starts_with("b1946ac92492d2347c6235b4d2611184")); |
| 129 | + assert!(result.stdout.contains(" -")); |
| 130 | + } |
| 131 | + |
| 132 | + #[tokio::test] |
| 133 | + async fn test_sha256sum_stdin() { |
| 134 | + let result = run_checksum(&Sha256sum, &[], Some("hello\n")).await; |
| 135 | + assert_eq!(result.exit_code, 0); |
| 136 | + // sha256("hello\n") = 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 |
| 137 | + assert!(result |
| 138 | + .stdout |
| 139 | + .starts_with("5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03")); |
| 140 | + } |
| 141 | + |
| 142 | + #[tokio::test] |
| 143 | + async fn test_sha1sum_stdin() { |
| 144 | + let result = run_checksum(&Sha1sum, &[], Some("hello\n")).await; |
| 145 | + assert_eq!(result.exit_code, 0); |
| 146 | + // sha1("hello\n") = f572d396fae9206628714fb2ce00f72e94f2258f |
| 147 | + assert!(result |
| 148 | + .stdout |
| 149 | + .starts_with("f572d396fae9206628714fb2ce00f72e94f2258f")); |
| 150 | + } |
| 151 | + |
| 152 | + #[tokio::test] |
| 153 | + async fn test_md5sum_empty() { |
| 154 | + let result = run_checksum(&Md5sum, &[], Some("")).await; |
| 155 | + assert_eq!(result.exit_code, 0); |
| 156 | + // md5("") = d41d8cd98f00b204e9800998ecf8427e |
| 157 | + assert!(result |
| 158 | + .stdout |
| 159 | + .starts_with("d41d8cd98f00b204e9800998ecf8427e")); |
| 160 | + } |
| 161 | +} |
0 commit comments