opentdf-protocol 0.16.1

OpenTDF protocol types and structures (no crypto, no I/O)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! TDF Manifest structures
//!
//! This module contains the data structures for TDF manifests, including:
//! - Payload information
//! - Encryption configuration
//! - Key access objects
//! - Integrity information (segments and root signature)
//! - Assertions (statement + binding)
//!
//! Note: Cryptographic operations (HMAC, policy binding generation) are in the crypto crate.

use serde::{Deserialize, Serialize};

/// The TDF spec version written into `schemaVersion` by every writer.
pub const TDF_SPEC_VERSION: &str = "4.3.0";

/// TDF manifest structure
#[derive(Debug, Serialize, Deserialize)]
pub struct TdfManifest {
    pub payload: Payload,
    #[serde(rename = "encryptionInformation")]
    pub encryption_information: EncryptionInformation,
    #[serde(rename = "schemaVersion", skip_serializing_if = "Option::is_none")]
    pub schema_version: Option<String>,
    /// OpenTDF specification version. Omitted by default so existing
    /// manifests serialize unchanged; the `gguf-tdf/1` writer sets it.
    #[serde(rename = "tdf_spec_version", skip_serializing_if = "Option::is_none")]
    pub tdf_spec_version: Option<String>,
    /// `gguf-tdf/1` hybrid index. Absent for every other TDF profile.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gguf: Option<crate::GgufIndex>,
    /// Optional top-level `assertions` array (spec: assertion.md). Empty
    /// when absent; omitted from JSON when empty so existing manifests
    /// serialize unchanged.
    #[serde(
        default,
        deserialize_with = "null_as_empty_vec",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub assertions: Vec<Assertion>,
}

/// Accept `"assertions": null` (Jackson-style writers emit it for an empty
/// list) as an empty vector. `#[serde(default)]` alone covers only a missing key.
fn null_as_empty_vec<'de, D>(deserializer: D) -> Result<Vec<Assertion>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    Ok(Option::<Vec<Assertion>>::deserialize(deserializer)?.unwrap_or_default())
}

/// A verifiable statement about the TDF or its payload (spec: assertion.md).
///
/// Field names and JSON shape match the OpenTDF spec and the Go SDK
/// (`sdk/assertion.go`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Assertion {
    /// Unique identifier for this assertion within the manifest.
    pub id: String,
    /// Categorizes the assertion's purpose, e.g. `handling` or `other`.
    #[serde(rename = "type")]
    pub assertion_type: String,
    /// What the assertion applies to: `tdo` or `payload`.
    pub scope: String,
    /// Whether the statement applies to `encrypted` or `unencrypted` data.
    /// Optional in the spec (default `encrypted`); omitted when unset.
    #[serde(rename = "appliesToState", skip_serializing_if = "Option::is_none")]
    pub applies_to_state: Option<String>,
    /// The assertion content (spec: assertion_statement.md).
    pub statement: AssertionStatement,
    /// Cryptographic binding of the assertion to this TDF
    /// (spec: assertion_binding.md). Omitted when unsigned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub binding: Option<AssertionBinding>,
}

/// The `statement` object of an assertion (spec: assertion_statement.md).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AssertionStatement {
    /// How `value` is encoded: `json-structured`, `base64binary`, `string`, ...
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub format: String,
    /// Optional URI identifying the schema of `value`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,
    /// The assertion content. Kept as raw JSON (string *or* structured
    /// object/array/number/bool) so it round-trips byte-for-byte for
    /// JCS hashing and never gets re-typed.
    #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
    pub value: serde_json::Value,
}

/// The `binding` object of an assertion (spec: assertion_binding.md).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
///
/// Both fields default to empty so that `"binding": {}`, which the Go SDK
/// emits for an unsigned assertion (non-pointer struct with `omitempty`
/// string fields), parses and re-serializes unchanged.
pub struct AssertionBinding {
    /// Signature method, e.g. `jws`.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub method: String,
    /// The Base64URL-encoded signature (e.g. a JWS compact serialization).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub signature: String,
}

