Skip to content

Commit 62b6ef3

Browse files
committed
Test reloading keys
1 parent 64aba78 commit 62b6ef3

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/test/mod.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{ensure, Context, Result};
44

55
use crate::log;
66

7-
fn run_alg_test(alg: String) -> Result<()> {
7+
fn run_alg_test(alg: &str, extra: Option<&str>) -> Result<()> {
88
log::LEVEL.store(log::LEVEL_DEBUG, Ordering::Relaxed);
99

1010
let work_dir = tempfile::tempdir().context("Failed to create tempdir")?;
@@ -30,7 +30,20 @@ fn run_alg_test(alg: String) -> Result<()> {
3030
path.push("jwks.json");
3131
path
3232
};
33+
let key_index_path = {
34+
let mut path = state_dir.clone();
35+
path.push("example.com");
36+
path.push("keys");
37+
path.push(alg);
38+
path.push("index.json");
39+
path
40+
};
3341

42+
let key_config = if let Some(extra) = extra {
43+
format!("{{ alg: {alg}, {extra} }}")
44+
} else {
45+
format!("{{ alg: {alg} }}")
46+
};
3447
fs::write(
3548
&config_path,
3649
format!(
@@ -39,7 +52,7 @@ state_dir: {state_dir:?}
3952
providers:
4053
- issuer: 'https://example.com'
4154
keys:
42-
- {alg}
55+
- {key_config}
4356
tokens:
4457
- path: {token_path:?}
4558
claims:
@@ -49,76 +62,94 @@ providers:
4962
)
5063
.context("Failed to write config")?;
5164

65+
// This is equivalent to a single update (`--once`).
5266
let mut next_keys_check = None;
5367
let mut next_tokens_check = None;
5468
let cfg = crate::read_config(&config_path)?;
5569
crate::init_state(cfg, &mut None, &mut next_keys_check, &mut next_tokens_check)?;
5670

71+
// Tests against a foreign implementation.
5772
let status = std::process::Command::new("./src/test/helper/main.js")
5873
.arg(&token_path)
5974
.arg(&jwks_path)
6075
.status()?;
6176
ensure!(status.success(), "Verification helper failed");
6277

78+
// Perform another update to test reloading, and ensure keys were not changed.
79+
let key_index_before =
80+
fs::read(&key_index_path).context("Failed to read key index (before check)")?;
81+
82+
let mut next_keys_check = None;
83+
let mut next_tokens_check = None;
84+
let cfg = crate::read_config(&config_path)?;
85+
crate::init_state(cfg, &mut None, &mut next_keys_check, &mut next_tokens_check)?;
86+
87+
let key_index_after =
88+
fs::read(&key_index_path).context("Failed to read key index (before check)")?;
89+
ensure!(
90+
key_index_before == key_index_after,
91+
"Keys unexpectedly changed after reload"
92+
);
93+
6394
Ok(())
6495
}
6596

6697
#[cfg(feature = "rsa")]
6798
#[test]
6899
fn rs256() -> Result<()> {
69-
run_alg_test("{ alg: RS256 }".into())
100+
run_alg_test("RS256", None)
70101
}
71102

72103
#[cfg(feature = "rsa")]
73104
#[test]
74105
fn rs384() -> Result<()> {
75-
run_alg_test("{ alg: RS384 }".into())
106+
run_alg_test("RS384", None)
76107
}
77108

78109
#[cfg(feature = "rsa")]
79110
#[test]
80111
fn rs512() -> Result<()> {
81-
run_alg_test("{ alg: RS512 }".into())
112+
run_alg_test("RS512", None)
82113
}
83114

84115
#[cfg(feature = "rsa")]
85116
#[test]
86117
fn ps256() -> Result<()> {
87-
run_alg_test("{ alg: PS256 }".into())
118+
run_alg_test("PS256", None)
88119
}
89120

90121
#[cfg(feature = "rsa")]
91122
#[test]
92123
fn ps384() -> Result<()> {
93-
run_alg_test("{ alg: PS384 }".into())
124+
run_alg_test("PS384", None)
94125
}
95126

96127
#[cfg(feature = "rsa")]
97128
#[test]
98129
fn ps512() -> Result<()> {
99-
run_alg_test("{ alg: PS512 }".into())
130+
run_alg_test("PS512", None)
100131
}
101132

102133
#[cfg(any(feature = "ring", feature = "rustcrypto"))]
103134
#[test]
104135
fn es256() -> Result<()> {
105-
run_alg_test("{ alg: ES256 }".into())
136+
run_alg_test("ES256", None)
106137
}
107138

108139
#[cfg(any(feature = "ring", feature = "rustcrypto"))]
109140
#[test]
110141
fn es384() -> Result<()> {
111-
run_alg_test("{ alg: ES384 }".into())
142+
run_alg_test("ES384", None)
112143
}
113144

114145
#[cfg(feature = "rustcrypto")]
115146
#[test]
116147
fn es256k() -> Result<()> {
117-
run_alg_test("{ alg: ES256K }".into())
148+
run_alg_test("ES256K", None)
118149
}
119150

120151
#[cfg(any(feature = "ring", feature = "rustcrypto"))]
121152
#[test]
122153
fn ed25519() -> Result<()> {
123-
run_alg_test("{ alg: EdDSA, crv: Ed25519 }".into())
154+
run_alg_test("EdDSA", Some("crv: Ed25519"))
124155
}

0 commit comments

Comments
 (0)