Merge BP-1: the content plane in cultnet-rs
The Rust runtime gains the reference's content chunk messages on the raw path, CultMeshCdnArtifactManifest with wire-ordered metadata, normalize_hash, pack_content, validate_manifest, answer_content_chunk_request (every bad request is answered, byte identical to the reference server) and fetch_content (one owner of receiving-side verification; refuses zero-size floods and unnormalised hashes before any request). Vectors run both ways between C# and Rust. The operator ruled Q-BP1 A: chunks travel on the session already held. This is the explicit Rust content path until an authenticated connector exists. Soul passed it after two fix batches. cultnet-rs: content 24, cultnet 53, rudp 7, lib 110. cargo-mutants on content.rs misses 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
6251cbcd37
10 changed files with 1997 additions and 0 deletions
189
contracts/cultmesh/content-vectors.cs-written.json
Normal file
189
contracts/cultmesh/content-vectors.cs-written.json
Normal file
File diff suppressed because one or more lines are too long
159
contracts/cultmesh/content-vectors.rs-written.json
Normal file
159
contracts/cultmesh/content-vectors.rs-written.json
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -73,6 +73,23 @@ to speak CultNet or participate in CultMesh.
|
|||
`tests/GameCult.Networking.Tests/SelectionParityVectorTests.cs` carry the
|
||||
semantic ones. A single rows-to-bytes path driving both halves in one call
|
||||
remains its own cut (R-AR) and is not part of this claim.
|
||||
- CultMesh content plane (`cultmesh.content_chunk_request.v1` and `.response.v1`
|
||||
plus the CDN manifest, C#/Rust only): byte parity in both directions (manifest metadata keeps its wire key order and a
|
||||
duplicate key is refused, as the reference refuses it), and the reference's answer
|
||||
to a chunk request, including its failure spellings: every bad request is
|
||||
answered `found: false`, never refused at decode.
|
||||
`packages/cultnet-rs/tests/content.rs` and
|
||||
`tests/GameCult.Mesh.Tests/CultMeshContentVectorTests.cs` judge
|
||||
`contracts/cultmesh/content-vectors.cs-written.json` and
|
||||
`content-vectors.rs-written.json`. Rust carries these chunks only on the
|
||||
session the client already holds (see `src/GameCult.Mesh/docs/transport-planes.md`);
|
||||
it has no authenticated content connector, so it claims no TCP+TLS or QUIC
|
||||
content path.
|
||||
Recorded limits: a caller that passes `max_bytes = u64::MAX` to `fetch_content` gets an unbounded
|
||||
body, because the reference refuses artifacts over `int.MaxValue` bytes and Rust bounds them only by the
|
||||
caller's cap.
|
||||
`normalize_hash` matches the `sha256:` prefix ASCII-case-insensitively while C# `OrdinalIgnoreCase` may
|
||||
also match non-ASCII forms (for example `ſha256:`); unprobed, and it affects hostile input only.
|
||||
|
||||
## Expansion Work Outside The Current Parity Claim
|
||||
|
||||
|
|
|
|||
426
packages/cultnet-rs/src/content.rs
Normal file
426
packages/cultnet-rs/src/content.rs
Normal file
|
|
@ -0,0 +1,426 @@
|
|||
//! CultMesh content plane: chunking, hashing, manifest shape and validation, answering one chunk
|
||||
//! request, and verifying a fetched body. Mirrors `CultMeshCdn` (`CultMeshCdn.cs`) and
|
||||
//! `CultMeshLegacyRudpContentServer` (`CultMeshContentSessions.cs`); the two chunk messages are
|
||||
//! `CultNetMessage::ContentChunkRequest`/`ContentChunkResponse` in `contracts.rs`.
|
||||
//!
|
||||
//! This is the only owner of these rules in Rust. A consumer hands `fetch_content` a transport
|
||||
//! closure and gets verified bytes; it never hashes, chunks or validates a manifest itself.
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::CultNetMessage;
|
||||
|
||||
/// One chunk of a packed body. `chunk_hash` is lowercase SHA-256 hex of `payload`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CultMeshCdnChunk {
|
||||
pub chunk_hash: String,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
/// `CultMeshCdnChunkRef`. Encodes as the reference's positional array under every encoder.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(from = "ChunkRefWire", into = "ChunkRefWire")]
|
||||
pub struct CultMeshCdnChunkRef {
|
||||
pub chunk_hash: String,
|
||||
pub offset: i64,
|
||||
pub size_bytes: i32,
|
||||
pub record_key: String,
|
||||
}
|
||||
|
||||
/// `CultMeshCdnArtifactManifest`. Encodes as the reference's positional array (keys 0-9).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(from = "ManifestWire", into = "ManifestWire")]
|
||||
pub struct CultMeshCdnArtifactManifest {
|
||||
pub artifact_id: String,
|
||||
pub kind: String,
|
||||
pub version: String,
|
||||
pub content_hash: String,
|
||||
pub size_bytes: i64,
|
||||
pub mime_type: String,
|
||||
pub created_at_utc: String,
|
||||
pub chunks: Vec<CultMeshCdnChunkRef>,
|
||||
pub tags: Vec<String>,
|
||||
/// Caller metadata in wire order. The reference writes a `Dictionary` in insertion order and
|
||||
/// refuses a duplicate key at read, so this keeps the order it was read in and refuses a duplicate.
|
||||
pub metadata: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
// The tuple mirrors: `to_vec_named` would write a map for a named-field struct, but a tuple
|
||||
// struct is an array under every serde encoder.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ChunkRefWire(String, i64, i32, String);
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ManifestWire(
|
||||
String,
|
||||
String,
|
||||
String,
|
||||
String,
|
||||
i64,
|
||||
String,
|
||||
String,
|
||||
Vec<CultMeshCdnChunkRef>,
|
||||
Vec<String>,
|
||||
#[serde(with = "ordered_map")] Vec<(String, String)>,
|
||||
);
|
||||
|
||||
/// A string map that keeps its key order and refuses a duplicate key, as `Dictionary<string, string>`
|
||||
/// deserialises. `Vec<(String, String)>` is the in-memory shape; the wire shape is a msgpack map.
|
||||
mod ordered_map {
|
||||
use std::fmt;
|
||||
|
||||
use serde::de::{self, MapAccess, Visitor};
|
||||
use serde::{Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S: Serializer>(pairs: &[(String, String)], serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.collect_map(pairs.iter().map(|(key, value)| (key, value)))
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<(String, String)>, D::Error> {
|
||||
struct Pairs;
|
||||
impl<'de> Visitor<'de> for Pairs {
|
||||
type Value = Vec<(String, String)>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a map of strings")
|
||||
}
|
||||
|
||||
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut pairs = Vec::new();
|
||||
while let Some((key, value)) = map.next_entry::<String, String>()? {
|
||||
if !seen.insert(key.clone()) {
|
||||
return Err(de::Error::custom(format!("duplicate metadata key '{key}'")));
|
||||
}
|
||||
pairs.push((key, value));
|
||||
}
|
||||
Ok(pairs)
|
||||
}
|
||||
}
|
||||
deserializer.deserialize_map(Pairs)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CultMeshCdnChunkRef> for ChunkRefWire {
|
||||
fn from(value: CultMeshCdnChunkRef) -> Self {
|
||||
Self(value.chunk_hash, value.offset, value.size_bytes, value.record_key)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ChunkRefWire> for CultMeshCdnChunkRef {
|
||||
fn from(value: ChunkRefWire) -> Self {
|
||||
Self {
|
||||
chunk_hash: value.0,
|
||||
offset: value.1,
|
||||
size_bytes: value.2,
|
||||
record_key: value.3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CultMeshCdnArtifactManifest> for ManifestWire {
|
||||
fn from(value: CultMeshCdnArtifactManifest) -> Self {
|
||||
Self(
|
||||
value.artifact_id,
|
||||
value.kind,
|
||||
value.version,
|
||||
value.content_hash,
|
||||
value.size_bytes,
|
||||
value.mime_type,
|
||||
value.created_at_utc,
|
||||
value.chunks,
|
||||
value.tags,
|
||||
value.metadata,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ManifestWire> for CultMeshCdnArtifactManifest {
|
||||
fn from(value: ManifestWire) -> Self {
|
||||
Self {
|
||||
artifact_id: value.0,
|
||||
kind: value.1,
|
||||
version: value.2,
|
||||
content_hash: value.3,
|
||||
size_bytes: value.4,
|
||||
mime_type: value.5,
|
||||
created_at_utc: value.6,
|
||||
chunks: value.7,
|
||||
tags: value.8,
|
||||
metadata: value.9,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn sha256_hex(bytes: &[u8]) -> String {
|
||||
format!("{:x}", Sha256::digest(bytes))
|
||||
}
|
||||
|
||||
fn chunk_record_key(hash: &str) -> String {
|
||||
format!("mesh:cdn:chunk:{hash}")
|
||||
}
|
||||
|
||||
/// `CultMeshCdn.NormalizeHash`: an optional case-insensitive `sha256:` prefix is stripped, then the
|
||||
/// rest is trimmed and lowercased. A blank hash is refused.
|
||||
pub fn normalize_hash(hash: &str) -> Result<String> {
|
||||
if hash.trim().is_empty() {
|
||||
bail!("Hash must be non-empty.");
|
||||
}
|
||||
let stripped = match hash.get(..7) {
|
||||
Some(prefix) if prefix.eq_ignore_ascii_case("sha256:") => &hash[7..],
|
||||
_ => hash,
|
||||
};
|
||||
Ok(stripped.trim().to_lowercase())
|
||||
}
|
||||
|
||||
/// A hash the client will put on the wire or compare against: normalized and not empty.
|
||||
fn non_empty_hash(hash: &str) -> Result<String> {
|
||||
let normalized = normalize_hash(hash)?;
|
||||
if normalized.is_empty() {
|
||||
bail!("Hash must be non-empty.");
|
||||
}
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
/// `CultMeshCdn.PackArtifact`: splits `bytes` into content-addressed chunks and builds the manifest.
|
||||
/// An empty body packs as one zero-length chunk, as the reference does.
|
||||
pub fn pack_content(
|
||||
artifact_id: &str,
|
||||
kind: &str,
|
||||
version: &str,
|
||||
mime_type: &str,
|
||||
created_at_utc: &str,
|
||||
bytes: &[u8],
|
||||
chunk_size: usize,
|
||||
) -> Result<(CultMeshCdnArtifactManifest, Vec<CultMeshCdnChunk>)> {
|
||||
if artifact_id.trim().is_empty() {
|
||||
bail!("Artifact id must be non-empty.");
|
||||
}
|
||||
if chunk_size == 0 || i32::try_from(chunk_size).is_err() {
|
||||
bail!("ChunkSizeBytes must be greater than zero and fit in an int.");
|
||||
}
|
||||
let pieces: Vec<&[u8]> = if bytes.is_empty() {
|
||||
vec![bytes]
|
||||
} else {
|
||||
bytes.chunks(chunk_size).collect()
|
||||
};
|
||||
let mut offset = 0i64;
|
||||
let mut chunks = Vec::with_capacity(pieces.len());
|
||||
let mut refs = Vec::with_capacity(pieces.len());
|
||||
for piece in pieces {
|
||||
let chunk_hash = sha256_hex(piece);
|
||||
refs.push(CultMeshCdnChunkRef {
|
||||
record_key: chunk_record_key(&chunk_hash),
|
||||
chunk_hash: chunk_hash.clone(),
|
||||
offset,
|
||||
// A piece is at most `chunk_size` bytes, which fits an i32 (checked above).
|
||||
size_bytes: piece.len() as i32,
|
||||
});
|
||||
offset += piece.len() as i64;
|
||||
chunks.push(CultMeshCdnChunk {
|
||||
chunk_hash,
|
||||
payload: piece.to_vec(),
|
||||
});
|
||||
}
|
||||
let manifest = CultMeshCdnArtifactManifest {
|
||||
artifact_id: artifact_id.to_string(),
|
||||
kind: non_blank_or(kind, "asset"),
|
||||
version: version.to_string(),
|
||||
content_hash: sha256_hex(bytes),
|
||||
size_bytes: bytes.len() as i64,
|
||||
mime_type: non_blank_or(mime_type, "application/octet-stream"),
|
||||
created_at_utc: created_at_utc.to_string(),
|
||||
chunks: refs,
|
||||
tags: Vec::new(),
|
||||
metadata: Vec::new(),
|
||||
};
|
||||
validate_manifest(&manifest)?;
|
||||
Ok((manifest, chunks))
|
||||
}
|
||||
|
||||
fn non_blank_or(value: &str, default: &str) -> String {
|
||||
if value.trim().is_empty() {
|
||||
default.to_string()
|
||||
} else {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// The chunk list in offset order, as the reference reads it (`OrderBy(chunk => chunk.Offset)`).
|
||||
fn in_offset_order(manifest: &CultMeshCdnArtifactManifest) -> Vec<&CultMeshCdnChunkRef> {
|
||||
let mut refs: Vec<&CultMeshCdnChunkRef> = manifest.chunks.iter().collect();
|
||||
refs.sort_by_key(|chunk| chunk.offset);
|
||||
refs
|
||||
}
|
||||
|
||||
/// `CultMeshCdn.ValidateManifestShape`: non-negative size, chunks contiguous from offset 0 once
|
||||
/// sorted by offset, non-negative chunk sizes, and chunk sizes summing to the manifest size.
|
||||
pub fn validate_manifest(manifest: &CultMeshCdnArtifactManifest) -> Result<()> {
|
||||
if manifest.size_bytes < 0 {
|
||||
bail!("CDN artifact manifest has a negative size.");
|
||||
}
|
||||
let mut expected_offset = 0i64;
|
||||
for chunk in in_offset_order(manifest) {
|
||||
if chunk.offset != expected_offset {
|
||||
bail!("CDN artifact chunks are not contiguous.");
|
||||
}
|
||||
if chunk.size_bytes < 0 {
|
||||
bail!("CDN artifact chunk has a negative size.");
|
||||
}
|
||||
expected_offset = expected_offset
|
||||
.checked_add(i64::from(chunk.size_bytes))
|
||||
.ok_or_else(|| anyhow!("CDN artifact chunk sizes overflow."))?;
|
||||
}
|
||||
if expected_offset != manifest.size_bytes {
|
||||
bail!("CDN artifact chunk sizes do not sum to the manifest size.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `CultMeshCdn.ValidateChunk`: the chunk's size and full SHA-256 must be what the reference and
|
||||
/// the chunk's own claim say. `ref_hash` is already normalized.
|
||||
fn validate_chunk(
|
||||
ref_hash: &str,
|
||||
ref_size: i32,
|
||||
claimed_hash: &str,
|
||||
claimed_size: i64,
|
||||
payload: &[u8],
|
||||
) -> Result<()> {
|
||||
if i64::try_from(payload.len()).ok() != Some(i64::from(ref_size)) || claimed_size != i64::from(ref_size) {
|
||||
bail!("CDN artifact chunk payload metadata does not match its manifest reference.");
|
||||
}
|
||||
let actual = sha256_hex(payload);
|
||||
if ref_hash != actual || normalize_hash(claimed_hash)? != actual {
|
||||
bail!("CDN artifact chunk hash does not match its payload.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `CultMeshLegacyRudpContentServer.HandleAsync`, in its validation order: message id, hash
|
||||
/// normalisation, record-key agreement with `mesh:cdn:chunk:<hash>`, lookup, then size and hash of
|
||||
/// what is served. A failure is `found: false` with an empty payload and the reference's
|
||||
/// `<ExceptionKind>: <message>` in `error`; it is never silence. `lookup` takes the normalized hash.
|
||||
pub fn answer_content_chunk_request<'a>(
|
||||
request: &CultNetMessage,
|
||||
lookup: impl Fn(&str) -> Option<&'a [u8]>,
|
||||
) -> CultNetMessage {
|
||||
let CultNetMessage::ContentChunkRequest {
|
||||
message_id,
|
||||
chunk_hash,
|
||||
record_key,
|
||||
expected_size_bytes,
|
||||
} = request
|
||||
else {
|
||||
return CultNetMessage::Error {
|
||||
error: "not a cultmesh.content_chunk_request.v1 message".to_string(),
|
||||
code: None,
|
||||
details: None,
|
||||
};
|
||||
};
|
||||
let served = (|| -> std::result::Result<(String, Vec<u8>), String> {
|
||||
if message_id.trim().is_empty() {
|
||||
return Err("InvalidDataException: Content chunk request requires a message identity.".to_string());
|
||||
}
|
||||
// The handler normalises the wire hash, then `CreateRecordKey` normalises that result again
|
||||
// (and spells its parameter `chunkHash`); the lookup, the size check and the hash check all
|
||||
// use the second result, and the answer echoes the first.
|
||||
let blank = |parameter: &str| format!("ArgumentException: Hash must be non-empty. (Parameter '{parameter}')");
|
||||
let hash = normalize_hash(chunk_hash).map_err(|_| blank("ChunkHash"))?;
|
||||
let canonical = normalize_hash(&hash).map_err(|_| blank("chunkHash"))?;
|
||||
if !record_key.trim().is_empty() && *record_key != chunk_record_key(&canonical) {
|
||||
return Err("InvalidDataException: Content chunk record key disagrees with its content hash.".to_string());
|
||||
}
|
||||
let payload = lookup(&canonical).ok_or_else(|| "FileNotFoundException: Content chunk is not available.".to_string())?;
|
||||
validate_chunk(&canonical, *expected_size_bytes, &canonical, payload.len() as i64, payload)
|
||||
.map_err(|error| format!("InvalidDataException: {error}"))?;
|
||||
Ok((hash, payload.to_vec()))
|
||||
})();
|
||||
match served {
|
||||
Ok((hash, payload)) => CultNetMessage::ContentChunkResponse {
|
||||
message_id: message_id.clone(),
|
||||
found: true,
|
||||
chunk_hash: hash,
|
||||
size_bytes: *expected_size_bytes,
|
||||
payload,
|
||||
error: String::new(),
|
||||
},
|
||||
Err(error) => CultNetMessage::ContentChunkResponse {
|
||||
message_id: message_id.clone(),
|
||||
found: false,
|
||||
chunk_hash: chunk_hash.clone(),
|
||||
size_bytes: 0,
|
||||
payload: Vec::new(),
|
||||
error,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// The client half: fetches every chunk of `manifest` through `ask`, one in flight, in offset
|
||||
/// order, and returns the body only after each chunk and then the whole body verify. `max_bytes`
|
||||
/// is checked against the manifest before the first request. `ask` sends a request on the
|
||||
/// caller's session and returns the answer; it owns transport, this function owns verification.
|
||||
pub fn fetch_content(
|
||||
manifest: &CultMeshCdnArtifactManifest,
|
||||
max_bytes: u64,
|
||||
mut ask: impl FnMut(CultNetMessage) -> Result<CultNetMessage>,
|
||||
) -> Result<Vec<u8>> {
|
||||
validate_manifest(manifest)?;
|
||||
if manifest.size_bytes as u64 > max_bytes {
|
||||
bail!(
|
||||
"CDN artifact of {} bytes exceeds the {max_bytes}-byte cap.",
|
||||
manifest.size_bytes
|
||||
);
|
||||
}
|
||||
// Everything the client will send or compare is settled before the first request. A zero-size
|
||||
// chunk is legal only as an empty body's single chunk (the reference packs it so); anywhere else
|
||||
// it is a request that yields nothing, and a manifest of them is a request flood.
|
||||
let ordered = in_offset_order(manifest);
|
||||
if ordered.len() > 1 && ordered.iter().any(|chunk| chunk.size_bytes == 0) {
|
||||
bail!("CDN artifact has a zero-size chunk beside other chunks.");
|
||||
}
|
||||
let content_hash = non_empty_hash(&manifest.content_hash)?;
|
||||
let hashes = ordered
|
||||
.iter()
|
||||
.map(|chunk| non_empty_hash(&chunk.chunk_hash))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let mut body = Vec::new();
|
||||
for (chunk, hash) in ordered.into_iter().zip(hashes) {
|
||||
let message_id = uuid::Uuid::new_v4().simple().to_string();
|
||||
let record_key = if chunk.record_key.trim().is_empty() {
|
||||
chunk_record_key(&hash)
|
||||
} else {
|
||||
chunk.record_key.clone()
|
||||
};
|
||||
let answer = ask(CultNetMessage::ContentChunkRequest {
|
||||
message_id: message_id.clone(),
|
||||
chunk_hash: hash.clone(),
|
||||
record_key,
|
||||
expected_size_bytes: chunk.size_bytes,
|
||||
})?;
|
||||
let CultNetMessage::ContentChunkResponse {
|
||||
message_id: answered,
|
||||
found,
|
||||
chunk_hash,
|
||||
size_bytes,
|
||||
payload,
|
||||
error,
|
||||
} = answer
|
||||
else {
|
||||
bail!("chunk '{hash}' was answered with something other than a content chunk response");
|
||||
};
|
||||
if answered != message_id {
|
||||
bail!("chunk '{hash}' was answered for message '{answered}', not '{message_id}'");
|
||||
}
|
||||
if !found {
|
||||
bail!("content provider rejected chunk '{hash}': {error}");
|
||||
}
|
||||
validate_chunk(&hash, chunk.size_bytes, &chunk_hash, i64::from(size_bytes), &payload)?;
|
||||
body.extend_from_slice(&payload);
|
||||
}
|
||||
if sha256_hex(&body) != content_hash {
|
||||
bail!("CDN artifact content hash does not match its manifest.");
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
|
@ -508,6 +508,26 @@ pub enum CultNetMessage {
|
|||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
source_runtime_id: Option<String>,
|
||||
},
|
||||
/// `cultmesh.content_chunk_request.v1`. Mirrors `CultMeshContentChunkRequestMessage`; raw path.
|
||||
#[serde(rename = "cultmesh.content_chunk_request.v1", rename_all = "camelCase")]
|
||||
ContentChunkRequest {
|
||||
message_id: String,
|
||||
chunk_hash: String,
|
||||
record_key: String,
|
||||
expected_size_bytes: i32,
|
||||
},
|
||||
/// `cultmesh.content_chunk_response.v1`. Mirrors `CultMeshContentChunkResponseMessage`. `payload`
|
||||
/// is `bin`, so it travels the raw path; the serde attribute never runs on the wire.
|
||||
#[serde(rename = "cultmesh.content_chunk_response.v1", rename_all = "camelCase")]
|
||||
ContentChunkResponse {
|
||||
message_id: String,
|
||||
found: bool,
|
||||
chunk_hash: String,
|
||||
size_bytes: i32,
|
||||
#[serde(with = "serde_bytes")]
|
||||
payload: Vec<u8>,
|
||||
error: String,
|
||||
},
|
||||
}
|
||||
|
||||
fn messagepack_base64_encoding() -> String {
|
||||
|
|
@ -529,6 +549,8 @@ pub fn parse_cultnet_message(
|
|||
"cultnet.document_put_raw.v0"
|
||||
| "cultnet.snapshot_response_raw.v0"
|
||||
| "cultnet.snapshot_response_raw.v1"
|
||||
| "cultmesh.content_chunk_request.v1"
|
||||
| "cultmesh.content_chunk_response.v1"
|
||||
| "cultnet.error.v0",
|
||||
) => parse_raw_cultnet_schema_message(wire_value)?,
|
||||
_ => {
|
||||
|
|
@ -553,6 +575,8 @@ pub fn encode_cultnet_message_for_wire(
|
|||
CultNetMessage::DocumentPutRaw { .. }
|
||||
| CultNetMessage::SnapshotResponseRaw { .. }
|
||||
| CultNetMessage::SnapshotResponseRawV1 { .. }
|
||||
| CultNetMessage::ContentChunkRequest { .. }
|
||||
| CultNetMessage::ContentChunkResponse { .. }
|
||||
| CultNetMessage::Error { .. } => encode_raw_cultnet_schema_message(message),
|
||||
_ => Ok(rmp_serde::from_slice(&rmp_serde::to_vec(
|
||||
&serde_json::to_value(message)?,
|
||||
|
|
@ -777,6 +801,10 @@ fn validate_message(message: &CultNetMessage) -> Result<()> {
|
|||
validate_shard_descriptor(shard)?;
|
||||
}
|
||||
}
|
||||
// The reference's serializer accepts any content chunk message. It refuses a bad request in
|
||||
// its handler and answers `found: false` (`answer_content_chunk_request`); a bad response is
|
||||
// refused by `fetch_content`'s verification. Refusing either here would leave a peer in silence.
|
||||
CultNetMessage::ContentChunkRequest { .. } | CultNetMessage::ContentChunkResponse { .. } => {}
|
||||
CultNetMessage::OperationRequest {
|
||||
message_id,
|
||||
service_id,
|
||||
|
|
@ -1044,6 +1072,23 @@ fn parse_raw_cultnet_schema_message(input: &rmpv::Value) -> Result<CultNetMessag
|
|||
)?,
|
||||
})
|
||||
}
|
||||
"cultmesh.content_chunk_request.v1" => Ok(CultNetMessage::ContentChunkRequest {
|
||||
message_id: legacy_string_or_nil(get("messageId"), "messageId")?,
|
||||
chunk_hash: legacy_string_or_nil(get("chunkHash"), "chunkHash")?,
|
||||
record_key: legacy_string_or_nil(get("recordKey"), "recordKey")?,
|
||||
expected_size_bytes: require_legacy_i32(get("expectedSizeBytes"), "expectedSizeBytes")?,
|
||||
}),
|
||||
"cultmesh.content_chunk_response.v1" => Ok(CultNetMessage::ContentChunkResponse {
|
||||
message_id: require_legacy_string(get("messageId"), "messageId")?,
|
||||
found: require_legacy_bool(get("found"), "found")?,
|
||||
chunk_hash: require_legacy_string(get("chunkHash"), "chunkHash")?,
|
||||
size_bytes: require_legacy_i32(get("sizeBytes"), "sizeBytes")?,
|
||||
payload: get("payload")
|
||||
.and_then(rmpv::Value::as_slice)
|
||||
.ok_or_else(|| anyhow!("payload must be binary data"))?
|
||||
.to_vec(),
|
||||
error: require_legacy_string(get("error"), "error")?,
|
||||
}),
|
||||
"cultnet.error.v0" => {
|
||||
let code = get("code")
|
||||
.filter(|value| !value.is_nil())
|
||||
|
|
@ -1209,6 +1254,43 @@ fn encode_raw_cultnet_schema_message(message: &CultNetMessage) -> Result<rmpv::V
|
|||
.unwrap_or(rmpv::Value::Nil),
|
||||
),
|
||||
],
|
||||
CultNetMessage::ContentChunkRequest {
|
||||
message_id,
|
||||
chunk_hash,
|
||||
record_key,
|
||||
expected_size_bytes,
|
||||
} => vec![
|
||||
(
|
||||
rmpv::Value::from("schemaVersion"),
|
||||
rmpv::Value::from("cultmesh.content_chunk_request.v1"),
|
||||
),
|
||||
(rmpv::Value::from("messageId"), rmpv::Value::from(message_id.as_str())),
|
||||
(rmpv::Value::from("chunkHash"), rmpv::Value::from(chunk_hash.as_str())),
|
||||
(rmpv::Value::from("recordKey"), rmpv::Value::from(record_key.as_str())),
|
||||
(
|
||||
rmpv::Value::from("expectedSizeBytes"),
|
||||
rmpv::Value::from(*expected_size_bytes),
|
||||
),
|
||||
],
|
||||
CultNetMessage::ContentChunkResponse {
|
||||
message_id,
|
||||
found,
|
||||
chunk_hash,
|
||||
size_bytes,
|
||||
payload,
|
||||
error,
|
||||
} => vec![
|
||||
(
|
||||
rmpv::Value::from("schemaVersion"),
|
||||
rmpv::Value::from("cultmesh.content_chunk_response.v1"),
|
||||
),
|
||||
(rmpv::Value::from("messageId"), rmpv::Value::from(message_id.as_str())),
|
||||
(rmpv::Value::from("found"), rmpv::Value::from(*found)),
|
||||
(rmpv::Value::from("chunkHash"), rmpv::Value::from(chunk_hash.as_str())),
|
||||
(rmpv::Value::from("sizeBytes"), rmpv::Value::from(*size_bytes)),
|
||||
(rmpv::Value::from("payload"), rmpv::Value::Binary(payload.clone())),
|
||||
(rmpv::Value::from("error"), rmpv::Value::from(error.as_str())),
|
||||
],
|
||||
// R-N: matches the landed C# `CultNetErrorMessage`
|
||||
// (src/GameCult.Networking/CultNetSchemaMessages.cs) key for key and in its declared
|
||||
// order - schemaVersion, error, routingHint, code, details - with every optional key
|
||||
|
|
@ -1422,6 +1504,13 @@ fn require_legacy_u32(value: Option<&rmpv::Value>, field_name: &str) -> Result<u
|
|||
u32::try_from(number).map_err(|_| anyhow!("{field_name} does not fit in u32"))
|
||||
}
|
||||
|
||||
fn require_legacy_i32(value: Option<&rmpv::Value>, field_name: &str) -> Result<i32> {
|
||||
let number = value
|
||||
.and_then(rmpv::Value::as_i64)
|
||||
.ok_or_else(|| anyhow!("{field_name} must be an integer"))?;
|
||||
i32::try_from(number).map_err(|_| anyhow!("{field_name} does not fit in i32"))
|
||||
}
|
||||
|
||||
fn require_legacy_u64(value: Option<&rmpv::Value>, field_name: &str) -> Result<u64> {
|
||||
value
|
||||
.and_then(rmpv::Value::as_u64)
|
||||
|
|
@ -1632,6 +1721,15 @@ fn require_legacy_bytes(value: Option<&rmpv::Value>, field_name: &str) -> Result
|
|||
Ok(URL_SAFE_NO_PAD.encode(bytes))
|
||||
}
|
||||
|
||||
/// A string the reference reads back as null when its writer set it to null (msgpack nil), which its
|
||||
/// handlers treat as blank. Only content chunk requests use it: they are answered, never dropped.
|
||||
fn legacy_string_or_nil(value: Option<&rmpv::Value>, field_name: &str) -> Result<String> {
|
||||
match value {
|
||||
Some(rmpv::Value::Nil) => Ok(String::new()),
|
||||
other => require_legacy_string(other, field_name),
|
||||
}
|
||||
}
|
||||
|
||||
fn require_legacy_string(value: Option<&rmpv::Value>, field_name: &str) -> Result<String> {
|
||||
let value = value
|
||||
.and_then(rmpv::Value::as_str)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
mod content;
|
||||
mod contracts;
|
||||
mod idunn_deployment_brake;
|
||||
mod idunn_lifecycle_brake;
|
||||
|
|
@ -19,6 +20,7 @@ mod selection;
|
|||
mod shard_catalog;
|
||||
mod transport;
|
||||
|
||||
pub use content::*;
|
||||
pub use contracts::*;
|
||||
pub use idunn_deployment_brake::*;
|
||||
pub use idunn_lifecycle_brake::*;
|
||||
|
|
|
|||
806
packages/cultnet-rs/tests/content.rs
Normal file
806
packages/cultnet-rs/tests/content.rs
Normal file
|
|
@ -0,0 +1,806 @@
|
|||
//! The content plane (`src/content.rs`, the two chunk messages in `src/contracts.rs`), pinned against
|
||||
//! the C# reference. `contracts/cultmesh/content-vectors.cs-written.json` is the reference's own
|
||||
//! bytes (written by `CultMeshContentVectorTests`, including the answers `CultMeshLegacyRudpContentServer`
|
||||
//! gives); `content-vectors.rs-written.json` is this binary's mirror, regenerated with
|
||||
//! `CULTNET_WRITE_VECTORS=1`, and judged by the C# side.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
use base64::Engine;
|
||||
use cultnet_rs::{
|
||||
CultMeshCdnArtifactManifest, CultNetMessage, CultNetWireContract, answer_content_chunk_request,
|
||||
decode_cultnet_message_from_slice, encode_cultnet_message_for_wire, encode_cultnet_message_to_vec,
|
||||
fetch_content, normalize_hash, pack_content, validate_manifest,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const CHUNK: usize = 262_144;
|
||||
const BODY_LEN: usize = 1_315_551;
|
||||
const CREATED: &str = "2026-09-22T00:00:00.0000000+00:00";
|
||||
const SCHEMA_V0: CultNetWireContract = CultNetWireContract::CultNetSchemaV0;
|
||||
|
||||
fn body() -> Vec<u8> {
|
||||
(0..BODY_LEN).map(|i| (i % 251) as u8).collect()
|
||||
}
|
||||
|
||||
fn pack(bytes: &[u8]) -> (CultMeshCdnArtifactManifest, Vec<cultnet_rs::CultMeshCdnChunk>) {
|
||||
pack_content(
|
||||
"eureka/content-vector",
|
||||
"asset",
|
||||
"1",
|
||||
"application/x-cultnet-vector",
|
||||
CREATED,
|
||||
bytes,
|
||||
CHUNK,
|
||||
)
|
||||
.expect("packs")
|
||||
}
|
||||
|
||||
fn store(chunks: &[cultnet_rs::CultMeshCdnChunk]) -> HashMap<String, Vec<u8>> {
|
||||
chunks
|
||||
.iter()
|
||||
.map(|chunk| (chunk.chunk_hash.clone(), chunk.payload.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn lookup_in<'a>(store: &'a HashMap<String, Vec<u8>>) -> impl Fn(&str) -> Option<&'a [u8]> {
|
||||
move |hash| store.get(hash).map(Vec::as_slice)
|
||||
}
|
||||
|
||||
fn wire(message: &CultNetMessage) -> Vec<u8> {
|
||||
encode_cultnet_message_to_vec(message, SCHEMA_V0).expect("encodes")
|
||||
}
|
||||
|
||||
fn unwire(bytes: &[u8]) -> Result<CultNetMessage> {
|
||||
decode_cultnet_message_from_slice(bytes, SCHEMA_V0)
|
||||
}
|
||||
|
||||
fn manifest_bytes(manifest: &CultMeshCdnArtifactManifest) -> Vec<u8> {
|
||||
rmp_serde::to_vec_named(manifest).expect("encodes")
|
||||
}
|
||||
|
||||
fn decode_manifest(bytes: &[u8]) -> CultMeshCdnArtifactManifest {
|
||||
rmp_serde::from_slice(bytes).expect("decodes")
|
||||
}
|
||||
|
||||
/// The manifest's bytes with its metadata map replaced by `pairs`, written in exactly that order
|
||||
/// (and with duplicates, if given): what a peer that is not this crate can put on the wire.
|
||||
fn manifest_with_metadata(manifest: &CultMeshCdnArtifactManifest, pairs: &[(&str, &str)]) -> Vec<u8> {
|
||||
let encoded = manifest_bytes(manifest);
|
||||
let mut value = rmpv::decode::read_value(&mut encoded.as_slice()).expect("a msgpack value");
|
||||
let rmpv::Value::Array(fields) = &mut value else {
|
||||
panic!("the manifest is a positional array");
|
||||
};
|
||||
fields[9] = rmpv::Value::Map(pairs.iter().map(|(k, v)| (rmpv::Value::from(*k), rmpv::Value::from(*v))).collect());
|
||||
let mut out = Vec::new();
|
||||
rmpv::encode::write_value(&mut out, &value).expect("writes");
|
||||
out
|
||||
}
|
||||
|
||||
fn request(id: &str, hash: &str, key: &str, size: i32) -> CultNetMessage {
|
||||
CultNetMessage::ContentChunkRequest {
|
||||
message_id: id.to_string(),
|
||||
chunk_hash: hash.to_string(),
|
||||
record_key: key.to_string(),
|
||||
expected_size_bytes: size,
|
||||
}
|
||||
}
|
||||
|
||||
fn response_of(message: &CultNetMessage) -> (&str, bool, &str, &[u8], &str) {
|
||||
match message {
|
||||
CultNetMessage::ContentChunkResponse {
|
||||
message_id,
|
||||
found,
|
||||
chunk_hash,
|
||||
payload,
|
||||
error,
|
||||
..
|
||||
} => (message_id, *found, chunk_hash, payload, error),
|
||||
other => panic!("not a content chunk response: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// A transport closure that answers from `chunks` through the wire, counting calls.
|
||||
fn serving<'a>(
|
||||
chunks: &'a HashMap<String, Vec<u8>>,
|
||||
calls: &'a std::cell::Cell<usize>,
|
||||
) -> impl FnMut(CultNetMessage) -> Result<CultNetMessage> + 'a {
|
||||
move |message| {
|
||||
calls.set(calls.get() + 1);
|
||||
let request = unwire(&wire(&message))?;
|
||||
unwire(&wire(&answer_content_chunk_request(&request, lookup_in(chunks))))
|
||||
}
|
||||
}
|
||||
|
||||
fn flip_last_digit(hash: &str) -> String {
|
||||
let last = if hash.ends_with('0') { '1' } else { '0' };
|
||||
format!("{}{last}", &hash[..hash.len() - 1])
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// Vector files
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct VectorFile {
|
||||
vectors: Vec<VectorEntry>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct VectorEntry {
|
||||
label: String,
|
||||
kind: String,
|
||||
#[serde(rename = "messagePackBase64")]
|
||||
message_pack_base64: String,
|
||||
}
|
||||
|
||||
struct Vector {
|
||||
label: String,
|
||||
kind: &'static str,
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
fn vector_path(name: &str) -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("..")
|
||||
.join("..")
|
||||
.join("contracts")
|
||||
.join("cultmesh")
|
||||
.join(name)
|
||||
}
|
||||
|
||||
fn read_vectors(name: &str) -> Vec<(String, String, Vec<u8>)> {
|
||||
let path = vector_path(name);
|
||||
let text = std::fs::read_to_string(&path).unwrap_or_else(|_| {
|
||||
panic!("{} is missing - write it first (CULTNET_WRITE_VECTORS=1)", path.display())
|
||||
});
|
||||
let file: VectorFile = serde_json::from_str(&text).expect("vector file parses");
|
||||
assert!(!file.vectors.is_empty(), "the vector file must carry at least one vector");
|
||||
file.vectors
|
||||
.into_iter()
|
||||
.map(|v| {
|
||||
let bytes = base64::engine::general_purpose::STANDARD
|
||||
.decode(&v.message_pack_base64)
|
||||
.expect("base64");
|
||||
(v.label, v.kind, bytes)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The vectors this runtime writes: the same labelled set the C# writer produces, built from Rust
|
||||
/// values and answered by Rust's `answer_content_chunk_request`.
|
||||
fn local_vectors() -> Vec<Vector> {
|
||||
let (manifest, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let last = chunks.last().expect("chunks");
|
||||
let last_size = last.payload.len() as i32;
|
||||
let key = format!("mesh:cdn:chunk:{}", last.chunk_hash);
|
||||
let mut vectors = Vec::new();
|
||||
let mut push_message = |label: String, kind: &'static str, message: &CultNetMessage| {
|
||||
vectors.push(Vector { label, kind, bytes: wire(message) });
|
||||
};
|
||||
let answers = [
|
||||
("answer_found", request("vector-found", &format!("SHA256:{}", last.chunk_hash), &key, last_size)),
|
||||
("answer_not_found", request("vector-missing", &"0".repeat(64), "", 100)),
|
||||
(
|
||||
"answer_record_key_disagrees",
|
||||
request("vector-key", &last.chunk_hash, "mesh:cdn:chunk:other", last_size),
|
||||
),
|
||||
("answer_size_mismatch", request("vector-size", &last.chunk_hash, &key, last_size - 1)),
|
||||
("answer_blank_message_id", request("", &last.chunk_hash, &key, last_size)),
|
||||
("answer_whitespace_message_id", request(" ", &last.chunk_hash, &key, last_size)),
|
||||
("answer_blank_hash", request("vector-blank-hash", "", "", 100)),
|
||||
("answer_whitespace_hash", request("vector-space-hash", " ", "", 100)),
|
||||
("answer_prefix_only_hash", request("vector-prefix-hash", "sha256: ", "", 100)),
|
||||
("answer_negative_size", request("vector-negative", &last.chunk_hash, &key, -1)),
|
||||
(
|
||||
"answer_record_key_case",
|
||||
request("vector-key-case", &last.chunk_hash, &key.to_uppercase(), last_size),
|
||||
),
|
||||
("answer_blank_record_key", request("vector-key-blank", &last.chunk_hash, " ", last_size)),
|
||||
(
|
||||
"answer_double_prefix",
|
||||
request("vector-double", &format!("sha256:sha256:{}", last.chunk_hash), "", last_size),
|
||||
),
|
||||
];
|
||||
for (name, request) in &answers {
|
||||
push_message(format!("{name}.request"), "request", request);
|
||||
let answer = answer_content_chunk_request(request, lookup_in(&store));
|
||||
push_message(format!("{name}.response"), "response", &answer);
|
||||
}
|
||||
push_message(
|
||||
"response_hash_last_digit".to_string(),
|
||||
"response",
|
||||
&CultNetMessage::ContentChunkResponse {
|
||||
message_id: "vector-digit".to_string(),
|
||||
found: true,
|
||||
chunk_hash: flip_last_digit(&last.chunk_hash),
|
||||
size_bytes: last_size,
|
||||
payload: last.payload.clone(),
|
||||
error: String::new(),
|
||||
},
|
||||
);
|
||||
let mut push_manifest = |label: &str, manifest: &CultMeshCdnArtifactManifest| {
|
||||
vectors.push(Vector {
|
||||
label: label.to_string(),
|
||||
kind: "manifest",
|
||||
bytes: manifest_bytes(manifest),
|
||||
});
|
||||
};
|
||||
push_manifest("manifest", &manifest);
|
||||
let mut shuffled = manifest.clone();
|
||||
shuffled.chunks = [3, 0, 5, 1, 4, 2].iter().map(|&i| manifest.chunks[i].clone()).collect();
|
||||
push_manifest("manifest_out_of_order", &shuffled);
|
||||
let tagged = decode_manifest(&manifest_with_metadata(&manifest, &[("zeta", "1"), ("alpha", "2"), ("mid", "3")]));
|
||||
push_manifest("manifest_metadata_order", &tagged);
|
||||
push_manifest("manifest_empty_body", &pack(&[]).0);
|
||||
vectors
|
||||
}
|
||||
|
||||
fn to_json(vectors: &[Vector]) -> String {
|
||||
let file = VectorFile {
|
||||
vectors: vectors
|
||||
.iter()
|
||||
.map(|v| VectorEntry {
|
||||
label: v.label.clone(),
|
||||
kind: v.kind.to_string(),
|
||||
message_pack_base64: base64::engine::general_purpose::STANDARD.encode(&v.bytes),
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
serde_json::to_string_pretty(&file).expect("serializes")
|
||||
}
|
||||
|
||||
/// The committed rs-written file is what this binary encodes now. Under `CULTNET_WRITE_VECTORS=1`
|
||||
/// it is rewritten instead, for the C# test `RustVectorsDecodeInTheReference` to judge.
|
||||
#[test]
|
||||
fn rust_written_vectors_are_current() {
|
||||
let fresh = local_vectors();
|
||||
let path = vector_path("content-vectors.rs-written.json");
|
||||
if std::env::var("CULTNET_WRITE_VECTORS").as_deref() == Ok("1") {
|
||||
std::fs::write(&path, to_json(&fresh)).expect("writes the vector file");
|
||||
return;
|
||||
}
|
||||
let committed = read_vectors("content-vectors.rs-written.json");
|
||||
assert_eq!(committed.len(), fresh.len());
|
||||
for ((label, kind, bytes), fresh) in committed.iter().zip(&fresh) {
|
||||
assert_eq!((label.as_str(), kind.as_str()), (fresh.label.as_str(), fresh.kind));
|
||||
assert_eq!(bytes, &fresh.bytes, "vector '{label}' drifted from this binary's encode");
|
||||
}
|
||||
}
|
||||
|
||||
/// Byte identity, both ways, against the reference's own bytes: every vector the reference wrote
|
||||
/// decodes here and this runtime's independently built value encodes to exactly those bytes. The
|
||||
/// manifest vector also pins `pack_content` (same body and options, same manifest, byte for byte).
|
||||
#[test]
|
||||
fn reference_vectors_encode_byte_identically() {
|
||||
let local = local_vectors();
|
||||
// The nil-field vectors are bytes only the reference writes; `nil_fields_are_answered_as_blanks` judges them.
|
||||
let reference: Vec<_> = read_vectors("content-vectors.cs-written.json")
|
||||
.into_iter()
|
||||
.filter(|(label, _, _)| !label.starts_with("nil_"))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
reference.iter().map(|(l, _, _)| l.as_str()).collect::<Vec<_>>(),
|
||||
local.iter().map(|v| v.label.as_str()).collect::<Vec<_>>(),
|
||||
"both runtimes write the same labelled vectors"
|
||||
);
|
||||
for ((label, kind, reference_bytes), mine) in reference.iter().zip(&local) {
|
||||
assert_eq!(reference_bytes, &mine.bytes, "vector '{label}' is not byte identical");
|
||||
match kind.as_str() {
|
||||
"request" | "response" => {
|
||||
let decoded = unwire(reference_bytes).expect("the reference's bytes decode");
|
||||
assert_eq!(&wire(&decoded), reference_bytes, "vector '{label}' does not re-encode identically");
|
||||
}
|
||||
"manifest" => {
|
||||
let decoded: CultMeshCdnArtifactManifest =
|
||||
rmp_serde::from_slice(reference_bytes).expect("the reference's manifest decodes");
|
||||
assert_eq!(&manifest_bytes(&decoded), reference_bytes, "manifest '{label}' does not re-encode identically");
|
||||
}
|
||||
other => panic!("unknown vector kind {other}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// F1's failure mode: a chunk response carries `bin`, which the JSON-value path cannot carry.
|
||||
#[test]
|
||||
fn a_chunk_response_carries_bin_through_the_raw_path() {
|
||||
let payload: Vec<u8> = (0..=255u8).collect();
|
||||
let message = CultNetMessage::ContentChunkResponse {
|
||||
message_id: "m".to_string(),
|
||||
found: true,
|
||||
chunk_hash: "ab".to_string(),
|
||||
size_bytes: 256,
|
||||
payload: payload.clone(),
|
||||
error: String::new(),
|
||||
};
|
||||
let wire_value = encode_cultnet_message_for_wire(&message, SCHEMA_V0).expect("encodes");
|
||||
let entries = wire_value.as_map().expect("a map");
|
||||
let (_, value) = entries.iter().find(|(k, _)| k.as_str() == Some("payload")).expect("payload key");
|
||||
assert!(matches!(value, rmpv::Value::Binary(bytes) if *bytes == payload), "payload must be bin");
|
||||
assert_eq!(unwire(&wire(&message)).expect("decodes"), message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_hash_matches_the_reference() {
|
||||
for (input, expected) in [
|
||||
("SHA256:ABCdef", "abcdef"),
|
||||
("sha256:abc", "abc"),
|
||||
("sha256: ABC ", "abc"),
|
||||
(" ABC ", "abc"),
|
||||
(" sha256:abc", "sha256:abc"),
|
||||
("sha256:", ""),
|
||||
("plain", "plain"),
|
||||
] {
|
||||
assert_eq!(normalize_hash(input).expect("normalizes"), expected, "input {input:?}");
|
||||
}
|
||||
for blank in ["", " "] {
|
||||
assert!(normalize_hash(blank).is_err(), "{blank:?} must be refused");
|
||||
}
|
||||
// The fixture's uppercase-prefix request names the same chunk as its bare hash.
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
let (_, _, bytes) = reference.iter().find(|(l, _, _)| l == "answer_found.request").expect("vector");
|
||||
let CultNetMessage::ContentChunkRequest { chunk_hash, .. } = unwire(bytes).expect("decodes") else {
|
||||
panic!("not a request");
|
||||
};
|
||||
assert!(chunk_hash.starts_with("SHA256:"));
|
||||
let (_, chunks) = pack(&body());
|
||||
assert_eq!(normalize_hash(&chunk_hash).expect("normalizes"), chunks.last().unwrap().chunk_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_manifest_out_of_offset_order_is_accepted_as_the_reference_accepts_it() {
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
let (_, _, bytes) = reference.iter().find(|(l, _, _)| l == "manifest_out_of_order").expect("vector");
|
||||
let manifest: CultMeshCdnArtifactManifest = rmp_serde::from_slice(bytes).expect("decodes");
|
||||
assert_ne!(manifest.chunks[0].offset, 0, "the fixture's chunk list is shuffled");
|
||||
validate_manifest(&manifest).expect("the reference accepts it");
|
||||
let (_, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let calls = std::cell::Cell::new(0);
|
||||
let fetched = fetch_content(&manifest, BODY_LEN as u64, serving(&store, &calls)).expect("fetches");
|
||||
assert_eq!(fetched, body());
|
||||
assert_eq!(calls.get(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hostile_manifest_is_refused_before_any_request() {
|
||||
let (good, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let mut hostile = Vec::new();
|
||||
let mut negative = good.clone();
|
||||
negative.size_bytes = -1;
|
||||
hostile.push(negative);
|
||||
let mut gap = good.clone();
|
||||
gap.chunks[2].offset += 1;
|
||||
hostile.push(gap);
|
||||
let mut overlap = good.clone();
|
||||
overlap.chunks[3].offset -= 1;
|
||||
hostile.push(overlap);
|
||||
let mut short = good.clone();
|
||||
short.size_bytes -= 1;
|
||||
hostile.push(short);
|
||||
let mut negative_chunk = good.clone();
|
||||
negative_chunk.chunks[5].size_bytes = -4831;
|
||||
hostile.push(negative_chunk);
|
||||
for manifest in hostile {
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert!(fetch_content(&manifest, u64::MAX, serving(&store, &calls)).is_err());
|
||||
assert_eq!(calls.get(), 0, "a corrupt manifest must not make the client ask");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_manifest_over_the_cap_is_refused_before_any_request() {
|
||||
let (manifest, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let size = BODY_LEN as u64;
|
||||
|
||||
let calls = std::cell::Cell::new(0);
|
||||
let fetched = fetch_content(&manifest, size, serving(&store, &calls)).expect("exactly the cap is accepted");
|
||||
assert_eq!(fetched.len(), BODY_LEN);
|
||||
assert_eq!(calls.get(), 6);
|
||||
|
||||
// One byte over, and sizes where doubling the cap would admit the body.
|
||||
for cap in [size - 1, size / 2 + 1, size / 2, 0] {
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert!(fetch_content(&manifest, cap, serving(&store, &calls)).is_err(), "cap {cap} must refuse");
|
||||
assert_eq!(calls.get(), 0, "cap {cap} must refuse before any request");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chunk_whose_hash_differs_in_the_last_digit_is_refused() {
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
let (_, _, bytes) = reference.iter().find(|(l, _, _)| l == "response_hash_last_digit").expect("vector");
|
||||
let CultNetMessage::ContentChunkResponse { chunk_hash: claimed, payload, .. } = unwire(bytes).expect("decodes") else {
|
||||
panic!("not a response");
|
||||
};
|
||||
// A one-chunk manifest for exactly that payload: its bytes are right, only the response's claim is off.
|
||||
let (manifest, chunks) = pack(&payload);
|
||||
let honest = chunks[0].chunk_hash.clone();
|
||||
assert_ne!(claimed, honest);
|
||||
assert_eq!(claimed[..claimed.len() - 1], honest[..honest.len() - 1], "they differ in the last digit only");
|
||||
let error = fetch_content(&manifest, u64::MAX, |message| {
|
||||
let CultNetMessage::ContentChunkRequest { message_id, .. } = message else {
|
||||
panic!("not a request");
|
||||
};
|
||||
let CultNetMessage::ContentChunkResponse { found, size_bytes, payload, error, .. } =
|
||||
unwire(bytes).expect("decodes")
|
||||
else {
|
||||
panic!("not a response");
|
||||
};
|
||||
Ok(CultNetMessage::ContentChunkResponse {
|
||||
message_id,
|
||||
found,
|
||||
chunk_hash: claimed.clone(),
|
||||
size_bytes,
|
||||
payload,
|
||||
error,
|
||||
})
|
||||
})
|
||||
.expect_err("a hash off by one digit is a different hash");
|
||||
assert!(error.to_string().contains("does not match its payload"), "{error}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_body_whose_chunks_verify_but_whole_hash_does_not_is_refused() {
|
||||
let (mut manifest, chunks) = pack(&body());
|
||||
manifest.content_hash = pack(b"another body").0.content_hash;
|
||||
let store = store(&chunks);
|
||||
let calls = std::cell::Cell::new(0);
|
||||
let error = fetch_content(&manifest, u64::MAX, serving(&store, &calls)).expect_err("whole body must verify");
|
||||
assert_eq!(calls.get(), 6, "every chunk verified; only the whole body is wrong");
|
||||
assert!(error.to_string().contains("content hash"), "{error}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_response_for_another_message_id_is_refused() {
|
||||
let (manifest, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let error = fetch_content(&manifest, u64::MAX, |message| {
|
||||
let mut answer = answer_content_chunk_request(&message, lookup_in(&store));
|
||||
if let CultNetMessage::ContentChunkResponse { message_id, .. } = &mut answer {
|
||||
*message_id = "someone-else".to_string();
|
||||
}
|
||||
Ok(answer)
|
||||
})
|
||||
.expect_err("an answer to another request is not this answer");
|
||||
assert!(error.to_string().contains("someone-else"), "{error}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_body_packs_one_zero_length_chunk_and_fetches() {
|
||||
let (manifest, chunks) = pack(&[]);
|
||||
assert_eq!((manifest.chunks.len(), chunks.len(), manifest.size_bytes), (1, 1, 0));
|
||||
let store = store(&chunks);
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert_eq!(fetch_content(&manifest, 0, serving(&store, &calls)).expect("fetches"), Vec::<u8>::new());
|
||||
assert_eq!(calls.get(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answer_refuses_a_record_key_that_disagrees_with_its_hash() {
|
||||
let (_, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let last = chunks.last().unwrap();
|
||||
let asked = std::cell::Cell::new(false);
|
||||
let answer = answer_content_chunk_request(
|
||||
&request("m", &last.chunk_hash, "mesh:cdn:chunk:other", last.payload.len() as i32),
|
||||
|hash| {
|
||||
asked.set(true);
|
||||
lookup_in(&store)(hash)
|
||||
},
|
||||
);
|
||||
let (id, found, _, payload, error) = response_of(&answer);
|
||||
assert_eq!((id, found, payload.len()), ("m", false, 0));
|
||||
assert_eq!(error, "InvalidDataException: Content chunk record key disagrees with its content hash.");
|
||||
assert!(!asked.get(), "the key check precedes the lookup, as in HandleAsync");
|
||||
}
|
||||
|
||||
/// Every failure is an answer (`found: false`, empty payload, the reference's spelling) and equals,
|
||||
/// byte for byte, what `CultMeshLegacyRudpContentServer` answered to the same request bytes.
|
||||
#[test]
|
||||
fn answer_serves_found_false_with_the_reference_error_spelling() {
|
||||
let (_, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
let mut judged = 0;
|
||||
for (label, _, request_bytes) in reference.iter().filter(|(l, _, _)| l.ends_with(".request")) {
|
||||
let name = label.trim_end_matches(".request");
|
||||
let request = unwire(request_bytes).expect("decodes");
|
||||
let answer = answer_content_chunk_request(&request, lookup_in(&store));
|
||||
let (_, _, expected_bytes) = reference
|
||||
.iter()
|
||||
.find(|(l, _, _)| *l == format!("{name}.response"))
|
||||
.expect("paired response");
|
||||
assert_eq!(&wire(&answer), expected_bytes, "{name}: answer differs from the reference's");
|
||||
let (_, found, _, payload, error) = response_of(&answer);
|
||||
if ["answer_found", "answer_blank_record_key", "answer_double_prefix"].contains(&name) {
|
||||
assert!(found && error.is_empty() && !payload.is_empty());
|
||||
} else {
|
||||
assert!(!found && payload.is_empty(), "{name}");
|
||||
assert!(error.contains("Exception: "), "{name}: {error}");
|
||||
}
|
||||
judged += 1;
|
||||
}
|
||||
assert_eq!(judged, 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gamecult_networking_contract_refuses_content_messages() {
|
||||
let legacy = CultNetWireContract::GameCultNetworkingV0;
|
||||
let message = request("m", "abc", "", 1);
|
||||
assert!(encode_cultnet_message_for_wire(&message, legacy).is_err());
|
||||
assert!(decode_cultnet_message_from_slice(&wire(&message), legacy).is_err());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// BP-1 fixes
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
fn chunk_ref(hash: &str, offset: i64, size: i32) -> cultnet_rs::CultMeshCdnChunkRef {
|
||||
cultnet_rs::CultMeshCdnChunkRef {
|
||||
chunk_hash: hash.to_string(),
|
||||
offset,
|
||||
size_bytes: size,
|
||||
record_key: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A manifest with the given chunk list and stated size, everything else from a real pack.
|
||||
fn manifest_of(size: i64, chunks: Vec<cultnet_rs::CultMeshCdnChunkRef>) -> CultMeshCdnArtifactManifest {
|
||||
let mut manifest = pack(&[]).0;
|
||||
manifest.size_bytes = size;
|
||||
manifest.chunks = chunks;
|
||||
manifest
|
||||
}
|
||||
|
||||
// F1: a zero-size chunk is legal only as an empty body's single chunk.
|
||||
#[test]
|
||||
fn a_zero_size_chunk_flood_is_refused_before_any_request() {
|
||||
let (empty, empty_chunks) = pack(&[]);
|
||||
let empty_store = store(&empty_chunks);
|
||||
let flood = manifest_of(0, (0..100_000).map(|_| chunk_ref(&empty.content_hash, 0, 0)).collect());
|
||||
validate_manifest(&flood).expect("the shared shape rule accepts it, as the reference does");
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert!(fetch_content(&flood, u64::MAX, serving(&empty_store, &calls)).is_err());
|
||||
assert_eq!(calls.get(), 0, "100,000 empty chunks must not become 100,000 requests");
|
||||
|
||||
// A zero-size chunk beside real ones is refused too, so skipping it is never the client's job.
|
||||
let (good, chunks) = pack(&body());
|
||||
let body_store = store(&chunks);
|
||||
let mut zero_inside = good.clone();
|
||||
let boundary = zero_inside.chunks[2].offset;
|
||||
zero_inside.chunks.insert(2, chunk_ref(&empty.content_hash, boundary, 0));
|
||||
validate_manifest(&zero_inside).expect("shape-valid");
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert!(fetch_content(&zero_inside, u64::MAX, serving(&body_store, &calls)).is_err());
|
||||
assert_eq!(calls.get(), 0);
|
||||
|
||||
// An empty body still fetches, through its one zero-length chunk.
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert_eq!(fetch_content(&empty, 0, serving(&empty_store, &calls)).expect("fetches"), Vec::<u8>::new());
|
||||
assert_eq!(calls.get(), 1);
|
||||
}
|
||||
|
||||
// F2, F5: every bad request is answered, in the reference's validation order and spelling. The
|
||||
// byte-identity against the reference server's own answers is `answer_serves_found_false_...`.
|
||||
#[test]
|
||||
fn a_bad_request_is_answered_never_dropped() {
|
||||
let (_, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let last = chunks.last().unwrap();
|
||||
let size = last.payload.len() as i32;
|
||||
let size_mismatch = "InvalidDataException: CDN artifact chunk payload metadata does not match its manifest reference.";
|
||||
for (label, bad, spelling) in [
|
||||
(
|
||||
"blank id",
|
||||
request("", &last.chunk_hash, "", size),
|
||||
"InvalidDataException: Content chunk request requires a message identity.",
|
||||
),
|
||||
("blank hash", request("m", "", "", size), "ArgumentException: Hash must be non-empty. (Parameter 'ChunkHash')"),
|
||||
(
|
||||
"prefix-only hash",
|
||||
request("m", "sha256: ", "", size),
|
||||
"ArgumentException: Hash must be non-empty. (Parameter 'chunkHash')",
|
||||
),
|
||||
("negative size", request("m", &last.chunk_hash, "", -1), size_mismatch),
|
||||
] {
|
||||
let answer = answer_content_chunk_request(&bad, lookup_in(&store));
|
||||
let (_, found, _, payload, error) = response_of(&answer);
|
||||
assert!(!found && payload.is_empty(), "{label}");
|
||||
assert_eq!(error, spelling, "{label}");
|
||||
// And both travel: neither the request nor its answer is stopped at decode or encode.
|
||||
assert_eq!(unwire(&wire(&bad)).expect("the request decodes"), bad, "{label}");
|
||||
assert_eq!(unwire(&wire(&answer)).expect("the answer encodes and decodes"), answer, "{label}");
|
||||
}
|
||||
}
|
||||
|
||||
// A caller that leaves a string null writes msgpack nil. The reference reads null, treats it as blank
|
||||
// and answers; Rust decodes nil as blank so the same request gets the same answer, byte for byte.
|
||||
#[test]
|
||||
fn nil_fields_are_answered_as_blanks() {
|
||||
let (_, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
for (name, spelling) in [
|
||||
("nil_message_id", "InvalidDataException: Content chunk request requires a message identity."),
|
||||
("nil_hash", "ArgumentException: Hash must be non-empty. (Parameter 'ChunkHash')"),
|
||||
("nil_record_key", "FileNotFoundException: Content chunk is not available."),
|
||||
] {
|
||||
let find = |suffix: &str| {
|
||||
let label = format!("{name}.{suffix}");
|
||||
reference.iter().find(|(l, _, _)| *l == label).expect("vector").2.clone()
|
||||
};
|
||||
let request_bytes = find("request");
|
||||
assert!(request_bytes.contains(&0xc0), "{name}: the reference wrote nil");
|
||||
let answer = answer_content_chunk_request(&unwire(&request_bytes).expect("nil decodes"), lookup_in(&store));
|
||||
assert_eq!(wire(&answer), find("response"), "{name}: answer differs from the reference's");
|
||||
assert_eq!(response_of(&answer).4, spelling, "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
// F3: metadata keeps the order it was read in and refuses a duplicate key.
|
||||
#[test]
|
||||
fn manifest_metadata_keeps_wire_order_and_refuses_a_duplicate_key() {
|
||||
let reference = read_vectors("content-vectors.cs-written.json");
|
||||
let (_, _, bytes) = reference.iter().find(|(l, _, _)| l == "manifest_metadata_order").expect("vector");
|
||||
let decoded = decode_manifest(bytes);
|
||||
assert_eq!(&manifest_bytes(&decoded), bytes, "zeta, alpha, mid re-encode as zeta, alpha, mid");
|
||||
|
||||
let base = pack(&body()).0;
|
||||
let duplicated = manifest_with_metadata(&base, &[("k", "1"), ("other", "2"), ("k", "3")]);
|
||||
assert!(rmp_serde::from_slice::<CultMeshCdnArtifactManifest>(&duplicated).is_err());
|
||||
let unique = manifest_with_metadata(&base, &[("k", "1"), ("other", "2")]);
|
||||
assert_eq!(manifest_bytes(&decode_manifest(&unique)), unique);
|
||||
|
||||
// Metadata that is not a map is refused, and says what was expected.
|
||||
let mut value = rmpv::decode::read_value(&mut manifest_bytes(&base).as_slice()).expect("a msgpack value");
|
||||
let rmpv::Value::Array(fields) = &mut value else {
|
||||
panic!("the manifest is a positional array");
|
||||
};
|
||||
fields[9] = rmpv::Value::from(5);
|
||||
let mut not_a_map = Vec::new();
|
||||
rmpv::encode::write_value(&mut not_a_map, &value).expect("writes");
|
||||
let error = rmp_serde::from_slice::<CultMeshCdnArtifactManifest>(¬_a_map).expect_err("not a map");
|
||||
assert!(error.to_string().contains("a map of strings"), "{error}");
|
||||
}
|
||||
|
||||
// F4: hashes are settled before the first request.
|
||||
#[test]
|
||||
fn a_blank_hash_is_refused_before_any_request() {
|
||||
let (good, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let mut blank_content = good.clone();
|
||||
blank_content.content_hash = " ".to_string();
|
||||
let mut prefix_content = good.clone();
|
||||
prefix_content.content_hash = "sha256: ".to_string();
|
||||
let mut blank_chunk = good.clone();
|
||||
blank_chunk.chunks[3].chunk_hash = " ".to_string();
|
||||
let mut prefix_chunk = good.clone();
|
||||
prefix_chunk.chunks[3].chunk_hash = "SHA256: ".to_string();
|
||||
for (label, manifest) in [
|
||||
("blank content hash", blank_content),
|
||||
("prefix-only content hash", prefix_content),
|
||||
("blank chunk hash at 3", blank_chunk),
|
||||
("prefix-only chunk hash at 3", prefix_chunk),
|
||||
] {
|
||||
let calls = std::cell::Cell::new(0);
|
||||
assert!(fetch_content(&manifest, u64::MAX, serving(&store, &calls)).is_err(), "{label}");
|
||||
assert_eq!(calls.get(), 0, "{label}: refused before any request");
|
||||
}
|
||||
}
|
||||
|
||||
// F6: `pack_content` refuses a chunk size the reference's int cannot hold, without panicking.
|
||||
#[test]
|
||||
fn pack_content_refuses_an_unusable_chunk_size() {
|
||||
for size in [0usize, i32::MAX as usize + 1, usize::MAX] {
|
||||
let refused = std::panic::catch_unwind(|| pack_content("a", "", "", "", "", b"abc", size));
|
||||
assert!(matches!(refused, Ok(Err(_))), "chunk size {size} must be refused cleanly");
|
||||
}
|
||||
let (manifest, _) =
|
||||
pack_content("a", "", "", "", "", b"abc", i32::MAX as usize).expect("i32::MAX is a valid chunk size");
|
||||
assert_eq!(manifest.chunks.len(), 1);
|
||||
}
|
||||
|
||||
// F6: `PackArtifact` defaults a blank kind and mime type and otherwise uses the value untouched.
|
||||
#[test]
|
||||
fn pack_defaults_only_a_blank_kind_and_mime_type_and_never_trims() {
|
||||
let packed = |kind: &str, mime: &str| pack_content("a", kind, "v", mime, CREATED, b"abc", 2).expect("packs").0;
|
||||
for blank in ["", " ", "\t \n"] {
|
||||
let manifest = packed(blank, blank);
|
||||
assert_eq!(
|
||||
(manifest.kind.as_str(), manifest.mime_type.as_str()),
|
||||
("asset", "application/octet-stream"),
|
||||
"{blank:?}"
|
||||
);
|
||||
}
|
||||
let manifest = packed(" build ", " text/plain ");
|
||||
assert_eq!((manifest.kind.as_str(), manifest.mime_type.as_str()), (" build ", " text/plain "));
|
||||
}
|
||||
|
||||
/// The honest answer to `request`, taken apart so a test can lie in one field.
|
||||
fn honest(request: &CultNetMessage, store: &HashMap<String, Vec<u8>>) -> (String, bool, String, i32, Vec<u8>, String) {
|
||||
match answer_content_chunk_request(request, lookup_in(store)) {
|
||||
CultNetMessage::ContentChunkResponse { message_id, found, chunk_hash, size_bytes, payload, error } => {
|
||||
(message_id, found, chunk_hash, size_bytes, payload, error)
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
// F6: the two halves of the per-chunk check, each on its own.
|
||||
#[test]
|
||||
fn a_chunk_whose_length_or_claim_disagrees_with_the_manifest_is_refused() {
|
||||
let (manifest, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
// Each lie maps (honest size, honest payload) to (claimed size, payload served).
|
||||
type Lie = fn(i32, Vec<u8>) -> (i32, Vec<u8>);
|
||||
let lies: [(&str, Lie); 4] = [
|
||||
("a payload of the claimed length that is not the manifest's length", |size, mut p| {
|
||||
p.truncate(p.len() - 1);
|
||||
(size - 1, p)
|
||||
}),
|
||||
("a payload whose length is not its claimed size, the claim being the manifest's", |size, mut p| {
|
||||
p.truncate(p.len() - 1);
|
||||
(size, p)
|
||||
}),
|
||||
("the right payload under a wrong claimed size", |size, p| (size - 1, p)),
|
||||
("a longer payload of its claimed length", |size, mut p| {
|
||||
p.push(0);
|
||||
(size + 1, p)
|
||||
}),
|
||||
];
|
||||
for (label, lie) in lies {
|
||||
let error = fetch_content(&manifest, u64::MAX, |message| {
|
||||
let (message_id, found, chunk_hash, honest_size, payload, error) = honest(&message, &store);
|
||||
let (size_bytes, payload) = lie(honest_size, payload);
|
||||
Ok(CultNetMessage::ContentChunkResponse { message_id, found, chunk_hash, size_bytes, payload, error })
|
||||
})
|
||||
.expect_err(label);
|
||||
assert!(error.to_string().contains("payload metadata does not match"), "{label}: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
// F6: `[5@0, 0@0]` is not contiguous under the reference's stable sort by offset alone.
|
||||
#[test]
|
||||
fn equal_offsets_keep_their_manifest_order() {
|
||||
let hash = "0".repeat(64);
|
||||
let five_then_zero = manifest_of(5, vec![chunk_ref(&hash, 0, 5), chunk_ref(&hash, 0, 0)]);
|
||||
assert!(validate_manifest(&five_then_zero).is_err(), "the stable sort keeps 5@0 first, so 0@0 lands at offset 5");
|
||||
let zero_then_five = manifest_of(5, vec![chunk_ref(&hash, 0, 0), chunk_ref(&hash, 0, 5)]);
|
||||
validate_manifest(&zero_then_five).expect("0@0 first leaves 5@0 at offset 0");
|
||||
}
|
||||
|
||||
// F6: what goes on the wire is the normalized hash and the canonical record key.
|
||||
#[test]
|
||||
fn the_client_sends_the_normalized_hash_and_canonical_record_key() {
|
||||
let (mut manifest, chunks) = pack(&body());
|
||||
let store = store(&chunks);
|
||||
let honest_hashes: Vec<String> = manifest.chunks.iter().map(|c| c.chunk_hash.clone()).collect();
|
||||
manifest.content_hash = format!("SHA256:{}", manifest.content_hash.to_uppercase());
|
||||
for (index, chunk) in manifest.chunks.iter_mut().enumerate() {
|
||||
chunk.chunk_hash = format!("sha256:{}", chunk.chunk_hash.to_uppercase());
|
||||
// Alternate chunks name their key; the rest leave it blank for the client to derive.
|
||||
chunk.record_key =
|
||||
if index % 2 == 0 { format!("mesh:cdn:chunk:{}", honest_hashes[index]) } else { " ".to_string() };
|
||||
}
|
||||
let mut sent = Vec::new();
|
||||
let fetched = fetch_content(&manifest, u64::MAX, |message| {
|
||||
sent.push(message.clone());
|
||||
Ok(answer_content_chunk_request(&message, lookup_in(&store)))
|
||||
})
|
||||
.expect("fetches");
|
||||
assert_eq!(fetched, body());
|
||||
assert_eq!(sent.len(), honest_hashes.len());
|
||||
for (message, hash) in sent.iter().zip(&honest_hashes) {
|
||||
let CultNetMessage::ContentChunkRequest { chunk_hash, record_key, .. } = message else {
|
||||
panic!("not a request");
|
||||
};
|
||||
assert_eq!(chunk_hash, hash, "the normalized hash is what is sent");
|
||||
assert_eq!(record_key, &format!("mesh:cdn:chunk:{hash}"), "and the canonical key");
|
||||
}
|
||||
}
|
||||
|
|
@ -47,6 +47,14 @@ The content protocol uses one bounded response per manifest chunk. A snapshot
|
|||
response containing `gamecult.mesh.cdn_artifact_chunk.v1` payload records is a
|
||||
legacy transport path and must not be used for bulk delivery.
|
||||
|
||||
The Rust runtime speaks the same chunk messages through `cultnet-rs`
|
||||
`fetch_content`, which verifies every chunk and then the whole body, and
|
||||
`answer_content_chunk_request`, which mirrors the legacy server's validation
|
||||
order and error spelling. Its transport is an explicit closure over the session
|
||||
the caller already holds, carrying the chunks as ordinary schema frames until
|
||||
Rust has an authenticated content connector; the chunk bytes, manifest shape
|
||||
and verification do not change when that connector arrives.
|
||||
|
||||
The transfer owner may keep a bounded window of requests in flight per content
|
||||
hash (four by default, never more than 32). Candidate chunks can arrive out of
|
||||
order, but only the transfer owner writes them, and it flushes and checkpoints
|
||||
|
|
|
|||
|
|
@ -102,6 +102,14 @@ lower priority than TCP. Existing hot-state publishers that still package
|
|||
payloads as schema messages must move to the realtime connector contract; the
|
||||
new seam does not pretend that migration has already occurred.
|
||||
|
||||
The Rust runtime's content path is this compatibility path, chosen explicitly:
|
||||
`cultnet-rs` sends chunk requests and reads chunk responses on the session the
|
||||
client already holds, byte-identical on the wire to
|
||||
`CultMeshLegacyRudpContentServer` (`fetch_content` and
|
||||
`answer_content_chunk_request`). It is never an implicit default, and it lasts
|
||||
until Rust has an authenticated content connector (TCP+TLS or QUIC), which will
|
||||
sit behind the same `fetch_content` owner rather than beside it.
|
||||
|
||||
## Security and release gate
|
||||
|
||||
The current TCP control and content connectors are plaintext and therefore
|
||||
|
|
|
|||
284
tests/GameCult.Mesh.Tests/CultMeshContentVectorTests.cs
Normal file
284
tests/GameCult.Mesh.Tests/CultMeshContentVectorTests.cs
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using GameCult.Caching;
|
||||
using GameCult.Networking;
|
||||
using MessagePack;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Mesh.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Content-plane parity vectors shared with <c>packages/cultnet-rs/tests/content.rs</c>. Each runtime
|
||||
/// writes its own file under <c>contracts/cultmesh</c> and judges both. The chunk answers in these
|
||||
/// files are produced by <see cref="CultMeshLegacyRudpContentServer"/> itself, so the failure
|
||||
/// spellings are the reference's own bytes.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public sealed class CultMeshContentVectorTests
|
||||
{
|
||||
private const int BodyLength = 1_315_551;
|
||||
private const int ChunkSize = 262_144;
|
||||
private const string CreatedAtUtc = "2026-09-22T00:00:00.0000000+00:00";
|
||||
private const string ArtifactId = "eureka/content-vector";
|
||||
|
||||
private sealed record Vector(string Label, string Kind, byte[] Bytes);
|
||||
|
||||
[Test]
|
||||
public void WriteContentVectors()
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable("CULTNET_WRITE_VECTORS") != "1")
|
||||
Assert.Ignore("Set CULTNET_WRITE_VECTORS=1 to (re)write the committed vector file.");
|
||||
File.WriteAllText(VectorPath("content-vectors.cs-written.json"), ToJson(BuildVectors(out _, out _)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CommittedCsVectorsEqualAFreshEncode()
|
||||
{
|
||||
var fresh = BuildVectors(out _, out _);
|
||||
var committed = ReadVectors("content-vectors.cs-written.json");
|
||||
committed.Select(v => (v.Label, v.Kind)).Should().Equal(fresh.Select(v => (v.Label, v.Kind)));
|
||||
foreach (var (expected, actual) in fresh.Zip(committed))
|
||||
actual.Bytes.Should().Equal(expected.Bytes, $"vector '{actual.Label}' drifted from the reference's encode");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RustVectorsDecodeInTheReference()
|
||||
{
|
||||
BuildVectors(out var cache, out var messages);
|
||||
var rust = ReadVectors("content-vectors.rs-written.json");
|
||||
rust.Should().NotBeEmpty();
|
||||
rust.Select(v => v.Label).Should().Equal(messages.Keys, "both runtimes write the same labelled vectors");
|
||||
foreach (var vector in rust)
|
||||
{
|
||||
var expected = messages[vector.Label];
|
||||
switch (vector.Kind)
|
||||
{
|
||||
case "request":
|
||||
var request = MessagePackSerializer.Deserialize<CultMeshContentChunkRequestMessage>(vector.Bytes, CultNetSchemaMessageSerialization.Options);
|
||||
request.Should().BeEquivalentTo((CultMeshContentChunkRequestMessage)expected);
|
||||
Reencode(request).Should().Equal(vector.Bytes, vector.Label);
|
||||
break;
|
||||
case "response":
|
||||
var response = MessagePackSerializer.Deserialize<CultMeshContentChunkResponseMessage>(vector.Bytes, CultNetSchemaMessageSerialization.Options);
|
||||
response.Should().BeEquivalentTo((CultMeshContentChunkResponseMessage)expected);
|
||||
Reencode(response).Should().Equal(vector.Bytes, vector.Label);
|
||||
break;
|
||||
case "manifest":
|
||||
var manifest = MessagePackSerializer.Deserialize<CultMeshCdnArtifactManifest>(vector.Bytes, CultNetSchemaMessageSerialization.Options);
|
||||
manifest.Should().BeEquivalentTo((CultMeshCdnArtifactManifest)expected);
|
||||
Reencode(manifest).Should().Equal(vector.Bytes, vector.Label);
|
||||
break;
|
||||
default:
|
||||
Assert.Fail($"unknown vector kind '{vector.Kind}'");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Rust's answers to Rust's requests must be what the reference answers to the same bytes.
|
||||
foreach (var vector in rust.Where(v => v.Label.EndsWith(".request", StringComparison.Ordinal)))
|
||||
{
|
||||
var request = MessagePackSerializer.Deserialize<CultMeshContentChunkRequestMessage>(vector.Bytes, CultNetSchemaMessageSerialization.Options);
|
||||
var rustAnswer = rust.Single(v => v.Label == vector.Label[..^".request".Length] + ".response");
|
||||
Reencode(Answer(cache, request)).Should().Equal(rustAnswer.Bytes, vector.Label);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PackArtifactOfTheFixtureBodyEqualsTheFixtureManifest()
|
||||
{
|
||||
var artifact = CultMeshCdn.PackArtifact(ArtifactId, Body(), Options());
|
||||
var committed = ReadVectors("content-vectors.cs-written.json").Single(v => v.Label == "manifest");
|
||||
Reencode(artifact.Manifest).Should().Equal(committed.Bytes);
|
||||
var rust = ReadVectors("content-vectors.rs-written.json").Single(v => v.Label == "manifest");
|
||||
rust.Bytes.Should().Equal(committed.Bytes, "Rust's pack_content and the reference pack the same bytes to the same manifest");
|
||||
}
|
||||
|
||||
private static byte[] Body() =>
|
||||
Enumerable.Range(0, BodyLength).Select(i => (byte)(i % 251)).ToArray();
|
||||
|
||||
private static CultMeshCdnPackOptions Options() => new()
|
||||
{
|
||||
ChunkSizeBytes = ChunkSize,
|
||||
Version = "1",
|
||||
MimeType = "application/x-cultnet-vector",
|
||||
CreatedAtUtc = CreatedAtUtc
|
||||
};
|
||||
|
||||
private static byte[] Reencode(CultMeshContentChunkRequestMessage message) =>
|
||||
CultNetSchemaMessageSerialization.Serialize(message);
|
||||
|
||||
private static byte[] Reencode(CultMeshContentChunkResponseMessage message) =>
|
||||
CultNetSchemaMessageSerialization.Serialize(message);
|
||||
|
||||
private static byte[] Reencode(CultMeshCdnArtifactManifest manifest) =>
|
||||
MessagePackSerializer.Serialize(manifest, CultNetSchemaMessageSerialization.Options);
|
||||
|
||||
private static CultMeshContentChunkRequestMessage Request(string id, string hash, string key, int size) => new()
|
||||
{
|
||||
MessageId = id,
|
||||
ChunkHash = hash,
|
||||
RecordKey = key,
|
||||
ExpectedSizeBytes = size
|
||||
};
|
||||
|
||||
/// <summary>The cs-written file's vectors, in file order. Messages are keyed by label.</summary>
|
||||
private static List<Vector> BuildVectors(out CultCache cache, out Dictionary<string, object> messages)
|
||||
{
|
||||
var artifact = CultMeshCdn.PackArtifact(ArtifactId, Body(), Options());
|
||||
var content = new CultCache(CultMesh.CreateCultCacheDocumentRegistry(
|
||||
typeof(CultMeshCdnArtifactManifest), typeof(CultMeshCdnArtifactChunk)));
|
||||
CultMeshCdn.PublishAsync(content, artifact).GetAwaiter().GetResult();
|
||||
cache = content;
|
||||
|
||||
var last = artifact.Chunks[^1];
|
||||
var key = "mesh:cdn:chunk:" + last.ChunkHash;
|
||||
var lastDigit = last.ChunkHash[^1] == '0' ? '1' : '0';
|
||||
var vectors = new List<Vector>();
|
||||
var map = new Dictionary<string, object>(StringComparer.Ordinal);
|
||||
messages = map;
|
||||
|
||||
void AddRequest(string label, CultMeshContentChunkRequestMessage message)
|
||||
{
|
||||
map[label] = message;
|
||||
vectors.Add(new Vector(label, "request", Reencode(message)));
|
||||
}
|
||||
|
||||
void AddResponse(string label, CultMeshContentChunkResponseMessage message)
|
||||
{
|
||||
map[label] = message;
|
||||
vectors.Add(new Vector(label, "response", Reencode(message)));
|
||||
}
|
||||
|
||||
void AddAnswer(string name, CultMeshContentChunkRequestMessage request)
|
||||
{
|
||||
AddRequest(name + ".request", request);
|
||||
AddResponse(name + ".response", Answer(content, request));
|
||||
}
|
||||
|
||||
AddAnswer("answer_found", Request("vector-found", "SHA256:" + last.ChunkHash, key, last.SizeBytes));
|
||||
AddAnswer("answer_not_found", Request("vector-missing", new string('0', 64), string.Empty, 100));
|
||||
AddAnswer("answer_record_key_disagrees", Request("vector-key", last.ChunkHash, "mesh:cdn:chunk:other", last.SizeBytes));
|
||||
AddAnswer("answer_size_mismatch", Request("vector-size", last.ChunkHash, key, last.SizeBytes - 1));
|
||||
// Requests the reference refuses in its handler, never at deserialisation: each is answered.
|
||||
AddAnswer("answer_blank_message_id", Request(string.Empty, last.ChunkHash, key, last.SizeBytes));
|
||||
AddAnswer("answer_whitespace_message_id", Request(" ", last.ChunkHash, key, last.SizeBytes));
|
||||
AddAnswer("answer_blank_hash", Request("vector-blank-hash", string.Empty, string.Empty, 100));
|
||||
AddAnswer("answer_whitespace_hash", Request("vector-space-hash", " ", string.Empty, 100));
|
||||
AddAnswer("answer_prefix_only_hash", Request("vector-prefix-hash", "sha256: ", string.Empty, 100));
|
||||
AddAnswer("answer_negative_size", Request("vector-negative", last.ChunkHash, key, -1));
|
||||
AddAnswer("answer_record_key_case", Request("vector-key-case", last.ChunkHash, key.ToUpperInvariant(), last.SizeBytes));
|
||||
AddAnswer("answer_blank_record_key", Request("vector-key-blank", last.ChunkHash, " ", last.SizeBytes));
|
||||
AddAnswer("answer_double_prefix", Request("vector-double", "sha256:sha256:" + last.ChunkHash, string.Empty, last.SizeBytes));
|
||||
|
||||
// A caller that leaves a string null writes msgpack nil; the reference reads it back as null and
|
||||
// answers. These are judged by Rust decoding the reference's bytes (Rust never writes nil), so
|
||||
// they stay out of the label-matched mirror set.
|
||||
void AddNilAnswer(string name, CultMeshContentChunkRequestMessage request)
|
||||
{
|
||||
vectors.Add(new Vector(name + ".request", "request", Reencode(request)));
|
||||
vectors.Add(new Vector(name + ".response", "response", Reencode(Answer(content, request))));
|
||||
}
|
||||
|
||||
AddNilAnswer("nil_message_id", Request(null, last.ChunkHash, key, last.SizeBytes));
|
||||
AddNilAnswer("nil_hash", Request("vector-nil-hash", null, string.Empty, 100));
|
||||
AddNilAnswer("nil_record_key", Request("vector-nil-key", new string('0', 64), null, 100));
|
||||
AddResponse("response_hash_last_digit", new CultMeshContentChunkResponseMessage
|
||||
{
|
||||
MessageId = "vector-digit",
|
||||
Found = true,
|
||||
ChunkHash = last.ChunkHash[..^1] + lastDigit,
|
||||
SizeBytes = last.SizeBytes,
|
||||
Payload = last.Payload,
|
||||
Error = string.Empty
|
||||
});
|
||||
|
||||
void AddManifest(string label, CultMeshCdnArtifactManifest manifest)
|
||||
{
|
||||
map[label] = manifest;
|
||||
vectors.Add(new Vector(label, "manifest", Reencode(manifest)));
|
||||
}
|
||||
|
||||
AddManifest("manifest", artifact.Manifest);
|
||||
var shuffled = CultMeshCdn.PackArtifact(ArtifactId, Body(), Options()).Manifest;
|
||||
shuffled.Chunks = new[] { 3, 0, 5, 1, 4, 2 }.Select(i => shuffled.Chunks[i]).ToArray();
|
||||
AddManifest("manifest_out_of_order", shuffled);
|
||||
var tagged = CultMeshCdn.PackArtifact(ArtifactId, Body(), Options()).Manifest;
|
||||
tagged.Metadata = new Dictionary<string, string> { ["zeta"] = "1", ["alpha"] = "2", ["mid"] = "3" };
|
||||
AddManifest("manifest_metadata_order", tagged);
|
||||
AddManifest("manifest_empty_body", CultMeshCdn.PackArtifact(ArtifactId, Array.Empty<byte>(), Options()).Manifest);
|
||||
return vectors;
|
||||
}
|
||||
|
||||
private static CultMeshContentChunkResponseMessage Answer(CultCache cache, CultMeshContentChunkRequestMessage request)
|
||||
{
|
||||
var host = new ReferenceHost();
|
||||
using var server = new CultMeshLegacyRudpContentServer(host, cache);
|
||||
var peer = new ReferencePeer();
|
||||
host.Dispatch(request, peer).GetAwaiter().GetResult();
|
||||
return peer.Sent ?? throw new InvalidOperationException("The reference server answered with silence.");
|
||||
}
|
||||
|
||||
private sealed class ReferenceHost : ICultNetSchemaServer
|
||||
{
|
||||
private Func<CultMeshContentChunkRequestMessage, ICultNetSchemaServerPeer, Task>? _handler;
|
||||
|
||||
public void OnCultNet<TMessage>(Func<TMessage, ICultNetSchemaServerPeer, Task> callback)
|
||||
where TMessage : ICultNetSchemaMessage =>
|
||||
_handler = (Func<CultMeshContentChunkRequestMessage, ICultNetSchemaServerPeer, Task>)(object)callback;
|
||||
|
||||
public void RemoveCultNetMessageListener<TMessage>(Delegate callback)
|
||||
where TMessage : ICultNetSchemaMessage => _handler = null;
|
||||
|
||||
public Task Dispatch(CultMeshContentChunkRequestMessage request, ICultNetSchemaServerPeer peer) =>
|
||||
_handler!(request, peer);
|
||||
}
|
||||
|
||||
private sealed class ReferencePeer : ICultNetSchemaServerPeer
|
||||
{
|
||||
public CultMeshContentChunkResponseMessage? Sent { get; private set; }
|
||||
|
||||
public void SendCultNet<TMessage>(TMessage message) where TMessage : ICultNetSchemaMessage =>
|
||||
Sent = (CultMeshContentChunkResponseMessage)(object)message;
|
||||
}
|
||||
|
||||
private static string ToJson(List<Vector> vectors) => JsonSerializer.Serialize(
|
||||
new
|
||||
{
|
||||
vectors = vectors.Select(v => new
|
||||
{
|
||||
label = v.Label,
|
||||
kind = v.Kind,
|
||||
messagePackBase64 = Convert.ToBase64String(v.Bytes)
|
||||
})
|
||||
},
|
||||
new JsonSerializerOptions { WriteIndented = true });
|
||||
|
||||
private static List<Vector> ReadVectors(string name)
|
||||
{
|
||||
var path = VectorPath(name);
|
||||
if (!File.Exists(path))
|
||||
Assert.Inconclusive($"{path} is missing - write it first (CULTNET_WRITE_VECTORS=1).");
|
||||
using var document = JsonDocument.Parse(File.ReadAllText(path));
|
||||
return document.RootElement.GetProperty("vectors").EnumerateArray()
|
||||
.Select(v => new Vector(
|
||||
v.GetProperty("label").GetString()!,
|
||||
v.GetProperty("kind").GetString()!,
|
||||
Convert.FromBase64String(v.GetProperty("messagePackBase64").GetString()!)))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static string VectorPath(string name) => Path.Combine(RepoRoot(), "contracts", "cultmesh", name);
|
||||
|
||||
private static string RepoRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
|
||||
while (directory != null && !File.Exists(Path.Combine(directory.FullName, "CultLib.sln")))
|
||||
directory = directory.Parent;
|
||||
return directory?.FullName ?? throw new InvalidOperationException("CultLib.sln was not found above the test directory.");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue