use super::*;
use crate::fingerprint::akamai_h2::{AkamaiH2Fingerprint, H2Priority, H2Setting, PseudoHeader};
use crate::fingerprint::StealthProfile;
use crate::http::session_coherence::{
persona_wire_self_probe, WireSelfProbe, CHROME_H2, FIREFOX_H2, SAFARI_H2,
};
const AUTHORITY: &str = "example.com";
fn families() -> Vec<(&'static H2Profile, StealthProfile)> {
vec![
(&CHROME_H2, StealthProfile::ChromeWindowsStable),
(&FIREFOX_H2, StealthProfile::FirefoxLinux),
(&SAFARI_H2, StealthProfile::SafariMacStable),
]
}
#[test]
fn round_trip_reconstructs_each_familys_exact_akamai_string() {
for (profile, _) in families() {
let bytes = encode_client_opening_for_profile(profile, AUTHORITY, "/")
.expect("canonical profile must encode");
let observed = parse_client_akamai(&bytes).expect("our own emission must parse");
assert_eq!(
observed,
profile.akamai_fingerprint(),
"{}: emitted wire bytes do not reconstruct the model Akamai string",
profile.family
);
}
}
#[test]
fn pseudo_header_order_segment_on_the_wire_matches_each_family() {
let expect = [
(&CHROME_H2, "m,a,s,p"),
(&FIREFOX_H2, "m,p,a,s"),
(&SAFARI_H2, "m,s,p,a"),
];
for (profile, want) in expect {
let bytes = encode_client_opening_for_profile(profile, AUTHORITY, "/").unwrap();
let observed = parse_client_akamai(&bytes).unwrap();
let segment = observed.split('|').nth(3).unwrap();
assert_eq!(
segment, want,
"{} pseudo-header order on the wire",
profile.family
);
}
}
#[test]
fn emitted_opening_self_probes_coherent_for_each_persona() {
for (profile, persona) in families() {
let bytes = encode_client_opening_for_profile(profile, AUTHORITY, "/").unwrap();
let capture = capture_client_opening(&bytes).expect("emission parses to a capture");
assert_eq!(
persona_wire_self_probe(persona, &capture),
WireSelfProbe::Coherent,
"{persona:?}: own emitted H2 opening must self-probe Coherent",
);
}
}
#[test]
fn firefox_persona_emitting_chrome_h2_is_caught_incoherent() {
let chrome_bytes = encode_client_opening_for_profile(&CHROME_H2, AUTHORITY, "/").unwrap();
let capture = capture_client_opening(&chrome_bytes).unwrap();
let verdict = persona_wire_self_probe(StealthProfile::FirefoxLinux, &capture);
match verdict {
WireSelfProbe::Incoherent(mismatches) => {
let akamai = mismatches
.iter()
.find_map(|m| match m {
crate::http::session_coherence::WireLayerMismatch::Akamai {
expected,
observed,
} => Some((expected.clone(), observed.clone())),
_ => None,
})
.expect("the Akamai layer must be the named mismatch");
assert_eq!(
akamai.0,
FIREFOX_H2.akamai_fingerprint(),
"expected = persona's Firefox H2"
);
assert_eq!(
akamai.1,
CHROME_H2.akamai_fingerprint(),
"observed = the emitted Chrome H2"
);
}
other => panic!("Firefox persona emitting Chrome H2 must be Incoherent, got {other:?}"),
}
}
#[test]
fn wire_round_trip_over_a_real_loopback_socket() {
use std::io::{Read, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback");
let addr = listener.local_addr().unwrap();
let server = std::thread::spawn(move || -> Result<String, WireParseError> {
let (mut sock, _) = listener.accept().expect("accept");
let mut buf = Vec::new();
sock.read_to_end(&mut buf).expect("read opening");
parse_client_akamai(&buf)
});
let bytes = encode_client_opening_for_profile(&FIREFOX_H2, AUTHORITY, "/").unwrap();
let mut client = TcpStream::connect(addr).expect("connect loopback");
client.write_all(&bytes).expect("write opening");
client
.shutdown(Shutdown::Write)
.expect("half-close so server sees EOF");
let observed = server
.join()
.expect("server thread")
.expect("server parses opening");
assert_eq!(observed, FIREFOX_H2.akamai_fingerprint());
let capture = crate::http::session_coherence::WireCapture {
akamai_fingerprint: Some(observed),
..Default::default()
};
assert!(persona_wire_self_probe(StealthProfile::FirefoxLinux, &capture).is_coherent());
}
#[test]
fn synthetic_priority_and_window_round_trip() {
let fp = AkamaiH2Fingerprint {
settings: vec![
H2Setting {
id: 1,
value: 65_536,
},
H2Setting {
id: 4,
value: 131_072,
},
H2Setting {
id: 5,
value: 16_384,
},
],
window_update: 12_517_377,
priorities: vec![H2Priority {
stream_id: 3,
exclusive: 0,
dependent: 0,
weight: 201,
}],
pseudo_header_order: vec![
PseudoHeader::Method,
PseudoHeader::Path,
PseudoHeader::Authority,
PseudoHeader::Scheme,
],
};
let canonical = fp.to_canonical();
assert_eq!(
canonical,
"1:65536;4:131072;5:16384|12517377|3:0:0:201|m,p,a,s"
);
let bytes = encode_client_opening(&fp, AUTHORITY, "/");
assert_eq!(parse_client_akamai(&bytes).unwrap(), canonical);
}
#[test]
fn non_root_path_round_trips_via_literal_path() {
let bytes = encode_client_opening_for_profile(&CHROME_H2, AUTHORITY, "/search?q=1").unwrap();
let observed = parse_client_akamai(&bytes).unwrap();
assert_eq!(observed, CHROME_H2.akamai_fingerprint());
}
#[test]
fn emitted_wire_reconstructs_the_published_catalogue_akamai_for_wire_measured_families() {
use crate::fingerprint::tls_targets::lookup;
let cases = [
(&CHROME_H2, "chrome-146-linux"),
(&FIREFOX_H2, "firefox-150-linux"),
];
for (profile, label) in cases {
let bytes = encode_client_opening_for_profile(profile, AUTHORITY, "/").unwrap();
let observed = parse_client_akamai(&bytes).unwrap();
assert_eq!(
observed,
lookup(label).unwrap().akamai_h2,
"{}: emitted wire Akamai must equal the published {label} catalogue target",
profile.family
);
}
}
#[test]
fn bad_preface_fails_closed() {
let err = parse_client_akamai(b"GET / HTTP/1.1\r\n\r\n").unwrap_err();
assert_eq!(err, WireParseError::BadPreface);
}
#[test]
fn truncated_opening_fails_closed_not_partial() {
let bytes = encode_client_opening_for_profile(&CHROME_H2, AUTHORITY, "/").unwrap();
let truncated = &bytes[..bytes.len() - 3];
assert!(matches!(
parse_client_akamai(truncated),
Err(WireParseError::Truncated | WireParseError::NoHeaders)
));
}
#[test]
fn opening_without_headers_fails_closed() {
let mut bytes = Vec::new();
bytes.extend_from_slice(H2_CLIENT_PREFACE);
super::push_frame(&mut bytes, 0x4, 0, 0, &[]);
assert_eq!(parse_client_akamai(&bytes), Err(WireParseError::NoHeaders));
}