Skip to content

Commit 8e60df7

Browse files
lukesteensenerickt
authored andcommitted
add tests for additional_fields behavior
Signed-off-by: Luke Steensen <luke.steensen@gmail.com>
1 parent 58e0139 commit 8e60df7

1 file changed

Lines changed: 186 additions & 1 deletion

File tree

tuf/src/metadata.rs

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,61 @@ mod test {
25802580
assert_eq!(decoded, root);
25812581
}
25822582

2583+
#[test]
2584+
fn serde_root_metadata_additional_fields() {
2585+
let jsn = json!({
2586+
"_type": "root",
2587+
"spec_version": "1.0",
2588+
"version": 1,
2589+
"expires": "2017-01-01T00:00:00Z",
2590+
"consistent_snapshot": true,
2591+
"keys": {
2592+
"09557ed63f91b5b95917d46f66c63ea79bdaef1b008ba823808bca849f1d18a1": {
2593+
"keytype": "ed25519",
2594+
"scheme": "ed25519",
2595+
"keyid_hash_algorithms": ["sha256", "sha512"],
2596+
"keyval": {
2597+
"public": "1410ae3053aa70bbfa98428a879d64d3002a3578f7dfaaeb1cb0764e860f7e0b",
2598+
},
2599+
},
2600+
},
2601+
"roles": {
2602+
"root": {
2603+
"threshold": 1,
2604+
"keyids": ["09557ed63f91b5b95917d46f66c63ea79bdaef1b008ba823808bca849f1d18a1"],
2605+
},
2606+
"snapshot": {
2607+
"threshold": 1,
2608+
"keyids": ["09557ed63f91b5b95917d46f66c63ea79bdaef1b008ba823808bca849f1d18a1"],
2609+
},
2610+
"targets": {
2611+
"threshold": 1,
2612+
"keyids": ["09557ed63f91b5b95917d46f66c63ea79bdaef1b008ba823808bca849f1d18a1"],
2613+
},
2614+
"timestamp": {
2615+
"threshold": 1,
2616+
"keyids": ["09557ed63f91b5b95917d46f66c63ea79bdaef1b008ba823808bca849f1d18a1"],
2617+
},
2618+
},
2619+
// additional_fields
2620+
"custom": {
2621+
"foo": 42,
2622+
"bar": "baz",
2623+
},
2624+
"quux": true,
2625+
});
2626+
2627+
let root: RootMetadata = serde_json::from_value(jsn.clone()).unwrap();
2628+
assert_eq!(
2629+
root.additional_fields()["custom"],
2630+
json!({"foo": 42, "bar": "baz"})
2631+
);
2632+
assert_eq!(root.additional_fields()["quux"], json!(true));
2633+
2634+
// make sure additional_fields are passed through serialization as well
2635+
assert_eq!(jsn, serde_json::to_value(&root).unwrap());
2636+
}
2637+
25832638
fn jsn_root_metadata_without_keyid_hash_algos() -> serde_json::Value {
25842639
json!({
25852640
"_type": "root",
@@ -2882,6 +2937,41 @@ mod test {
28822937
assert_eq!(decoded, timestamp);
28832938
}
28842939

2940+
#[test]
2941+
fn serde_timestamp_metadata_additional_fields() {
2942+
let jsn = json!({
2943+
"_type": "timestamp",
2944+
"spec_version": "1.0",
2945+
"version": 1,
2946+
"expires": "2017-01-01T00:00:00Z",
2947+
"meta": {
2948+
"snapshot.json": {
2949+
"version": 1,
2950+
"length": 100,
2951+
"hashes": {
2952+
"sha256": "",
2953+
},
2954+
},
2955+
},
2956+
// additional_fields
2957+
"custom": {
2958+
"foo": 42,
2959+
"bar": "baz",
2960+
},
2961+
"quux": true,
2962+
});
2963+
2964+
let timestamp: TimestampMetadata = serde_json::from_value(jsn.clone()).unwrap();
2965+
assert_eq!(
2966+
timestamp.additional_fields()["custom"],
2967+
json!({"foo": 42, "bar": "baz"})
2968+
);
2969+
assert_eq!(timestamp.additional_fields()["quux"], json!(true));
2970+
2971+
// make sure additional_fields are passed through serialization as well
2972+
assert_eq!(jsn, serde_json::to_value(&timestamp).unwrap());
2973+
}
2974+
28852975
// Deserialize timestamp metadata with optional length and hashes
28862976
#[test]
28872977
fn serde_timestamp_metadata_without_length_and_hashes() {
@@ -2901,7 +2991,7 @@ mod test {
29012991
"snapshot.json": {
29022992
"version": 1
29032993
},
2904-
}
2994+
},
29052995
});
29062996

29072997
let encoded = serde_json::to_value(&timestamp).unwrap();
@@ -2996,6 +3086,41 @@ mod test {
29963086
assert_eq!(decoded, snapshot);
29973087
}
29983088

3089+
#[test]
3090+
fn serde_snapshot_metadata_additional_fields() {
3091+
let jsn = json!({
3092+
"_type": "snapshot",
3093+
"spec_version": "1.0",
3094+
"version": 1,
3095+
"expires": "2017-01-01T00:00:00Z",
3096+
"meta": {
3097+
"targets.json": {
3098+
"version": 1,
3099+
"length": 100,
3100+
"hashes": {
3101+
"sha256": "",
3102+
},
3103+
},
3104+
},
3105+
// additional_fields
3106+
"custom": {
3107+
"foo": 42,
3108+
"bar": "baz",
3109+
},
3110+
"quux": true,
3111+
});
3112+
3113+
let snapshot: SnapshotMetadata = serde_json::from_value(jsn.clone()).unwrap();
3114+
assert_eq!(
3115+
snapshot.additional_fields()["custom"],
3116+
json!({"foo": 42, "bar": "baz"})
3117+
);
3118+
assert_eq!(snapshot.additional_fields()["quux"], json!(true));
3119+
3120+
// make sure additional_fields are passed through serialization as well
3121+
assert_eq!(jsn, serde_json::to_value(&snapshot).unwrap());
3122+
}
3123+
29993124
// Deserialize snapshot metadata with optional length and hashes
30003125
#[test]
30013126
fn serde_snapshot_optional_length_and_hashes() {
@@ -3117,6 +3242,66 @@ mod test {
31173242
})
31183243
}
31193244

