Skip to main content

qualia_core_db/net/qdnf/fixtures/
golden.rs

1//! FND-03.07 golden capture of a live empty DiscoveryBeacon (PARTIAL).
2//!
3//! Bytes come from the production `encode_frame` path. This module is not the
4//! independent codec oracle (QA-01.03).
5
6use crate::net::qdnf::errors::QdnfError;
7use crate::net::qdnf::frame::{encode_frame, FrameError, FrameHeader};
8use crate::net::qdnf::registries::{FrameType, NextProtocol};
9use crate::net::qdnf::types::ProfileId;
10
11/// Frozen MAGIC expected on the empty-beacon golden.
12pub const GOLDEN_MAGIC: [u8; 4] = *b"QDNF";
13/// Frozen VERSION expected on the empty-beacon golden.
14pub const GOLDEN_VERSION: u8 = 1;
15/// Frozen base-header length expected on the empty-beacon golden.
16pub const GOLDEN_BASE_HEADER_LEN: usize = 80;
17
18/// First 16 bytes of a live-encoded empty DiscoveryBeacon.
19///
20/// Layout: MAGIC | version | DiscoveryBeacon | flags | header_len | payload_len
21/// | hop_limit | QLink | reserved.
22pub const EMPTY_BEACON_PREFIX: [u8; 16] =
23    [b'Q', b'D', b'N', b'F', 1, 1, 0, 0, 0, 80, 0, 0, 0, 1, 0, 0];
24
25/// Encode an empty DiscoveryBeacon into `out` using the production encoder.
26pub fn capture_empty_discovery_beacon(out: &mut [u8]) -> Result<usize, FrameError> {
27    let header = FrameHeader::new(FrameType::DiscoveryBeacon, NextProtocol::QLink);
28    encode_frame(&header, &[], out)
29}
30
31/// Known profiles fail closed. Unknown ids never infer a suite.
32pub fn require_known_profile(id: ProfileId) -> Result<(), QdnfError> {
33    if id == ProfileId::QPR_PQ_1 || id == ProfileId::QDNF_CRYPTO_1 {
34        Ok(())
35    } else {
36        Err(QdnfError::UnknownProfile)
37    }
38}
39
40/// Classical retry is not an automatic downgrade path.
41#[inline]
42pub fn allow_classical_retry() -> bool {
43    false
44}
45
46#[cfg(not(target_arch = "wasm32"))]
47pub use crate::q42::q42_volume::NetworkCursor;
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use crate::net::qdnf::frame::decode_frame;
53
54    #[test]
55    fn empty_beacon_matches_frozen_prefix() {
56        let mut wire = [0u8; GOLDEN_BASE_HEADER_LEN];
57        let n = capture_empty_discovery_beacon(&mut wire).expect("encode");
58        assert_eq!(n, GOLDEN_BASE_HEADER_LEN);
59        assert_eq!(&wire[0..4], &GOLDEN_MAGIC);
60        assert_eq!(wire[4], GOLDEN_VERSION);
61        assert_eq!(&wire[0..16], &EMPTY_BEACON_PREFIX);
62        assert_eq!(&wire[80..], &[] as &[u8]);
63    }
64
65    #[test]
66    fn empty_beacon_round_trips_decode_frame() {
67        let mut wire = [0u8; GOLDEN_BASE_HEADER_LEN];
68        let n = capture_empty_discovery_beacon(&mut wire).expect("encode");
69        let (header, off, len) = decode_frame(&wire[..n]).expect("decode");
70        assert_eq!(header.version, GOLDEN_VERSION);
71        assert_eq!(header.header_len as usize, GOLDEN_BASE_HEADER_LEN);
72        assert_eq!(header.payload_len, 0);
73        assert_eq!(header.frame_type, FrameType::DiscoveryBeacon);
74        assert_eq!(header.next_protocol, NextProtocol::QLink);
75        assert_eq!(off, GOLDEN_BASE_HEADER_LEN);
76        assert_eq!(len, 0);
77    }
78
79    #[test]
80    fn malformed_magic_and_truncated_input() {
81        let mut wire = [0u8; GOLDEN_BASE_HEADER_LEN];
82        let n = capture_empty_discovery_beacon(&mut wire).expect("encode");
83        wire[0] ^= 0xFF;
84        assert_eq!(decode_frame(&wire[..n]), Err(FrameError::Malformed));
85
86        let short = [0u8; 16];
87        assert_eq!(decode_frame(&short), Err(FrameError::Truncated));
88    }
89
90    #[test]
91    fn unknown_profile_fails_closed_without_downgrade() {
92        assert_eq!(require_known_profile(ProfileId::QPR_PQ_1), Ok(()));
93        assert_eq!(require_known_profile(ProfileId::QDNF_CRYPTO_1), Ok(()));
94        assert_eq!(
95            require_known_profile(ProfileId(0x0000)),
96            Err(QdnfError::UnknownProfile)
97        );
98        assert!(!allow_classical_retry());
99    }
100
101    #[cfg(not(target_arch = "wasm32"))]
102    #[test]
103    fn network_cursor_binds_and_rejects_cross_scope() {
104        let source = [0xABu8; 32];
105        let generation = 4u64;
106        let profile = ProfileId::QPR_PQ_1.0;
107        let scope = 0x51C0u64;
108        let mut cursor = NetworkCursor::bind(source, generation, profile, scope);
109        assert_eq!(
110            cursor
111                .advance(&source, generation, profile, scope, 1)
112                .expect("same-scope"),
113            1
114        );
115        assert_eq!(
116            cursor.advance(&source, generation, profile, scope ^ 1, 1),
117            Err(QdnfError::Unauthorized)
118        );
119        assert_eq!(cursor.offset, 1);
120        assert_eq!(cursor.query_generation, generation);
121        assert_eq!(cursor.scope, scope);
122    }
123}