tests first: repeated messageId key, each runtime's declared policy (C# failing)
Vectors admittedMessageIdDuplicates carry [invalid,valid], [valid,invalid], [valid,valid]. Decoders that see the duplicate (C#, Rust) must refuse all three; map-collapsing decoders (Python, TS, Kotlin) judge the last key. Python, TS, Kotlin and Rust already behave as declared; C# fails: it accepted [valid,valid]. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a97861945c
commit
cc5a9cec9b
6 changed files with 94 additions and 2 deletions
|
|
@ -606,6 +606,31 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"admittedMessageIdDuplicates": {
|
||||
"semantics": "A frame with more than one messageId key is malformed. A decoder that can see the duplicate (C#, Rust: raw MessagePack reader or ordered pairs) refuses it, whatever the values (`seesDuplicate`). A decoder that collapses the map (Python, TypeScript, Kotlin) judges the value it ends up with, the last key, and refuses exactly when that value is invalid (`lastKeyWins`); where it accepts it echoes the last key's id (`lastKeyWinsEcho`). Neither behaviour ever echoes an invalid id. Each runtime declares which policy it has and its runner asserts that policy's column, so a change of a decoder's key policy fails a named test. The first key is ZWSP (U+200B, Default_Ignorable) in `invalid_then_valid`; the second key is the last entry of the map. Frames are MessagePack hex.",
|
||||
"cases": [
|
||||
{
|
||||
"name": "invalid_then_valid",
|
||||
"wireHex": "86AD736368656D6156657273696F6EB963756C746E65742E77726974655F61646D69747465642E7630A96D6573736167654964A3E2808BA96F7065726174696F6EA3707574A8736368656D614964A76E6F74652E7630A97265636F72644B6579A66E6F74653A31A96D6573736167654964A161",
|
||||
"seesDuplicate": "refuse",
|
||||
"lastKeyWins": "accept",
|
||||
"lastKeyWinsEcho": "a"
|
||||
},
|
||||
{
|
||||
"name": "valid_then_invalid",
|
||||
"wireHex": "86AD736368656D6156657273696F6EB963756C746E65742E77726974655F61646D69747465642E7630A96D6573736167654964A161A96F7065726174696F6EA3707574A8736368656D614964A76E6F74652E7630A97265636F72644B6579A66E6F74653A31A96D6573736167654964A3E2808B",
|
||||
"seesDuplicate": "refuse",
|
||||
"lastKeyWins": "refuse"
|
||||
},
|
||||
{
|
||||
"name": "valid_then_valid",
|
||||
"wireHex": "86AD736368656D6156657273696F6EB963756C746E65742E77726974655F61646D69747465642E7630A96D6573736167654964A161A96F7065726174696F6EA3707574A8736368656D614964A76E6F74652E7630A97265636F72644B6579A66E6F74653A31A96D6573736167654964A162",
|
||||
"seesDuplicate": "refuse",
|
||||
"lastKeyWins": "accept",
|
||||
"lastKeyWinsEcho": "b"
|
||||
}
|
||||
]
|
||||
},
|
||||
"schemaInvalid": [
|
||||
{
|
||||
"name": "write_admitted_with_an_error_key",
|
||||
|
|
@ -1640,7 +1665,8 @@
|
|||
"schemaInvalid": 7,
|
||||
"contentHashCases": 4,
|
||||
"scenarioCases": 25,
|
||||
"admittedMessageIdCases": 59
|
||||
"admittedMessageIdCases": 59,
|
||||
"admittedMessageIdDuplicateCases": 3
|
||||
},
|
||||
"contentHash": {
|
||||
"definition": "sha256(msgpack([operation, schemaId, recordKey, payload])), payload a msgpack bin for a put and nil for a delete; hex lowercase",
|
||||
|
|
|
|||
|
|
@ -100,6 +100,20 @@ fun replayWriteReceiptVectors(path: String): Boolean {
|
|||
if (text.isNotEmpty()) emptyList() else listOf("decoded to an empty string")
|
||||
}
|
||||
|
||||
// admittedMessageIdDuplicates: Kotlin decodes the frame to a map, so it cannot see a repeated key: it judges the last one.
|
||||
for (case in list(obj(vectors["admittedMessageIdDuplicates"])["cases"])) {
|
||||
val c = obj(case)
|
||||
check("admittedMessageIdDuplicates/${c["name"]}") {
|
||||
val decoded = try { decodeReceiptFrame(parseCultNetMessage(unhex(c["wireHex"] as String))) } catch (_: IOException) { null }
|
||||
when {
|
||||
c["lastKeyWins"] == "refuse" -> if (decoded == null) emptyList() else listOf("accepted a frame whose last messageId key is invalid")
|
||||
decoded == null -> listOf("refused a frame whose last messageId key is valid")
|
||||
decoded.toWireMap()["messageId"] != c["lastKeyWinsEcho"] -> listOf("echoed ${decoded.toWireMap()["messageId"]}, wanted ${c["lastKeyWinsEcho"]}")
|
||||
else -> emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (case in list(vectors["schemaInvalid"])) {
|
||||
val c = obj(case)
|
||||
check("schemaInvalid/${c["name"]}") {
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class WriteReceiptVectorTests(unittest.TestCase):
|
|||
def test_vector_sections_are_the_ones_this_file_accounts_for(self) -> None:
|
||||
self.assertEqual(
|
||||
set(_VECTORS) - {"provenance", "specification"},
|
||||
{"constants", "contentHash", "dedupe", "wire", "blankFields", "schemaInvalid", "scenarios", "counts", "admittedMessageId"},
|
||||
{"constants", "contentHash", "dedupe", "wire", "blankFields", "schemaInvalid", "scenarios", "counts", "admittedMessageId", "admittedMessageIdDuplicates"},
|
||||
)
|
||||
self.assertEqual(len(_VECTORS["contentHash"]["cases"]), 4)
|
||||
self.assertEqual(len(_VECTORS["wire"]), 7)
|
||||
|
|
@ -94,6 +94,18 @@ class WriteReceiptVectorTests(unittest.TestCase):
|
|||
failures.append(f"{case['name']}: expected {case['expect']}")
|
||||
self.assertEqual(failures, [])
|
||||
|
||||
def test_a_repeated_message_id_key_follows_the_last_key_wins_policy(self) -> None:
|
||||
# Python decodes the frame to a dict, so it cannot see a duplicate: it judges the last key (admittedMessageIdDuplicates).
|
||||
section = _VECTORS["admittedMessageIdDuplicates"]
|
||||
self.assertEqual(_VECTORS["counts"]["admittedMessageIdDuplicateCases"], len(section["cases"]))
|
||||
for case in section["cases"]:
|
||||
raw = bytes.fromhex(case["wireHex"])
|
||||
if case["lastKeyWins"] == "refuse":
|
||||
with self.assertRaises(ValueError, msg=case["name"]):
|
||||
parse_message(raw)
|
||||
else:
|
||||
self.assertEqual(parse_message(raw).body["messageId"], case["lastKeyWinsEcho"], case["name"])
|
||||
|
||||
def test_every_range_of_the_unicode_tables_is_pinned_at_both_ends(self) -> None:
|
||||
# write-admitted-message-id-ranges.json is the generated, independent copy: for each letter-or-number range its first and
|
||||
# last point are accepted and the points either side refused; for each Default_Ignorable range "a" beside its first and
|
||||
|
|
|
|||
|
|
@ -238,3 +238,17 @@ fn every_range_of_the_unicode_tables_is_pinned_at_both_ends() {
|
|||
}
|
||||
assert!(failures.is_empty(), "{} failing:\n{}", failures.len(), failures.join("\n"));
|
||||
}
|
||||
|
||||
/// Vectors `admittedMessageIdDuplicates`: Rust decodes a map it can see into, so a repeated messageId key is refused whatever the values.
|
||||
#[test]
|
||||
fn a_repeated_message_id_key_is_refused_whatever_the_values() {
|
||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../../contracts/cultnet/interop/write-receipt-vectors.json");
|
||||
let vectors: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
|
||||
let cases = vectors["admittedMessageIdDuplicates"]["cases"].as_array().unwrap();
|
||||
assert_eq!(vectors["counts"]["admittedMessageIdDuplicateCases"].as_u64().unwrap() as usize, cases.len());
|
||||
for case in cases {
|
||||
assert_eq!(case["seesDuplicate"], "refuse");
|
||||
let decoded = decode_cultnet_message_from_slice(&unhex(case["wireHex"].as_str().unwrap()), CultNetWireContract::CultNetSchemaV0);
|
||||
assert!(decoded.is_err(), "{}: a repeated messageId key must be refused", case["name"]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -323,3 +323,15 @@ test("write_admitted messageId: every range of the Unicode 16.0 tables is pinned
|
|||
}
|
||||
assert.deepEqual(failures, []);
|
||||
});
|
||||
|
||||
// TypeScript decodes the frame to an object, so it cannot see a duplicate key: it judges the last one (admittedMessageIdDuplicates).
|
||||
test("write_admitted messageId: a repeated key follows the last-key-wins policy", () => {
|
||||
const vectors = JSON.parse(readFileSync(join(__dirname, "..", "..", "..", "..", "contracts", "cultnet", "interop", "write-receipt-vectors.json"), "utf8"));
|
||||
const section = vectors.admittedMessageIdDuplicates;
|
||||
assert.equal(vectors.counts.admittedMessageIdDuplicateCases, section.cases.length);
|
||||
for (const vector of section.cases as { name: string; wireHex: string; lastKeyWins: "accept" | "refuse"; lastKeyWinsEcho?: string }[]) {
|
||||
const frame = decode(Uint8Array.from(Buffer.from(vector.wireHex, "hex")));
|
||||
if (vector.lastKeyWins === "refuse") assert.throws(() => parseCultNetMessage(frame), /./s, vector.name);
|
||||
else assert.equal((parseCultNetMessage(frame) as CultNetWriteAdmittedMessage).messageId, vector.lastKeyWinsEcho, vector.name);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -87,5 +87,19 @@ namespace GameCult.Networking.Tests
|
|||
if (Accepted("a" + char.ConvertFromUtf32(point)) != want) failures.Add($"a + U+{point:X} should be {(want ? "accepted" : "refused")}");
|
||||
Assert.That(failures, Is.Empty);
|
||||
}
|
||||
|
||||
// admittedMessageIdDuplicates: C# reads the raw map, so it sees a repeated messageId key and refuses it whatever the values.
|
||||
[Test]
|
||||
public void ARepeatedMessageIdKeyIsRefusedWhateverTheValues()
|
||||
{
|
||||
var cases = Vectors().GetProperty("admittedMessageIdDuplicates").GetProperty("cases").EnumerateArray().ToArray();
|
||||
Assert.That(cases.Length, Is.EqualTo(Vectors().GetProperty("counts").GetProperty("admittedMessageIdDuplicateCases").GetInt32()));
|
||||
foreach (var vector in cases)
|
||||
{
|
||||
Assert.That(vector.GetProperty("seesDuplicate").GetString(), Is.EqualTo("refuse"));
|
||||
Assert.That(() => CultNetSchemaMessageSerialization.Deserialize(Convert.FromHexString(vector.GetProperty("wireHex").GetString()!)),
|
||||
Throws.Exception, vector.GetProperty("name").GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue