asn1parse/tests/cli_flags.rs

73 lines
2.2 KiB
Rust

use std::env;
use std::fs;
use std::io::Write;
use std::process::Command;
fn bin() -> String {
env!("CARGO_BIN_EXE_asn1parse").to_string()
}
#[test]
fn parses_positional_hex_sequence_of_integers() {
// SEQUENCE { INTEGER 1, INTEGER 2 }
let hex = "3006020101020102";
let out = Command::new(bin())
.arg(hex)
.args(["--color", "never"])
.output()
.expect("run binary");
assert!(out.status.success(), "binary exited non-zero");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("SEQUENCE"));
assert!(stdout.contains("INTEGER"));
}
#[test]
fn mutual_exclusive_hex_and_file_exits_2() {
let out = Command::new(bin())
.args(["--file", "tests/data/test.cert.der"])
.arg("00")
.output()
.expect("run binary");
assert_eq!(out.status.code(), Some(2));
}
#[test]
fn input_format_auto_parses_text_file_with_hex() {
// Create a temporary file with hex for INTEGER 1: 02 01 01
let mut path = env::temp_dir();
let fname = format!("asn1parse_test_{}.hex", std::time::SystemTime::now().elapsed().unwrap().as_nanos());
path.push(fname);
let mut f = fs::File::create(&path).expect("create temp file");
writeln!(f, "02 01 01").unwrap();
drop(f);
let out = Command::new(bin())
.args(["--file"])
.arg(path.as_os_str())
.args(["--input-format", "auto", "--color", "never"])
.output()
.expect("run binary");
assert!(out.status.success(), "binary exited non-zero");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("INTEGER"));
assert!(stdout.contains("1"));
// cleanup
let _ = fs::remove_file(path);
}
#[test]
fn bits_format_hex_with_truncate_bytes() {
// BIT STRING: 03 03 00 DE AD (unused=0, bytes=DE AD)
let hex = "030300DEAD";
let out = Command::new(bin())
.args([hex, "--bits-format", "hex", "--bits-truncate-bytes", "1", "--color", "never"])
.output()
.expect("run binary");
assert!(out.status.success(), "binary exited non-zero");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("BIT STRING"));
assert!(stdout.contains("0xDE"));
assert!(stdout.contains("truncated"));
}