mirror of
https://github.com/zap-proto/rust.git
synced 2026-10-30 15:04:23 +00:00
Cap'n Proto was the canonical name for this Rust implementation. The
ZAP rebrand had only been applied as a directory rename so far —
crate names, schema file extensions, import paths, and binaries all
still said `capnp` / `capnpc`. This commit makes the rebrand real.
Workspace renames:
- crate capnp/ → zap/ (name = "zap")
- crate capnpc/ → zapc/ (name = "zapc")
- crate capnp-futures/ → zap-futures/
- crate capnp-rpc/ → zap-rpc/
Source-file renames:
- *_capnp.rs → *_zap.rs (~12 files: schema_zap.rs, rpc_zap.rs, etc.)
- capnpc-rust.rs → zapc-rust.rs (the codegen binary entry point)
- *.capnp → *.zap (33 schema files)
Identifier renames across the workspace (96 files rewritten):
- `use capnp::` → `use zap::`
- `extern crate capnp` → `extern crate zap`
- `capnp::generated_code!` → `zap::generated_code!`
- `capnp::traits::Owned` → `zap::traits::Owned`
- `capnp::message::...` → `zap::message::...`
- `capnp::Error/Result` → `zap::Error/Result`
- `capnp::Word` → `zap::Word`
- `capnp::primitive_list` → `zap::primitive_list`
- `capnp::any_pointer` → `zap::any_pointer`
- (etc. — see python script in commit history for full list)
Cargo.toml dep keys:
- `capnp = { path = ... }` → `zap = { path = ... }`
- `[dependencies.capnp]` → `[dependencies.zap]`
- `capnpc = { ... }` → `zapc = { ... }`
- Per-crate name= field and bin = "capnpc-rust" → "zapc"
Schema header strip:
- `using Cxx = import "/capnp/c++.capnp";` lines removed (C++ binding
hints; Rust output doesn't need them, and they referenced a system
/usr/include path we don't ship)
- `$Cxx.namespace("capnp::...")` annotations removed
Build-time decoupling for the benchmark:
- Pre-generated `carsales_zap.rs`, `catrank_zap.rs`, `eval_zap.rs` are
now committed in benchmark/. benchmark/build.rs just copies them
into OUT_DIR instead of invoking the schema compiler at build time.
- benchmark/Cargo.toml drops the [build-dependencies] zapc entry.
Building the benchmark needs ONLY the `zap` runtime crate; no
schema-compiler binary, no plugin, no shell-out.
To regenerate the schema-bound .rs from .zap (developer-only step):
cargo build --release --bin zapc
ln -sf zapc target/release/capnpc-rust # plugin discovery name
cd benchmark && PATH=../target/release:$PATH \
capnp compile -orust eval.zap catrank.zap carsales.zap
The `capnp` tool here is the upstream C++ schema-text parser — only
used as a parser frontend at codegen time, never linked into the
benchmark binary. Runtime + build = 100% zap/zapc only.
Verified by:
cargo tree -p benchmark --release → no capnp dep
grep -rE 'capnp|Capnp|CAPNP' . → 0 hits in active source
cargo build -p benchmark --release → clean build, no system tool
bash ./benchmark/bench.sh quick → runs end-to-end
Other workspace members (fill_random_values, addressbook,
wasm-hello-world, capnpc/test fixtures) still use build.rs schema
compilation at build time; they require a `zap` binary in PATH.
That's pre-existing — the upstream Cap'n Proto Rust workspace had the
same requirement against the `capnp` binary. Out of scope for this PR.
501 lines
21 KiB
Rust
501 lines
21 KiB
Rust
// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors
|
|
// Licensed under the MIT License:
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
use zap::message;
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn canonicalize_succeeds_on_null_message() {
|
|
let segment: &[zap::Word] = &[zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().unwrap());
|
|
|
|
let canonical_bytes = message.canonicalize().unwrap();
|
|
assert_eq!(&canonical_bytes[..], segment);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn dont_truncate_struct_too_far() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, body immediately follows, three data words
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00),
|
|
// First data word
|
|
zap::word(0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11),
|
|
// Second data word, all zero except most significant bit
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80),
|
|
// Third data word, all zero
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonicalized = message.canonicalize().unwrap();
|
|
|
|
let canonical_segment: &[zap::Word] = &[
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00),
|
|
zap::word(0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80),
|
|
];
|
|
|
|
assert_eq!(&canonicalized[..], canonical_segment);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn dont_truncate_struct_list_too_far() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, body immediately follows, one pointer
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00),
|
|
// List pointer, no offset, inline composite, three words long
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00),
|
|
// Tag word, list has one element with three data words and no pointers
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00),
|
|
// First data word
|
|
zap::word(0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22),
|
|
// Second data word, all zero except most significant bit
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80),
|
|
// Third data word, all zero
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonicalized = message.canonicalize().unwrap();
|
|
|
|
let canonical_segment: &[zap::Word] = &[
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00),
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00),
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00),
|
|
zap::word(0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80),
|
|
];
|
|
|
|
assert_eq!(&canonicalized[..], canonical_segment);
|
|
}
|
|
|
|
#[test]
|
|
fn canonical_non_null_empty_struct_field() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, body immediately follows, two pointer fields, no data.
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00),
|
|
// First pointer field, struct, offset of 1, data size 1, no pointers.
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
// Non-null pointer to empty struct.
|
|
zap::word(0xfc, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00),
|
|
// Body of struct filled with non-zero data.
|
|
zap::word(0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn pointer_to_empty_struct_preorder_not_canonical() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, body immediately follows, two pointer fields, no data.
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00),
|
|
// First pointer field, struct, offset of 1, data size 1, no pointers.
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
// Non-null pointer to empty struct. Offset puts it in "preorder". Would need to have
|
|
// an offset of -1 to be canonical.
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
// Body of struct filled with non-zero data.
|
|
zap::word(0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn is_canonical_requires_pointer_preorder() {
|
|
let segment: &[zap::Word] = &[
|
|
//Struct pointer, data immediately follows, two pointer fields, no data
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00),
|
|
//Pointer field 1, pointing to the last entry, data size 1, no pointer
|
|
zap::word(0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
//Pointer field 2, pointing to the next entry, data size 2, no pointer
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
//Data for field 2
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
//Data for field 1
|
|
zap::word(0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn is_canonical_requires_dense_packing() {
|
|
let segment: &[zap::Word] = &[
|
|
//Struct pointer, data after a gap
|
|
zap::word(0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
//The gap
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
//Data for field 1
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn simple_multisegment_message() {
|
|
let segment0: &[zap::Word] = &[
|
|
//Far pointer to next segment
|
|
zap::word(0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segment1: &[zap::Word] = &[
|
|
//Struct pointer (needed to make the far pointer legal)
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
//Dummy data
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
];
|
|
|
|
let segments = &[
|
|
zap::Word::words_to_bytes(segment0),
|
|
zap::Word::words_to_bytes(segment1),
|
|
];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonicalized = message.canonicalize().unwrap();
|
|
let canonical_segment: &[zap::Word] = &[
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
];
|
|
|
|
assert_eq!(&canonicalized[..], canonical_segment);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn multisegment_only_first_segment_used() {
|
|
// A segment with a canonicalized struct.
|
|
let segment0: &[zap::Word] = &[
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
];
|
|
|
|
let segment1: &[zap::Word] = &[zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)];
|
|
|
|
let segments = &[
|
|
zap::Word::words_to_bytes(segment0),
|
|
zap::Word::words_to_bytes(segment1),
|
|
];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonicalized = message.canonicalize().unwrap();
|
|
let canonical_segment: &[zap::Word] = &[
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
];
|
|
|
|
assert_eq!(&canonicalized[..], canonical_segment);
|
|
}
|
|
|
|
#[test]
|
|
fn is_canonical_requires_truncation_of_0_valued_struct_fields() {
|
|
let segment: &[zap::Word] = &[
|
|
//Struct pointer, data immediately follows
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
//Default data value, should have been truncated
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn is_canonical_rejects_unused_trailing_words() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, data in next word
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
// Data section of struct
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
// Trailing zero word
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn empty_inline_composite_list_of_0_sized_structs() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, pointer in next word
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00),
|
|
// List pointer, inline composite, zero words long
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00),
|
|
// Tag word
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
assert_eq!(segment, &canonical_words[..]);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn inline_composite_list_with_void_list() {
|
|
let segment: &[zap::Word] = &[
|
|
// List, inline composite
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00),
|
|
// One element, one pointer
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00),
|
|
// List of 1 VOID
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
assert_eq!(segment, &canonical_words[..]);
|
|
}
|
|
|
|
#[test]
|
|
fn is_canonical_rejects_inline_composite_list_with_inaccurate_word_length() {
|
|
let segment: &[zap::Word] = &[
|
|
// Struct pointer, no offset, pointer section has two entries
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00),
|
|
// List pointer, offset of one, inline composite, two words long
|
|
// (The list only needs to be one word long to hold its actual elements;
|
|
// therefore this message is not canonical.)
|
|
zap::word(0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00),
|
|
// Struct pointer, offset two, data section has one word
|
|
zap::word(0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
// Tag word, struct, one element, one word data section
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
// Data section of struct element of list
|
|
zap::word(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07),
|
|
// Data section of struct field in top-level struct
|
|
zap::word(0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn truncate_data_section_inline_composite() {
|
|
let segment: &[zap::Word] = &[
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00),
|
|
zap::word(0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00),
|
|
zap::word(0x35, 0x35, 0x35, 0x2d, 0x31, 0x32, 0x31, 0x32),
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
|
|
let canonical_segments = &[zap::Word::words_to_bytes(&canonical_words[..])];
|
|
let canonical_segment_array = message::SegmentArray::new(canonical_segments);
|
|
let canonical_message = message::Reader::new(canonical_segment_array, Default::default());
|
|
assert!(canonical_message.is_canonical().unwrap());
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn truncate_pointer_section_inline_composite() {
|
|
let segment: &[zap::Word] = &[
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00),
|
|
zap::word(0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
zap::word(0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
|
|
let canonical_segments = &[zap::Word::words_to_bytes(&canonical_words[..])];
|
|
let canonical_segment_array = message::SegmentArray::new(canonical_segments);
|
|
let canonical_message = message::Reader::new(canonical_segment_array, Default::default());
|
|
assert!(canonical_message.is_canonical().unwrap());
|
|
|
|
let expected_canonical_words: &[zap::Word] = &[
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00),
|
|
zap::word(0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00),
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
zap::word(0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa),
|
|
];
|
|
|
|
assert_eq!(expected_canonical_words, &canonical_words[..]);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn list_padding_must_be_zero() {
|
|
let segment: &[zap::Word] = &[
|
|
// List of three single-byte elements
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00),
|
|
// Fourth byte is also nonzero, so this list is not canonical
|
|
zap::word(0x01, 0x02, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
|
|
let canonical_segments = &[zap::Word::words_to_bytes(&canonical_words[..])];
|
|
let canonical_segment_array = message::SegmentArray::new(canonical_segments);
|
|
let canonical_message = message::Reader::new(canonical_segment_array, Default::default());
|
|
assert!(canonical_message.is_canonical().unwrap());
|
|
|
|
let expected_canonical_words: &[zap::Word] = &[
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00),
|
|
zap::word(0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
assert_eq!(expected_canonical_words, &canonical_words[..]);
|
|
}
|
|
|
|
#[cfg(feature = "alloc")]
|
|
#[test]
|
|
fn bit_list_padding_must_be_zero() {
|
|
let segment: &[zap::Word] = &[
|
|
// List of eleven single-bit elements
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00),
|
|
// Twelfth bit is nonzero, so list is not canonical
|
|
zap::word(0xee, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
|
|
let canonical_words = message.canonicalize().unwrap();
|
|
|
|
let canonical_segments = &[zap::Word::words_to_bytes(&canonical_words[..])];
|
|
let canonical_segment_array = message::SegmentArray::new(canonical_segments);
|
|
let canonical_message = message::Reader::new(canonical_segment_array, Default::default());
|
|
assert!(canonical_message.is_canonical().unwrap());
|
|
|
|
let expected_canonical_words: &[zap::Word] = &[
|
|
zap::word(0x01, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00),
|
|
zap::word(0xee, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
assert_eq!(expected_canonical_words, &canonical_words[..]);
|
|
}
|
|
|
|
#[test]
|
|
fn out_of_bounds_zero_sized_list_returns_error() {
|
|
let segment: &[zap::Word] = &[
|
|
// List pointer, offset out of bounds, elements are byte-sized, zero elements.
|
|
zap::word(0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn out_of_bounds_zero_sized_void_list_returns_error() {
|
|
let segment: &[zap::Word] = &[
|
|
// List pointer, offset out of bounds, elements have size zero, two elements.
|
|
zap::word(0x01, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(message.is_canonical().is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn far_pointer_to_same_segment() {
|
|
let segment: &[zap::Word] = &[
|
|
// Far pointer to this same segment. Landing pad is two words, offset of one.
|
|
zap::word(0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
// Landing pad. Again, points back to this same segment.
|
|
zap::word(0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
// Tag word, describing struct with 2-word data section.
|
|
zap::word(0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00),
|
|
];
|
|
|
|
let segments = &[zap::Word::words_to_bytes(segment)];
|
|
let segment_array = message::SegmentArray::new(segments);
|
|
let message = message::Reader::new(segment_array, Default::default());
|
|
assert!(!message.is_canonical().unwrap());
|
|
}
|