/// Payload reference in TDF manifest
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Payload {
    #[serde(rename = "type")]
    pub payload_type: String,
    pub url: String,
    pub protocol: String,
    #[serde(rename = "isEncrypted")]
    pub is_encrypted: bool,
    #[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    #[serde(rename = "tdf_spec_version", skip_serializing_if = "Option::is_none")]
    pub tdf_spec_version: Option<String>,
}

impl Default for Payload {
    fn default() -> Self {
        Self {
            payload_type: "reference".to_string(),
            url: "0.payload".to_string(),
            protocol: "zip".to_string(),
            is_encrypted: true,
            mime_type: Some("application/octet-stream".to_string()),
            // Match go/Swift: version lives on root `schemaVersion`, not payload.
            // Emitting payload.tdf_spec_version breaks strict readers (e.g. otdf-python
            // ManifestPayload, which has no such field).
            tdf_spec_version: None,
        }
    }
}

/// Encryption information in TDF manifest
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionInformation {
    #[serde(rename = "type")]
    pub encryption_type: String,
    #[serde(rename = "keyAccess")]
    pub key_access: Vec<KeyAccess>,
    pub method: EncryptionMethod,
    #[serde(rename = "integrityInformation")]
    pub integrity_information: IntegrityInformation,
    pub policy: String,
}

/// Policy binding structure
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PolicyBinding {
    pub alg: String,
    pub hash: String,
}

/// Key access type constants for TDF3
pub mod key_access_type {
    /// Standard RSA-wrapped key (RSA-OAEP)
    pub const WRAPPED: &str = "wrapped";
    /// EC-wrapped key (ECIES: ECDH + HKDF + AES-GCM)
    pub const EC_WRAPPED: &str = "ec-wrapped";
    /// Remote key access (key stored on KAS)
    pub const REMOTE: &str = "remote";
    /// Remote wrapped key access
    pub const REMOTE_WRAPPED: &str = "remoteWrapped";
}

/// Key access object in manifest
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct KeyAccess {
    #[serde(rename = "type")]
    pub access_type: String,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,
    /// Optional key split (share) identifier (spec: key_access_object.md `sid`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sid: Option<String>,
    pub protocol: String,
    #[serde(rename = "wrappedKey")]
    pub wrapped_key: String,
    #[serde(rename = "policyBinding")]
    pub policy_binding: PolicyBinding,
    #[serde(rename = "encryptedMetadata", skip_serializing_if = "Option::is_none")]
    pub encrypted_metadata: Option<String>,
    #[serde(rename = "schemaVersion", skip_serializing_if = "Option::is_none")]
    pub schema_version: Option<String>,
    /// Ephemeral public key for EC-wrapped keys (PEM format)
    #[serde(rename = "ephemeralPublicKey", skip_serializing_if = "Option::is_none")]
    pub ephemeral_public_key: Option<String>,
}

impl KeyAccess {
    /// Creates a new KeyAccess object with default values (RSA-wrapped)
    pub fn new(url: String) -> Self {
        KeyAccess {
            access_type: key_access_type::WRAPPED.to_string(),
            url,
            kid: None,
            sid: None,
            protocol: "kas".to_string(),
            wrapped_key: String::new(),
            policy_binding: PolicyBinding {
                alg: "HS256".to_string(),
                hash: String::new(),
            },
            encrypted_metadata: None,
            schema_version: Some("1.0".to_string()),
            ephemeral_public_key: None,
        }
    }

    /// Creates a new KeyAccess object for EC-wrapped keys
    pub fn new_ec_wrapped(url: String) -> Self {
        KeyAccess {
            access_type: key_access_type::EC_WRAPPED.to_string(),
            url,
            kid: None,
            sid: None,
            protocol: "kas".to_string(),
            wrapped_key: String::new(),
            policy_binding: PolicyBinding {
                alg: "HS256".to_string(),
                hash: String::new(),
            },
            encrypted_metadata: None,
            schema_version: Some("1.0".to_string()),
            ephemeral_public_key: None,
        }
    }

