Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Apr 14, 2023
1 parent 73f7a68 commit 3c01b51
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
103 changes: 103 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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);
6 changes: 5 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub const RPMTAG_SIG_BASE: u32 = HEADER_SIGBASE;
num_derive::ToPrimitive,
Debug,
PartialEq,
PartialOrd,
Ord,
Eq,
Copy,
Clone,
Expand Down Expand Up @@ -361,6 +363,8 @@ pub enum IndexTag {
num_derive::ToPrimitive,
Debug,
PartialEq,
PartialOrd,
Ord,
Eq,
Copy,
Clone,
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions src/rpm/headers/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,18 @@ pub(crate) struct IndexEntry<T: Tag> {
pub(crate) num_items: u32,
}

impl<T: Tag> PartialOrd for IndexEntry<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.tag.cmp(&other.tag))
}
}

impl<T: Tag> Ord for IndexEntry<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.tag.cmp(&other.tag)
}
}

impl<T: Tag> IndexEntry<T> {
// 16 bytes
pub(crate) fn parse(input: &[u8]) -> Result<(&[u8], Self), RPMError> {
Expand Down
7 changes: 7 additions & 0 deletions src/rpm/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: AsyncWrite + Unpin>(&self, out: &mut W) -> Result<(), RPMError> {
self.metadata.write_async(out).await?;
Expand Down

0 comments on commit 3c01b51

Please sign in to comment.