c2pa 0.80.3

Rust SDK for C2PA (Coalition for Content Provenance and Authenticity) implementors
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
// Copyright 2022 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
use std::collections::HashMap;

use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
    assertion::{Assertion, AssertionBase, AssertionCbor, AssertionJson},
    assertions::labels,
    Error,
};

const ASSERTION_CREATION_VERSION: usize = 1;

/// A `Metadata` assertion provides structured metadata using JSON-LD format for
/// both standardized C2PA metadata and custom metadata schemas.
///
/// This assertion contains a context object defining namespace mappings and a set
///of metadata fields. For `c2pa.metadata` assertions, only specific schemas and fields
/// are allowed as defined in the C2PA specification.
///
/// See [metadata_assertions - C2PA Technical Specification](https://spec.c2pa.org/specifications/specifications/2.3/specs/C2PA_Specification.html#_metadata_assertions)
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Metadata {
    /// JSON-LD context mapping prefixes to namespace URIs.
    #[serde(rename = "@context")]
    pub context: HashMap<String, String>,
    /// Metadata fields with namespace prefixes.
    #[serde(flatten)]
    pub value: HashMap<String, Value>,

    /// Custom assertion label (not serialized into content).
    #[serde(skip)]
    custom_metadata_label: Option<String>,
}

impl Metadata {
    /// Creates a new metadata assertion from a JSON-LD string.
    pub fn new(metadata_label: &str, jsonld: &str) -> Result<Self, Error> {
        let metadata = serde_json::from_slice::<Metadata>(jsonld.as_bytes())
            .map_err(|e| Error::BadParam(format!("Invalid JSON format: {e}")))?;

        // is this a standard c2pa.metadata assertion or a custom field
        let custom_metadata_label = if metadata_label != labels::METADATA {
            Some(metadata_label.to_owned())
        } else {
            None
        };

        Ok(Self {
            context: metadata.context,
            value: metadata.value,
            custom_metadata_label,
        })
    }

