From 3c01b5137f7314b8021f9225525bd7d2a88d22ca Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Fri, 14 Apr 2023 02:15:43 -0400 Subject: [PATCH] temp --- benches/bench.rs | 103 ++++++++++++++++++++++++++++++++++++++ src/constants.rs | 6 ++- src/rpm/headers/header.rs | 12 +++++ src/rpm/package.rs | 7 +++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 benches/bench.rs diff --git a/benches/bench.rs b/benches/bench.rs new file mode 100644 index 00000000..82597bba --- /dev/null +++ b/benches/bench.rs @@ -0,0 +1,103 @@ +use std::error::Error; +use std::str::FromStr; + +use criterion::{self, criterion_group, criterion_main, Criterion}; + +use rpm::signature::pgp::{Signer, Verifier}; +use rpm::{self, RPMPackage}; + +fn build_rpm(c: &mut Criterion) { + let mut group = c.benchmark_group("build_rpm"); + + group.bench_function("build_rpm", |b| { + b.iter(|| { + let pkg = + rpm::RPMBuilder::new("test", "1.0.0", "MIT", "x86_64", "some awesome package") + .compression(rpm::Compressor::from_str("gzip").unwrap()) + .with_file( + "./test_assets/awesome.toml", + rpm::RPMFileOptions::new("/etc/awesome/config.toml").is_config(), + ) + .unwrap() + // file mode is inherited from source file + .with_file( + "./test_assets/awesome.py", + rpm::RPMFileOptions::new("/usr/bin/awesome"), + ) + .unwrap() + .with_file( + "./test_assets/awesome.toml", + // you can set a custom mode and custom user too + rpm::RPMFileOptions::new("/etc/awesome/second.toml") + .mode(0o100744) + .user("hugo"), + ) + .unwrap() + .pre_install_script("echo preinst") + .add_changelog_entry("me", "was awesome, eh?", 123123123) + .add_changelog_entry("you", "yeah, it was", 12312312) + .requires(rpm::Dependency::any("wget")) + .vendor("corporation or individual") + .url("www.github.com/repo") + .vcs("git:repo=example_repo:branch=example_branch:sha=example_sha") + .build() + .unwrap(); + }) + }); + group.finish(); +} + +fn parse_rpm(c: &mut Criterion) -> Result<(), Box> { + let mut group = c.benchmark_group("parse_rpm"); + let rpm_file = std::fs::read("test_assets/389-ds-base-devel-1.3.8.4-15.el7.x86_64.rpm")?; + + group.bench_function("parse_rpm", |b| { + b.iter(|| { + RPMPackage::parse(&mut std::io::Cursor::new(&rpm_file)).unwrap(); + }) + }); + group.finish(); + + Ok(()) +} + +fn verify_rpm(c: &mut Criterion) -> Result<(), Box> { + let mut group = c.benchmark_group("verify_rpm"); + + let raw_pub_key = std::fs::read("test_assets/public_key.asc")?; + let pkg = rpm::RPMPackage::open("test_assets/ima_signed.rpm")?; + let verifier = Verifier::load_from_asc_bytes(&raw_pub_key).unwrap(); + + pkg.verify_signature(&verifier).unwrap(); + + group.bench_function("verify_rpm", |b| { + b.iter(|| pkg.verify_signature(&verifier).unwrap()); + }); + group.finish(); + + Ok(()) +} + +fn sign_rpm(c: &mut Criterion) -> Result<(), Box> { + let mut group = c.benchmark_group("sign_rpm"); + + let raw_secret_key = std::fs::read("./test_assets/secret_key.asc")?; + group.bench_function("sign_rpm", |b| { + let mut pkg = + rpm::RPMPackage::open("test_assets/389-ds-base-devel-1.3.8.4-15.el7.x86_64.rpm") + .unwrap(); + + b.iter(|| { + criterion::black_box( + pkg.sign(Signer::load_from_asc_bytes(&raw_secret_key).unwrap()) + .unwrap(), + ); + }) + }); + group.finish(); + + Ok(()) +} + +criterion_group!(benches, build_rpm, parse_rpm, sign_rpm, verify_rpm); +criterion_main!(benches); diff --git a/src/constants.rs b/src/constants.rs index 5fdeeed9..05670ec0 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -20,6 +20,8 @@ pub const RPMTAG_SIG_BASE: u32 = HEADER_SIGBASE; num_derive::ToPrimitive, Debug, PartialEq, + PartialOrd, + Ord, Eq, Copy, Clone, @@ -361,6 +363,8 @@ pub enum IndexTag { num_derive::ToPrimitive, Debug, PartialEq, + PartialOrd, + Ord, Eq, Copy, Clone, @@ -428,7 +432,7 @@ pub enum IndexSignatureTag { /// Each and every header has a particular header tag that identifies the type of /// the header the format / information contained in that header. pub trait Tag: - num::FromPrimitive + num::ToPrimitive + PartialEq + std::fmt::Display + std::fmt::Debug + Copy + num::FromPrimitive + num::ToPrimitive + PartialEq + std::fmt::Display + std::fmt::Debug + Copy + Ord { fn tag_type_name() -> &'static str; } diff --git a/src/rpm/headers/header.rs b/src/rpm/headers/header.rs index 08df432d..2e2d9e9f 100644 --- a/src/rpm/headers/header.rs +++ b/src/rpm/headers/header.rs @@ -601,6 +601,18 @@ pub(crate) struct IndexEntry { pub(crate) num_items: u32, } +impl PartialOrd for IndexEntry { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.tag.cmp(&other.tag)) + } +} + +impl Ord for IndexEntry { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.tag.cmp(&other.tag) + } +} + impl IndexEntry { // 16 bytes pub(crate) fn parse(input: &[u8]) -> Result<(&[u8], Self), RPMError> { diff --git a/src/rpm/package.rs b/src/rpm/package.rs index ac135547..9875d8ae 100644 --- a/src/rpm/package.rs +++ b/src/rpm/package.rs @@ -86,6 +86,13 @@ impl RPMPackage { Ok(()) } + pub fn canonicalize(&mut self) -> Result<&mut Self, RPMError> { + self.metadata.header.index_entries.sort(); + self.metadata.signature.index_entries.sort(); + + Ok(self) + } + #[cfg(feature = "async-futures")] pub async fn write_async(&self, out: &mut W) -> Result<(), RPMError> { self.metadata.write_async(out).await?;