    /// Check if this key access uses EC wrapping
    pub fn is_ec_wrapped(&self) -> bool {
        self.access_type == key_access_type::EC_WRAPPED
    }

    /// Check if this key access uses RSA wrapping
    pub fn is_rsa_wrapped(&self) -> bool {
        self.access_type == key_access_type::WRAPPED
    }

    /// Set the ephemeral public key (for EC-wrapped keys)
    pub fn set_ephemeral_public_key(&mut self, pem: String) {
        self.ephemeral_public_key = Some(pem);
    }
}

/// Encryption method configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionMethod {
    pub algorithm: String,
    /// Spec: required. Pre-4.3.0 Java manifests omit it; default false.
    #[serde(rename = "isStreamable", default)]
    pub is_streamable: bool,
    /// Spec prose: required. Every streaming SDK carries per-segment IVs in
    /// the payload and writes `""` or omits the field (Java), so it defaults
    /// to empty on read.
    #[serde(default)]
    pub iv: String,
}

impl Default for EncryptionMethod {
    fn default() -> Self {
        Self {
            algorithm: "AES-256-GCM".to_string(),
            is_streamable: true,
            iv: String::new(),
        }
    }
}

/// Integrity information including segments and root signature
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntegrityInformation {
    #[serde(rename = "rootSignature")]
    pub root_signature: RootSignature,
    #[serde(rename = "segmentHashAlg")]
    pub segment_hash_alg: String,
    pub segments: Vec<Segment>,
    #[serde(rename = "segmentSizeDefault")]
    pub segment_size_default: u64,
    #[serde(rename = "encryptedSegmentSizeDefault")]
    pub encrypted_segment_size_default: u64,
}

impl Default for IntegrityInformation {
    fn default() -> Self {
        Self {
            root_signature: RootSignature::default(),
            segment_hash_alg: "GMAC".to_string(),
            segments: Vec::new(),
            segment_size_default: 1024 * 1024,                // 1MB
            encrypted_segment_size_default: 1024 * 1024 + 28, // +IV+tag
        }
    }
}

impl IntegrityInformation {
    /// Resolve every segment's `(plaintext_size, encrypted_size)`.
    ///
    /// Spec (integrity_information.md): `segmentSize` and
    /// `encryptedSegmentSize` are optional on a segment and are inferred from
    /// `segmentSizeDefault` / `encryptedSegmentSizeDefault` when omitted.
    pub fn segment_sizes(&self) -> Vec<(u64, u64)> {
        self.segments
            .iter()
            .map(|s| {
                (
                    s.segment_size.unwrap_or(self.segment_size_default),
                    s.encrypted_segment_size
                        .unwrap_or(self.encrypted_segment_size_default),
                )
            })
            .collect()
    }
}

/// Root signature for integrity verification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootSignature {
    pub alg: String,
    pub sig: String,
}

impl Default for RootSignature {
    fn default() -> Self {
        Self {
            alg: "HS256".to_string(),
            sig: String::new(),
        }
    }
}

/// Segment information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Segment {
    pub hash: String,
    #[serde(rename = "segmentSize", skip_serializing_if = "Option::is_none")]
    pub segment_size: Option<u64>,
    #[serde(
        rename = "encryptedSegmentSize",
        skip_serializing_if = "Option::is_none"
    )]
    pub encrypted_segment_size: Option<u64>,
}

impl TdfManifest {
    /// Creates a new TDF manifest with basic structure
    pub fn new(payload_url: String, kas_url: String) -> Self {
        TdfManifest {
            payload: Payload {
                payload_type: "reference".to_string(),
                url: payload_url,
                protocol: "zip".to_string(),
                is_encrypted: true,
                mime_type: Some("application/octet-stream".to_string()),
                // Omit by default — go/Swift only set root schemaVersion.
                tdf_spec_version: None,
            },
            encryption_information: EncryptionInformation {
                encryption_type: "split".to_string(),
                key_access: vec![KeyAccess::new(kas_url)],
                method: EncryptionMethod {
                    algorithm: "AES-256-GCM".to_string(),
                    is_streamable: true,
                    iv: String::new(),
                },
                integrity_information: IntegrityInformation {
                    root_signature: RootSignature {
                        alg: "HS256".to_string(),
                        sig: String::new(),
                    },
                    segment_hash_alg: "GMAC".to_string(),
                    segments: Vec::new(),
                    segment_size_default: 1024 * 1024, // 1MB default
                    encrypted_segment_size_default: 1024 * 1024 + 28, // +IV+tag
                },
                policy: String::new(),
            },
            schema_version: Some(TDF_SPEC_VERSION.to_string()),
            tdf_spec_version: None,
            gguf: None,
            assertions: Vec::new(),
        }
    }

