Skip to main content

proto_rs/
lib.rs

1#![no_std]
2#![forbid(unsafe_code)]
3#![deny(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
4
5//! An allocation-backed, dynamically typed Protocol Buffers implementation.
6//!
7//! # `no_std` contract
8//!
9//! This library is unconditionally `#![no_std]` and only uses [`core`] and
10//! [`alloc`]. It has no `std` Cargo feature and no default features. File I/O
11//! is intentionally left to callers; imported schemas are supplied as source
12//! strings through [`Registry`].
13//!
14//! Unit tests and the official conformance test adapter may use the standard
15//! library, but `proto_rs` itself never does.
16//!
17//! # Workflow
18//!
19//! A caller first creates a [`Registry`] when imports are involved.
20//! Every `.proto` source is registered under its logical import path.
21//! Calling [`Registry::parse`] resolves the complete reachable import graph.
22//! A single import-free source can instead be parsed with [`parse`].
23//! The resulting [`Schema`] owns runtime message and enum descriptors.
24//! Callers select a [`MessageDescriptor`] by its protobuf full name.
25//! A dynamic [`Message`] is populated with named [`Value`] instances.
26//! [`encode`] turns that value into protobuf binary wire bytes.
27//! [`decode`] reconstructs a dynamic value using the same descriptor.
28//! No generated Rust source participates in this workflow.
29//!
30//! # Registry example
31//!
32//! ```
33//! use proto_rs::{Registry, Message, Value, encode};
34//!
35//! let mut registry = Registry::new();
36//! registry.register(
37//!     "model.proto",
38//!     r#"syntax="proto3"; package model; message Id { uint64 value = 1; }"#,
39//! );
40//! registry.register(
41//!     "request.proto",
42//!     r#"syntax="proto3"; package api; import "model.proto";
43//!        message Request { model.Id id = 1; }"#,
44//! );
45//! let schema = registry.parse("request.proto")?;
46//! let id_descriptor = schema.message("model.Id").unwrap();
47//! let request_descriptor = schema.message("api.Request").unwrap();
48//! let mut id = Message::new();
49//! id.insert("value", Value::Uint64(42));
50//! let mut request = Message::new();
51//! request.insert("id", Value::Message(id));
52//! let bytes = encode(&schema, request_descriptor, &request)?;
53//! assert!(!bytes.is_empty());
54//! assert_eq!(id_descriptor.name, "Id");
55//! # Ok::<(), proto_rs::Error>(())
56//! ```
57//!
58//! # Audit example
59//!
60//! ```
61//! use proto_rs::{AuditTag, EncodeOptions, DuplicatePolicy, FieldOrder, decode,
62//!                encode_with_options, parse};
63//!
64//! let schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#)?;
65//! let descriptor = schema.message("A").unwrap();
66//! let message = decode(&schema, descriptor, &[8, 1, 8, 2, 16, 3])?;
67//! assert!(message.audit.iter().any(|entry| {
68//!     entry.tag == AuditTag::DuplicateLastWins
69//! }));
70//! assert!(message.audit.iter().any(|entry| {
71//!     entry.tag == AuditTag::UnknownField
72//! }));
73//! let output = encode_with_options(
74//!     &schema,
75//!     descriptor,
76//!     &message,
77//!     &EncodeOptions {
78//!         forward_unknown_fields: false,
79//!         forward_unknown_messages: false,
80//!         forward_added_fields: false,
81//!         duplicates: DuplicatePolicy::LastOnly,
82//!         field_order: FieldOrder::FieldNumber,
83//!         ..EncodeOptions::default()
84//!     },
85//! )?;
86//! assert_eq!(output.bytes, [8, 2]);
87//! # Ok::<(), proto_rs::Error>(())
88//! ```
89//!
90//! # Compatibility
91//!
92//! The binary codec supports the complete exercised proto3 conformance set.
93//! It also supports the ordinary, non-MessageSet proto2 binary feature set.
94//! Proto2 group and extension declarations are recognized but not decoded.
95//! JSON, text format, JSPB, and Editions are separate unsupported formats.
96//! The repository's `CONFORMANCE.md` records the precise boundary.
97//! Unsupported wire types return errors instead of being misinterpreted.
98//! Unknown supported wire fields remain auditable and forwardable.
99//! Serialization policy can prevent forwarding data absent from the schema.
100//!
101//! # Stability
102//!
103//! Descriptors and values are intentionally generic public data structures.
104//! Audit metadata is excluded from semantic [`Message`] equality.
105//! Exact encoded byte order is deterministic for a given dynamic message.
106//! Protobuf semantic equivalence does not require a unique byte ordering.
107//! Errors contain an owned message and the relevant byte offset.
108//! APIs return [`Result`] and never require unwinding for malformed input.
109
110extern crate alloc;
111#[cfg(test)]
112extern crate std;
113mod codec;
114mod constants;
115mod json;
116mod schema;
117use alloc::string::String;
118pub use codec::{
119    AddedField, AuditMode, AuditRecord, AuditTag, BooleanValuePolicy, DecodeOptions,
120    DuplicateInputPolicy, DuplicatePolicy, EncodeOptions, EncodeOutput, EnumValuePolicy,
121    FieldOrder, FloatEncoding, MapOrder, Message, UnknownField, UnknownFieldPolicy, Value, decode,
122    decode_with_options, encode, encode_with_options,
123};
124use core::fmt;
125pub use json::{JsonDecodeOptions, decode_json, decode_json_with_options, encode_json};
126pub use schema::{
127    Cardinality, CustomOptionDescriptor, Enum, EnumType, EnumValue, ExtensionDescriptor,
128    FeatureSet, Field, FieldPresence, FieldType, Import, ImportKind, JsonFormat, MessageDescriptor,
129    MessageEncoding, MethodDescriptor, OneofDescriptor, OptionSetting, OptionValueKind, Registry,
130    RepeatedFieldEncoding, Schema, SchemaParseOptions, ServiceDescriptor, Syntax, Utf8Validation,
131    parse, parse_with_options,
132};
133/// Error returned when schema or wire data cannot be processed safely.
134#[derive(Clone, Debug, Eq, PartialEq)]
135pub struct Error {
136    /// Byte offset at which the failure became observable.
137    pub offset: usize,
138    /// Human-readable description of the failed validation or operation.
139    pub message: String,
140}
141impl Error {
142    /// Creates an error at the byte offset where parsing or encoding failed.
143    pub(crate) fn new(offset: usize, message: impl Into<String>) -> Self {
144        Self {
145            offset,
146            message: message.into(),
147        }
148    }
149}
150impl fmt::Display for Error {
151    /// Formats the diagnostic message together with its byte offset.
152    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153        write!(f, "{} at byte {}", self.message, self.offset)
154    }
155}
156/// Result type returned by schema parsing and protobuf codec operations.
157pub type Result<T> = core::result::Result<T, Error>;
158
159#[cfg(test)]
160#[allow(clippy::panic, clippy::unwrap_used)]
161mod tests {
162    use super::*;
163    use alloc::{string::ToString, vec, vec::Vec};
164
165    const PROTO: &str = r#"
166        syntax = "proto3";
167        package demo;
168        message Person {
169          uint32 id = 1;
170          string name = 2;
171          repeated sint32 scores = 3;
172          message Address { string city = 1; }
173          Address address = 4;
174        }
175    "#;
176    /// Deterministic randomized decoder cases executed by the normal test suite.
177    const RANDOMIZED_DECODER_CASES: usize = 10_000;
178    /// Largest deterministic randomized packet passed to either decoder.
179    const MAX_RANDOMIZED_PACKET_BYTES: usize = 128;
180    /// Initial state for the reproducible linear congruential generator.
181    const RANDOMIZED_DECODER_SEED: u64 = 0x7a5d_39e1_42c6_b8f0;
182    /// Full-period multiplier used by the deterministic test generator.
183    const RANDOMIZED_DECODER_MULTIPLIER: u64 = 6_364_136_223_846_793_005;
184    /// Odd increment used by the deterministic test generator.
185    const RANDOMIZED_DECODER_INCREMENT: u64 = 1_442_695_040_888_963_407;
186
187    /// Verifies descriptor-driven parsing and dynamic binary round-tripping.
188    #[test]
189    fn parses_and_round_trips_dynamic_message() {
190        let schema = parse(PROTO).unwrap();
191        let descriptor = schema.message("Person").unwrap();
192        let mut address = Message::new();
193        address.insert("city", Value::String("Paris".to_string()));
194        let mut person = Message::new();
195        person.insert("id", Value::Uint32(42));
196        person.insert("name", Value::String("Ada".to_string()));
197        person.insert(
198            "scores",
199            Value::Repeated(vec![Value::Int32(-1), Value::Int32(150)]),
200        );
201        person.insert("address", Value::Message(address));
202
203        let bytes = encode(&schema, descriptor, &person).unwrap();
204        assert_eq!(decode(&schema, descriptor, &bytes).unwrap(), person);
205    }
206
207    /// Verifies that unknown wire occurrences survive decoding and encoding.
208    #[test]
209    fn preserves_unknown_fields() {
210        let schema = parse(r#"syntax="proto3"; message Empty {}"#).unwrap();
211        let descriptor = schema.message("Empty").unwrap();
212        let bytes = [0x98, 0x06, 0x96, 0x01]; // field 99, varint 150
213        let value = decode(&schema, descriptor, &bytes).unwrap();
214        assert_eq!(encode(&schema, descriptor, &value).unwrap(), bytes);
215    }
216
217    /// Verifies rejection of protobuf's implementation-reserved tag range.
218    #[test]
219    fn rejects_reserved_field_number() {
220        assert!(parse("message Bad { string x = 19000; }").is_err());
221    }
222
223    /// Verifies dynamic map encoding and protobuf's duplicate-key last-wins rule.
224    #[test]
225    fn maps_round_trip_and_duplicate_keys_use_last_value() {
226        let schema =
227            parse(r#"syntax="proto3"; message Index { map<string, int32> counts = 1; }"#).unwrap();
228        let descriptor = schema.message("Index").unwrap();
229        let mut message = Message::new();
230        message.insert(
231            "counts",
232            Value::Map(vec![
233                (Value::String("a".into()), Value::Int32(1)),
234                (Value::String("b".into()), Value::Int32(2)),
235            ]),
236        );
237        let encoded = encode(&schema, descriptor, &message).unwrap();
238        assert_eq!(decode(&schema, descriptor, &encoded).unwrap(), message);
239
240        // Two entries for key "a"; protobuf map semantics retain the latter.
241        let duplicate = [
242            0x0a, 0x05, 0x0a, 0x01, b'a', 0x10, 0x01, 0x0a, 0x05, 0x0a, 0x01, b'a', 0x10, 0x09,
243        ];
244        assert_eq!(
245            decode(&schema, descriptor, &duplicate)
246                .unwrap()
247                .get("counts"),
248            Some(&Value::Map(vec![(
249                Value::String("a".into()),
250                Value::Int32(9)
251            )]))
252        );
253    }
254
255    /// Verifies type resolution across transitively imported registry sources.
256    #[test]
257    fn resolves_transitive_cross_file_imports() {
258        let mut registry = Registry::new();
259        for (path, source) in [
260            (
261                "app.proto",
262                r#"syntax="proto3"; package app; import "model.proto";
263                       message Request { model.User user = 1; }"#,
264            ),
265            (
266                "model.proto",
267                r#"syntax="proto3"; package model; import "id.proto";
268                       message User { ids.Id id = 1; }"#,
269            ),
270            (
271                "id.proto",
272                r#"syntax="proto3"; package ids; message Id { uint64 value = 1; }"#,
273            ),
274        ] {
275            registry.register(path, source);
276        }
277        let schema = registry.parse("app.proto").unwrap();
278        assert!(schema.message("app.Request").is_some());
279        assert!(schema.message("model.User").is_some());
280        assert!(schema.message("ids.Id").is_some());
281
282        let request = schema.message("app.Request").unwrap();
283        let mut id = Message::new();
284        id.insert("value", Value::Uint64(7));
285        let mut user = Message::new();
286        user.insert("id", Value::Message(id));
287        let mut value = Message::new();
288        value.insert("user", Value::Message(user));
289        let bytes = encode(&schema, request, &value).unwrap();
290        assert_eq!(decode(&schema, request, &bytes).unwrap(), value);
291    }
292
293    /// Verifies weak-import tolerance and required-import failure behavior.
294    #[test]
295    fn permits_missing_weak_import_but_rejects_missing_normal_import() {
296        let mut registry = Registry::new();
297        registry.register("root.proto", "import weak \"optional.proto\"; message A {}");
298        assert!(registry.parse("root.proto").is_ok());
299        registry.register("root.proto", "import \"missing.proto\"; message A {}");
300        assert!(registry.parse("root.proto").is_err());
301    }
302
303    /// Verifies bounds and encoded-width validation for protobuf field keys.
304    #[test]
305    fn rejects_out_of_range_and_overlong_field_tags() {
306        let schema = parse(r#"syntax="proto3"; message A { int32 x = 1; }"#).unwrap();
307        let descriptor = schema.message("A").unwrap();
308        assert!(decode(&schema, descriptor, &[0x88, 0x80, 0x80, 0x80, 0x40, 1]).is_err());
309        assert!(
310            decode(
311                &schema,
312                descriptor,
313                &[0x88, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 1]
314            )
315            .is_err()
316        );
317    }
318
319    /// Verifies recursive merge semantics for repeated singular messages.
320    #[test]
321    fn merges_repeated_singular_message_occurrences() {
322        let schema = parse(
323            r#"syntax="proto3";
324               message Inner { int32 a = 1; int32 b = 2; repeated int32 values = 3; }
325               message Outer { Inner inner = 1; }"#,
326        )
327        .unwrap();
328        let descriptor = schema.message("Outer").unwrap();
329        let wire = [
330            0x0a, 0x04, 0x08, 0x01, 0x18, 0x03, 0x0a, 0x04, 0x10, 0x02, 0x18, 0x04,
331        ];
332        let decoded = decode(&schema, descriptor, &wire).unwrap();
333        let Some(Value::Message(inner)) = decoded.get("inner") else {
334            panic!("inner message missing")
335        };
336        assert_eq!(inner.get("a"), Some(&Value::Int32(1)));
337        assert_eq!(inner.get("b"), Some(&Value::Int32(2)));
338        assert_eq!(
339            inner.get("values"),
340            Some(&Value::Repeated(vec![Value::Int32(3), Value::Int32(4)]))
341        );
342    }
343
344    /// Verifies proto3 implicit defaults and explicit-presence serialization.
345    #[test]
346    fn proto3_elides_implicit_defaults_but_preserves_explicit_presence() {
347        let schema = parse(
348            r#"syntax="proto3"; message A {
349                 int32 implicit = 1;
350                 optional int32 explicit = 2;
351                 oneof choice { int32 selected = 3; }
352               }"#,
353        )
354        .unwrap();
355        let descriptor = schema.message("A").unwrap();
356        let mut message = Message::new();
357        message.insert("implicit", Value::Int32(0));
358        message.insert("explicit", Value::Int32(0));
359        message.insert("selected", Value::Int32(0));
360        assert_eq!(
361            encode(&schema, descriptor, &message).unwrap(),
362            [0x10, 0, 0x18, 0]
363        );
364    }
365
366    /// Verifies audit classification and configurable forwarding policies.
367    #[test]
368    fn audits_and_filters_duplicates_unknowns_and_added_fields() {
369        let schema = parse(r#"syntax="proto3"; message A { int32 x = 1; }"#).unwrap();
370        let descriptor = schema.message("A").unwrap();
371        let wire = [0x08, 1, 0x08, 2, 0x10, 3, 0x1a, 1, 0xff];
372        let mut message = decode(&schema, descriptor, &wire).unwrap();
373        assert_eq!(
374            message
375                .audit
376                .iter()
377                .map(|record| record.tag)
378                .collect::<Vec<_>>(),
379            vec![
380                AuditTag::DuplicateDiscarded,
381                AuditTag::DuplicateLastWins,
382                AuditTag::UnknownField,
383                AuditTag::UnknownMessage,
384            ]
385        );
386        message.add_field(AddedField {
387            name: "application_field".into(),
388            number: 4,
389            wire_type: 0,
390            encoded_value: vec![9],
391        });
392
393        let filtered = encode_with_options(
394            &schema,
395            descriptor,
396            &message,
397            &EncodeOptions {
398                forward_unknown_fields: false,
399                forward_unknown_messages: false,
400                forward_added_fields: false,
401                duplicates: DuplicatePolicy::LastOnly,
402                field_order: FieldOrder::Declaration,
403                ..EncodeOptions::default()
404            },
405        )
406        .unwrap();
407        assert_eq!(filtered.bytes, [0x08, 2]);
408
409        let preserved = encode_with_options(
410            &schema,
411            descriptor,
412            &message,
413            &EncodeOptions {
414                forward_unknown_fields: false,
415                forward_unknown_messages: false,
416                forward_added_fields: true,
417                duplicates: DuplicatePolicy::PreserveAll,
418                field_order: FieldOrder::Declaration,
419                ..EncodeOptions::default()
420            },
421        )
422        .unwrap();
423        assert_eq!(preserved.bytes, [0x08, 1, 0x08, 2, 0x20, 9]);
424    }
425
426    /// Verifies numeric field ordering overrides an out-of-order declaration.
427    #[test]
428    fn encoder_can_order_all_occurrences_by_field_number() {
429        let schema = parse(
430            r#"syntax="proto3";
431               message Ordered {
432                 string second = 2;
433                 int32 first = 1;
434               }"#,
435        )
436        .unwrap();
437        let descriptor = schema.message("Ordered").unwrap();
438        let mut message = Message::new();
439        message.insert("first", Value::Int32(1));
440        message.insert("second", Value::String("two".into()));
441        message.add_field(AddedField {
442            name: "third".into(),
443            number: 3,
444            wire_type: 0,
445            encoded_value: vec![3],
446        });
447
448        assert_eq!(
449            encode(&schema, descriptor, &message).unwrap(),
450            [0x12, 3, b't', b'w', b'o', 0x08, 1, 0x18, 3]
451        );
452        let ordered = encode_with_options(
453            &schema,
454            descriptor,
455            &message,
456            &EncodeOptions {
457                field_order: FieldOrder::FieldNumber,
458                ..EncodeOptions::default()
459            },
460        )
461        .unwrap();
462        assert_eq!(ordered.bytes, [0x08, 1, 0x12, 3, b't', b'w', b'o', 0x18, 3]);
463    }
464
465    /// Verifies unknown fields can be preserved, dropped with audit, or rejected.
466    #[test]
467    fn decode_unknown_field_policies_are_explicit() {
468        let schema = parse(r#"syntax="proto3"; message A { int32 known = 1; }"#).unwrap();
469        let descriptor = schema.message("A").unwrap();
470        let wire = [0x08, 1, 0x10, 2];
471
472        let preserved = decode(&schema, descriptor, &wire).unwrap();
473        assert_eq!(preserved.unknown_fields.len(), 1);
474        let dropped = decode_with_options(
475            &schema,
476            descriptor,
477            &wire,
478            &DecodeOptions {
479                unknown_fields: UnknownFieldPolicy::Drop,
480                ..DecodeOptions::default()
481            },
482        )
483        .unwrap();
484        assert!(dropped.unknown_fields.is_empty());
485        assert!(
486            dropped
487                .audit
488                .iter()
489                .any(|record| { record.tag == AuditTag::UnknownField && record.field_number == 2 })
490        );
491        assert!(
492            decode_with_options(
493                &schema,
494                descriptor,
495                &wire,
496                &DecodeOptions {
497                    unknown_fields: UnknownFieldPolicy::Reject,
498                    ..DecodeOptions::default()
499                },
500            )
501            .is_err()
502        );
503    }
504
505    /// Verifies strict decoding rejects non-minimal keys, values, and lengths.
506    #[test]
507    fn strict_decode_rejects_every_non_minimal_varint_position() {
508        let scalar_schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
509        let scalar = scalar_schema.message("A").unwrap();
510        let string_schema = parse(r#"syntax="proto3"; message A { string value = 1; }"#).unwrap();
511        let string = string_schema.message("A").unwrap();
512        let strict = DecodeOptions {
513            require_minimal_varints: true,
514            ..DecodeOptions::default()
515        };
516
517        assert!(decode(&scalar_schema, scalar, &[0x88, 0x00, 1]).is_ok());
518        assert!(decode_with_options(&scalar_schema, scalar, &[0x88, 0x00, 1], &strict).is_err());
519        assert!(decode_with_options(&scalar_schema, scalar, &[0x08, 0x81, 0x00], &strict).is_err());
520        assert!(
521            decode_with_options(&string_schema, string, &[0x0a, 0x81, 0x00, b'x'], &strict)
522                .is_err()
523        );
524
525        let five_byte_negative_one = [0x08, 0xff, 0xff, 0xff, 0xff, 0x0f];
526        assert!(decode(&scalar_schema, scalar, &five_byte_negative_one).is_ok());
527        assert!(
528            decode_with_options(&scalar_schema, scalar, &five_byte_negative_one, &strict).is_err()
529        );
530        let uint_schema = parse(r#"syntax="proto3"; message A { uint32 value = 1; }"#).unwrap();
531        let uint = uint_schema.message("A").unwrap();
532        let value_above_u32 = [0x08, 0x80, 0x80, 0x80, 0x80, 0x10];
533        assert!(decode(&uint_schema, uint, &value_above_u32).is_ok());
534        assert!(decode_with_options(&uint_schema, uint, &value_above_u32, &strict).is_err());
535    }
536
537    /// Verifies root and nested message byte limits are enforced.
538    #[test]
539    fn decode_enforces_message_and_length_limits() {
540        let schema = parse(r#"syntax="proto3"; message A { string value = 1; }"#).unwrap();
541        let descriptor = schema.message("A").unwrap();
542        let wire = [0x0a, 2, b'o', b'k'];
543        assert!(
544            decode_with_options(
545                &schema,
546                descriptor,
547                &wire,
548                &DecodeOptions {
549                    max_message_bytes: wire.len() - 1,
550                    ..DecodeOptions::default()
551                },
552            )
553            .is_err()
554        );
555        assert!(
556            decode_with_options(
557                &schema,
558                descriptor,
559                &wire,
560                &DecodeOptions {
561                    max_length_delimited_bytes: 1,
562                    ..DecodeOptions::default()
563                },
564            )
565            .is_err()
566        );
567    }
568
569    /// Verifies a 64-bit wire length cannot truncate on a 32-bit target.
570    #[cfg(target_pointer_width = "32")]
571    #[test]
572    fn decode_rejects_lengths_that_do_not_fit_usize() {
573        let schema = parse(r#"syntax="proto3"; message A { bytes value = 1; }"#).unwrap();
574        let descriptor = schema.message("A").unwrap();
575        let length_above_u32 = [0x0a, 0x80, 0x80, 0x80, 0x80, 0x10];
576        assert!(decode(&schema, descriptor, &length_above_u32).is_err());
577    }
578
579    /// Verifies recursive messages cannot exceed the configured nesting depth.
580    #[test]
581    fn decode_enforces_recursion_depth() {
582        let schema = parse(r#"syntax="proto3"; message Node { Node child = 1; }"#).unwrap();
583        let descriptor = schema.message("Node").unwrap();
584        let two_nested_children = [0x0a, 2, 0x0a, 0];
585        assert!(
586            decode_with_options(
587                &schema,
588                descriptor,
589                &two_nested_children,
590                &DecodeOptions {
591                    max_recursion_depth: 1,
592                    ..DecodeOptions::default()
593                },
594            )
595            .is_err()
596        );
597    }
598
599    /// Verifies total field occurrences are bounded across a decode operation.
600    #[test]
601    fn decode_enforces_field_occurrence_limit() {
602        let schema = parse(r#"syntax="proto3"; message A { repeated int32 values = 1; }"#).unwrap();
603        let descriptor = schema.message("A").unwrap();
604        assert!(
605            decode_with_options(
606                &schema,
607                descriptor,
608                &[0x08, 1, 0x08, 2],
609                &DecodeOptions {
610                    max_field_occurrences: 1,
611                    ..DecodeOptions::default()
612                },
613            )
614            .is_err()
615        );
616    }
617
618    /// Verifies repeated limits apply to both packed and unpacked encodings.
619    #[test]
620    fn decode_enforces_repeated_value_limit() {
621        let schema = parse(r#"syntax="proto3"; message A { repeated int32 values = 1; }"#).unwrap();
622        let descriptor = schema.message("A").unwrap();
623        let limited = DecodeOptions {
624            max_repeated_values: 1,
625            ..DecodeOptions::default()
626        };
627        assert!(decode_with_options(&schema, descriptor, &[0x08, 1, 0x08, 2], &limited).is_err());
628        assert!(decode_with_options(&schema, descriptor, &[0x0a, 2, 1, 2], &limited).is_err());
629    }
630
631    /// Verifies maps cannot retain more entries than the configured limit.
632    #[test]
633    fn decode_enforces_map_entry_limit() {
634        let schema =
635            parse(r#"syntax="proto3"; message A { map<int32, int32> values = 1; }"#).unwrap();
636        let descriptor = schema.message("A").unwrap();
637        let two_entries = [0x0a, 4, 0x08, 1, 0x10, 10, 0x0a, 4, 0x08, 2, 0x10, 20];
638        assert!(
639            decode_with_options(
640                &schema,
641                descriptor,
642                &two_entries,
643                &DecodeOptions {
644                    max_map_entries: 1,
645                    ..DecodeOptions::default()
646                },
647            )
648            .is_err()
649        );
650    }
651
652    /// Verifies full audit copies are bounded and metadata-only mode copies none.
653    #[test]
654    fn decode_bounds_or_omits_audit_bytes() {
655        let schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
656        let descriptor = schema.message("A").unwrap();
657        assert!(
658            decode_with_options(
659                &schema,
660                descriptor,
661                &[0x08, 1],
662                &DecodeOptions {
663                    max_audit_bytes: 1,
664                    ..DecodeOptions::default()
665                },
666            )
667            .is_err()
668        );
669        let metadata = decode_with_options(
670            &schema,
671            descriptor,
672            &[0x08, 1],
673            &DecodeOptions {
674                max_audit_bytes: 0,
675                audit_mode: AuditMode::MetadataOnly,
676                ..DecodeOptions::default()
677            },
678        )
679        .unwrap();
680        assert_eq!(metadata.audit.len(), 1);
681        assert!(metadata.audit[0].encoded_field.is_empty());
682    }
683
684    /// Verifies strict duplicate policy covers scalars, oneofs, and map keys.
685    #[test]
686    fn decode_can_reject_ambiguous_duplicates() {
687        let scalar_schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
688        let scalar = scalar_schema.message("A").unwrap();
689        let oneof_schema =
690            parse(r#"syntax="proto3"; message A { oneof choice { int32 a = 1; int32 b = 2; } }"#)
691                .unwrap();
692        let oneof = oneof_schema.message("A").unwrap();
693        let map_schema =
694            parse(r#"syntax="proto3"; message A { map<int32, int32> values = 1; }"#).unwrap();
695        let map = map_schema.message("A").unwrap();
696        let strict = DecodeOptions {
697            duplicates: DuplicateInputPolicy::Reject,
698            ..DecodeOptions::default()
699        };
700
701        assert!(decode_with_options(&scalar_schema, scalar, &[0x08, 1, 0x08, 2], &strict).is_err());
702        assert!(decode_with_options(&oneof_schema, oneof, &[0x08, 1, 0x10, 2], &strict).is_err());
703        assert!(
704            decode_with_options(
705                &map_schema,
706                map,
707                &[0x0a, 4, 0x08, 1, 0x10, 10, 0x0a, 4, 0x08, 1, 0x10, 20],
708                &strict,
709            )
710            .is_err()
711        );
712        let duplicate_inside_one_entry = [0x0a, 6, 0x08, 1, 0x08, 2, 0x10, 10];
713        assert!(
714            decode_with_options(&map_schema, map, &duplicate_inside_one_entry, &strict,).is_err()
715        );
716    }
717
718    /// Verifies every displaced scalar occurrence has an accurate final tag.
719    #[test]
720    fn audit_demotes_each_previous_duplicate_winner() {
721        let schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
722        let descriptor = schema.message("A").unwrap();
723        let message = decode(&schema, descriptor, &[0x08, 1, 0x08, 2, 0x08, 3]).unwrap();
724        assert_eq!(
725            message
726                .audit
727                .iter()
728                .map(|record| record.tag)
729                .collect::<Vec<_>>(),
730            vec![
731                AuditTag::DuplicateDiscarded,
732                AuditTag::DuplicateDiscarded,
733                AuditTag::DuplicateLastWins,
734            ]
735        );
736    }
737
738    /// Verifies strict scalar policies reject noncanonical bools and unknown enums.
739    #[test]
740    fn decode_can_reject_noncanonical_boolean_and_unknown_enum_values() {
741        let schema = parse(
742            r#"syntax="proto3";
743               enum State { STATE_UNSPECIFIED = 0; STATE_READY = 1; }
744               message A { bool enabled = 1; State state = 2; }"#,
745        )
746        .unwrap();
747        let descriptor = schema.message("A").unwrap();
748        assert!(decode(&schema, descriptor, &[0x08, 2, 0x10, 9]).is_ok());
749        assert!(
750            decode_with_options(
751                &schema,
752                descriptor,
753                &[0x08, 2],
754                &DecodeOptions {
755                    booleans: BooleanValuePolicy::RejectNonCanonical,
756                    ..DecodeOptions::default()
757                },
758            )
759            .is_err()
760        );
761        assert!(
762            decode_with_options(
763                &schema,
764                descriptor,
765                &[0x10, 9],
766                &DecodeOptions {
767                    enum_values: EnumValuePolicy::RejectUnknown,
768                    ..DecodeOptions::default()
769                },
770            )
771            .is_err()
772        );
773    }
774
775    /// Verifies raw duplicate replay fails when metadata-only audit discarded it.
776    #[test]
777    fn metadata_only_audit_cannot_preserve_displaced_wire_bytes() {
778        let schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
779        let descriptor = schema.message("A").unwrap();
780        let message = decode_with_options(
781            &schema,
782            descriptor,
783            &[0x08, 1, 0x08, 2],
784            &DecodeOptions {
785                audit_mode: AuditMode::MetadataOnly,
786                ..DecodeOptions::default()
787            },
788        )
789        .unwrap();
790        assert!(
791            encode_with_options(
792                &schema,
793                descriptor,
794                &message,
795                &EncodeOptions {
796                    duplicates: DuplicatePolicy::PreserveAll,
797                    ..EncodeOptions::default()
798                },
799            )
800            .is_err()
801        );
802    }
803
804    /// Verifies map-key ordering is independent of insertion order when selected.
805    #[test]
806    fn encoder_can_sort_map_entries_by_key() {
807        let schema =
808            parse(r#"syntax="proto3"; message A { map<int32, int32> values = 1; }"#).unwrap();
809        let descriptor = schema.message("A").unwrap();
810        let mut message = Message::new();
811        message.insert(
812            "values",
813            Value::Map(vec![
814                (Value::Int32(2), Value::Int32(20)),
815                (Value::Int32(1), Value::Int32(10)),
816            ]),
817        );
818        let ordered = encode_with_options(
819            &schema,
820            descriptor,
821            &message,
822            &EncodeOptions {
823                map_order: MapOrder::Key,
824                ..EncodeOptions::default()
825            },
826        )
827        .unwrap();
828        assert_eq!(
829            ordered.bytes,
830            [0x0a, 4, 0x08, 1, 0x10, 10, 0x0a, 4, 0x08, 2, 0x10, 20]
831        );
832    }
833
834    /// Verifies float normalization removes NaN payload and signed-zero variance.
835    #[test]
836    fn encoder_can_normalize_floating_point_bits() {
837        let schema =
838            parse(r#"syntax="proto3"; message A { optional float f = 1; optional double d = 2; }"#)
839                .unwrap();
840        let descriptor = schema.message("A").unwrap();
841        let options = EncodeOptions {
842            floats: FloatEncoding::Normalize,
843            ..EncodeOptions::default()
844        };
845        let mut first = Message::new();
846        first.insert("f", Value::Float(f32::from_bits(0x7fc0_0001)));
847        first.insert("d", Value::Double(-0.0));
848        let mut second = Message::new();
849        second.insert("f", Value::Float(f32::from_bits(0x7fff_ffff)));
850        second.insert("d", Value::Double(0.0));
851
852        assert_eq!(
853            encode_with_options(&schema, descriptor, &first, &options)
854                .unwrap()
855                .bytes,
856            encode_with_options(&schema, descriptor, &second, &options)
857                .unwrap()
858                .bytes
859        );
860    }
861
862    /// Verifies the encoder refuses output above its configured byte budget.
863    #[test]
864    fn encoder_enforces_output_size_limit() {
865        let schema = parse(r#"syntax="proto3"; message A { int32 value = 1; }"#).unwrap();
866        let descriptor = schema.message("A").unwrap();
867        let mut message = Message::new();
868        message.insert("value", Value::Int32(1));
869        assert!(
870            encode_with_options(
871                &schema,
872                descriptor,
873                &message,
874                &EncodeOptions {
875                    max_output_bytes: 1,
876                    ..EncodeOptions::default()
877                },
878            )
879            .is_err()
880        );
881    }
882
883    /// Verifies supported proto2 parsing, defaults, packing, and group skipping.
884    #[test]
885    fn parses_and_encodes_basic_proto2_with_defaults_and_groups_declared() {
886        let schema = parse(
887            r#"syntax="proto2";
888               message Legacy {
889                 required int32 id = 1 [default = -1];
890                 optional string name = 2 [default = "unknown"];
891                 repeated uint64 values = 3 [packed = true];
892                 optional group IgnoredLegacyGroup = 4 { optional int32 x = 5; }
893               }"#,
894        )
895        .unwrap();
896        let descriptor = schema.message("Legacy").unwrap();
897        assert_eq!(
898            descriptor.field_by_name("id").unwrap().default.as_deref(),
899            Some("-1")
900        );
901        assert_eq!(
902            descriptor.field_by_name("name").unwrap().default.as_deref(),
903            Some("unknown")
904        );
905        let mut message = Message::new();
906        message.insert("id", Value::Int32(7));
907        message.insert(
908            "values",
909            Value::Repeated(vec![Value::Uint64(1), Value::Uint64(300)]),
910        );
911        let bytes = encode(&schema, descriptor, &message).unwrap();
912        assert_eq!(decode(&schema, descriptor, &bytes).unwrap(), message);
913    }
914
915    /// Verifies representative malformed schemas and wire inputs return errors
916    /// rather than unwinding through the public parser and decoder APIs.
917    #[test]
918    fn malformed_inputs_do_not_panic() {
919        let malformed_schemas = [
920            "/*",
921            "message A {",
922            "enum A { VALUE = 0 [deprecated = true",
923            "syntax = \"unknown\";",
924            "message A { bytes value = 536870912; }",
925        ];
926        for source in malformed_schemas {
927            assert!(std::panic::catch_unwind(|| parse(source)).is_ok());
928        }
929
930        let schema = parse(r#"syntax="proto3"; message A { fixed64 value = 1; }"#).unwrap();
931        let descriptor = schema.message("A").unwrap();
932        let malformed_wire = [
933            &[][..],
934            &[0x08][..],
935            &[0x09, 0, 0, 0][..],
936            &[0x0a, 0xff, 0xff, 0xff, 0xff, 0x0f][..],
937            &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02][..],
938        ];
939        for bytes in malformed_wire {
940            assert!(std::panic::catch_unwind(|| decode(&schema, descriptor, bytes)).is_ok());
941        }
942
943        let exhaustive_short_inputs = std::panic::catch_unwind(|| {
944            for first in u8::MIN..=u8::MAX {
945                let _ = decode(&schema, descriptor, &[first]);
946                for second in u8::MIN..=u8::MAX {
947                    let _ = decode(&schema, descriptor, &[first, second]);
948                }
949            }
950        });
951        assert!(exhaustive_short_inputs.is_ok());
952
953        let strict_short_inputs = std::panic::catch_unwind(|| {
954            let strict = DecodeOptions {
955                max_message_bytes: 2,
956                max_recursion_depth: 2,
957                max_field_occurrences: 2,
958                max_length_delimited_bytes: 2,
959                max_repeated_values: 2,
960                max_map_entries: 2,
961                max_audit_bytes: 2,
962                unknown_fields: UnknownFieldPolicy::Drop,
963                duplicates: DuplicateInputPolicy::Reject,
964                audit_mode: AuditMode::MetadataOnly,
965                require_minimal_varints: true,
966                booleans: BooleanValuePolicy::RejectNonCanonical,
967                enum_values: EnumValuePolicy::RejectUnknown,
968            };
969            for byte in u8::MIN..=u8::MAX {
970                let _ = decode_with_options(&schema, descriptor, &[byte], &strict);
971            }
972        });
973        assert!(strict_short_inputs.is_ok());
974    }
975
976    /// Runs a reproducible random corpus through compatibility and strict paths.
977    #[test]
978    fn randomized_decoder_smoke_corpus_does_not_panic() {
979        let schema = parse(
980            r#"syntax="proto3";
981               enum Mode { MODE_UNSPECIFIED = 0; MODE_ACTIVE = 1; }
982               message Child { string name = 1; bytes data = 2; }
983               message Packet {
984                 uint64 id = 1;
985                 repeated sint32 samples = 2;
986                 map<string, uint32> counters = 3;
987                 oneof choice { bool enabled = 4; Child child = 5; }
988                 Mode mode = 6;
989               }"#,
990        )
991        .unwrap();
992        let descriptor = schema.message("Packet").unwrap();
993        let strict = DecodeOptions {
994            max_message_bytes: MAX_RANDOMIZED_PACKET_BYTES,
995            max_recursion_depth: 8,
996            max_field_occurrences: 64,
997            max_length_delimited_bytes: 64,
998            max_repeated_values: 32,
999            max_map_entries: 16,
1000            max_audit_bytes: 256,
1001            unknown_fields: UnknownFieldPolicy::Drop,
1002            duplicates: DuplicateInputPolicy::Reject,
1003            audit_mode: AuditMode::MetadataOnly,
1004            require_minimal_varints: true,
1005            booleans: BooleanValuePolicy::RejectNonCanonical,
1006            enum_values: EnumValuePolicy::RejectUnknown,
1007        };
1008        let encoder = EncodeOptions {
1009            forward_unknown_fields: false,
1010            forward_unknown_messages: false,
1011            forward_added_fields: false,
1012            duplicates: DuplicatePolicy::LastOnly,
1013            field_order: FieldOrder::FieldNumber,
1014            map_order: MapOrder::Key,
1015            floats: FloatEncoding::Normalize,
1016            max_output_bytes: 256,
1017        };
1018
1019        let result = std::panic::catch_unwind(|| {
1020            let mut state = RANDOMIZED_DECODER_SEED;
1021            let mut bytes = [0; MAX_RANDOMIZED_PACKET_BYTES];
1022            for _ in 0..RANDOMIZED_DECODER_CASES {
1023                state = state
1024                    .wrapping_mul(RANDOMIZED_DECODER_MULTIPLIER)
1025                    .wrapping_add(RANDOMIZED_DECODER_INCREMENT);
1026                let length = state as usize % (MAX_RANDOMIZED_PACKET_BYTES + 1);
1027                for byte in &mut bytes[..length] {
1028                    state = state
1029                        .wrapping_mul(RANDOMIZED_DECODER_MULTIPLIER)
1030                        .wrapping_add(RANDOMIZED_DECODER_INCREMENT);
1031                    *byte = state as u8;
1032                }
1033                let packet = &bytes[..length];
1034                if let Ok(message) = decode(&schema, descriptor, packet) {
1035                    let _ = encode(&schema, descriptor, &message);
1036                }
1037                if let Ok(message) = decode_with_options(&schema, descriptor, packet, &strict) {
1038                    let _ = encode_with_options(&schema, descriptor, &message, &encoder);
1039                }
1040            }
1041        });
1042        assert!(result.is_ok());
1043    }
1044
1045    /// Verifies syntactic and semantic protobuf schema invariants are enforced.
1046    #[test]
1047    fn rejects_structurally_or_semantically_invalid_schemas() {
1048        let invalid = [
1049            r#"syntax="proto3"; message A { int32 a = 1; int32 b = 1; }"#,
1050            r#"syntax="proto3"; message A { int32 a = 1; string a = 2; }"#,
1051            r#"syntax="proto3"; message A { int32 foo_bar = 1; string fooBar = 2; }"#,
1052            r#"syntax="proto3"; message A {
1053                 int32 first = 1 [json_name="value"];
1054                 string second = 2 [json_name="value"]; }"#,
1055            r#"syntax="proto3"; message A { reserved 1; int32 a = 1; }"#,
1056            r#"syntax="proto3"; message A { reserved "a"; int32 a = 1; }"#,
1057            r#"syntax="proto2"; message A { int32 a = 1; }"#,
1058            r#"syntax="proto3"; message A { required int32 a = 1; }"#,
1059            r#"syntax="proto3"; message A { int32 a = 1 [default = 1]; }"#,
1060            r#"syntax="proto3"; message A { repeated map<string, int32> a = 1; }"#,
1061            r#"syntax="proto3"; message A { oneof x { map<string, int32> a = 1; } }"#,
1062            r#"syntax="proto3"; message A { int32 a = 1 [packed = true]; }"#,
1063            r#"syntax="proto3"; enum E { ONE = 1; }"#,
1064            r#"syntax="proto3"; enum E { ZERO = 0; OTHER = 0; }"#,
1065            r#"syntax="proto3"; enum E { ZERO = 0; BIG = 2147483648; }"#,
1066            r#"syntax="proto3"; message A {} enum A { ZERO = 0; }"#,
1067            r#"syntax="proto3"; message A { message child {} int32 child = 1; }"#,
1068            r#"message A {} syntax="proto3";"#,
1069            r#"syntax="proto3"; package a; package b; message A {}"#,
1070            r#"syntax="proto3"; package a..b; message A {}"#,
1071            r#"syntax="proto3"; nonsense Thing {};"#,
1072            r#"syntax="proto3"; message A { reserved 1 to 3, 3 to 5; }"#,
1073            r#"syntax="proto3"; message A { oneof x { int32 a = 1; } int32 x = 2; }"#,
1074            r#"syntax="proto3"; message B {} message A { repeated B b = 1 [packed=true]; }"#,
1075            r#"syntax="proto2"; message B {} message A { optional B b = 1 [default=x]; }"#,
1076            r#"syntax="proto2"; message A { optional string a = 1 [default="\q"]; }"#,
1077            r#"syntax="proto2"; message A { optional string a = 1 [default="\uD800"]; }"#,
1078            r#"syntax="proto3"; service S { rpc Broken Request returns (Response); }"#,
1079            r#"syntax="proto3"; option = true; message A {}"#,
1080            r#"syntax="proto3"; message A { int32 invalid_octal = 08; }"#,
1081        ];
1082        for source in invalid {
1083            assert!(
1084                parse(source).is_err(),
1085                "schema unexpectedly accepted: {source}"
1086            );
1087        }
1088    }
1089
1090    /// Verifies valid aliases, reservations, maps, oneofs, and packing coexist.
1091    #[test]
1092    fn accepts_semantically_valid_schema_features() {
1093        let schema = parse(
1094            r#"syntax = "proto3";
1095               package valid;
1096               option java_package = "valid.\"escaped";
1097               enum State {
1098                 option allow_alias = true;
1099                 STATE_UNSPECIFIED = 0;
1100                 STATE_READY = 1;
1101                 STATE_READY_ALIAS = 1;
1102               }
1103               message Example {
1104                 reserved 2, 4 to 6;
1105                 reserved "removed";
1106                 oneof choice { string name = 1; State state = 3; }
1107                 repeated State history = 7 [packed = true];
1108                 map<string, int32> counts = 8;
1109               }
1110               message Escaped {
1111                 optional string text = 01;
1112               }"#,
1113        )
1114        .unwrap();
1115        let service_schema = parse(
1116            r#"syntax="proto3";
1117               message Request {}
1118               message Response {}
1119               service Api {
1120                 rpc Call(stream Request) returns (stream Response);
1121               }"#,
1122        )
1123        .unwrap();
1124        assert!(service_schema.message("Request").is_some());
1125        assert!(schema.message("valid.Example").is_some());
1126    }
1127
1128    /// Verifies normal transitive imports do not leak types, while public
1129    /// imports deliberately re-export their declarations.
1130    #[test]
1131    fn enforces_import_visibility() {
1132        let mut registry = Registry::new();
1133        registry.register(
1134            "common.proto",
1135            r#"syntax="proto3"; package common; message Hidden {}"#,
1136        );
1137        registry.register(
1138            "middle.proto",
1139            r#"syntax="proto3"; import "common.proto"; message Middle {}"#,
1140        );
1141        registry.register(
1142            "root.proto",
1143            r#"syntax="proto3"; import "middle.proto"; message Root { common.Hidden value = 1; }"#,
1144        );
1145        assert!(registry.parse("root.proto").is_err());
1146
1147        registry.register(
1148            "middle.proto",
1149            r#"syntax="proto3"; import public "common.proto"; message Middle {}"#,
1150        );
1151        assert!(registry.parse("root.proto").is_ok());
1152    }
1153
1154    /// Unknown legacy groups remain auditable and round-trip without interpretation.
1155    #[test]
1156    fn preserves_unknown_wire_groups_and_rejects_malformed_groups() {
1157        let schema = parse(r#"syntax="proto3"; message Empty {}"#).unwrap();
1158        let descriptor = schema.message("Empty").unwrap();
1159        let group = [0x53, 0x08, 0x07, 0x54];
1160        let message = decode(&schema, descriptor, &group).unwrap();
1161        assert!(
1162            message.audit.iter().any(|record| {
1163                record.tag == AuditTag::UnknownMessage && record.field_number == 10
1164            })
1165        );
1166        assert_eq!(encode(&schema, descriptor, &message).unwrap(), group);
1167        assert!(decode(&schema, descriptor, &[0x53, 0x08, 0x07]).is_err());
1168        assert!(decode(&schema, descriptor, &[0x53, 0x5c]).is_err());
1169        assert!(decode(&schema, descriptor, &[0x54]).is_err());
1170
1171        let nested_schema = parse(
1172            r#"syntax="proto2";
1173               message MessageSetLike { extensions 4 to max; }
1174               message Outer { optional MessageSetLike value = 500; }"#,
1175        )
1176        .unwrap();
1177        let outer = nested_schema.message("Outer").unwrap();
1178        let message_set = [
1179            0xa2, 0x1f, 0x0b, 0x0b, 0x10, 0x90, 0xb3, 0xfc, 0x01, 0x1a, 0x02, 0x48, 0x63, 0x0c,
1180        ];
1181        let message = decode(&nested_schema, outer, &message_set).unwrap();
1182        assert_eq!(
1183            encode(&nested_schema, outer, &message).unwrap(),
1184            message_set
1185        );
1186    }
1187
1188    /// Proto3 rejects legacy declarations while retaining custom-option extensions.
1189    #[test]
1190    fn enforces_proto3_group_and_extension_rules() {
1191        let invalid = [
1192            r#"syntax="proto3"; message A { optional group G = 1 {} }"#,
1193            r#"syntax="proto3"; message A { extensions 100 to 200; }"#,
1194            r#"syntax="proto3"; message A {} extend A { optional int32 x = 100; }"#,
1195        ];
1196        for source in invalid {
1197            assert!(
1198                parse(source).is_err(),
1199                "schema unexpectedly accepted: {source}"
1200            );
1201        }
1202    }
1203
1204    /// Services preserve streaming and method metadata and require message endpoints.
1205    #[test]
1206    fn resolves_and_validates_proto3_services() {
1207        let schema = parse(
1208            r#"syntax="proto3"; package api;
1209               message Request {} message Response {}
1210               service Gateway {
1211                 option deprecated = true;
1212                 rpc Exchange(stream Request) returns (stream Response) {
1213                   option idempotency_level = IDEMPOTENT;
1214                 }
1215               }"#,
1216        )
1217        .unwrap();
1218        let service = schema.service("Gateway").unwrap();
1219        let method = &service.methods[0];
1220        assert_eq!(method.input_type, "api.Request");
1221        assert_eq!(method.output_type, "api.Response");
1222        assert!(method.client_streaming && method.server_streaming);
1223        assert_eq!(method.options[0].name, "idempotency_level");
1224
1225        assert!(
1226            parse(
1227                r#"syntax="proto3"; enum E { ZERO = 0; }
1228               message R {} service S { rpc Bad(E) returns (R); }"#,
1229            )
1230            .is_err()
1231        );
1232        assert!(
1233            parse(
1234                r#"syntax="proto3"; message R {}
1235               service S { rpc Same(R) returns (R); rpc Same(R) returns (R); }"#,
1236            )
1237            .is_err()
1238        );
1239    }
1240
1241    /// Built-in and custom options are retained only after semantic validation.
1242    #[test]
1243    fn resolves_and_validates_proto3_custom_options() {
1244        const DESCRIPTOR_OPTIONS: &str = r#"syntax="proto2"; package google.protobuf;
1245               message MessageOptions { extensions 1000 to max; }
1246               message FieldOptions { extensions 1000 to max; }"#;
1247        let mut registry = Registry::new();
1248        registry.register("google/protobuf/descriptor.proto", DESCRIPTOR_OPTIONS);
1249        registry.register(
1250            "options.proto",
1251            r#"syntax="proto3"; package audit;
1252               import "google/protobuf/descriptor.proto";
1253               message Metadata { string label = 1; }
1254               extend google.protobuf.MessageOptions {
1255                 Metadata annotation = 50001;
1256               }
1257               extend google.protobuf.FieldOptions {
1258                 bool sensitive = 50002;
1259                 repeated string labels = 50003;
1260               }
1261               message Record {
1262                 option (annotation) = { label: "important" };
1263                 string value = 1 [(sensitive) = true, (labels) = "one", (labels) = "two"];
1264               }"#,
1265        );
1266        let schema = registry.parse("options.proto").unwrap();
1267        assert_eq!(schema.custom_options.len(), 3);
1268        let record = schema.message("audit.Record").unwrap();
1269        assert_eq!(record.options[0].name, "(annotation)");
1270        assert_eq!(record.fields[0].options.len(), 3);
1271
1272        let mut imported = Registry::new();
1273        imported.register("google/protobuf/descriptor.proto", DESCRIPTOR_OPTIONS);
1274        imported.register(
1275            "declared_options.proto",
1276            r#"syntax="proto3"; package audit;
1277               import "google/protobuf/descriptor.proto";
1278               message Metadata { string label = 1; }
1279               extend google.protobuf.MessageOptions { Metadata annotation = 50001; }"#,
1280        );
1281        imported.register(
1282            "consumer.proto",
1283            r#"syntax="proto3"; package consumer;
1284               import "declared_options.proto";
1285               message Event { option (audit.annotation).label = "external"; }"#,
1286        );
1287        let imported_schema = imported.parse("consumer.proto").unwrap();
1288        assert_eq!(
1289            imported_schema.message("consumer.Event").unwrap().options[0].name,
1290            "(audit.annotation).label"
1291        );
1292
1293        let invalid_builtin = [
1294            r#"syntax="proto3"; message A { option unknown = true; }"#,
1295            r#"syntax="proto3"; option packed = true; message A {}"#,
1296            r#"syntax="proto3"; message A { option (missing) = true; }"#,
1297        ];
1298        for source in invalid_builtin {
1299            assert!(
1300                parse(source).is_err(),
1301                "schema unexpectedly accepted: {source}"
1302            );
1303        }
1304
1305        let invalid_custom = [
1306            r#"syntax="proto3"; package p; import "google/protobuf/descriptor.proto";
1307               extend google.protobuf.FieldOptions { bool flag = 50001; }
1308               message A { option (flag) = true; }"#,
1309            r#"syntax="proto3"; package p; import "google/protobuf/descriptor.proto";
1310               extend google.protobuf.FieldOptions { bool flag = 50001; }
1311               message A { string x = 1 [(flag) = "yes"]; }"#,
1312            r#"syntax="proto3"; package p; import "google/protobuf/descriptor.proto";
1313               extend google.protobuf.FieldOptions { bool reserved_number = 999; }"#,
1314        ];
1315        for source in invalid_custom {
1316            let mut registry = Registry::new();
1317            registry.register("google/protobuf/descriptor.proto", DESCRIPTOR_OPTIONS);
1318            registry.register("invalid.proto", source);
1319            assert!(
1320                registry.parse("invalid.proto").is_err(),
1321                "schema unexpectedly accepted: {source}"
1322            );
1323        }
1324    }
1325
1326    /// Verifies Edition 2023 declarations expose their official defaults.
1327    #[test]
1328    fn parses_edition_2023_defaults() {
1329        let schema = parse(
1330            r#"edition = "2023";
1331               message Packet { int32 id = 1; repeated uint32 samples = 2; }"#,
1332        )
1333        .unwrap();
1334        assert_eq!(schema.syntax, Syntax::Edition2023);
1335        assert_eq!(schema.features.field_presence, FieldPresence::Explicit);
1336        let packet = schema.message("Packet").unwrap();
1337        assert!(packet.field_by_name("id").unwrap().explicit_presence);
1338        assert_eq!(packet.field_by_name("samples").unwrap().packed, Some(true));
1339    }
1340
1341    /// Verifies inherited Edition features affect field wire metadata.
1342    #[test]
1343    fn resolves_edition_2023_feature_overrides() {
1344        let schema = parse(
1345            r#"edition = "2023";
1346               option features.field_presence = IMPLICIT;
1347               message Packet {
1348                 repeated uint32 samples = 1
1349                   [features.repeated_field_encoding = EXPANDED];
1350                 int32 count = 2 [features.field_presence = EXPLICIT];
1351               }"#,
1352        )
1353        .unwrap();
1354        let packet = schema.message("Packet").unwrap();
1355        assert_eq!(packet.features.field_presence, FieldPresence::Implicit);
1356        assert_eq!(packet.field_by_name("samples").unwrap().packed, Some(false));
1357        assert!(packet.field_by_name("count").unwrap().explicit_presence);
1358    }
1359
1360    /// Verifies Editions reject labels removed from the language grammar.
1361    #[test]
1362    fn rejects_labels_in_edition_2023() {
1363        for label in ["optional", "required"] {
1364            let source =
1365                alloc::format!("edition = \"2023\"; message Packet {{ {label} int32 id = 1; }}");
1366            assert!(parse(&source).is_err());
1367        }
1368    }
1369
1370    /// Verifies Edition feature settings cannot alter proto3 source semantics.
1371    #[test]
1372    fn rejects_editions_features_in_proto3() {
1373        assert!(
1374            parse(
1375                r#"syntax = "proto3";
1376                   option features.field_presence = EXPLICIT;
1377                   message Packet {}"#
1378            )
1379            .is_err()
1380        );
1381    }
1382
1383    /// Verifies DELIMITED known messages use matching group tags and round trip.
1384    #[test]
1385    fn edition_delimited_messages_round_trip() {
1386        let schema = parse(
1387            r#"edition = "2023";
1388               message Child { int32 value = 1; }
1389               message Parent {
1390                 Child child = 2 [features.message_encoding = DELIMITED];
1391               }"#,
1392        )
1393        .unwrap();
1394        let parent = schema.message("Parent").unwrap();
1395        let mut child = Message::new();
1396        child.insert("value", Value::Int32(7));
1397        let mut message = Message::new();
1398        message.insert("child", Value::Message(child));
1399        let bytes = encode(&schema, parent, &message).unwrap();
1400        assert_eq!(bytes, [0x13, 0x08, 0x07, 0x14]);
1401        assert_eq!(decode(&schema, parent, &bytes).unwrap(), message);
1402    }
1403
1404    /// Verifies LEGACY_REQUIRED is enforced during both decode and encode.
1405    #[test]
1406    fn edition_legacy_required_is_enforced() {
1407        let schema = parse(
1408            r#"edition = "2023";
1409               message Packet {
1410                 int32 id = 1 [features.field_presence = LEGACY_REQUIRED];
1411               }"#,
1412        )
1413        .unwrap();
1414        let packet = schema.message("Packet").unwrap();
1415        assert!(decode(&schema, packet, &[]).is_err());
1416        assert!(encode(&schema, packet, &Message::new()).is_err());
1417    }
1418
1419    /// Verifies UTF-8 NONE preserves arbitrary string bytes losslessly.
1420    #[test]
1421    fn edition_unverified_strings_round_trip() {
1422        let schema = parse(
1423            r#"edition = "2023";
1424               message Packet {
1425                 string text = 1 [features.utf8_validation = NONE];
1426               }"#,
1427        )
1428        .unwrap();
1429        let packet = schema.message("Packet").unwrap();
1430        let bytes = [0x0a, 0x02, 0xff, 0xfe];
1431        let decoded = decode(&schema, packet, &bytes).unwrap();
1432        assert_eq!(
1433            decoded.get("text"),
1434            Some(&Value::RawString(vec![0xff, 0xfe]))
1435        );
1436        assert_eq!(encode(&schema, packet, &decoded).unwrap(), bytes);
1437    }
1438
1439    /// Verifies closed enum unknown numbers move to the unknown-field set.
1440    #[test]
1441    fn edition_closed_enum_values_are_unknown_fields() {
1442        let schema = parse(
1443            r#"edition = "2023";
1444               enum State {
1445                 option features.enum_type = CLOSED;
1446                 ZERO = 0;
1447               }
1448               message Packet { State state = 1; }"#,
1449        )
1450        .unwrap();
1451        let packet = schema.message("Packet").unwrap();
1452        let decoded = decode(&schema, packet, &[0x08, 0x07]).unwrap();
1453        assert!(decoded.get("state").is_none());
1454        assert_eq!(decoded.unknown_fields.len(), 1);
1455        assert_eq!(encode(&schema, packet, &decoded).unwrap(), [0x08, 0x07]);
1456    }
1457
1458    /// Verifies descriptor-driven protobuf JSON scalar, enum, repeated, and map mappings.
1459    #[test]
1460    fn protobuf_json_round_trip() {
1461        let schema = parse(
1462            r#"syntax = "proto3";
1463               enum State { ZERO = 0; READY = 1; }
1464               message Packet {
1465                 int64 sequence_id = 1;
1466                 bytes body = 2;
1467                 State state = 3;
1468                 repeated uint32 samples = 4;
1469                 map<string, int32> counts = 5;
1470               }"#,
1471        )
1472        .unwrap();
1473        let packet = schema.message("Packet").unwrap();
1474        let json = r#"{
1475          "sequenceId":"9007199254740993",
1476          "body":"AQI=",
1477          "state":"READY",
1478          "samples":[1,2],
1479          "counts":{"ok":3}
1480        }"#;
1481        let message = decode_json(&schema, packet, json).unwrap();
1482        assert_eq!(
1483            message.get("sequence_id"),
1484            Some(&Value::Int64(9_007_199_254_740_993))
1485        );
1486        assert_eq!(message.get("body"), Some(&Value::Bytes(vec![1, 2])));
1487        let encoded = encode_json(&schema, packet, &message).unwrap();
1488        let reparsed = decode_json(&schema, packet, &encoded).unwrap();
1489        assert_eq!(reparsed, message);
1490    }
1491
1492    /// Verifies typed extensions resolve, participate in the codec, and obey ranges.
1493    #[test]
1494    fn typed_extensions_are_semantically_validated() {
1495        let schema = parse(
1496            r#"edition = "2023";
1497               message Packet { extensions 100 to 199; }
1498               extend Packet { int32 audit_code = 100; }"#,
1499        )
1500        .unwrap();
1501        let packet = schema.message("Packet").unwrap();
1502        let mut message = Message::new();
1503        message.insert("audit_code", Value::Int32(7));
1504        let bytes = encode(&schema, packet, &message).unwrap();
1505        assert_eq!(decode(&schema, packet, &bytes).unwrap(), message);
1506
1507        assert!(
1508            parse(
1509                r#"edition = "2023";
1510                   message Packet { extensions 100 to 199; }
1511                   extend Packet { int32 outside = 200; }"#
1512            )
1513            .is_err()
1514        );
1515        assert!(
1516            parse(
1517                r#"edition = "2023";
1518                   message Packet { int32 regular = 100; extensions 100 to 199; }
1519                   extend Packet { int32 collision = 100; }"#
1520            )
1521            .is_err()
1522        );
1523    }
1524
1525    /// Verifies duplicate JSON names and malformed well-known values are rejected.
1526    #[test]
1527    fn protobuf_json_semantics_reject_invalid_inputs() {
1528        let schema = parse(r#"syntax = "proto3"; message Packet { int32 value = 1; }"#).unwrap();
1529        let packet = schema.message("Packet").unwrap();
1530        assert!(decode_json(&schema, packet, r#"{"value":1,"value":2}"#).is_err());
1531        assert!(decode_json(&schema, packet, r#"{"unknown":1}"#).is_err());
1532        assert!(decode_json(&schema, packet, r#"{"value":"9223372036854775808"}"#).is_err());
1533    }
1534
1535    /// Import cycles and proto3 references to proto2 enums are semantic errors.
1536    #[test]
1537    fn rejects_import_cycles_and_proto2_enum_references() {
1538        let mut registry = Registry::new();
1539        registry.register(
1540            "a.proto",
1541            r#"syntax="proto3"; import "b.proto"; message A {}"#,
1542        );
1543        registry.register(
1544            "b.proto",
1545            r#"syntax="proto3"; import "a.proto"; message B {}"#,
1546        );
1547        assert!(registry.parse("a.proto").is_err());
1548
1549        let mut registry = Registry::new();
1550        registry.register(
1551            "legacy.proto",
1552            r#"syntax="proto2"; package legacy; enum State { ZERO = 0; } message Data {}"#,
1553        );
1554        registry.register(
1555            "root.proto",
1556            r#"syntax="proto3"; import "legacy.proto";
1557               message Invalid { legacy.State state = 1; }"#,
1558        );
1559        assert!(registry.parse("root.proto").is_err());
1560        registry.register(
1561            "root.proto",
1562            r#"syntax="proto3"; import "legacy.proto";
1563               message Valid { legacy.Data data = 1; }"#,
1564        );
1565        assert!(registry.parse("root.proto").is_ok());
1566    }
1567
1568    /// Public raw fields cannot smuggle additional wire occurrences.
1569    #[test]
1570    fn encoder_rejects_malformed_or_trailing_raw_field_data() {
1571        let schema = parse(r#"syntax="proto3"; message Packet {}"#).unwrap();
1572        let packet = schema.message("Packet").unwrap();
1573        let mut message = Message::new();
1574        message.add_field(AddedField {
1575            name: "external".into(),
1576            number: 1,
1577            wire_type: constants::WIRE_TYPE_VARINT,
1578            encoded_value: vec![1, 0x10, 1],
1579        });
1580        assert!(encode(&schema, packet, &message).is_err());
1581
1582        message.added_fields[0].encoded_value = vec![constants::VARINT_CONTINUATION_BIT];
1583        assert!(encode(&schema, packet, &message).is_err());
1584
1585        let mut replay = Message::new();
1586        replay.audit.push(AuditRecord {
1587            tag: AuditTag::DuplicateDiscarded,
1588            field_name: None,
1589            field_number: 1,
1590            wire_type: constants::WIRE_TYPE_VARINT,
1591            encoded_field: vec![0x08, 1, 0x10, 1],
1592        });
1593        let options = EncodeOptions {
1594            duplicates: DuplicatePolicy::PreserveAll,
1595            ..EncodeOptions::default()
1596        };
1597        assert!(encode_with_options(&schema, packet, &replay, &options).is_err());
1598    }
1599
1600    /// Dynamic values must obey repeated, singular, oneof, and map invariants.
1601    #[test]
1602    fn encoder_rejects_semantically_inconsistent_dynamic_messages() {
1603        let schema = parse(
1604            r#"syntax="proto3";
1605               message Packet {
1606                 int32 singular = 1;
1607                 repeated int32 many = 2;
1608                 oneof choice { int32 left = 3; int32 right = 4; }
1609                 map<string, int32> lookup = 5;
1610               }"#,
1611        )
1612        .unwrap();
1613        let packet = schema.message("Packet").unwrap();
1614
1615        let mut singular = Message::new();
1616        singular.insert("singular", Value::Repeated(vec![Value::Int32(1)]));
1617        assert!(encode(&schema, packet, &singular).is_err());
1618
1619        let mut repeated = Message::new();
1620        repeated.insert("many", Value::Int32(1));
1621        assert!(encode(&schema, packet, &repeated).is_err());
1622
1623        let mut oneof = Message::new();
1624        oneof.insert("left", Value::Int32(1));
1625        oneof.insert("right", Value::Int32(2));
1626        assert!(encode(&schema, packet, &oneof).is_err());
1627
1628        let mut map = Message::new();
1629        map.insert(
1630            "lookup",
1631            Value::Map(vec![
1632                (Value::String("key".into()), Value::Int32(1)),
1633                (Value::String("key".into()), Value::Int32(2)),
1634            ]),
1635        );
1636        assert!(encode(&schema, packet, &map).is_err());
1637    }
1638
1639    /// Closed enum descriptors reject programmatically supplied unknown values.
1640    #[test]
1641    fn encoder_rejects_unknown_closed_enum_values() {
1642        let schema = parse(
1643            r#"syntax="proto2"; enum State { ZERO = 0; } message Packet { optional State state = 1; }"#,
1644        )
1645        .unwrap();
1646        let packet = schema.message("Packet").unwrap();
1647        let mut message = Message::new();
1648        message.insert("state", Value::Enum(7));
1649        assert!(encode(&schema, packet, &message).is_err());
1650    }
1651
1652    /// Output budgets are enforced before copying a large length-delimited value.
1653    #[test]
1654    fn encoder_preflights_large_output_appends() {
1655        let schema = parse(r#"syntax="proto3"; message Packet { bytes data = 1; }"#).unwrap();
1656        let packet = schema.message("Packet").unwrap();
1657        let mut message = Message::new();
1658        message.insert("data", Value::Bytes(vec![0; 1024]));
1659        let options = EncodeOptions {
1660            max_output_bytes: 32,
1661            ..EncodeOptions::default()
1662        };
1663        assert!(encode_with_options(&schema, packet, &message, &options).is_err());
1664    }
1665
1666    /// JSON limits stop deeply nested input before semantic traversal.
1667    #[test]
1668    fn json_hardened_profile_bounds_nesting_and_input_size() {
1669        let schema = parse(r#"syntax="proto3"; message Packet {}"#).unwrap();
1670        let packet = schema.message("Packet").unwrap();
1671        let options = JsonDecodeOptions {
1672            max_nesting_depth: 2,
1673            max_input_bytes: 32,
1674            ..JsonDecodeOptions::hardened()
1675        };
1676        assert!(
1677            decode_json_with_options(&schema, packet, r#"{"a":{"b":{"c":0}}}"#, &options).is_err()
1678        );
1679        assert!(decode_json_with_options(&schema, packet, &" ".repeat(33), &options).is_err());
1680    }
1681
1682    /// JSON integer conversion rejects values just beyond 64-bit boundaries.
1683    #[test]
1684    fn json_rejects_rounded_64_bit_integer_overflow() {
1685        let schema =
1686            parse(r#"syntax="proto3"; message Packet { int64 signed = 1; uint64 unsigned = 2; }"#)
1687                .unwrap();
1688        let packet = schema.message("Packet").unwrap();
1689        assert!(decode_json(&schema, packet, r#"{"signed":9.223372036854776e18}"#).is_err());
1690        assert!(decode_json(&schema, packet, r#"{"unsigned":1.8446744073709552e19}"#).is_err());
1691        assert!(decode_json(&schema, packet, r#"{"signed":"1.5e1"}"#).is_ok());
1692    }
1693
1694    /// Bytes JSON rejects invalid padding and data following the padding.
1695    #[test]
1696    fn json_rejects_noncanonical_base64() {
1697        let schema = parse(r#"syntax="proto3"; message Packet { bytes data = 1; }"#).unwrap();
1698        let packet = schema.message("Packet").unwrap();
1699        assert!(decode_json(&schema, packet, r#"{"data":"Zg==junk"}"#).is_err());
1700        assert!(decode_json(&schema, packet, r#"{"data":"Zh=="}"#).is_ok());
1701        assert!(decode_json(&schema, packet, r#"{"data":"Zg=="}"#).is_ok());
1702        assert!(decode_json(&schema, packet, r#"{"data":"Zg"}"#).is_ok());
1703    }
1704
1705    /// Proto-name and jsonName spellings still denote one logical occurrence.
1706    #[test]
1707    fn json_rejects_duplicate_field_aliases_after_implicit_default() {
1708        let schema = parse(r#"syntax="proto3"; message Packet { int32 foo_bar = 1; }"#).unwrap();
1709        let packet = schema.message("Packet").unwrap();
1710        assert!(decode_json(&schema, packet, r#"{"foo_bar":0,"fooBar":1}"#).is_err());
1711    }
1712
1713    /// Timestamp JSON accepts only normalized seconds from zero through 59.
1714    #[test]
1715    fn json_rejects_leap_second_timestamp_spelling() {
1716        let schema = parse(
1717            r#"syntax="proto3"; package google.protobuf;
1718               message Timestamp { int64 seconds = 1; int32 nanos = 2; }"#,
1719        )
1720        .unwrap();
1721        let timestamp = schema.message("google.protobuf.Timestamp").unwrap();
1722        assert!(decode_json(&schema, timestamp, r#""2016-12-31T23:59:60Z""#).is_err());
1723    }
1724
1725    /// Schema parsing exposes finite source, nesting, token, and graph limits.
1726    #[test]
1727    fn hardened_schema_options_bound_untrusted_registries() {
1728        let options = SchemaParseOptions {
1729            max_source_bytes: 32,
1730            ..SchemaParseOptions::hardened()
1731        };
1732        assert!(parse_with_options(&" ".repeat(33), &options).is_err());
1733
1734        let options = SchemaParseOptions {
1735            max_nesting_depth: 1,
1736            ..SchemaParseOptions::hardened()
1737        };
1738        assert!(parse_with_options(r#"message A { message B {} }"#, &options).is_err());
1739
1740        let mut registry = Registry::new();
1741        registry.register("root.proto", r#"import "child.proto"; message Root {}"#);
1742        registry.register("child.proto", "message Child {}");
1743        let options = SchemaParseOptions {
1744            max_registry_files: 1,
1745            ..SchemaParseOptions::hardened()
1746        };
1747        assert!(registry.parse_with_options("root.proto", &options).is_err());
1748    }
1749}