use http::Method;
use parlov_core::{
always_applicable, NormativeStrength, OracleClass, SignalSurface, Technique, Vector,
};
use crate::strategy::Strategy;
use crate::types::{BurstSpec, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::build_pair;
use crate::ScanContext;
const BURST_COUNT: usize = 100;
static METADATA: StrategyMetadata = StrategyMetadata {
strategy_id: "rate-limit-burst-elicit",
strategy_name: "Rate Limit Burst Elicitation",
risk: RiskLevel::OperationDestructive,
};
static TECHNIQUE: Technique = Technique {
id: "rate-limit-burst",
name: "Rate-limit burst volume probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::May,
normalization_weight: Some(0.02),
inverted_signal_weight: Some(0.02),
method_relevant: false,
parser_relevant: false,
applicability: always_applicable,
contradiction_surface: SignalSurface::Status,
};
pub struct RateLimitBurstElicitation;
impl Strategy for RateLimitBurstElicitation {
fn metadata(&self) -> &'static StrategyMetadata {
&METADATA
}
fn technique_def(&self) -> &'static Technique {
&TECHNIQUE
}
fn methods(&self) -> &[Method] {
&[Method::GET, Method::HEAD]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let mut specs = Vec::with_capacity(2);
for method in [Method::GET, Method::HEAD] {
let pair = build_pair(
ctx,
method,
ctx.headers.clone(),
ctx.headers.clone(),
None,
METADATA.clone(),
TECHNIQUE,
);
specs.push(ProbeSpec::Burst(BurstSpec {
baseline: pair.baseline,
probe: pair.probe,
burst_count: BURST_COUNT,
metadata: pair.metadata,
technique: pair.technique,
chain_provenance: None,
}));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::ctx_operation_destructive;
use crate::util::substitute_url;
#[test]
fn risk_is_operation_destructive() {
assert_eq!(
RateLimitBurstElicitation.risk(),
RiskLevel::OperationDestructive
);
}
#[test]
fn generate_returns_two_items() {
assert_eq!(
RateLimitBurstElicitation
.generate(&ctx_operation_destructive())
.len(),
2
);
}
#[test]
fn all_items_are_burst_variants() {
for spec in &RateLimitBurstElicitation.generate(&ctx_operation_destructive()) {
assert!(matches!(spec, ProbeSpec::Burst(_)));
}
}
#[test]
fn burst_count_is_100() {
for spec in &RateLimitBurstElicitation.generate(&ctx_operation_destructive()) {
let ProbeSpec::Burst(burst) = spec else {
panic!("expected Burst")
};
assert_eq!(burst.burst_count, 100);
}
}
#[test]
fn baseline_url_uses_baseline_id() {
let ctx = ctx_operation_destructive();
let specs = RateLimitBurstElicitation.generate(&ctx);
let ProbeSpec::Burst(burst) = &specs[0] else {
panic!("expected Burst")
};
assert_eq!(
burst.baseline.url,
substitute_url(&ctx.target, &ctx.baseline_id)
);
}
#[test]
fn technique_strength_is_may() {
let specs = RateLimitBurstElicitation.generate(&ctx_operation_destructive());
assert_eq!(specs[0].technique().strength, NormativeStrength::May);
}
#[test]
fn normalization_weight_is_0_02() {
assert_eq!(TECHNIQUE.normalization_weight, Some(0.02));
}
#[test]
fn inverted_signal_weight_is_0_02() {
assert_eq!(TECHNIQUE.inverted_signal_weight, Some(0.02));
}
}