#![cfg(test)]
pub(crate) mod name_comparisons {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn get() -> u64 {
COUNT.with(Cell::get)
}
}
pub(crate) mod key_op_comparisons {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod cbc_hmac_decryptions {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod pbkdf2_derivations {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod ecdh_agreements {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod inflate_calls {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod jws_family_verifies {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
pub(crate) mod jws_family_signs {
use core::cell::Cell;
std::thread_local! {
static COUNT: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn tick() {
COUNT.with(|c| c.set(c.get().saturating_add(1)));
}
pub(crate) fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
let before = COUNT.with(Cell::get);
let result = f();
(result, COUNT.with(Cell::get) - before)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn jws_counters_count_only_their_own_ticks_while_f_runs() {
let ((), n) = jws_family_verifies::count(|| {
jws_family_verifies::tick();
jws_family_signs::tick();
});
assert_eq!(n, 1);
assert_eq!(jws_family_signs::count(jws_family_signs::tick).1, 1);
assert_eq!(jws_family_signs::count(jws_family_verifies::tick).1, 0);
}
#[test]
fn jwe_counters_count_only_their_own_ticks_while_f_runs() {
cbc_hmac_decryptions::tick();
let ((), n) = cbc_hmac_decryptions::count(|| {
cbc_hmac_decryptions::tick();
cbc_hmac_decryptions::tick();
pbkdf2_derivations::tick();
});
assert_eq!(n, 2);
assert_eq!(pbkdf2_derivations::count(|| ()).1, 0);
let (value, n) = pbkdf2_derivations::count(|| {
pbkdf2_derivations::tick();
7
});
assert_eq!((value, n), (7, 1));
assert_eq!(ecdh_agreements::count(ecdh_agreements::tick).1, 1);
assert_eq!(inflate_calls::count(inflate_calls::tick).1, 1);
assert_eq!(inflate_calls::count(ecdh_agreements::tick).1, 0);
}
}