    /// Resolve the spec version a peer wrote, in priority order:
    /// root `schemaVersion` (what every SDK writes), then root `tdf_spec_version`
    /// (spec prose), then `payload.tdf_spec_version` (spec JSON schema).
    pub fn spec_version(&self) -> Option<&str> {
        [
            self.schema_version.as_deref(),
            self.tdf_spec_version.as_deref(),
            self.payload.tdf_spec_version.as_deref(),
        ]
        .into_iter()
        .flatten()
        .find(|v| !v.is_empty())
    }

    /// Set the policy for the manifest using a raw string
    pub fn set_policy_raw(&mut self, policy: &str) {
        use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
        self.encryption_information.policy = BASE64.encode(policy);
    }

    /// Get the decoded policy from the manifest as a raw string
    pub fn get_policy_raw(&self) -> Result<String, base64::DecodeError> {
        use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
        let bytes = BASE64.decode(&self.encryption_information.policy)?;
        String::from_utf8(bytes)
            .map_err(|err| base64::DecodeError::InvalidByte(err.utf8_error().valid_up_to(), 0))
    }

    /// Add a segment to the manifest
    pub fn add_segment(
        &mut self,
        hash: String,
        segment_size: Option<u64>,
        encrypted_segment_size: Option<u64>,
    ) {
        self.encryption_information
            .integrity_information
            .segments
            .push(Segment {
                hash,
                segment_size,
                encrypted_segment_size,
            });
    }

    /// Serialize to JSON
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Deserialize from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

impl KeyAccess {
    /// Set encrypted metadata
    pub fn set_encrypted_metadata(&mut self, metadata: &str) {
        use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
        self.encrypted_metadata = Some(BASE64.encode(metadata));
    }

    /// Clear encrypted metadata
    pub fn clear_encrypted_metadata(&mut self) {
        self.encrypted_metadata = None;
    }
}

#[cfg(test)]
mod gguf_index_tests {
    use super::*;

    #[test]
    fn gguf_index_round_trips_and_stays_absent_by_default() {
        // Existing manifests must serialize unchanged: no new keys appear.
        let plain = TdfManifest::new(
            "0.payload".to_string(),
            "https://kas.example.com".to_string(),
        );
        let value: serde_json::Value = serde_json::from_str(&plain.to_json().unwrap()).unwrap();
        assert!(
            value.get("gguf").is_none(),
            "gguf must be absent when unset: {value}"
        );
        assert!(
            value.get("tdf_spec_version").is_none(),
            "tdf_spec_version must be absent when unset: {value}"
        );

        // The gguf-tdf/1 writer sets both and they round-trip.
        let mut m = TdfManifest::new("header".to_string(), "https://kas.example.com".to_string());
        m.tdf_spec_version = Some("4.3.0".to_string());
        m.gguf = Some(crate::GgufIndex {
            profile: crate::GGUF_TDF_PROFILE_V1.to_string(),
            alignment: 32,
            header_bytes: 64,
            virtual_size: 352,
            max_segment: 128,
            tensors: vec![crate::GgufTensor {
                name: "token_embd.weight".to_string(),
                offset: 64,
                size: 256,
                segments: [1, 3],
            }],
            segments: vec![crate::GgufSegment {
                id: 0,
                kind: crate::GgufSegmentKind::Header,
                plain: 64,
                entry: "header".to_string(),
            }],
        });

        let json = m.to_json().unwrap();
        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(value["tdf_spec_version"], "4.3.0");
        assert_eq!(value["gguf"]["profile"], "gguf-tdf/1");
        assert_eq!(value["gguf"]["headerBytes"], 64);
        assert_eq!(value["gguf"]["maxSegment"], 128);
        assert_eq!(value["gguf"]["segments"][0]["kind"], "header");
        assert_eq!(value["gguf"]["tensors"][0]["segments"][1], 3);

        let back = TdfManifest::from_json(&json).unwrap();
        assert_eq!(back.gguf.as_ref().unwrap().virtual_size, 352);
        assert_eq!(back.tdf_spec_version.as_deref(), Some("4.3.0"));
    }
}

#[cfg(test)]
mod spec_version_tests {
    use super::*;

