Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename pouf::Json to Pouf1 #394

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 28 additions & 19 deletions tuf/src/pouf/cjson/pretty.rs → interop-tests/src/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
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.
///
/// This is identical to [Json] in all manners except for the `canonicalize` method. Instead of
/// writing the metadata in the canonical format, it first canonicalizes it, then pretty prints
/// the metadata.
/// This is identical to [tuf::pouf::Pouf1] in all manners except for the `canonicalize` method.
/// Instead of writing the metadata in the canonical format, it first canonicalizes it, then pretty
/// prints the metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonPretty;

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