    /// Validates that each field in the assertion has a namespace within the '@context'.
    /// For 'c2pa.metadata' assertions, ensures only allowed fields are present.
    ///
    /// See [_c2pa_metadata_validation - C2PA Technical Specification](https://spec.c2pa.org/specifications/specifications/2.3/specs/C2PA_Specification.html#_c2pa_metadata_validation)
    /// # Returns
    /// * Returns `true` if the metadata assertion passes validation.
    pub fn is_valid(&self) -> bool {
        if self.context.is_empty() {
            return false;
        }

        if self.label() == labels::METADATA {
            for (namespace, uri) in &self.context {
                if let Some(expected_uri) = ALLOWED_SCHEMAS.get(namespace.as_str()) {
                    if uri != expected_uri {
                        // check the backcompat list
                        if let Some(bcl) = BACKCOMPAT_LIST.get(namespace.as_str()) {
                            if !bcl.iter().any(|v| v == uri) {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }
                }
            }
        }

        for label in self.value.keys() {
            if let Some((prefix, _)) = label.split_once(':') {
                if !self.context.contains_key(prefix) {
                    return false;
                }
            }
            if self.label() == labels::METADATA && !ALLOWED_FIELDS.contains(&label.as_str()) {
                return false;
            }
        }
        true
    }

    /// Get the label for the metadata
    pub fn get_label(&self) -> &str {
        self.label()
    }
}

impl AssertionJson for Metadata {}
impl AssertionCbor for Metadata {}

impl AssertionBase for Metadata {
    const LABEL: &'static str = labels::METADATA;
    const VERSION: Option<usize> = Some(ASSERTION_CREATION_VERSION);

    fn label(&self) -> &str {
        match &self.custom_metadata_label {
            Some(cm) => cm,
            None => Self::LABEL,
        }
    }

    fn to_assertion(&self) -> Result<Assertion, Error> {
        Self::to_json_assertion(self)
    }

    fn from_assertion(assertion: &Assertion) -> Result<Self, Error> {
        let mut metadata = Self::from_json_assertion(assertion).or_else(|_e| {
            // some older files may have stored this in error as cbor
            Self::from_cbor_assertion(assertion)
        })?;

        metadata.custom_metadata_label =
            (assertion.label() != labels::METADATA).then(|| assertion.label().to_owned());

        Ok(metadata)
    }
}

lazy_static! {
    /// The c2pa.metadata assertion shall only contain certain schemas.
    ///
    /// See [metadata_annex - C2PA Technical Specification](https://spec.c2pa.org/specifications/specifications/2.3/specs/C2PA_Specification.html#metadata_annex)
    static ref ALLOWED_SCHEMAS: HashMap<&'static str, &'static str> = vec![
        ("xmp", "http://ns.adobe.com/xap/1.0/"),
        ("xmpMM", "http://ns.adobe.com/xap/1.0/mm/"),
        ("xmpTPg", "http://ns.adobe.com/xap/1.0/t/pg/"),
        ("crs", "http://ns.adobe.com/camera-raw-settings/1.0/"),
        ("pdf", "http://ns.adobe.com/pdf/1.3/"),
        ("dc", "http://purl.org/dc/elements/1.1/"),
        ("Iptc4xmpExt", "http://iptc.org/std/Iptc4xmpExt/2008-02-29/"),
        ("exif", "http://ns.adobe.com/exif/1.0/"),
        ("exifEX", "http://cipa.jp/exif/1.0/"),
        ("photoshop", "http://ns.adobe.com/photoshop/1.0/"),
        ("tiff", "http://ns.adobe.com/tiff/1.0/"),
        ("xmpDM", "http://ns.adobe.com/xmp/1.0/DynamicMedia/"),
        ("plus", "http://ns.useplus.org/ldf/xmp/1.0/"),
    ]
    .into_iter()
    .collect();

    // list is to support versions that have changed since the current spec
    static ref BACKCOMPAT_LIST: HashMap<&'static str, Vec<&'static str>> = vec![
        ("exifEX", vec!["http://cipa.jp/exif/1.0/exifEX", "http://cipa.jp/exif/2.32/"])
    ]
    .into_iter()
    .collect();
}

/// The c2pa.metadata assertion shall only contain certain fields.
///
/// See [metadata_annex - C2PA Technical Specification](https://spec.c2pa.org/specifications/specifications/2.3/specs/C2PA_Specification.html#metadata_annex)
static ALLOWED_FIELDS: [&str; 292] = [
    // xmp:
    "xmp:CreateDate",
    "xmp:CreatorTool",
    "xmp:Identifier",
    "xmp:Label",
    "xmp:MetadataDate",
    "xmp:ModifyDate",
    "xmp:Rating",
    "xmp:BaseURL",
    "xmp:Nickname",
    "xmp:Thumbnails",
    // xmpMM:
    "xmpMM:DerivedFrom",
    "xmpMM:DocumentID",
    "xmpMM:InstanceID",
    "xmpMM:OriginalDocumentID",
    "xmpMM:RenditionClass",
    "xmpMM:RenditionParams",
    "xmpMM:History",
    "xmpMM:Ingredients",
    "xmpMM:Pantry",
    "xmpMM:ManagedFrom",
    "xmpMM:Manager",
    "xmpMM:ManageTo",
    "xmpMM:ManageUI",
    "xmpMM:ManagerVariant",
    "xmpMM:VersionID",
    "xmpMM:Versions",
    // xmpTPg:
    "xmpTPg:Colorants",
    "xmpTPg:Fonts",
    "xmpTPg:MaxPageSize",
    "xmpTPg:NPages",
    "xmpTPg:PlateNames",
    // crs:
    "crs:AutoBrightness",
    "crs:AutoContrast",
    "crs:AutoExposure",
    "crs:AutoShadows",
    "crs:BlueHue",
    "crs:BlueSaturation",
    "crs:Brightness",
    "crs:CameraProfile",
    "crs:ChromaticAberrationB",
    "crs:ChromaticAberrationR",
    "crs:ColorNoiseReduction",
    "crs:Contrast",
    "crs:CropTop",
    "crs:CropLeft",
    "crs:CropBottom",
    "crs:CropRight",
    "crs:CropAngle",
    "crs:CropWidth",
    "crs:CropHeight",
    "crs:CropUnits",
    "crs:Exposure",
    "crs:GreenHue",
    "crs:GreenSaturation",
    "crs:HasCrop",
    "crs:HasSettings",
    "crs:LuminanceSmoothing",
    "crs:RawFileName",
    "crs:RedHue",
    "crs:RedSaturation",
    "crs:Saturation",
    "crs:Shadows",
    "crs:ShadowTint",
    "crs:Sharpness",
    "crs:Temperature",
    "crs:Tint",
    "crs:ToneCurve",
    "crs:ToneCurveName",
    "crs:Version",
    "crs:VignetteAmount",
    "crs:VignetteMidpoint",
    "crs:WhiteBalance",
    // pdf:
    "pdf:Keywords",
    "pdf:PDFVersion",
    "pdf:Producer",
    "pdf:Trapped",
    // dc:
    "dc:coverage",
    "dc:date",
    "dc:format",
    "dc:identifier",
    "dc:language",
    "dc:relation",
    "dc:type",
    // Iptc4xmpExt:
    "Iptc4xmpExt:DigImageGUID",
    "Iptc4xmpExt:DigitalSourceType",
    "Iptc4xmpExt:EventId",
    "Iptc4xmpExt:Genre",
    "Iptc4xmpExt:ImageRating",
    "Iptc4xmpExt:ImageRegion",
    "Iptc4xmpExt:RegistryId",
    "Iptc4xmpExt:LocationCreated",
    "Iptc4xmpExt:LocationShown",
    "Iptc4xmpExt:MaxAvailHeight",
    "Iptc4xmpExt:MaxAvailWidth",
    // exif:
    "exif:ApertureValue",
    "exif:BrightnessValue",
    "exif:CFAPattern",
    "exif:ColorSpace",
    "exif:CompressedBitsPerPixel",
    "exif:Contrast",
    "exif:CustomRendered",
    "exif:DateTimeDigitized",
    "exif:DateTimeOriginal",
    "exif:DeviceSettingDescription",
    "exif:DigitalZoomRatio",
    "exif:ExifVersion",
    "exif:ExposureBiasValue",
    "exif:ExposureIndex",
    "exif:ExposureMode",
    "exif:ExposureProgram",
    "exif:ExposureTime",
    "exif:FileSource",
    "exif:Flash",
    "exif:FlashEnergy",
    "exif:FlashpixVersion",
    "exif:FNumber",
    "exif:FocalLength",
    "exif:FocalLengthIn35mmFilm",
    "exif:FocalPlaneResolutionUnit",
    "exif:FocalPlaneXResolution",
    "exif:FocalPlaneYResolution",
    "exif:GainControl",
    "exif:ImageUniqueID",
    "exif:ISOSpeedRatings",
    "exif:LightSource",
    "exif:MaxApertureValue",
    "exif:MeteringMode",
    "exif:OECF",
    "exif:OffsetTimeOriginal",
    "exif:PixelXDimension",
    "exif:PixelYDimension",
    "exif:RelatedSoundFile",
    "exif:Saturation",
    "exif:SceneCaptureType",
    "exif:SceneType",
    "exif:SensingMethod",
    "exif:Sharpness",
    "exif:ShutterSpeedValue",
    "exif:SpatialFrequencyResponse",
    "exif:SpectralSensitivity",
    "exif:SubjectArea",
    "exif:SubjectDistance",
    "exif:SubjectDistanceRange",
    "exif:SubjectLocation",
    "exif:WhiteBalance",
    "exif:GPSAltitude",
    "exif:GPSAltitudeRef",
    "exif:GPSDateStamp",
    "exif:GPSDestBearing",
    "exif:GPSDestBearingRef",
    "exif:GPSDestDistance",
    "exif:GPSDestDistanceRef",
    "exif:GPSDestLatitude",
    "exif:GPSDestLongitude",
    "exif:GPSDifferential",
    "exif:GPSDOP",
    "exif:GPSHPositioningError",
    "exif:GPSImgDirection",
    "exif:GPSImgDirectionRef",
    "exif:GPSLatitude",
    "exif:GPSLongitude",
    "exif:GPSMapDatum",
    "exif:GPSMeasureMode",
    "exif:GPSProcessingMethod",
    "exif:GPSSatellites",
    "exif:GPSSpeed",
    "exif:GPSSpeedRef",
    "exif:GPSStatus",
    "exif:GPSTimeStamp",
    "exif:GPSTrack",
    "exif:GPSTrackRef",
    "exif:GPSVersionID",
    // exifEX:
    "exifEX:BodySerialNumber",
    "exifEX:Gamma",
    "exifEX:InteroperabilityIndex",
    "exifEX:ISOSpeed",
    "exifEX:ISOSpeedLatitudeyyy",
    "exifEX:ISOSpeedLatitudezzz",
    "exifEX:LensMake",
    "exifEX:LensModel",
    "exifEX:LensSerialNumber",
    "exifEX:LensSpecification",
    "exifEX:PhotographicSensitivity",
    "exifEX:RecommendedExposureIndex",
    "exifEX:SensitivityType",
    "exifEX:StandardOutput-Sensitivity",
    // photoshop:
    "photoshop:Category",
    "photoshop:City",
    "photoshop:ColorMode",
    "photoshop:Country",
    "photoshop:DateCreated",
    "photoshop:DocumentAncestors",
    "photoshop:History",
    "photoshop:ICCProfile",
    "photoshop:State",
    "photoshop:SupplementalCategories",
    "photoshop:TextLayers",
    "photoshop:TransmissionReference",
    "photoshop:Urgency",
    // tiff:
    "tiff:BitsPerSample",
    "tiff:Compression",
    "tiff:DateTime",
    "tiff:ImageLength",
    "tiff:ImageWidth",
    "tiff:Make",
    "tiff:Model",
    "tiff:Orientation",
    "tiff:PhotometricInterpretation",
    "tiff:PlanarConfiguration",
    "tiff:PrimaryChromaticities",
    "tiff:ReferenceBlackWhite",
    "tiff:ResolutionUnit",
    "tiff:SamplesPerPixel",
    "tiff:Software",
    "tiff:TransferFunction",
    "tiff:WhitePoint",
    "tiff:XResolution",
    "tiff:YResolution",
    "tiff:YCbCrCoefficients",
    "tiff:YCbCrPositioning",
    "tiff:YCbCrSubSampling",
    // xmpDM:
    "xmpDM:absPeakAudioFilePath",
    "xmpDM:album",
    "xmpDM:altTapeName",
    "xmpDM:altTimecode",
    "xmpDM:audioChannelType",
    "xmpDM:audioCompressor",
    "xmpDM:audioSampleRate",
    "xmpDM:audioSampleType",
    "xmpDM:beatSpliceParams",
    "xmpDM:cameraAngle",
    "xmpDM:cameraLabel",
    "xmpDM:cameraModel",
    "xmpDM:cameraMove",
    "xmpDM:comment",
    "xmpDM:contributedMedia",
    "xmpDM:duration",
    "xmpDM:fileDataRate",
    "xmpDM:genre",
    "xmpDM:good",
    "xmpDM:instrument",
    "xmpDM:introTime",
    "xmpDM:key",
    "xmpDM:logComment",
    "xmpDM:loop",
    "xmpDM:numberOfBeats",
    "xmpDM:markers",
    "xmpDM:outCue",
    "xmpDM:projectName",
    "xmpDM:projectRef",
    "xmpDM:pullDown",
    "xmpDM:relativePeakAudioFilePath",
    "xmpDM:relativeTimestamp",
    "xmpDM:releaseDate",
    "xmpDM:resampleParams",
    "xmpDM:scaleType",
    "xmpDM:scene",
    "xmpDM:shotDate",
    "xmpDM:shotDay",
    "xmpDM:shotLocation",
    "xmpDM:shotName",
    "xmpDM:shotNumber",
    "xmpDM:shotSize",
    "xmpDM:speakerPlacement",
    "xmpDM:startTimecode",
    "xmpDM:stretchMode",
    "xmpDM:takeNumber",
    "xmpDM:tapeName",
    "xmpDM:tempo",
    "xmpDM:timeScaleParams",
    "xmpDM:timeSignature",
    "xmpDM:trackNumber",
    "xmpDM:Tracks",
    "xmpDM:videoAlphaMode",
    "xmpDM:videoAlphaPremultipleColor",
    "xmpDM:videoAlphaUnityIsTransparent",
    "xmpDM:videoColorSpace",
    "xmpDM:videoCompressor",
    "xmpDM:videoFieldOrder",
    "xmpDM:videoFrameRate",
    "xmpDM:videoFrameSize",
    "xmpDM:videoPixelAspectRatio",
    "xmpDM:videoPixelDepth",
    "xmpDM:partOfCompilation",
    "xmpDM:lyrics",
    "xmpDM:discNumber",
    // plus:
    "plus:FileNameAsDelivered",
    "plus:FirstPublicationDate",
    "plus:ImageFileFormatAsDelivered",
    "plus:ImageFileSizeAsDelivered",
    "plus:ImageType",
    "plus:Version",
];

#[cfg(test)]
pub mod tests {
    #![allow(clippy::expect_used)]
    #![allow(clippy::unwrap_used)]

    use crate::{
        assertion::AssertionBase,
        assertions::{
            labels::{CAWG_METADATA, METADATA},
            metadata::Metadata,
        },
    };

    const SPEC_EXAMPLE: &str = r#"{
        "@context" : {
            "exif": "http://ns.adobe.com/exif/1.0/",
            "exifEX": "http://cipa.jp/exif/1.0/",
            "tiff": "http://ns.adobe.com/tiff/1.0/",
            "Iptc4xmpExt": "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
            "photoshop" : "http://ns.adobe.com/photoshop/1.0/"
        },
        "photoshop:DateCreated": "Aug 31, 2022",
        "Iptc4xmpExt:DigitalSourceType": "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
        "exif:GPSVersionID": "2.2.0.0",
        "exif:GPSLatitude": "39,21.102N",
        "exif:GPSLongitude": "74,26.5737W",
        "exif:GPSAltitudeRef": 0,
        "exif:GPSAltitude": "100963/29890",
        "exif:GPSTimeStamp": "18:22:57",
        "exif:GPSDateStamp": "2019:09:22",
        "exif:GPSSpeedRef": "K",
        "exif:GPSSpeed": "4009/161323",
        "exif:GPSImgDirectionRef": "T",
        "exif:GPSImgDirection": "296140/911",
        "exif:GPSDestBearingRef": "T",
        "exif:GPSDestBearing": "296140/911",
        "exif:GPSHPositioningError": "13244/2207",
        "exif:ExposureTime": "1/100",
        "exif:FNumber": 4.0,
        "exif:ColorSpace": 1,
        "exif:DigitalZoomRatio": 2.0,
        "tiff:Make": "CameraCompany",
        "tiff:Model": "Shooter S1",
        "exifEX:LensMake": "CameraCompany",
        "exifEX:LensModel": "17.0-35.0 mm",
        "exifEX:LensSpecification": { "@list": [ 1.55, 4.2, 1.6, 2.4 ] }
    }"#;

    const CAWG_METADATA_EXAMPLE: &str = r#" {
        "@context" : {
            "dc" : "http://purl.org/dc/elements/1.1/"
        },
        "dc:created": "2025 August 13", 
        "dc:creator": [
             "John Doe"
        ]
        }
        "#;

