Skip to content

Commit 79ec06e

Browse files
committed
Merge branch 'chore-remove-deny-warnings' of github.com:mozilla/nss-rs into chore-remove-deny-warnings
2 parents 3e7e6bb + ccc8f39 commit 79ec06e

7 files changed

Lines changed: 39 additions & 27 deletions

File tree

.github/actions/check-android/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ runs:
8989
adb push "$test" "$TMP/"
9090
adb shell chmod +x "$TMP/$(basename "$test")"
9191
# See https://unix.stackexchange.com/a/451140/409256
92-
adb shell "CARGO_TERM_COLOR=always RUST_BACKTRACE=1 LD_LIBRARY_PATH=$TMP/lib NSS_DB_PATH=$TMP/db API_LEVEL=$API_LEVEL $TMP/$(basename "$test") || echo _FAIL_" 2>&1 | tee output
92+
adb shell "CARGO_TERM_COLOR=always RUST_BACKTRACE=1 LD_LIBRARY_PATH=$TMP/lib TEST_FIXTURE_DB=$TMP/db API_LEVEL=$API_LEVEL $TMP/$(basename "$test") || echo _FAIL_" 2>&1 | tee output
9393
grep _FAIL_ output > /dev/null && any_failures=1
9494
done
9595
exit $any_failures

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nss-rs"
3-
version = "0.10.0"
3+
version = "0.10.2"
44
authors = ["Martin Thomson <mt@lowentropy.net>", "Andy Leiserson <aleiserson@mozilla.com>", "John M. Schanck <jschanck@mozilla.com>", "Benjamin Beurdouche <beurdouche@mozilla.com>", "Anna Weine <anna.weine@mozilla.com>"]
55
categories = ["network-programming", "web-programming"]
66
keywords = ["nss", "crypto", "mozilla", "firefox"]

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ mod ssl;
3838
pub mod time;
3939

