Skip to content

Commit

Permalink
Fix #[cfg] to build and test without default features
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Nov 26, 2024
1 parent e727140 commit 54e0a60
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 50 deletions.
6 changes: 3 additions & 3 deletions internal/crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub(crate) mod internal;

pub mod ocsp;

#[cfg(all(feature = "openssl", not(target_arch = "wasm32")))]
pub mod openssl;

#[cfg(all(feature = "openssl", target_arch = "wasm32"))]
compile_error!("OpenSSL feature is not compatible with WASM platform");

#[cfg(feature = "openssl")]
pub mod openssl;

pub mod raw_signature;

mod signing_alg;
Expand Down
1 change: 1 addition & 0 deletions internal/crypto/src/raw_signature/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn validator_for_signing_alg(alg: SigningAlg) -> Option<Box<dyn RawSignature
return Some(validator);
}

let _ = alg; // mark as used if none are enabled
None
}

Expand Down
1 change: 1 addition & 0 deletions internal/crypto/src/tests/raw_signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
// specific language governing permissions and limitations under
// each license.

#[cfg(any(target_arch = "wasm32", feature = "openssl"))]
mod validator;
4 changes: 2 additions & 2 deletions sdk/examples/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use anyhow::Result;
// This example is not designed to work with a wasm build
// so we provide this shell to avoid testing errors

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
mod client;
fn main() -> Result<()> {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
client::main()?;
Ok(())
}
17 changes: 10 additions & 7 deletions sdk/examples/data_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@ use std::{
path::{Path, PathBuf},
};

#[cfg(feature = "openssl_sign")]
use c2pa::create_signer;

#[cfg(not(target_arch = "wasm32"))]
use c2pa::{
assertions::{
c2pa_action, labels::*, Action, Actions, CreativeWork, DataHash, Exif, SchemaDotOrgPerson,
},
create_signer, hash_stream_by_alg, Builder, ClaimGeneratorInfo, HashRange, Ingredient, Reader,
Relationship, Result,
hash_stream_by_alg, Builder, ClaimGeneratorInfo, HashRange, Ingredient, Reader, Relationship,
Result,
};
#[cfg(not(target_arch = "wasm32"))]
use c2pa_crypto::SigningAlg;

fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
println!("DataHash demo");

