Skip to content

Commit 9b5c509

Browse files
committed
Merge #386: Enable test with --no-default-features
c30026d Fix typo 'epected' -> 'expected' (Tobin Harding) f3688ec Use rand-std in key rustdoc examples (Tobin Harding) ae3e06f Fix lint warnings in test code (Tobin Harding) c01cd8f Enable running tests without default features (Tobin Harding) a79840e Be explicit about example feature requirements (Tobin Harding) 433c350 Add multiple implementations of Debug for secrets (Tobin Harding) 632ecc4 Use fully qualified path for mem (Tobin Harding) Pull request description: As indicated by the comment in `contrib/test.sh` we should be able to test with `--no-default-features`. - Patch 1 uses fully qualified path to remove a build warning. - Patch 2 adds additional `Debug` implementations for secrets, uses `bitcoin_hashes` if available, please review carefully. - Patch 3 adds `std` as an explicit requirement for the three examples - Patch 4 enables `cargo test --no-default-features, fixes all the feature gating in unit tests. - Patch 5 fixes lint warnings generated while running the feature matrix in `contrib/test.sh`. **Please Note**: Currently the `alloc` feature cannot be built with Rust 1.29, this made it into master because we don't build ever with the `alloc` feature enabled in CI. This PR _should_ add `alloc` to the features matrix but it does not. Adds a TODO comment to `contrib/test.sh` to add it once we bump MSRV. ACKs for top commit: apoelstra: ACK c30026d Tree-SHA512: 3bbdda332ab1e04eaa3479d9e9c7463a54347f56019ce5366bb36eb8d5ccaced32539e2c58454a7714d76b7bab9f1ab56accb04de67c826165dd104ac0b3b893
2 parents f7d637e + c30026d commit 9b5c509

File tree

11 files changed

+168
-61
lines changed

11 files changed

+168
-61
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ rand = { version = "0.6", features = ["wasm-bindgen"] }
4949

5050
[[example]]
5151
name = "sign_verify_recovery"
52-
required-features = ["recovery"]
52+
required-features = ["std", "recovery"]
5353

5454
[[example]]
5555
name = "sign_verify"
56+
required-features = ["std"]
5657

5758
[[example]]
5859
name = "generate_keys"
59-
required-features = ["rand"]
60+
required-features = ["std", "rand-std"]
6061

6162
[workspace]
6263
members = ["secp256k1-sys"]

contrib/test.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh -ex
22

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

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

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

2626
# All features
2727
cargo build --all --no-default-features --features="$FEATURES"
28-
cargo test --all --features="$FEATURES"
28+
cargo test --all --no-default-features --features="$FEATURES"
2929
# Single features
3030
for feature in ${FEATURES}
3131
do
3232
cargo build --all --no-default-features --features="$feature"
33-
cargo test --all --features="$feature"
33+
cargo test --all --no-default-features --features="$feature"
3434
done
3535

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

4747
# Examples
48-
cargo run --example sign_verify
49-
cargo run --example sign_verify_recovery --features=recovery
50-
cargo run --example generate_keys --features=rand
48+
cargo run --example sign_verify --features=std
49+
cargo run --example sign_verify_recovery --features=std,recovery
50+
cargo run --example generate_keys --features=std,rand-std
5151
fi
5252

5353
# Docs

secp256k1-sys/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(non_camel_case_types)]
2-
use core::{fmt, mem};
2+
use core::fmt;
33

44
pub type c_int = i32;
55
pub type c_uchar = u8;
@@ -46,7 +46,7 @@ impl AlignedType {
4646
}
4747

4848
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]
49-
pub(crate) const ALIGN_TO: usize = mem::align_of::<AlignedType>();
49+
pub(crate) const ALIGN_TO: usize = ::core::mem::align_of::<AlignedType>();
5050

5151
#[cfg(test)]
5252
mod tests {

src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Secp256k1;
99
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
1010
pub use self::alloc_only::*;
1111

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

38+
#[allow(unused_mut)] // Unused when "global-context" is not enabled.
3839
fn deref(&self) -> &Self::Target {
3940
static ONCE: Once = Once::new();
4041
static mut CONTEXT: Option<Secp256k1<All>> = None;

src/ecdh.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl SharedSecret {
127127
/// `SharedSecret` can be easily created via the `From` impl from arrays.
128128
/// # Examples
129129
/// ```
130+
/// # #[cfg(any(feature = "alloc", features = "std"))] {
130131
/// # use secp256k1::ecdh::SharedSecret;
131132
/// # use secp256k1::{Secp256k1, PublicKey, SecretKey};
132133
/// # fn sha2(_a: &[u8], _b: &[u8]) -> [u8; 32] {[0u8; 32]}
@@ -139,7 +140,7 @@ impl SharedSecret {
139140
/// let hash: [u8; 32] = sha2(&x,&y);
140141
/// hash.into()
141142
/// });
142-
///
143+
/// # }
143144
/// ```
144145
pub fn new_with_hash<F>(point: &PublicKey, scalar: &SecretKey, mut hash_function: F) -> SharedSecret
145146
where F: FnMut([u8; 32], [u8; 32]) -> SharedSecret {
@@ -168,6 +169,7 @@ impl SharedSecret {
168169
}
169170

170171
#[cfg(test)]
172+
#[allow(unused_imports)]
171173
mod tests {
172174
use super::*;
173175
use rand::thread_rng;
@@ -177,6 +179,7 @@ mod tests {
177179
use wasm_bindgen_test::wasm_bindgen_test as test;
178180

179181
#[test]
182+
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
180183
fn ecdh() {
181184
let s = Secp256k1::signing_only();
182185
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
@@ -190,6 +193,7 @@ mod tests {
190193
}
191194

192195
#[test]
196+
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
193197
fn ecdh_with_hash() {
194198
let s = Secp256k1::signing_only();
195199
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
@@ -203,6 +207,7 @@ mod tests {
203207
}
204208

205209
#[test]
210+
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
206211
fn ecdh_with_hash_callback() {
207212
let s = Secp256k1::signing_only();
208213
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());

src/ecdsa/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl<C: Verification> Secp256k1<C> {
431431
/// verify-capable context.
432432
///
433433
/// ```rust
434-
/// # #[cfg(feature="rand")] {
434+
/// # #[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))] {
435435
/// # use secp256k1::rand::rngs::OsRng;
436436
/// # use secp256k1::{Secp256k1, Message, Error};
437437
/// #
@@ -460,7 +460,7 @@ impl<C: Verification> Secp256k1<C> {
460460
/// verify-capable context.
461461
///
462462
/// ```rust
463-
/// # #[cfg(feature="rand")] {
463+
/// # #[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))] {
464464
/// # use secp256k1::rand::rngs::OsRng;
465465
/// # use secp256k1::{Secp256k1, Message, Error};
466466
/// #

src/ecdsa/recovery.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ impl<C: Verification> Secp256k1<C> {
201201

202202

203203
#[cfg(test)]
204+
#[allow(unused_imports)]
204205
mod tests {
205206
use super::*;
206207
use rand::{RngCore, thread_rng};
@@ -210,6 +211,7 @@ mod tests {
210211
use wasm_bindgen_test::wasm_bindgen_test as test;
211212

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

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

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

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

307312
#[test]
313+
#[cfg(all(feature="rand-std", any(feature = "alloc", feature = "std")))]
308314
fn bad_recovery() {
309315
let mut s = Secp256k1::new();
310316
s.randomize(&mut thread_rng());

0 commit comments

Comments
 (0)