Skip to content

Commit

Permalink
Enable running tests without default features
Browse files Browse the repository at this point in the history
Currently various features fail to build when enabled without default
features. This is because many tests need feature gating.

Feature gating the import statements quickly turns into spaghetti when
trying to cover all combinations of two features correctly, instead just
allow unused imports on `tests` modules where needed.

Add correct feature requirements to the examples so they also can be run
without default features.

Improve the CI script by adding `std` and `alloc` to the feature matrix.
Add `--no-default-features` to test runs in the CI script.
  • Loading branch information
tcharding committed Feb 1, 2022
1 parent 7ed31c0 commit 25fde65
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 44 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ rand = { version = "0.6", features = ["wasm-bindgen"] }

[[example]]
name = "sign_verify_recovery"
required-features = ["recovery"]
required-features = ["alloc", "recovery"]

[[example]]
name = "sign_verify"
required-features = ["alloc"]

[[example]]
name = "generate_keys"
required-features = ["rand"]
required-features = ["alloc", "rand"]

[workspace]
members = ["secp256k1-sys"]
Expand Down
16 changes: 8 additions & 8 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh -ex

FEATURES="bitcoin_hashes global-context lowmemory rand rand-std recovery serde"
# TODO: Add "alloc" once we bump MSRV to past 1.29
FEATURES="bitcoin_hashes global-context lowmemory rand rand-std recovery serde std"

# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
Expand All @@ -20,17 +21,16 @@ cargo test --all

if [ "$DO_FEATURE_MATRIX" = true ]; then
cargo build --all --no-default-features
#This doesn't work but probably should --andrew
#cargo test --all --no-default-features
cargo test --all --no-default-features

# All features
cargo build --all --no-default-features --features="$FEATURES"
cargo test --all --features="$FEATURES"
cargo test --all --no-default-features --features="$FEATURES"
# Single features
for feature in ${FEATURES}
do
cargo build --all --no-default-features --features="$feature"
cargo test --all --features="$feature"
cargo test --all --no-default-features --features="$feature"
done

# Other combos
Expand All @@ -45,9 +45,9 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
fi

# Examples
cargo run --example sign_verify
cargo run --example sign_verify_recovery --features=recovery
cargo run --example generate_keys --features=rand
cargo run --example sign_verify --features=alloc
cargo run --example sign_verify_recovery --features=recovery,alloc
cargo run --example generate_keys --features=rand,alloc
fi

# Docs
Expand Down
3 changes: 2 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Secp256k1;
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
pub use self::alloc_only::*;

#[cfg(feature = "global-context-less-secure")]
#[cfg(all(feature = "global-context-less-secure", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context-less-secure"))))]
/// Module implementing a singleton pattern for a global `Secp256k1` context
pub mod global {
Expand All @@ -35,6 +35,7 @@ pub mod global {
impl Deref for GlobalContext {
type Target = Secp256k1<All>;

#[allow(unused_mut)] // Unused when "global-context" is not enabled.
fn deref(&self) -> &Self::Target {
static ONCE: Once = Once::new();
static mut CONTEXT: Option<Secp256k1<All>> = None;
Expand Down
10 changes: 8 additions & 2 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl SharedSecret {
/// `SharedSecret` can be easily created via the `From` impl from arrays.
/// # Examples
/// ```
/// # #[cfg(any(feature = "alloc", features = "std"))] {
/// # use secp256k1::ecdh::SharedSecret;
/// # use secp256k1::{Secp256k1, PublicKey, SecretKey};
/// # fn sha2(_a: &[u8], _b: &[u8]) -> [u8; 32] {[0u8; 32]}
Expand All @@ -139,7 +140,7 @@ impl SharedSecret {
/// let hash: [u8; 32] = sha2(&x,&y);
/// hash.into()
/// });
///
/// # }
/// ```
pub fn new_with_hash<F>(point: &PublicKey, scalar: &SecretKey, mut hash_function: F) -> SharedSecret
where F: FnMut([u8; 32], [u8; 32]) -> SharedSecret {
Expand Down Expand Up @@ -170,13 +171,16 @@ impl SharedSecret {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(feature = "alloc", feature = "std"))]
use rand::thread_rng;
use super::super::Secp256k1;
#[cfg(any(feature = "alloc", feature = "std"))]
use Secp256k1;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[test]
#[cfg(any(feature = "alloc", feature = "std"))]
fn ecdh() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
Expand All @@ -190,6 +194,7 @@ mod tests {
}

#[test]
#[cfg(any(feature = "alloc", feature = "std"))]
fn ecdh_with_hash() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
Expand All @@ -203,6 +208,7 @@ mod tests {
}

#[test]
#[cfg(any(feature = "alloc", feature = "std"))]
fn ecdh_with_hash_callback() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
Expand Down
4 changes: 2 additions & 2 deletions src/ecdsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl<C: Verification> Secp256k1<C> {
/// verify-capable context.
///
/// ```rust
/// # #[cfg(feature="rand")] {
/// # #[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))] {
/// # use secp256k1::rand::rngs::OsRng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
Expand Down Expand Up @@ -460,7 +460,7 @@ impl<C: Verification> Secp256k1<C> {
/// verify-capable context.
///
/// ```rust
/// # #[cfg(feature="rand")] {
/// # #[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))] {
/// # use secp256k1::rand::rngs::OsRng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
Expand Down
6 changes: 6 additions & 0 deletions src/ecdsa/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl<C: Verification> Secp256k1<C> {


#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use super::*;
use rand::{RngCore, thread_rng};
Expand All @@ -210,6 +211,7 @@ mod tests {
use wasm_bindgen_test::wasm_bindgen_test as test;

#[test]
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
fn capabilities() {
let sign = Secp256k1::signing_only();
let vrfy = Secp256k1::verification_only();
Expand Down Expand Up @@ -243,6 +245,7 @@ mod tests {

#[test]
#[cfg(not(fuzzing))] // fixed sig vectors can't work with fuzz-sigs
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
fn sign() {
let mut s = Secp256k1::new();
s.randomize(&mut thread_rng());
Expand All @@ -266,6 +269,7 @@ mod tests {
}

#[test]
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
fn sign_and_verify_fail() {
let mut s = Secp256k1::new();
s.randomize(&mut thread_rng());
Expand All @@ -289,6 +293,7 @@ mod tests {
}

#[test]
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
fn sign_with_recovery() {
let mut s = Secp256k1::new();
s.randomize(&mut thread_rng());
Expand All @@ -305,6 +310,7 @@ mod tests {
}

#[test]
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
fn bad_recovery() {
let mut s = Secp256k1::new();
s.randomize(&mut thread_rng());
Expand Down
Loading

0 comments on commit 25fde65

Please sign in to comment.