Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 14, 2024
2 parents 3112dd0 + b685e38 commit c9c6ff5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions modules/fundamental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ log = { workspace = true }
osv = { workspace = true }
packageurl = { workspace = true }
regex = { workspace = true }
roxmltree = { workspace = true }
semver = { workspace = true }
serde_json = { workspace = true }
serde_yml = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions modules/fundamental/tests/advisory/osv/reingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ async fn withdrawn(ctx: &TrustifyContext) -> anyhow::Result<()> {
.purls(Default::default(), Default::default(), ())
.await?;

println!("PURLs: {purls:#?}");

let purl = purls
.items
.iter()
Expand Down
1 change: 1 addition & 0 deletions modules/fundamental/tests/fundamental.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod advisory;
mod sbom;
mod weakness;
99 changes: 99 additions & 0 deletions modules/fundamental/tests/weakness/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#![allow(clippy::expect_used)]

use roxmltree::Document;
use std::io::Read;
use test_context::test_context;
use test_log::test;
use trustify_common::{hashing::HashingRead, model::Paginated};
use trustify_entity::labels::Labels;
use trustify_module_fundamental::weakness::service::WeaknessService;
use trustify_module_ingestor::{graph::Graph, service::weakness::CweCatalogLoader};
use trustify_test_context::{document_read, TrustifyContext};
use zip::ZipArchive;

#[test_context(TrustifyContext)]
#[test(tokio::test)]
async fn simple(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
const TOTAL_ITEMS_FOUND: u64 = 964;

let graph = Graph::new(ctx.db.clone());
let loader = CweCatalogLoader::new(&graph);
let service = WeaknessService::new(ctx.db.clone());

// extract document from zip file

let zip = document_read("cwec_latest.xml.zip").await?;

let mut archive = ZipArchive::new(zip)?;

let entry = archive.by_index(0)?;

let mut hashing = HashingRead::new(entry);
let mut xml = String::new();
hashing.read_to_string(&mut xml)?;
let digests = hashing.finish()?;

// load

let doc = Document::parse(&xml)?;
loader.load(Labels::default(), &doc, &digests).await?;

// fetch data

let all = service
.list_weaknesses(
Default::default(),
Paginated {
offset: 0,
limit: 10,
},
)
.await?;

assert_eq!(TOTAL_ITEMS_FOUND, all.total);

let w = service
.get_weakness("CWE-1004")
.await?
.expect("must be found");

assert_eq!(w.head.description.as_deref(), Some("The product uses a cookie to store sensitive information, but the cookie is not marked with the HttpOnly flag."));

// now update (poor man's XML update)

let xml = xml.replace("<Description>The product uses a cookie to store sensitive information, but the cookie is not marked with the HttpOnly flag.</Description>", "<Description>Foo Bar Update</Description>");

// load again

let doc = Document::parse(&xml)?;
loader.load(Labels::default(), &doc, &digests).await?;

// fetch data again

let all = service
.list_weaknesses(
Default::default(),
Paginated {
offset: 0,
limit: 10,
},
)
.await?;

// must be the same number of items

assert_eq!(964, all.total);

let w = service
.get_weakness("CWE-1004")
.await?
.expect("must be found");

// but a different description

assert_eq!(w.head.description.as_deref(), Some("Foo Bar Update"));

// done

Ok(())
}

0 comments on commit c9c6ff5

Please sign in to comment.