Make codec shape authority compile-time and fixed
This commit is contained in:
parent
bfe21dfb3f
commit
54cc167f81
3 changed files with 322 additions and 52 deletions
|
|
@ -15,9 +15,39 @@ use lazaret_core::identity::{
|
|||
SessionAnchorId, SessionId, SourceIdentity, StreamDigest, TrustManifestDigest,
|
||||
};
|
||||
use lazaret_core::network::readiness_fingerprint as derive_readiness_fingerprint;
|
||||
use lazaret_core::protocol::{BootstrapWaitOutcome, BootstrapWaitRequest};
|
||||
use lazaret_core::records::*;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[derive(Default)]
|
||||
struct SchemaCorpus {
|
||||
names: HashSet<&'static str>,
|
||||
digest: Sha256,
|
||||
}
|
||||
|
||||
impl SchemaCorpus {
|
||||
fn insert(&mut self, name: &'static str, encoded: &[u8]) -> bool {
|
||||
self.digest.update(name.len().to_be_bytes());
|
||||
self.digest.update(name.as_bytes());
|
||||
self.digest.update(encoded.len().to_be_bytes());
|
||||
self.digest.update(encoded);
|
||||
self.names.insert(name)
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.names.len()
|
||||
}
|
||||
|
||||
fn contains(&self, name: &&str) -> bool {
|
||||
self.names.contains(name)
|
||||
}
|
||||
|
||||
fn digest(&self) -> [u8; 32] {
|
||||
self.digest.clone().finalize().into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct RawParticipant {
|
||||
|
|
@ -168,6 +198,39 @@ struct RawTrustedHostConfigurationV1 {
|
|||
profiles: BTreeMap<String, ProfileConfigurationV1>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct OversizedRecord(Vec<u8>);
|
||||
|
||||
macro_rules! test_schema_types {
|
||||
($($record:ty),+ $(,)?) => {
|
||||
$(
|
||||
impl SchemaType for $record {
|
||||
const NAME: &'static str = concat!("test::", stringify!($record));
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
test_schema_types!(
|
||||
RawParticipant,
|
||||
RawTlsCredential,
|
||||
RawProtocolCredential,
|
||||
RawSandboxCredentialBundle,
|
||||
RawMetadata,
|
||||
MissingVersion,
|
||||
RawDomainStore,
|
||||
RawStorageCatalog,
|
||||
RawTerminalResize,
|
||||
RawSessionTerminal,
|
||||
InvalidSessionExit,
|
||||
RawDerivedNetworkPolicy,
|
||||
RawNetworkReadinessAttestation,
|
||||
RawNetworkReadinessIdentity,
|
||||
RawMailboxRequest,
|
||||
RawTrustedHostConfigurationV1,
|
||||
OversizedRecord,
|
||||
);
|
||||
|
||||
fn opaque(byte: u8) -> [u8; 32] {
|
||||
[byte; 32]
|
||||
}
|
||||
|
|
@ -604,12 +667,16 @@ fn participant_edge_snapshot(
|
|||
.unwrap()
|
||||
}
|
||||
|
||||
fn verify_shape<T>(record: T, seen: &mut HashSet<&'static str>)
|
||||
fn verify_shape<T>(record: T, seen: &mut SchemaCorpus)
|
||||
where
|
||||
T: Clone + Debug + DeserializeOwned + Eq + SchemaType + Serialize,
|
||||
{
|
||||
assert!(seen.insert(T::NAME), "duplicate test for {}", T::NAME);
|
||||
let encoded = encode(&record).expect("representative record encodes");
|
||||
assert!(
|
||||
seen.insert(T::NAME, &encoded),
|
||||
"duplicate test for {}",
|
||||
T::NAME
|
||||
);
|
||||
assert_eq!(encode(&record).unwrap(), encoded);
|
||||
assert_eq!(decode::<T>(&encoded).unwrap(), record);
|
||||
|
||||
|
|
|
|||
|
|
@ -223,11 +223,11 @@ fn zero_terminal_dimensions_are_rejected() {
|
|||
fn generic_schema_record_limit_is_enforced_before_decode() {
|
||||
let oversized = vec![0; MAX_SCHEMA_RECORD_BYTES + 1];
|
||||
assert!(matches!(
|
||||
decode::<Vec<u8>>(&oversized),
|
||||
decode::<StoreMetadata>(&oversized),
|
||||
Err(CodecError::RecordTooLarge { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
encode(&oversized),
|
||||
encode(&OversizedRecord(oversized)),
|
||||
Err(CodecError::RecordTooLarge { .. })
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,9 +84,11 @@ fn every_schema_shape_round_trips_and_rejects_malformed_or_unknown_versions() {
|
|||
inode: InodeId::new(47),
|
||||
state: ArtifactState::Available,
|
||||
};
|
||||
let mut seen = HashSet::new();
|
||||
let mut seen = SchemaCorpus::default();
|
||||
|
||||
verify_shape(metadata.clone(), &mut seen);
|
||||
verify_shape(DomainStoreMetadata::v1(), &mut seen);
|
||||
verify_shape(StorageCatalogMetadata::v1(), &mut seen);
|
||||
verify_shape(domain.clone(), &mut seen);
|
||||
verify_shape(generation.clone(), &mut seen);
|
||||
verify_shape(participant.clone(), &mut seen);
|
||||
|
|
@ -669,6 +671,18 @@ image = "example.invalid/agent:v1"
|
|||
},
|
||||
&mut seen,
|
||||
);
|
||||
verify_shape(
|
||||
BootstrapWaitRequest {
|
||||
protocol_version: ProtocolVersion::V1,
|
||||
domain_id: domain_id(),
|
||||
configuration_fingerprint: ConfigurationFingerprint::new(opaque(6)),
|
||||
cli_id: ClientId::new(opaque(13)),
|
||||
session_id: session_id(),
|
||||
session_anchor_id: SessionAnchorId::new(opaque(38)),
|
||||
},
|
||||
&mut seen,
|
||||
);
|
||||
verify_shape(BootstrapWaitOutcome::Published, &mut seen);
|
||||
|
||||
assert_eq!(seen.len(), SERIALIZED_RECORD_TYPE_NAMES.len());
|
||||
assert!(
|
||||
|
|
@ -676,11 +690,18 @@ image = "example.invalid/agent:v1"
|
|||
.iter()
|
||||
.all(|name| seen.contains(name))
|
||||
);
|
||||
assert_eq!(
|
||||
seen.digest(),
|
||||
[
|
||||
255, 220, 6, 56, 49, 12, 147, 182, 148, 61, 218, 228, 124, 211, 136, 128, 186, 186,
|
||||
138, 239, 246, 197, 75, 0, 75, 85, 27, 244, 21, 21, 246, 41,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_unknown_protocol_version<T>(record: &T)
|
||||
where
|
||||
T: DeserializeOwned + Serialize,
|
||||
T: DeserializeOwned + SchemaType + Serialize,
|
||||
{
|
||||
let mut encoded = encode(record).unwrap();
|
||||
encoded[0] = u8::MAX;
|
||||
|
|
|
|||
Loading…
Reference in a new issue