    const CUSTOM_METADATA: &str = r#" {
        "@context" : {
            "bar": "http://foo.com/bar/1.0/"
        },
        "bar:baz" : "foo"
        }
        "#;

    const MISSING_CONTEXT: &str = r#" {
        "@context" : {
            "exif": "http://ns.adobe.com/exif/1.0/"
        },
        "exif:GPSVersionID": "2.2.0.0",
        "exif:GPSLatitude": "39,21.102N",
        "exif:GPSLongitude": "74,26.5737W",
        "tiff:Make": "CameraCompany",
        "tiff:Model": "Shooter S1"
        }
        "#;
    const EMPTY_CONTEXT: &str = r#" {
        "@context" : {
        }
        }
        "#;

    const MISMATCH_URI: &str = r#" {
        "@context" : {
            "exif": "http://ns.adobe.com/exif/10.0/"
        },
        "exif:GPSVersionID": "2.2.0.0",
        "exif:GPSLatitude": "39,21.102N",
        "exif:GPSLongitude": "74,26.5737W"
        }
        "#;

    const BACKCOMPAT: &str = r#" {
        "@context" : {
            "exif": "http://ns.adobe.com/exif/1.0/",
            "exifEX": "http://cipa.jp/exif/2.32/",
            "tiff": "http://ns.adobe.com/tiff/1.0/",
            "Iptc4xmpExt": "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
            "photoshop" : "http://ns.adobe.com/photoshop/1.0/"
        },
        "photoshop:DateCreated": "Aug 31, 2022",
        "Iptc4xmpExt:DigitalSourceType": "https://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
        "exif:GPSVersionID": "2.2.0.0",
        "exif:GPSLatitude": "39,21.102N",
        "exif:GPSLongitude": "74,26.5737W",
        "exif:GPSAltitudeRef": 0,
        "exif:GPSAltitude": "100963/29890",
        "exifEX:LensSpecification": { "@list": [ 1.55, 4.2, 1.6, 2.4 ] }
    }
    "#;

    #[test]
    fn metadata_from_json() {
        let metadata = Metadata::new(METADATA, SPEC_EXAMPLE).unwrap();
        assert!(metadata.is_valid());
    }

    #[test]
    fn assertion_round_trip() {
        let metadata = Metadata::new(METADATA, SPEC_EXAMPLE).unwrap();
        let assertion = metadata.to_assertion().unwrap();
        let result = Metadata::from_assertion(&assertion).unwrap();
        assert_eq!(metadata, result);
    }

    #[test]
    fn backcompat() {
        let metadata = Metadata::new(METADATA, BACKCOMPAT).unwrap();
        assert!(metadata.is_valid());
    }

    #[test]
    fn assertion_custom_round_trip() {
        let metadata = Metadata::new("custom.metadata", CUSTOM_METADATA).unwrap();
        let assertion = metadata.to_assertion().unwrap();
        let result = Metadata::from_assertion(&assertion).unwrap();
        assert_eq!(metadata, result);
    }

    #[test]
    fn test_custom_validation() {
        let mut metadata = Metadata::new("custom.metadata", CUSTOM_METADATA).unwrap();
        assert!(metadata.is_valid());
        // c2pa.metadata has restrictions on fields
        metadata.custom_metadata_label = Some(METADATA.to_owned());
        assert!(!metadata.is_valid());
    }

    #[test]
    fn test_cawg_metadata() {
        let metadata = Metadata::new(CAWG_METADATA, CAWG_METADATA_EXAMPLE).unwrap();
        assert!(metadata.is_valid());
    }

    #[test]
    fn test_field_not_in_context() {
        let mut metadata = Metadata::new("custom.metadata", MISSING_CONTEXT).unwrap();
        assert!(!metadata.is_valid());
        metadata.custom_metadata_label = Some(METADATA.to_owned());
        assert!(!metadata.is_valid());
    }

    #[test]
    fn test_uri_is_not_allowed() {
        let mut metadata = Metadata::new(METADATA, MISMATCH_URI).unwrap();
        assert!(!metadata.is_valid());
        // custom metadata does not have restriction on uris
        metadata.custom_metadata_label = Some("custom.metadata".to_owned());
        assert!(metadata.is_valid());
    }

    #[test]
    fn test_empty_context() {
        let metadata = Metadata::new(METADATA, EMPTY_CONTEXT).unwrap();
        assert!(!metadata.is_valid());
    }
}