    fn minimal_json(top: &str, payload_extra: &str) -> String {
        format!(
            r#"{{
            "payload": {{"type":"reference","url":"0.payload","protocol":"zip","isEncrypted":true{payload_extra}}},
            "encryptionInformation": {{
                "type":"split","keyAccess":[],
                "method":{{"algorithm":"AES-256-GCM","isStreamable":true,"iv":""}},
                "integrityInformation":{{"rootSignature":{{"alg":"HS256","sig":""}},"segmentHashAlg":"GMAC","segments":[],"segmentSizeDefault":0,"encryptedSegmentSizeDefault":0}},
                "policy":""
            }}{top}
        }}"#
        )
    }

    #[test]
    fn spec_version_prefers_schema_version() {
        let m = TdfManifest::from_json(&minimal_json(
            r#","schemaVersion":"4.3.0","tdf_spec_version":"9.9.9""#,
            r#","tdf_spec_version":"8.8.8""#,
        ))
        .unwrap();
        assert_eq!(m.spec_version(), Some("4.3.0"));
    }

    #[test]
    fn spec_version_then_top_level_tdf_spec_version() {
        let m = TdfManifest::from_json(&minimal_json(
            r#","tdf_spec_version":"9.9.9""#,
            r#","tdf_spec_version":"8.8.8""#,
        ))
        .unwrap();
        assert_eq!(m.spec_version(), Some("9.9.9"));
    }

    #[test]
    fn spec_version_then_payload_tdf_spec_version() {
        let m =
            TdfManifest::from_json(&minimal_json("", r#","tdf_spec_version":"8.8.8""#)).unwrap();
        assert_eq!(m.spec_version(), Some("8.8.8"));
    }

    #[test]
    fn spec_version_absent_is_none() {
        let m = TdfManifest::from_json(&minimal_json("", "")).unwrap();
        assert_eq!(m.spec_version(), None);
    }

    #[test]
    fn new_manifest_writes_schema_version_4_3_0_and_no_tdf_spec_version() {
        let m = TdfManifest::new(
            "0.payload".to_string(),
            "https://kas.example.com".to_string(),
        );
        let v: serde_json::Value = serde_json::from_str(&m.to_json().unwrap()).unwrap();
        assert_eq!(v["schemaVersion"], TDF_SPEC_VERSION);
        assert_eq!(v["schemaVersion"], "4.3.0");
        assert!(v.get("tdf_spec_version").is_none());
        assert!(v["payload"].get("tdf_spec_version").is_none());
    }
}

#[cfg(test)]
mod peer_method_tolerance_tests {
    use super::*;

    /// Java SDK manifests omit `method.iv` (and the oldest omit `isStreamable`).
    #[test]
    fn method_without_iv_or_is_streamable_parses() {
        let m: EncryptionMethod = serde_json::from_str(r#"{"algorithm":"AES-256-GCM"}"#).unwrap();
        assert_eq!(m.algorithm, "AES-256-GCM");
        assert!(!m.is_streamable);
        assert_eq!(m.iv, "");

        let m: EncryptionMethod =
            serde_json::from_str(r#"{"algorithm":"AES-256-GCM","isStreamable":true}"#).unwrap();
        assert!(m.is_streamable);
        assert_eq!(m.iv, "");
    }
}

#[cfg(test)]
mod sid_and_assertion_tests {
    use super::*;

    /// Manifest carrying `keyAccess[].sid` and one assertion whose
    /// `statement.value` is a structured JSON object (spec: assertion_statement.md).
    const MANIFEST_WITH_SID_AND_ASSERTION: &str = r#"{
        "payload": {"type":"reference","url":"0.payload","protocol":"zip","isEncrypted":true,"mimeType":"text/plain"},
        "encryptionInformation": {
            "type":"split",
            "keyAccess":[{
                "type":"wrapped",
                "url":"https://kas.example.com",
                "kid":"r1",
                "sid":"split-id-1",
                "protocol":"kas",
                "wrappedKey":"AAAA",
                "policyBinding":{"alg":"HS256","hash":"BBBB"}
            }],
            "method":{"algorithm":"AES-256-GCM","isStreamable":true,"iv":""},
            "integrityInformation":{"rootSignature":{"alg":"HS256","sig":""},"segmentHashAlg":"GMAC","segments":[],"segmentSizeDefault":0,"encryptedSegmentSizeDefault":0},
            "policy":""
        },
        "assertions":[{
            "id":"nato-label-1",
            "type":"handling",
            "scope":"payload",
            "appliesToState":"encrypted",
            "statement":{
                "schema":"urn:nato:stanag:4774:confidentialitymetadatalabel:1:0",
                "format":"json-structured",
                "value":{
                    "Xmlns":"urn:nato:stanag:4774:confidentialitymetadatalabel:1:0",
                    "CreationTime":"2015-08-29T16:15:00Z",
                    "ConfidentialityInformation":{"PolicyIdentifier":"NATO","Classification":"SECRET","nested":[1,true,null,{"k":"v"}]}
                }
            },
            "binding":{"method":"jws","signature":"eyJhbGciOiJSUzI1NiJ9.e30.sig"}
        }],
        "schemaVersion":"4.3.0"
    }"#;

    #[test]
    fn manifest_round_trips_key_access_sid_and_assertions() {
        let input: serde_json::Value =
            serde_json::from_str(MANIFEST_WITH_SID_AND_ASSERTION).unwrap();

        let m = TdfManifest::from_json(MANIFEST_WITH_SID_AND_ASSERTION).unwrap();

        // Typed access documents the intent, not just Value equality.
        assert_eq!(
            m.encryption_information.key_access[0].sid.as_deref(),
            Some("split-id-1")
        );
        assert_eq!(m.assertions.len(), 1);
        let a = &m.assertions[0];
        assert_eq!(a.id, "nato-label-1");
        assert_eq!(a.assertion_type, "handling");
        assert_eq!(a.scope, "payload");
        assert_eq!(a.applies_to_state.as_deref(), Some("encrypted"));
        assert_eq!(a.statement.format, "json-structured");
        assert_eq!(
            a.statement.schema.as_deref(),
            Some("urn:nato:stanag:4774:confidentialitymetadatalabel:1:0")
        );
        assert!(
            a.statement.value.is_object(),
            "statement.value must stay structured JSON: {:?}",
            a.statement.value
        );
        let b = a.binding.as_ref().expect("binding present");
        assert_eq!(b.method, "jws");
        assert_eq!(b.signature, "eyJhbGciOiJSUzI1NiJ9.e30.sig");

        // Re-serialize: nothing dropped, nothing invented.
        let output: serde_json::Value = serde_json::from_str(&m.to_json().unwrap()).unwrap();
        assert_eq!(output, input);
    }

    #[test]
    fn manifest_with_string_statement_value_round_trips() {
        let json = MANIFEST_WITH_SID_AND_ASSERTION
            .replace(r#""format":"json-structured","#, r#""format":"string","#);
        // Swap the object value for a plain string value.
        let mut input: serde_json::Value = serde_json::from_str(&json).unwrap();
        input["assertions"][0]["statement"]["value"] =
            serde_json::Value::String("plain text".into());

        let m: TdfManifest = serde_json::from_value(input.clone()).unwrap();
        assert_eq!(
            m.assertions[0].statement.value,
            serde_json::json!("plain text")
        );

        let output = serde_json::to_value(&m).unwrap();
        assert_eq!(output, input);
    }

    #[test]
    fn manifest_without_sid_or_assertions_serializes_unchanged() {
        // Existing manifests (no sid, no assertions) must not grow new keys.
        let m = TdfManifest::new(
            "0.payload".to_string(),
            "https://kas.example.com".to_string(),
        );
        assert!(m.encryption_information.key_access[0].sid.is_none());
        assert!(m.assertions.is_empty());

        let v: serde_json::Value = serde_json::from_str(&m.to_json().unwrap()).unwrap();
        assert!(v.get("assertions").is_none(), "{v}");
        assert!(
            v["encryptionInformation"]["keyAccess"][0]
                .get("sid")
                .is_none(),
            "{v}"
        );

        // And a peer manifest that omits both still parses (serde default).
        let json = MANIFEST_WITH_SID_AND_ASSERTION.replace(r#""sid":"split-id-1","#, "");
        let mut without: serde_json::Value = serde_json::from_str(&json).unwrap();
        without.as_object_mut().unwrap().remove("assertions");
        let m: TdfManifest = serde_json::from_value(without).unwrap();
        assert!(m.assertions.is_empty());
        assert!(m.encryption_information.key_access[0].sid.is_none());
    }

    /// Jackson-style writers serialize an empty list as `null`. serde's
    /// `default` covers only a missing key, so this must be handled explicitly.
    #[test]
    fn assertions_null_parses_as_empty() {
        let json = r#"{
            "payload": {"type":"reference","url":"0.payload","protocol":"zip","isEncrypted":true},
            "encryptionInformation": {
                "type":"split",
                "keyAccess":[],
                "method":{"algorithm":"AES-256-GCM","isStreamable":true,"iv":""},
                "integrityInformation":{"rootSignature":{"alg":"HS256","sig":""},"segmentHashAlg":"GMAC","segments":[],"segmentSizeDefault":0,"encryptedSegmentSizeDefault":0},
                "policy":""
            },
            "assertions": null
        }"#;
        let m = TdfManifest::from_json(json).expect("null assertions must parse");
        assert!(m.assertions.is_empty());
    }

    /// Go's `Assertion.Binding` is a non-pointer struct with `omitempty` on its
    /// string fields, so an unsigned assertion serializes as `"binding": {}`.
    #[test]
    fn unsigned_assertion_with_empty_binding_object_parses_and_round_trips() {
        let json = r#"{"id":"a1","type":"handling","scope":"payload","statement":{"format":"string","value":"x"},"binding":{}}"#;
        let a: Assertion = serde_json::from_str(json).expect("empty binding object must parse");
        let binding = a.binding.as_ref().expect("binding object is present");
        assert_eq!(binding.method, "");
        assert_eq!(binding.signature, "");

        let out = serde_json::to_value(&a).unwrap();
        assert_eq!(
            out,
            serde_json::from_str::<serde_json::Value>(json).unwrap()
        );
    }
}

#[cfg(test)]
mod segment_size_fallback_tests {
    use super::*;

    #[test]
    fn segment_sizes_fall_back_to_integrity_defaults_when_omitted() {
        let mut info = IntegrityInformation {
            segment_size_default: 1000,
            encrypted_segment_size_default: 1028,
            ..IntegrityInformation::default()
        };
        // Spec (integrity_information.md): segmentSize / encryptedSegmentSize are
        // optional and inferred from the *Default values when omitted.
        info.segments.push(Segment {
            hash: "a".into(),
            segment_size: None,
            encrypted_segment_size: None,
        });
        // Explicit values always win (a re-encrypted or short tail segment).
        info.segments.push(Segment {
            hash: "b".into(),
            segment_size: Some(7),
            encrypted_segment_size: Some(35),
        });
        // Mixed: only one side omitted.
        info.segments.push(Segment {
            hash: "c".into(),
            segment_size: Some(1000),
            encrypted_segment_size: None,
        });

        assert_eq!(
            info.segment_sizes(),
            vec![(1000, 1028), (7, 35), (1000, 1028)]
        );
    }
}