mod memory_fixture;
use super::REMAINING;
use crate::{
receiver_memory::{
AdmissionHost, EnrollmentBinding, EnrollmentMetadata, MemoryConfig, MemoryError as E,
MemoryReceiver,
},
receiver_policy::{
LoginLimits, LogoutError, SessionAuthority, SessionCapacity, UnknownLogoutLimits,
},
session_request::{SessionRequest, SessionTtlPolicy},
verification::{
AuthorityGrants, Ed25519Key, VerificationContext, VerificationCosts, VerificationProfile,
},
};
use ic_auth_protocol_types::{
AudienceId, AuthRole, CanonicalId, Credential, CredentialProof, DelegatedRoleGrant,
PortableMechanism, Principal,
};
use memory_fixture::*;
use std::panic::{AssertUnwindSafe, catch_unwind};
struct Armed;
impl Armed {
fn after(steps: usize) -> Self {
REMAINING.with(|remaining| {
assert!(remaining.replace(Some(steps)).is_none());
});
Self
}
}
impl Drop for Armed {
fn drop(&mut self) {
REMAINING.with(|remaining| remaining.set(None));
}
}
fn exercise(
name: &str,
build: impl Fn(&Host) -> MemoryReceiver,
operation: impl Fn(&Host, &mut MemoryReceiver),
) -> usize {
let host = Host::new();
for stage in 0..128 {
let mut receiver = build(&host);
let result = {
let _armed = Armed::after(stage);
catch_unwind(AssertUnwindSafe(|| operation(&host, &mut receiver)))
};
if let Err(payload) = result {
assert_eq!(
payload.downcast_ref::<&str>(),
Some(&"auth-publication-fault"),
"unexpected panic in {name} at stage {stage}"
);
let context = host.context("initial", 125);
assert_eq!(
receiver.admit(&credential("initial"), request(), host.admission(&context)),
Err(E::Closed)
);
assert_eq!(
receiver.read(&host.read("initial", 125), &[]),
Err(E::Closed)
);
assert_eq!(
receiver.authorize(&host.read("initial", 125), &[], |_| {
panic!("projection invoked on poisoned receiver")
}),
Err(E::Closed)
);
assert_eq!(
receiver.logout(context.caller, 125),
Err(LogoutError::Closed)
);
assert_eq!(receiver.prune(125, 128), Err(E::Closed));
assert_eq!(receiver.advance_generation(125), Err(E::Closed));
assert_eq!(
receiver.request_enrollment(125, [9; 32], 135),
Err(E::Closed)
);
assert_eq!(
receiver.install_enrollment(
125,
host.config().enrollment.authority,
host.snapshot(9)
),
Err(E::Closed)
);
assert!(matches!(
receiver.inspect_sessions(None, 32, 128, 125),
Err(E::Closed)
));
} else {
assert!(stage > 0, "{name} exercised no publication boundary");
assert!(receiver.inspect_sessions(None, 32, 128, 125).is_ok());
return stage;
}
}
panic!("unbounded publication trace: {name}")
}
fn admit(host: &Host, receiver: &mut MemoryReceiver, name: &str, now: u64) {
let context = host.context(name, now);
receiver
.admit(&credential(name), request(), host.admission(&context))
.unwrap();
}
fn populated(host: &Host) -> MemoryReceiver {
let mut receiver = host.receiver();
admit(host, &mut receiver, "initial", 25);
receiver
}
#[test]
fn publication_faults_close_every_admission_and_logout_path() {
let mut stages = 0;
stages += exercise("fresh admission", Host::receiver, |h, r| {
admit(h, r, "initial", 25)
});
stages += exercise("renewal", populated, |h, r| admit(h, r, "renewal", 25));
stages += exercise("replacement", populated, |h, r| {
admit(h, r, "replacement", 25)
});
stages += exercise(
"replacement after logout",
|h| {
let mut r = populated(h);
r.logout(h.context("initial", 25).caller, 25).unwrap();
r
},
|h, r| admit(h, r, "replacement", 25),
);
stages += exercise(
"unknown-to-known transfer",
|h| {
let mut r = h.receiver();
r.logout(h.context("future", 25).caller, 25).unwrap();
r
},
|h, r| admit(h, r, "future", 125),
);
stages += exercise("known logout", populated, |h, r| {
r.logout(h.context("initial", 25).caller, 25).unwrap();
});
stages += exercise("unknown logout", Host::receiver, |h, r| {
r.logout(h.context("initial", 25).caller, 25).unwrap();
});
stages += exercise(
"expired unknown logout",
|h| {
let mut r = h.receiver();
r.logout(h.context("initial", 25).caller, 25).unwrap();
r
},
|h, r| {
r.logout(h.context("initial", 125).caller, 125).unwrap();
},
);
assert!(stages >= 7); }
#[test]
fn publication_faults_close_pruning_of_all_retained_record_kinds() {
exercise(
"combined prune",
|h| {
let mut r = populated(h);
admit(h, &mut r, "renewal", 25);
r.logout(h.context("replacement", 25).caller, 25).unwrap();
r
},
|_, r| {
let report = r.prune(125, 128).unwrap();
assert_eq!(
(report.sessions, report.replays, report.unknown_denials),
(1, 2, 1)
);
assert_eq!(r.known_logins(), 1);
assert_eq!(r.usage(account()).encoded_bytes, 0);
},
);
}
#[cfg(feature = "receiver-stable")]
#[test]
fn reopen_rejects_resealed_session_size_and_application_binding_changes() {
use crate::{receiver_memory::StableReceiver, stable_upgrade::UpgradeError};
use ic_storage::{StableKey, VectorMemory, adapter::StableCell, testing::fixture_runtime};
use sha2::{Digest, Sha256};
let h = Host::new();
for change in ["size", "drop", "same-length"] {
let key = "auth.receiver.v1";
let store = fixture_runtime(
VectorMemory::default(),
"auth",
"auth.",
&[key],
Default::default(),
)
.unwrap();
let key = StableKey::parse(key).unwrap();
let lease = || store.open_memory(&key).unwrap();
let mut stable = StableReceiver::new_fresh(lease(), h.config(), 0).unwrap();
h.enroll(stable.state(), 7).unwrap();
admit(&h, stable.state(), "registered", 25);
let caller = h.context("registered", 25).caller;
let r = stable.state();
let s = r
.callers
.get_mut(&caller)
.unwrap()
.session
.as_mut()
.unwrap();
match change {
"size" => {
s.bytes += 1;
r.encoded_bytes += 1;
}
"drop" => s.ext = None,
_ => {}
}
stable.seal(5).unwrap();
drop(stable);
if change == "same-length" {
let mut cell = StableCell::<Vec<u8>, _>::load(lease()).unwrap();
let mut image = cell.get().clone();
let (digest, payload) = image.split_at_mut(32);
let offset = payload.windows(16).position(|w| w == [0xab; 16]).unwrap();
payload[offset] ^= 1;
digest.copy_from_slice(
&Sha256::new()
.chain_update(b"ic-auth.receiver.v3")
.chain_update(6u64.to_le_bytes())
.chain_update(payload)
.finalize(),
);
cell.set(image);
}
assert!(
matches!(
StableReceiver::reopen(lease(), h.config(), 26, 6),
Err(UpgradeError::InvalidCheckpoint)
),
"{change}"
);
}
}