Skip to content

Commit

Permalink
pb-rust: fixup tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Pan <[email protected]>
  • Loading branch information
tnytown committed Feb 2, 2024
1 parent 0587941 commit a9ebad0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 91 deletions.
12 changes: 8 additions & 4 deletions gen/pb-rust/sigstore-protobuf-specs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ fn main() -> anyhow::Result<()> {
];

let mut config = prost_build::Config::new();
config.include_file("mod.rs").type_attribute(
".",
"#[derive(derive::Deserialize_proto, derive::Serialize_proto)]",
);
config
.include_file("mod.rs")
.type_attribute(
".",
"#[derive(derive::Deserialize_proto, derive::Serialize_proto)]",
)
// Disable problematic comments interpreted as doctests.
.disable_comments([".io.intoto.Envelope"]);

prost_reflect_build::Builder::new()
.file_descriptor_set_bytes("crate::FILE_DESCRIPTOR_SET_BYTES")
Expand Down
72 changes: 0 additions & 72 deletions gen/pb-rust/sigstore-protobuf-specs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,3 @@ static FILE_DESCRIPTOR_SET_BYTES: &'static [u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin"));

include!(concat!(env!("OUT_DIR"), "/mod.rs"));

#[cfg(test)]
mod tests {
use std::io;

use crate::dev::sigstore::bundle::v1::Bundle;
macro_rules! include_asset {
($path:literal) => {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/", $path))
};
}

struct SpaceSeparatorFormatter {}

impl serde_json::ser::Formatter for SpaceSeparatorFormatter {
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}

fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + io::Write,
{
writer.write_all(b": ")
}

fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}
}

/// Test re-serializing a known-good bundle from sigstore-python.
#[test]
fn bundle_roundtrip() {
// Deserialize bundle, trimming trailing whitespace.
let input = include_asset!("a.txt.sigstore").trim_end();
let bundle: Bundle = serde_json::from_str(input).expect("failed to deserialize Bundle!");

// Re-serialize bundle with our python-like formatter.
let formatter = SpaceSeparatorFormatter {};
let mut result = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut result, formatter);
serde::Serialize::serialize(&bundle, &mut ser).expect("failed to re-serialize Bundle!");

// Replace em-dash with a unicode escape. serde_json unescapes it.
let result = std::str::from_utf8(&result)
.unwrap()
.replace("\u{2014}", "\\u2014");

// Notwithstanding the workarounds above, our serialized bundle should be
// byte-for-byte identical to the input bundle.
assert_eq!(
input,
&result[..],
"re-serialized Bundle does not match original!"
);
}
}
78 changes: 69 additions & 9 deletions gen/pb-rust/sigstore-protobuf-specs/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,73 @@
use sigstore_protobuf_specs::dev::sigstore::{
bundle::v1::{bundle::Content, Bundle},
common::v1::MessageSignature,
};
use std::io;

use sigstore_protobuf_specs::dev::sigstore::bundle::v1::Bundle;

macro_rules! include_asset {
($path:literal) => {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/", $path))
};
}

struct SpaceSeparatorFormatter;

impl serde_json::ser::Formatter for SpaceSeparatorFormatter {
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}

fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + io::Write,
{
writer.write_all(b": ")
}

fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
where
W: ?Sized + io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}

fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
where
W: ?Sized + io::Write,
{
// Replace em-dashes with a unicode escape. serde_json unescapes it.
writer.write_all(fragment.replace("\u{2014}", "\\u2014").as_bytes())
}
}

/// Test re-serializing a known-good bundle from sigstore-python.
#[test]
fn bundle() {
let bundle_json = std::fs::read_to_string("tests/bundle.json").unwrap();
fn bundle_roundtrip() {
// Deserialize bundle, trimming trailing whitespace.
let input = include_asset!("a.txt.sigstore").trim_end();
let bundle: Bundle = serde_json::from_str(input).expect("failed to deserialize Bundle!");

// Re-serialize bundle with our python-like formatter.
let formatter = SpaceSeparatorFormatter {};
let mut result = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut result, formatter);
serde::Serialize::serialize(&bundle, &mut ser).expect("failed to re-serialize Bundle!");

let deserialize = serde_json::from_str::<Bundle>(&bundle_json);
println!("{:?}", deserialize);
assert!(deserialize.is_ok());
// Notwithstanding the workarounds above, our serialized bundle should be
// byte-for-byte identical to the input bundle.
let result = std::str::from_utf8(&result).unwrap();
assert_eq!(
input,
&result[..],
"re-serialized Bundle does not match original!"
);
}
12 changes: 6 additions & 6 deletions gen/pb-rust/sigstore-protobuf-specs/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sigstore_protobuf_specs::dev::sigstore::common::v1::{HashOutput, LogId, Mess
/// HashOutput, a structure using only primitive types
#[test]
fn primitives() {
let hash_output_json = r#"{"algorithm":0,"digest":[1,2,3]}"#;
let hash_output_json = r#"{"digest":"AQID"}"#;
let hash_output_rs = HashOutput {
algorithm: 0i32,
digest: vec![1u8, 2u8, 3u8],
Expand All @@ -21,7 +21,7 @@ fn primitives() {
/// LogId, a structure with a field using camelCase
#[test]
fn camel_case() {
let log_id_json = r#"{"keyId":[0]}"#;
let log_id_json = r#"{"keyId":"AA=="}"#;
let log_id_rs = LogId { key_id: vec![0] };

let serialize = serde_json::to_string(&log_id_rs);
Expand All @@ -38,15 +38,15 @@ fn camel_case() {
fn nested() {
let message_signature_json = r#"{
"messageDigest": {
"algorithm": 0,
"digest": [1,2,3]
"algorithm": "SHA2_256",
"digest": "AQID"
},
"signature": [1]
"signature": "AQ=="
}"#;

let message_signature_rs = MessageSignature {
message_digest: Some(HashOutput {
algorithm: 0i32,
algorithm: 1i32,
digest: vec![1u8, 2u8, 3u8],
}),
signature: vec![1u8],
Expand Down

0 comments on commit a9ebad0

Please sign in to comment.