3245+
#[test]
3246+
fn serde_targets_metadata_additional_fields() {
3247+
let jsn = json!({
3248+
"_type": "targets",
3249+
"spec_version": "1.0",
3250+
"version": 1,
3251+
"expires": "2017-01-01T00:00:00Z",
3252+
"targets": {
3253+
"insert-target-from-slice": {
3254+
"length": 3,
3255+
"hashes": {
3256+
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483\
3257+
bfa0f98a5e886266e7ae",
3258+
},
3259+
},
3260+
"insert-target-description-from-slice-with-custom": {
3261+
"length": 3,
3262+
"hashes": {
3263+
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483\
3264+
bfa0f98a5e886266e7ae",
3265+
},
3266+
},
3267+
"insert-target-from-reader": {
3268+
"length": 3,
3269+
"hashes": {
3270+
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483\
3271+
bfa0f98a5e886266e7ae",
3272+
},
3273+
},
3274+
"insert-target-description-from-reader-with-custom": {
3275+
"length": 3,
3276+
"hashes": {
3277+
"sha256": "2c26b46b68ffc68ff99b453c1d30413413422d706483\
3278+
bfa0f98a5e886266e7ae",
3279+
},
3280+
"custom": {
3281+
"foo": 1,
3282+
"bar": "baz",
3283+
},
3284+
},
3285+
},
3286+
// additional_fields
3287+
"custom": {
3288+
"foo": 42,
3289+
"bar": "baz",
3290+
},
3291+
"quux": true,
3292+
});
3293+
3294+
let targets: TargetsMetadata = serde_json::from_value(jsn.clone()).unwrap();
3295+
assert_eq!(
3296+
targets.additional_fields()["custom"],
3297+
json!({"foo": 42, "bar": "baz"})
3298+
);
3299+
assert_eq!(targets.additional_fields()["quux"], json!(true));
3300+
3301+
// make sure additional_fields are passed through serialization as well
3302+
assert_eq!(jsn, serde_json::to_value(&targets).unwrap());
3303+
}
3304+
31203305
#[test]
31213306
fn serde_targets_with_delegations_metadata() {
31223307
let key = Ed25519PrivateKey::from_pkcs8(ED25519_1_PK8).unwrap();

0 commit comments

Comments
 (0)