#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
user_data_hash_with_sdk_hashing()?;
println!("Done with SDK hashing1");
#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
user_data_hash_with_user_hashing()?;
println!("Done with SDK hashing2");
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "file_io")]
fn builder_from_source<S: AsRef<Path>>(source: S) -> Result<Builder> {
let mut parent = Ingredient::from_file(source.as_ref())?;
parent.set_relationship(Relationship::ParentOf);
Expand Down Expand Up @@ -86,7 +89,7 @@ fn builder_from_source<S: AsRef<Path>>(source: S) -> Result<Builder> {
Ok(builder)
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
fn user_data_hash_with_sdk_hashing() -> Result<()> {
// You will often implement your own Signer trait to perform on device signing
let signcert_path = "sdk/tests/fixtures/certs/es256.pub";
Expand Down Expand Up @@ -146,7 +149,7 @@ fn user_data_hash_with_sdk_hashing() -> Result<()> {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
fn user_data_hash_with_user_hashing() -> Result<()> {
// You will often implement your own Signer trait to perform on device signing
let signcert_path = "sdk/tests/fixtures/certs/es256.pub";
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use anyhow::Result;
use c2pa::Reader;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
fn main() -> Result<()> {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
Expand Down
1 change: 1 addition & 0 deletions sdk/examples/v2api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ mod tests {

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl")), ignore)]
async fn test_v2_api() -> Result<()> {
main()
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/v2show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> Result<()> {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
fn main() -> Result<()> {
use std::io::Read;

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/assertions/bmff_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ pub mod tests {
//use super::*;
use crate::utils::test::fixture_path;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
#[test]
fn test_fragemented_mp4() {
use crate::{
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/asset_handlers/bmff_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1853,8 +1853,7 @@ pub mod tests {
use super::*;
use crate::utils::test::{fixture_path, temp_dir_path};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "file_io")]
#[cfg(all(feature = "openssl", feature = "file_io"))]
#[test]
fn test_read_mp4() {
use c2pa_status_tracker::DetailedStatusTracker;
Expand Down
8 changes: 6 additions & 2 deletions sdk/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ mod tests {
}

#[test]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
fn test_builder_sign() {
#[derive(Serialize, Deserialize)]
struct TestAssertion {
Expand Down Expand Up @@ -1421,6 +1422,7 @@ mod tests {

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
async fn test_builder_remote_sign() {
let format = "image/jpeg";
let mut source = Cursor::new(TEST_IMAGE);
Expand Down Expand Up @@ -1458,7 +1460,7 @@ mod tests {
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl_sign")]
fn test_builder_remote_url() {
let mut source = Cursor::new(TEST_IMAGE_CLEAN);
let mut dest = Cursor::new(Vec::new());
Expand Down Expand Up @@ -1492,6 +1494,7 @@ mod tests {
}

#[test]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
fn test_builder_data_hashed_embeddable() {
const CLOUD_IMAGE: &[u8] = include_bytes!("../tests/fixtures/cloud.jpg");
let mut input_stream = Cursor::new(CLOUD_IMAGE);
Expand Down Expand Up @@ -1552,6 +1555,7 @@ mod tests {

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg(any(target_arch = "wasm32", all(feature = "openssl_sign", feature = "file_io")))]
async fn test_builder_box_hashed_embeddable() {
use crate::asset_io::{CAIWriter, HashBlockObjectType};
const BOX_HASH_IMAGE: &[u8] = include_bytes!("../tests/fixtures/boxhash.jpg");
Expand Down Expand Up @@ -1655,7 +1659,7 @@ mod tests {
);
}

#[cfg(feature = "file_io")]
#[cfg(feature = "openssl_sign")]
const MANIFEST_JSON: &str = r#"{
"claim_generator": "test",
"claim_generator_info": [
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ impl Claim {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
#[cfg(test)]
pub mod tests {
#![allow(clippy::expect_used)]
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/cose_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ mod tests {
use crate::{claim::Claim, utils::test::temp_signer};

#[test]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
fn test_sign_claim() {
let mut claim = Claim::new("extern_sign_test", Some("contentauth"));
claim.build().unwrap();
Expand All @@ -376,8 +377,7 @@ mod tests {
assert_eq!(cose_sign1.len(), box_size);
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "openssl")]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
#[actix::test]
async fn test_sign_claim_async() {
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/cose_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ fn check_trust(
verify_trust(th, chain_der, cert_der, signing_time_epoc)
}

#[cfg(all(not(feature = "openssl"), not(target_arch = "wasm32")))]
#[cfg(not(any(feature = "openssl", target_arch = "wasm32")))]
{
Err(Error::NotImplemented(
"no trust handler for this feature".to_string(),
Expand Down Expand Up @@ -1380,7 +1380,7 @@ pub mod tests {
}

#[test]
#[cfg(feature = "openssl_sign")]
#[cfg(all(feature = "openssl_sign", feature = "file_io"))]
fn test_cert_algorithms() {
let cert_dir = crate::utils::test::fixture_path("certs");
let th = crate::openssl::OpenSSLTrustHandlerConfig::new();
Expand Down
27 changes: 18 additions & 9 deletions sdk/src/ingredient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,7 @@ mod tests {

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl")), ignore)]
async fn test_stream_async_jpg() {
let image_bytes = include_bytes!("../tests/fixtures/CA.jpg");
let title = "Test Image";
Expand All @@ -1536,12 +1537,18 @@ mod tests {
&"ingredient_from_memory_async:".into(),
&ingredient.to_string().into(),
);
assert!(ingredient.validation_status().is_none());
assert_eq!(None, ingredient.validation_status());
}

#[cfg_attr(not(target_arch = "wasm32"), test)]
#[test]
#[cfg_attr(
any(
target_arch = "wasm32",
not(feature = "openssl")
),
ignore
)]
// Note this does not work from wasm32, due to validation issues
#[cfg(not(target_arch = "wasm32"))]
fn test_stream_jpg() {
let image_bytes = include_bytes!("../tests/fixtures/CA.jpg");
let title = "Test Image";
Expand All @@ -1554,11 +1561,12 @@ mod tests {
assert_eq!(ingredient.format(), format);
assert!(ingredient.manifest_data().is_some());
assert!(ingredient.metadata().is_none());
assert!(ingredient.validation_status().is_none());
assert_eq!(None, ingredient.validation_status());
}

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl")), ignore)]
async fn test_stream_ogp() {
let image_bytes = include_bytes!("../tests/fixtures/XCA.jpg");
let title = "XCA.jpg";
Expand All @@ -1583,8 +1591,8 @@ mod tests {
}

#[allow(dead_code)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "file_io")), actix::test)]
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(not(all(feature = "openssl", feature = "fetch_remote_manifests")), ignore)]
async fn test_jpg_cloud_from_memory() {
let image_bytes = include_bytes!("../tests/fixtures/cloud.jpg");
let format = "image/jpeg";
Expand All @@ -1597,7 +1605,7 @@ mod tests {
assert!(ingredient.provenance().is_some());
assert!(ingredient.provenance().unwrap().starts_with("https:"));
assert!(ingredient.manifest_data().is_some());
assert!(ingredient.validation_status().is_none());
assert_eq!(None, ingredient.validation_status());
}

#[allow(dead_code)]
Expand All @@ -1624,6 +1632,7 @@ mod tests {

#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl")), ignore)]
async fn test_jpg_cloud_from_memory_and_manifest() {
let asset_bytes = include_bytes!("../tests/fixtures/cloud.jpg");
let manifest_bytes = include_bytes!("../tests/fixtures/cloud_manifest.c2pa");
Expand All @@ -1640,7 +1649,7 @@ mod tests {
&"ingredient_from_memory_async:".into(),
&ingredient.to_string().into(),
);
assert!(ingredient.validation_status().is_none());
assert_eq!(None, ingredient.validation_status());
assert!(ingredient.manifest_data().is_some());
assert!(ingredient.provenance().is_some());
}
Expand Down Expand Up @@ -1838,7 +1847,7 @@ mod tests_file_io {
let ap = fixture_path("CIE-sig-CA.jpg");
let ingredient = Ingredient::from_file(ap).expect("from_file");
// println!("ingredient = {ingredient}");
assert!(ingredient.validation_status().is_none());
assert_eq!(None, ingredient.validation_status());
assert!(ingredient.manifest_data().is_some());
}

Expand Down
8 changes: 6 additions & 2 deletions sdk/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,7 @@ pub(crate) mod tests {
#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[allow(deprecated)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
async fn test_embed_jpeg_stream_wasm() {
use crate::assertions::User;
let image = include_bytes!("../tests/fixtures/earth_apollo17.jpg");
Expand Down Expand Up @@ -2023,6 +2024,7 @@ pub(crate) mod tests {
#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[allow(deprecated)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
async fn test_embed_png_stream_wasm() {
use crate::assertions::User;
let image = include_bytes!("../tests/fixtures/libpng-test.png");
Expand Down Expand Up @@ -2056,6 +2058,7 @@ pub(crate) mod tests {
#[cfg_attr(not(target_arch = "wasm32"), actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[allow(deprecated)]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
async fn test_embed_webp_stream_wasm() {
use crate::assertions::User;
let image = include_bytes!("../tests/fixtures/mars.webp");
Expand Down Expand Up @@ -2087,6 +2090,7 @@ pub(crate) mod tests {
}

#[test]
#[cfg_attr(not(any(target_arch = "wasm32", feature = "openssl_sign")), ignore)]
fn test_embed_stream() {
use crate::assertions::User;
let image = include_bytes!("../tests/fixtures/earth_apollo17.jpg");
Expand Down Expand Up @@ -2122,9 +2126,9 @@ pub(crate) mod tests {
//println!("{manifest_store}");main
}

#[cfg(any(target_arch = "wasm32", feature = "openssl_sign"))]
#[cfg_attr(feature = "openssl_sign", actix::test)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg(any(target_arch = "wasm32", all(feature = "openssl_sign", feature = "file_io")))]
async fn test_embed_from_memory_async() {
use crate::{assertions::User, utils::test::temp_async_signer};
let image = include_bytes!("../tests/fixtures/earth_apollo17.jpg");
Expand Down Expand Up @@ -2252,7 +2256,7 @@ pub(crate) mod tests {
assert_eq!(image.into_owned(), thumb_data);
}

#[cfg(feature = "file_io")]
#[cfg(feature = "openssl_sign")]
const MANIFEST_JSON: &str = r#"{
"claim_generator": "test",
"claim_generator_info": [
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/manifest_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,10 @@ impl std::fmt::Display for ManifestStore {
}
}

#[cfg(test)]
#[cfg(all(
test,
any(target_arch = "wasm32", feature = "openssl")
))]
mod tests {
#![allow(clippy::expect_used)]
#![allow(clippy::unwrap_used)]
Expand Down
Loading

0 comments on commit 54e0a60

Please sign in to comment.