From 51158216ca2302c49b7580eeb927eaab076b00f7 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 11 Nov 2026 21:11:30 +0100 Subject: [PATCH] [protocol] F20: ids, tokens and bodies with one wire spelling each; parsers hammered by property tests --- Cargo.lock | 1019 +++++++++++++++++++++++++++++++++ crates/protocol/Cargo.toml | 7 + crates/protocol/src/lib.rs | 331 ++++++++++- crates/protocol/tests/wire.rs | 186 ++++++ 4 files changed, 1541 insertions(+), 2 deletions(-) create mode 100644 crates/protocol/tests/wire.rs diff --git a/Cargo.lock b/Cargo.lock index ff4c686..a4ca4ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,802 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bit-set" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72503af1e72be411d74ca45cd6b309878fcb35354e252fc2a3a8bda3a97082a0" +dependencies = [ + "borsh", + "serde", +] + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "borsh" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a" +dependencies = [ + "borsh-derive", + "bytes", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cdfe656708a01f89b451a7d36466e6fe6c414de0aa18fc54f864f6f9ca9f56" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core", +] + +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "rand_core", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "bytes", + "http", + "http-body", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libsqlite3-sys" +version = "0.39.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14a5f01b87b92705d07b433c06f96cbc764637d81a0bffb5734ce17cf0f9a2b3" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "mime" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1" + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "proc-macro-crate" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proptest" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags", + "chacha20", + "core_detect", + "num-traits", + "rand", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_xorshift" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" +dependencies = [ + "rand_core", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rsqlite-vfs" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353b74f9810d5b085b6c42c9d86a5f0fdb795553235cdd48753b32e3809d18a7" +dependencies = [ + "thiserror", +] + +[[package]] +name = "rusqlite" +version = "0.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da9bba6f3506135e120a6586df05be18b48d18896600646a4bec8f993d91a594" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "sqlite-wasm-rs" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cd504cecbfe7eef649988840f9a6cb768d11085efec138f8e99f160632d867" +dependencies = [ + "cc", + "rsqlite-vfs", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + [[package]] name = "tabellarius-client" version = "0.0.0" @@ -12,10 +808,233 @@ dependencies = [ [[package]] name = "tabellarius-protocol" version = "0.0.0" +dependencies = [ + "base64", + "proptest", + "serde", + "serde_json", + "sha2", +] [[package]] name = "tabellarius-relay" version = "0.0.0" dependencies = [ + "axum", + "getrandom 0.3.4", + "http-body-util", + "rusqlite", + "serde", + "serde_json", + "subtle", "tabellarius-protocol", + "tempfile", + "tokio", + "tower", ] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio" +version = "1.54.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" +dependencies = [ + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "toml_datetime" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.25.19+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd43688c4858d7264ae1cf9a9f874a67c3feb74a883b1cd7bebd6a4ea8307218" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "winnow", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "winnow" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" +dependencies = [ + "memchr", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/crates/protocol/Cargo.toml b/crates/protocol/Cargo.toml index 813ad60..01b9813 100644 --- a/crates/protocol/Cargo.toml +++ b/crates/protocol/Cargo.toml @@ -7,6 +7,13 @@ edition.workspace = true publish.workspace = true [dependencies] +base64 = "0.22" +serde = { version = "1", features = ["derive"] } +sha2 = "0.10" + +[dev-dependencies] +proptest = "1.11" +serde_json = "1" [lints] workspace = true diff --git a/crates/protocol/src/lib.rs b/crates/protocol/src/lib.rs index 38795cb..6af4536 100644 --- a/crates/protocol/src/lib.rs +++ b/crates/protocol/src/lib.rs @@ -1,2 +1,329 @@ -//! Wire types for the Tabellarius protocol (`docs/PROTOCOL.md`). -//! Empty until the protocol review is approved. +//! Wire types for the Tabellarius protocol v0 (`docs/PROTOCOL.md`, approved 2026-11-08). +//! +//! No I/O. Every value that crosses the wire has exactly one spelling: ids and tokens are unpadded +//! base64url of a fixed number of bytes, and parsing refuses anything else with a [`ParseError`]. +//! The HTTP routes that carry these types are listed in `.agents/features/relay.md` §2. + +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use base64::Engine as _; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use sha2::{Digest, Sha256}; +use std::borrow::Cow; +use std::fmt; + +/// The protocol version, as it appears in every path (`/v0/...`). +pub const VERSION: &str = "v0"; +/// The largest blob (one MLS message) the relay accepts. +pub const MAX_BLOB_BYTES: usize = 256 * 1024; +/// The largest KeyPackage an invite may carry. +pub const MAX_KEY_PACKAGE_BYTES: usize = 16 * 1024; +/// At most this many blobs come back from one fetch; the rest wait for the next. +pub const MAX_BLOBS_PER_FETCH: usize = 100; +/// A blob not acknowledged is deleted after this many seconds (30 days, `PROTOCOL.md` §5). +pub const BLOB_TTL_SECS: i64 = 30 * 24 * 3600; +/// An unclaimed invite expires after this many seconds (7 days, `PROTOCOL.md` §4). +pub const INVITE_TTL_SECS: i64 = 7 * 24 * 3600; + +/// Why a value from the wire was refused. Carries no part of the input. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ParseError { + /// Not unpadded base64url, or not its one canonical spelling. + Encoding, + /// Decoded to the wrong number of bytes. + Length { expected: usize, got: usize }, + /// The bearer header is missing its `Bearer ` prefix. + Scheme, + /// More bytes than the protocol allows. + TooLarge { max: usize }, +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ParseError::Encoding => write!(f, "not canonical unpadded base64url"), + ParseError::Length { expected, got } => { + write!(f, "expected {expected} bytes, got {got}") + } + ParseError::Scheme => write!(f, "expected 'Bearer '"), + ParseError::TooLarge { max } => write!(f, "more than {max} bytes"), + } + } +} + +impl std::error::Error for ParseError {} + +fn is_base64url(text: &str) -> bool { + text.bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') +} + +/// Decodes canonical unpadded base64url: refuses other alphabets, padding, and spellings whose +/// unused trailing bits are not zero (those would give one value a second spelling). +fn decode_canonical(text: &str) -> Result, ParseError> { + if !is_base64url(text) { + return Err(ParseError::Encoding); + } + let bytes = URL_SAFE_NO_PAD + .decode(text) + .map_err(|_| ParseError::Encoding)?; + if URL_SAFE_NO_PAD.encode(&bytes) != text { + return Err(ParseError::Encoding); + } + Ok(bytes) +} + +fn decode_exact(text: &str) -> Result<[u8; N], ParseError> { + // A cheap bound first: anything longer than N bytes' worth of characters is refused unread. + if text.len() > N.div_ceil(3) * 4 { + return Err(ParseError::Length { + expected: N, + got: text.len() / 4 * 3, + }); + } + let bytes = decode_canonical(text)?; + let got = bytes.len(); + bytes + .try_into() + .map_err(|_| ParseError::Length { expected: N, got }) +} + +macro_rules! fixed_bytes { + ($(#[$doc:meta])* $name:ident, $len:expr) => { + $(#[$doc])* + #[derive(Clone, Copy, PartialEq, Eq, Hash)] + pub struct $name([u8; $len]); + + impl $name { + pub const LEN: usize = $len; + + pub fn from_bytes(bytes: [u8; $len]) -> Self { + Self(bytes) + } + + pub fn as_bytes(&self) -> &[u8; $len] { + &self.0 + } + + /// Parses the wire spelling: unpadded base64url of exactly `LEN` bytes. + pub fn parse(text: &str) -> Result { + decode_exact::<$len>(text).map(Self) + } + + pub fn to_wire(&self) -> String { + URL_SAFE_NO_PAD.encode(self.0) + } + } + + impl Serialize for $name { + fn serialize(&self, s: S) -> Result { + s.serialize_str(&self.to_wire()) + } + } + + impl<'de> Deserialize<'de> for $name { + fn deserialize>(d: D) -> Result { + let text = Cow::<'de, str>::deserialize(d)?; + Self::parse(&text).map_err(serde::de::Error::custom) + } + } + }; +} + +fixed_bytes!( + /// A mailbox: 16 random bytes made by its owner. One per contact (`PROTOCOL.md` §3). + MailboxId, + 16 +); +fixed_bytes!( + /// An invite: 16 random bytes made by the inviter (`PROTOCOL.md` §4). + InviteId, + 16 +); +fixed_bytes!( + /// SHA-256 of a token, or of a blob when it is acknowledged. The relay stores token hashes only. + Sha256Hash, + 32 +); +fixed_bytes!( + /// A capability: a post token, a fetch token or an invite's one-time secret. 32 random bytes. + /// Never stored by the relay; only its [`Sha256Hash`] is. + Token, + 32 +); + +impl Token { + /// The hash the relay stores and compares against. + pub fn hash(&self) -> Sha256Hash { + Sha256Hash(Sha256::digest(self.0).into()) + } + + /// Parses an `Authorization` header value: `Bearer `. + pub fn from_bearer(header: &str) -> Result { + let token = header.strip_prefix("Bearer ").ok_or(ParseError::Scheme)?; + Self::parse(token) + } + + pub fn to_bearer(&self) -> String { + format!("Bearer {}", self.to_wire()) + } +} + +// Tokens are secrets: never printed, not even in a debug log. +impl fmt::Debug for Token { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Token(..)") + } +} + +macro_rules! debug_as_wire { + ($($name:ident),*) => {$( + impl fmt::Debug for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}({})", stringify!($name), self.to_wire()) + } + } + )*}; +} +debug_as_wire!(MailboxId, InviteId, Sha256Hash); + +/// The SHA-256 of a blob: how its recipient acknowledges it. +pub fn blob_hash(blob: &[u8]) -> Sha256Hash { + Sha256Hash(Sha256::digest(blob).into()) +} + +/// Bytes inside JSON as unpadded base64url, with an upper bound checked before decoding. +#[derive(Clone, PartialEq, Eq)] +pub struct WireBytes(pub Vec); + +impl WireBytes { + pub fn parse(text: &str, max: usize) -> Result { + // 4 characters carry 3 bytes: anything longer cannot fit, and is refused before decoding. + if text.len() > max.div_ceil(3) * 4 { + return Err(ParseError::TooLarge { max }); + } + let bytes = decode_canonical(text)?; + if bytes.len() > max { + return Err(ParseError::TooLarge { max }); + } + Ok(Self(bytes)) + } +} + +impl fmt::Debug for WireBytes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "WireBytes({} bytes)", self.0.len()) + } +} + +impl Serialize for WireBytes { + fn serialize(&self, s: S) -> Result { + s.serialize_str(&URL_SAFE_NO_PAD.encode(&self.0)) + } +} + +macro_rules! bounded_bytes { + ($(#[$doc:meta])* $name:ident, $max:expr) => { + $(#[$doc])* + #[derive(Clone, PartialEq, Eq, Debug, Serialize)] + #[serde(transparent)] + pub struct $name(pub WireBytes); + + impl $name { + pub fn new(bytes: Vec) -> Result { + if bytes.len() > $max { + return Err(ParseError::TooLarge { max: $max }); + } + Ok(Self(WireBytes(bytes))) + } + + pub fn bytes(&self) -> &[u8] { + &self.0 .0 + } + } + + impl<'de> Deserialize<'de> for $name { + fn deserialize>(d: D) -> Result { + let text = Cow::<'de, str>::deserialize(d)?; + WireBytes::parse(&text, $max) + .map(Self) + .map_err(serde::de::Error::custom) + } + } + }; +} + +bounded_bytes!( + /// A KeyPackage inside an invite, at most [`MAX_KEY_PACKAGE_BYTES`]. + KeyPackageBytes, + MAX_KEY_PACKAGE_BYTES +); +bounded_bytes!( + /// A blob inside a fetch response, at most [`MAX_BLOB_BYTES`]. + BlobBytes, + MAX_BLOB_BYTES +); + +/// `POST /v0/mailboxes`. The owner sends hashes, so the relay never sees the tokens themselves here. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct CreateMailbox { + pub id: MailboxId, + pub post_token_hash: Sha256Hash, + pub fetch_token_hash: Sha256Hash, +} + +/// `PUT /v0/mailboxes/{id}/post-token`: blocking someone = giving everyone else a new post token. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct RotatePostToken { + pub post_token_hash: Sha256Hash, +} + +/// One waiting blob in `GET /v0/mailboxes/{id}/blobs`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct FetchedBlob { + pub blob: BlobBytes, + /// Unix time in hours, rounded down: all the relay keeps of when it arrived. + pub received_hour: i64, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct FetchedBlobs { + pub blobs: Vec, +} + +/// `POST /v0/invites`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct CreateInvite { + pub id: InviteId, + pub secret_hash: Sha256Hash, + pub key_package: KeyPackageBytes, +} + +/// `GET /v0/invites/{id}`: the inviter's KeyPackage, handed out once. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct ClaimedInvite { + pub key_package: KeyPackageBytes, +} + +/// `GET /version`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct VersionInfo { + pub protocol: String, + pub relay: String, +} + +/// Rounds unix seconds down to the hour, as the relay stores arrival times. +pub fn hour_of(unix_secs: i64) -> i64 { + unix_secs.div_euclid(3600) +} + +/// Rounds unix seconds down to the day, as the relay stores when a mailbox was made. +pub fn day_of(unix_secs: i64) -> i64 { + unix_secs.div_euclid(86_400) +} diff --git a/crates/protocol/tests/wire.rs b/crates/protocol/tests/wire.rs new file mode 100644 index 0000000..05e946b --- /dev/null +++ b/crates/protocol/tests/wire.rs @@ -0,0 +1,186 @@ +//! The wire spelling: one value, one spelling, and no input that panics a parser. + +use proptest::prelude::*; +use tabellarius_protocol::*; + +#[test] +fn ids_and_tokens_round_trip_through_their_wire_spelling() { + let id = MailboxId::from_bytes([7; 16]); + assert_eq!(id.to_wire().len(), 22); + assert_eq!(MailboxId::parse(&id.to_wire()), Ok(id)); + let token = Token::from_bytes([9; 32]); + assert_eq!(token.to_wire().len(), 43); + assert_eq!(Token::from_bearer(&token.to_bearer()), Ok(token)); +} + +#[test] +fn a_wrong_length_alphabet_padding_or_spelling_is_refused() { + let good = MailboxId::from_bytes([0xAB; 16]).to_wire(); + // one character short, one too many + assert!(MailboxId::parse(&good[..21]).is_err()); + assert!(MailboxId::parse(&format!("{good}A")).is_err()); + // the standard alphabet and padding are not this protocol's + assert_eq!( + MailboxId::parse(&good.replace('-', "+").replace('_', "/").replace('q', "+")), + Err(ParseError::Encoding) + ); + assert_eq!( + MailboxId::parse(&format!("{good}==")), + Err(ParseError::Encoding) + ); + // a Token is not a MailboxId + assert!(matches!( + MailboxId::parse(&Token::from_bytes([1; 32]).to_wire()), + Err(ParseError::Length { expected: 16, .. }) + )); + assert_eq!( + MailboxId::parse(""), + Err(ParseError::Length { + expected: 16, + got: 0 + }) + ); +} + +#[test] +fn a_second_spelling_of_the_same_bytes_is_refused() { + // 16 bytes leave 4 unused bits in the last character. 'A' (000000) is canonical for a zero + // tail; 'B' (000001) decodes to the same bytes with a non-zero unused bit. + let canonical = MailboxId::from_bytes([0; 16]).to_wire(); + assert!(canonical.ends_with('A')); + let other = format!("{}B", &canonical[..21]); + assert_eq!(MailboxId::parse(&other), Err(ParseError::Encoding)); +} + +#[test] +fn a_bearer_header_needs_its_scheme() { + let token = Token::from_bytes([3; 32]); + assert_eq!( + Token::from_bearer(&token.to_wire()), + Err(ParseError::Scheme) + ); + assert_eq!( + Token::from_bearer(&format!("bearer {}", token.to_wire())), + Err(ParseError::Scheme) + ); + assert!(Token::from_bearer(&format!("Bearer {}", token.to_wire())).is_err()); +} + +#[test] +fn a_token_is_never_printed() { + let token = Token::from_bytes([0x5A; 32]); + let shown = format!("{token:?}"); + assert_eq!(shown, "Token(..)"); + assert!(!shown.contains(&token.to_wire())); +} + +#[test] +fn the_hash_of_a_token_is_its_sha256() { + // SHA-256 of 32 zero bytes, a published value. + let hash = Token::from_bytes([0; 32]).hash(); + let hex: String = hash.as_bytes().iter().map(|b| format!("{b:02x}")).collect(); + assert_eq!( + hex, + "66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925" + ); +} + +#[test] +fn json_bodies_refuse_unknown_fields_and_bad_values() { + let ok = serde_json::json!({ + "id": MailboxId::from_bytes([1; 16]).to_wire(), + "postTokenHash": Sha256Hash::from_bytes([2; 32]).to_wire(), + "fetchTokenHash": Sha256Hash::from_bytes([3; 32]).to_wire(), + }); + let parsed: CreateMailbox = serde_json::from_value(ok.clone()).expect("valid body"); + assert_eq!(parsed.id, MailboxId::from_bytes([1; 16])); + + let mut extra = ok.clone(); + extra["name"] = serde_json::json!("Eva"); + assert!(serde_json::from_value::(extra).is_err()); + + let mut short = ok; + short["id"] = serde_json::json!("AAAA"); + assert!(serde_json::from_value::(short).is_err()); +} + +#[test] +fn a_key_package_over_the_limit_is_refused_before_decoding() { + let at_limit = KeyPackageBytes::new(vec![0; MAX_KEY_PACKAGE_BYTES]).expect("at the limit"); + let body = serde_json::json!({ + "id": InviteId::from_bytes([1; 16]).to_wire(), + "secretHash": Sha256Hash::from_bytes([2; 32]).to_wire(), + "keyPackage": at_limit, + }); + assert!(serde_json::from_value::(body.clone()).is_ok()); + let mut over = body; + over["keyPackage"] = serde_json::json!("A".repeat(MAX_KEY_PACKAGE_BYTES / 3 * 4 + 8)); + let err = serde_json::from_value::(over).expect_err("over the limit"); + assert!(err.to_string().contains("more than"), "{err}"); + assert!(KeyPackageBytes::new(vec![0; MAX_KEY_PACKAGE_BYTES + 1]).is_err()); +} + +#[test] +fn time_is_kept_only_to_the_hour_and_the_day() { + assert_eq!(hour_of(7199), 1); + assert_eq!(hour_of(-1), -1); + assert_eq!(day_of(86_399), 0); + assert_eq!(day_of(86_400), 1); +} + +fn config() -> ProptestConfig { + ProptestConfig { + cases: 10_000, + ..ProptestConfig::default() + } +} + +proptest! { + #![proptest_config(config())] + + /// Any string at all: every parser answers with a value or an error, never a panic. + #[test] + fn no_string_panics_a_parser(text in any::()) { + let _ = MailboxId::parse(&text); + let _ = InviteId::parse(&text); + let _ = Sha256Hash::parse(&text); + let _ = Token::parse(&text); + let _ = Token::from_bearer(&text); + let _ = WireBytes::parse(&text, MAX_BLOB_BYTES); + } + + /// Strings from the base64url alphabet (the ones that get furthest into the decoder). + #[test] + fn no_base64url_string_panics_a_parser(text in "[A-Za-z0-9_=+/-]{0,90}") { + let _ = MailboxId::parse(&text); + let _ = Token::parse(&text); + let _ = Token::from_bearer(&format!("Bearer {text}")); + let _ = WireBytes::parse(&text, 64); + } + + /// Any JSON-ish bytes given to every request body: an error or a value, never a panic. + #[test] + fn no_body_panics_the_json_parsers(bytes in proptest::collection::vec(any::(), 0..300)) { + let _ = serde_json::from_slice::(&bytes); + let _ = serde_json::from_slice::(&bytes); + let _ = serde_json::from_slice::(&bytes); + let _ = serde_json::from_slice::(&bytes); + let _ = serde_json::from_slice::(&bytes); + } + + /// Every id that parses has exactly the spelling it was given. + #[test] + fn whatever_parses_has_one_spelling(text in "[A-Za-z0-9_-]{22}") { + if let Ok(id) = MailboxId::parse(&text) { + prop_assert_eq!(id.to_wire(), text); + } + } + + /// Bytes survive the wire unchanged. + #[test] + fn bytes_round_trip(bytes in proptest::collection::vec(any::(), 0..512)) { + let wire = serde_json::to_string(&BlobBytes::new(bytes.clone()).expect("small")).expect("json"); + let back: BlobBytes = serde_json::from_str(&wire).expect("parses"); + prop_assert_eq!(back.bytes(), &bytes[..]); + } +}