use http::Method;
use parlov_core::{
always_applicable, NormativeStrength, OracleClass, SignalSurface, Technique, Vector,
};
use crate::context::ScanContext;
use crate::strategy::Strategy;
use crate::types::{ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::{build_pair, clone_headers_static};
static METADATA: StrategyMetadata = StrategyMetadata {
strategy_id: "cp-accept",
strategy_name: "Cache Probe: Accept negotiation",
risk: RiskLevel::Safe,
};
static TECHNIQUE: Technique = Technique {
id: "cp-accept",
name: "Accept content negotiation probe",
oracle_class: OracleClass::Existence,
vector: Vector::CacheProbing,
strength: NormativeStrength::May,
normalization_weight: None,
inverted_signal_weight: None,
method_relevant: false,
parser_relevant: false,
applicability: always_applicable,
contradiction_surface: SignalSurface::Status,
};
pub struct CpAccept;
impl Strategy for CpAccept {
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 hdrs =
clone_headers_static(&ctx.headers, "accept", "application/x-parlov-nonexistent");
let pair = build_pair(
ctx,
method,
hdrs.clone(),
hdrs,
None,
METADATA.clone(),
TECHNIQUE,
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::minimal_ctx;
#[test]
fn generates_correct_technique_vector() {
let specs = CpAccept.generate(&minimal_ctx());
assert_eq!(specs[0].technique().vector, Vector::CacheProbing);
}
#[test]
fn generates_correct_technique_strength() {
let specs = CpAccept.generate(&minimal_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::May);
}
#[test]
fn generates_probe_with_correct_header() {
let specs = CpAccept.generate(&minimal_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
assert_eq!(
pair.probe.headers.get("accept").unwrap(),
"application/x-parlov-nonexistent"
);
}
#[test]
fn is_always_applicable() {
assert!(CpAccept.is_applicable(&minimal_ctx()));
}
#[test]
fn risk_is_safe() {
assert_eq!(CpAccept.risk(), RiskLevel::Safe);
}
}