Skip to content

Commit

Permalink
Rename pouf::Json to Pouf1
Browse files Browse the repository at this point in the history
After a discussion with the TUF community, we decided it made more sense
to name our implementation of [POUF-1] as `Pouf1`, not `Json`, because it
has additional restrictions on top of JSON. Furthermore, it doesn't make
sense to call this [CJSON] since TUF doesn't allow for raw bytes in
strings.

This also moves `JsonPretty` into the interop-tests, since this is only
used to make the interop tests easier to review. I've filed [161] with
the TUF spec to consider allowing whitespace in POUF-1. If this is accepted
we can move this back into the tuf::pouf submodule.

[POUF-1]: https://github.com/theupdateframework/taps/blob/master/POUFs/reference-POUF/pouf1.md
[161]: theupdateframework/taps#161
[CJSON]: https://wiki.laptop.org/go/Canonical_JSON
  • Loading branch information
erickt committed Sep 28, 2022
1 parent 9fcf8c7 commit 49f7868
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 238 deletions.
4 changes: 3 additions & 1 deletion interop-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use tuf::metadata::{
MetadataPath, MetadataVersion, Role, SnapshotMetadataBuilder, TargetPath,
TargetsMetadataBuilder, TimestampMetadataBuilder,
};
use tuf::pouf::JsonPretty;
use tuf::repo_builder::RepoBuilder;
use tuf::repository::{FileSystemRepository, FileSystemRepositoryBuilder, RepositoryStorage};
use walkdir::WalkDir;

mod pretty;
pub use pretty::JsonPretty;

// These structs and functions are necessary to parse keys.json, which contains the keys
// used by go-tuf to generate the equivalent metadata. We use the same keys to facilitate
// compatibility testing.
Expand Down
41 changes: 25 additions & 16 deletions tuf/src/pouf/cjson/pretty.rs → interop-tests/src/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use serde::de::DeserializeOwned;
use serde::ser::Serialize;

use super::Json;
use crate::pouf::Pouf;
use crate::Result;
use {
serde::de::DeserializeOwned,
serde::ser::Serialize,
tuf::{
pouf::{Pouf, Pouf1},
Result,
},
};

/// Pretty JSON data pouf.
///
Expand All @@ -17,16 +19,19 @@ impl Pouf for JsonPretty {
type RawData = serde_json::Value;

/// ```
/// # use tuf::pouf::{Pouf, JsonPretty};
/// # use interop_tests::JsonPretty;
/// # use tuf::pouf::Pouf;
/// #
/// assert_eq!(JsonPretty::extension(), "json");
/// ```
fn extension() -> &'static str {
Json::extension()
Pouf1::extension()
}

/// ```
/// # use interop_tests::JsonPretty;
/// # use serde_json::json;
/// # use tuf::pouf::{Pouf, JsonPretty};
/// # use tuf::pouf::Pouf;
/// #
/// let json = json!({
/// "o": {
Expand Down Expand Up @@ -57,17 +62,18 @@ impl Pouf for JsonPretty {
/// }"#);
/// ```
fn canonicalize(raw_data: &Self::RawData) -> Result<Vec<u8>> {
let bytes = Json::canonicalize(raw_data)?;
let bytes = Pouf1::canonicalize(raw_data)?;
Ok(serde_json::to_vec_pretty(&Self::from_slice::<
Self::RawData,
>(&bytes)?)?)
}

/// ```
/// # use interop_tests::JsonPretty;
/// # use serde_derive::Deserialize;
/// # use serde_json::json;
/// # use std::collections::HashMap;
/// # use tuf::pouf::{Pouf, JsonPretty};
/// # use tuf::pouf::Pouf;
/// #
/// #[derive(Deserialize, Debug, PartialEq)]
/// struct Thing {
Expand All @@ -84,14 +90,15 @@ impl Pouf for JsonPretty {
where
T: DeserializeOwned,
{
Json::deserialize(raw_data)
Pouf1::deserialize(raw_data)
}

/// ```
/// # use interop_tests::JsonPretty;
/// # use serde_derive::Serialize;
/// # use serde_json::json;
/// # use std::collections::HashMap;
/// # use tuf::pouf::{Pouf, JsonPretty};
/// # use tuf::pouf::Pouf;
/// #
/// #[derive(Serialize)]
/// struct Thing {
Expand All @@ -108,19 +115,21 @@ impl Pouf for JsonPretty {
where
T: Serialize,
{
Json::serialize(data)
Pouf1::serialize(data)
}

/// ```
/// # use tuf::pouf::{Pouf, JsonPretty};
/// # use interop_tests::JsonPretty;
/// # use std::collections::HashMap;
/// # use tuf::pouf::Pouf;
/// #
/// let jsn: &[u8] = br#"{"foo": "bar", "baz": "quux"}"#;
/// let _: HashMap<String, String> = JsonPretty::from_slice(&jsn).unwrap();
/// ```
fn from_slice<T>(slice: &[u8]) -> Result<T>
where
T: DeserializeOwned,
{
Json::from_slice(slice)
Pouf1::from_slice(slice)
}
}
11 changes: 6 additions & 5 deletions interop-tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@
use assert_matches::assert_matches;
use futures_executor::block_on;
use futures_util::io::AsyncReadExt;
use interop_tests::JsonPretty;
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use tuf::client::{Client, Config};
use tuf::crypto::PublicKey;
use tuf::metadata::{MetadataPath, MetadataVersion, RawSignedMetadata, RootMetadata, TargetPath};
use tuf::pouf::{Json, JsonPretty, Pouf};
use tuf::pouf::{Pouf, Pouf1};
use tuf::repository::{
EphemeralRepository, FileSystemRepository, FileSystemRepositoryBuilder, RepositoryProvider,
};

#[test]
fn fuchsia_go_tuf_consistent_snapshot_false() {
test_key_rotation::<Json>(
test_key_rotation::<Pouf1>(
Path::new("tests")
.join("fuchsia-go-tuf-5527fe")
.join("consistent-snapshot-false"),
Expand All @@ -59,7 +60,7 @@ fn fuchsia_go_tuf_consistent_snapshot_false() {

#[test]
fn fuchsia_go_tuf_consistent_snapshot_true() {
test_key_rotation::<Json>(
test_key_rotation::<Pouf1>(
Path::new("tests")
.join("fuchsia-go-tuf-5527fe")
.join("consistent-snapshot-true"),
Expand All @@ -68,7 +69,7 @@ fn fuchsia_go_tuf_consistent_snapshot_true() {

#[test]
fn fuchsia_go_tuf_transition_m4_consistent_snapshot_false() {
test_key_rotation::<Json>(
test_key_rotation::<Pouf1>(
Path::new("tests")
.join("fuchsia-go-tuf-transition-M4")
.join("consistent-snapshot-false"),
Expand All @@ -77,7 +78,7 @@ fn fuchsia_go_tuf_transition_m4_consistent_snapshot_false() {

#[test]
fn fuchsia_go_tuf_transition_m4_consistent_snapshot_true() {
test_key_rotation::<Json>(
test_key_rotation::<Pouf1>(
Path::new("tests")
.join("fuchsia-go-tuf-transition-M4")
.join("consistent-snapshot-true"),
Expand Down
Loading

0 comments on commit 49f7868

Please sign in to comment.