4040
use std::{
41-
env,
4241
ffi::CString,
4342
path::{Path, PathBuf},
4443
ptr::null,
@@ -189,15 +188,15 @@ pub fn init() -> Res<()> {
189188
res.as_ref().map(|_| ()).map_err(Clone::clone)
190189
}
191190

191+
/// The path to the default NSS database used for testing.
192+
pub const TEST_FIXTURE_DB: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/test-fixture/db");
193+
192194
/// Initialize with a database.
193195
///
194196
/// # Errors
195197
///
196198
/// If NSS cannot be initialized.
197199
pub fn init_db<P: Into<PathBuf>>(dir: P) -> Res<()> {
198-
// Allow overriding the NSS database path with an environment variable.
199-
let dir =
200-
env::var("NSS_DB_PATH").unwrap_or(dir.into().to_str().ok_or(Error::Internal)?.to_string());
201200
let res = INITIALIZED.get_or_init(|| init_once(Some(dir.into())));
202201
res.as_ref().map(|_| ()).map_err(Clone::clone)
203202
}

src/pbkdf2.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ pub fn pbkdf2(
7373

7474
#[cfg(test)]
7575
mod tests {
76+
use test_fixture::fixture_init;
77+
7678
use super::*;
7779

7880
#[test]
7981
fn rfc_7914_vector_1() {
82+
fixture_init();
8083
// RFC 7914 §11 provides PBKDF2-HMAC-SHA256 vectors. Using a common one:
8184
// password="password", salt="salt", iter=1, dkLen=32.
8285
let dk = pbkdf2(&HmacAlgorithm::HMAC_SHA2_256, b"password", b"salt", 1, 32).unwrap();
@@ -90,6 +93,7 @@ mod tests {
9093

9194
#[test]
9295
fn rfc_7914_vector_iter_2() {
96+
fixture_init();
9397
let dk = pbkdf2(&HmacAlgorithm::HMAC_SHA2_256, b"password", b"salt", 2, 32).unwrap();
9498
let expected = [
9599
0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0,
@@ -101,6 +105,7 @@ mod tests {
101105

102106
#[test]
103107
fn pbkdf2_sha384_vector() {
108+
fixture_init();
104109
let dk = pbkdf2(&HmacAlgorithm::HMAC_SHA2_384, b"password", b"salt", 1, 20).unwrap();
105110
let expected = [
106111
0xc0, 0xe1, 0x4f, 0x06, 0xe4, 0x9e, 0x32, 0xd7, 0x3f, 0x9f, 0x52, 0xdd, 0xf1, 0xd0,
@@ -111,6 +116,7 @@ mod tests {
111116

112117
#[test]
113118
fn pbkdf2_sha512_vector() {
119+
fixture_init();
114120
let dk = pbkdf2(&HmacAlgorithm::HMAC_SHA2_512, b"password", b"salt", 1, 20).unwrap();
115121
let expected = [
116122
0x86, 0x7f, 0x70, 0xcf, 0x1a, 0xde, 0x02, 0xcf, 0xf3, 0x75, 0x25, 0x99, 0xa3, 0xa5,
@@ -121,6 +127,7 @@ mod tests {
121127

122128
#[test]
123129
fn deterministic_across_calls() {
130+
fixture_init();
124131
let a = pbkdf2(
125132
&HmacAlgorithm::HMAC_SHA2_256,
126133
b"hello",
@@ -142,6 +149,7 @@ mod tests {
142149

143150
#[test]
144151
fn different_salt_different_key() {
152+
fixture_init();
145153
let a = pbkdf2(
146154
&HmacAlgorithm::HMAC_SHA2_256,
147155
b"hello",

test-fixture/src/lib.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@
88

99
use std::{
1010
cell::OnceCell,
11+
path::PathBuf,
12+
sync::Once,
1113
time::{Duration, Instant},
1214
};
1315

14-
use nss_rs::{init_db, AntiReplay};
16+
use nss_rs::{init_db, AntiReplay, TEST_FIXTURE_DB};
1517

16-
/// The path for the database used in tests.
18+
/// Returns the path to the NSS test fixture database.
1719
///
18-
/// Initialized via the `NSS_DB_PATH` environment variable. If that is not set,
19-
/// it defaults to the `db` directory in the current crate. If the environment
20-
/// variable is set to `$ARGV0`, it will be initialized to the directory of the
21-
/// current executable.
22-
pub const NSS_DB_PATH: &str = if let Some(dir) = option_env!("NSS_DB_PATH") {
23-
dir
24-
} else {
25-
concat!(env!("CARGO_MANIFEST_DIR"), "/db")
26-
};
20+
/// Reads the `TEST_FIXTURE_DB` environment variable if set, falling back to
21+
/// [`TEST_FIXTURE_DB`]. If the value is `$ARGV0`, returns the directory of
22+
/// the current executable instead.
23+
#[must_use]
24+
pub fn db_path() -> PathBuf {
25+
match std::env::var("TEST_FIXTURE_DB").as_deref() {
26+
Ok("$ARGV0") => {
27+
let mut exe = std::env::current_exe().unwrap();
28+
exe.pop();
29+
exe
30+
}
31+
Ok(path) => PathBuf::from(path),
32+
Err(_) => PathBuf::from(TEST_FIXTURE_DB),
33+
}
34+
}
35+
36+
static FIXTURE_INIT: Once = Once::new();
2737

2838
/// Initialize the test fixture. Only call this if you aren't also calling a
2939
/// fixture function that depends on setup. Other functions in the fixture
@@ -34,14 +44,9 @@ pub const NSS_DB_PATH: &str = if let Some(dir) = option_env!("NSS_DB_PATH") {
3444
/// When the NSS initialization fails.
3545
#[allow(dead_code)]
3646
pub fn fixture_init() {
37-
if NSS_DB_PATH == "$ARGV0" {
38-
let mut current_exe = std::env::current_exe().unwrap();
39-
current_exe.pop();
40-
let nss_db_path = current_exe.to_str().unwrap();
41-
init_db(nss_db_path).unwrap();
42-
} else {
43-
init_db(NSS_DB_PATH).unwrap();
44-
}
47+
FIXTURE_INIT.call_once(|| {
48+
init_db(db_path()).unwrap();
49+
});
4550
}
4651

4752
// This needs to be > 2ms to avoid it being rounded to zero.

tests/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn init_nodb() {
4242
#[cfg(not(nss_nodb))]
4343
#[test]
4444
fn init_withdb() {
45-
init_db(::test_fixture::NSS_DB_PATH).unwrap();
45+
init_db(::test_fixture::db_path()).unwrap();
4646
assert_initialized();
4747
unsafe {
4848
assert_ne!(nss_init::NSS_IsInitialized(), 0);

0 commit comments

Comments
 (0)