Debian Codegraph @

gen/pb-rust/sigstore-protobuf-specs/tests/integration.rs

raw
1 use std::io;
2
3 use sigstore_protobuf_specs::dev::sigstore::bundle::v1::Bundle;
4
5 macro_rules! include_asset {
6 ($path:literal) => {
7 include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/", $path))
8 };
9 }
10
11 struct SpaceSeparatorFormatter;
12
13 impl serde_json::ser::Formatter for SpaceSeparatorFormatter {
14 fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
15 where
16 W: ?Sized + io::Write,
17 {
18 if first {
19 Ok(())
20 } else {
21 writer.write_all(b", ")
22 }
23 }
24
25 fn begin_object_value<W>(&mut self, writer: &mut W) -> io::Result<()>
26 where
27 W: ?Sized + io::Write,
28 {
29 writer.write_all(b": ")
30 }
31
32 fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
33 where
34 W: ?Sized + io::Write,
35 {
36 if first {
37 Ok(())
38 } else {
39 writer.write_all(b", ")
40 }
41 }
42
43 fn write_string_fragment<W>(&mut self, writer: &mut W, fragment: &str) -> io::Result<()>
44 where
45 W: ?Sized + io::Write,
46 {
47 // Replace em-dashes with a unicode escape. serde_json unescapes it.
48 writer.write_all(fragment.replace("\u{2014}", "\\u2014").as_bytes())
49 }
50 }
51
52 /// Test re-serializing a known-good bundle from sigstore-python.
53 #[test]
54 fn bundle_roundtrip() {
55 // Deserialize bundle, trimming trailing whitespace.
56 let input = include_asset!("a.txt.sigstore").trim_end();
57 let bundle: Bundle = serde_json::from_str(input).expect("failed to deserialize Bundle!");
58
59 // Re-serialize bundle with our python-like formatter.
60 let formatter = SpaceSeparatorFormatter {};
61 let mut result = Vec::new();
62 let mut ser = serde_json::Serializer::with_formatter(&mut result, formatter);
63 serde::Serialize::serialize(&bundle, &mut ser).expect("failed to re-serialize Bundle!");
64
65 // Notwithstanding the workarounds above, our serialized bundle should be
66 // byte-for-byte identical to the input bundle.
67 let result = std::str::from_utf8(&result).unwrap();
68 assert_eq!(
69 input,
70 &result[..],
71 "re-serialized Bundle does not match original!"
72 );
73 }