Skip to content

Commit 450e052

Browse files
committed
make test builder public
1 parent 07c9440 commit 450e052

File tree

6 files changed

+69
-25
lines changed

6 files changed

+69
-25
lines changed

rust/signed_doc/bins/mk_signed_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Cli {
7777
let signed_doc = signed_doc_from_bytes(cose_bytes.as_slice())?;
7878

7979
let new_signed_doc = signed_doc
80-
.into_builder()?
80+
.into_builder()
8181
.add_signature(
8282
|message| sk.sign::<()>(&message).to_bytes().to_vec(),
8383
kid.clone(),

rust/signed_doc/src/builder.rs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,28 @@ fn build_signature(
183183
Ok(Signature::new(kid, sign_bytes))
184184
}
185185

186-
impl TryFrom<&CatalystSignedDocument> for SignaturesBuilder {
187-
type Error = anyhow::Error;
188-
189-
fn try_from(value: &CatalystSignedDocument) -> Result<Self, Self::Error> {
190-
Ok(Self {
186+
impl From<&CatalystSignedDocument> for SignaturesBuilder {
187+
fn from(value: &CatalystSignedDocument) -> Self {
188+
Self {
191189
metadata: value.inner.metadata.clone(),
192190
content: value.inner.content.clone(),
193191
signatures: value.inner.signatures.clone(),
194-
})
192+
}
195193
}
196194
}
197195

198-
#[cfg(test)]
199-
pub(crate) mod tests {
196+
pub mod tests {
197+
//! Catalyst Signed Document Builder for testing purposes.
198+
200199
use cbork_utils::with_cbor_bytes::WithCborBytes;
201200

201+
use super::{CatalystSignedDocument, Content};
202+
use crate::Signatures;
203+
202204
/// A test version of the builder, which allows to build a not fully valid catalyst
203205
/// signed document
204206
#[derive(Default)]
205-
pub(crate) struct Builder {
207+
pub struct Builder {
206208
/// metadata
207209
metadata: super::Metadata,
208210
/// content
@@ -211,36 +213,64 @@ pub(crate) mod tests {
211213
signatures: super::Signatures,
212214
}
213215

216+
impl From<&CatalystSignedDocument> for Builder {
217+
fn from(value: &CatalystSignedDocument) -> Self {
218+
Self {
219+
metadata: value.inner.metadata.clone().inner(),
220+
content: value.inner.content.clone(),
221+
signatures: value.inner.signatures.clone(),
222+
}
223+
}
224+
}
225+
214226
impl Builder {
215227
/// Start building a signed document
216228
#[must_use]
217-
pub(crate) fn new() -> Self {
229+
pub fn new() -> Self {
218230
Self::default()
219231
}
220232

221233
/// Add provided `SupportedField` into the `Metadata`.
222-
pub(crate) fn with_metadata_field(
234+
pub fn with_metadata_field(
223235
mut self,
224236
field: crate::metadata::SupportedField,
225237
) -> Self {
226238
self.metadata.add_field(field);
227239
self
228240
}
229241

242+
/// Add provided `SupportedField` into the `Metadata`.
243+
pub fn remove_metadata_field(
244+
mut self,
245+
field_label: crate::metadata::SupportedLabel,
246+
) -> Self {
247+
self.metadata.remove_field(field_label);
248+
self
249+
}
250+
230251
/// Set the content (COSE payload) to the document builder.
231252
/// It will set the content as its provided, make sure by yourself that
232253
/// `content-type` and `content-encoding` fields are aligned with the
233254
/// provided content bytes.
234-
pub(crate) fn with_content(
255+
pub fn with_content(
235256
mut self,
236257
content: Vec<u8>,
237258
) -> Self {
238259
self.content = content.into();
239260
self
240261
}
241262

263+
/// Set the content (COSE payload) to CBOR nil to the document builder.
264+
/// It will set the content as its provided, make sure by yourself that
265+
/// `content-type` and `content-encoding` fields are aligned with the
266+
/// provided content bytes.
267+
pub fn with_empty_content(mut self) -> Self {
268+
self.content = Content::default();
269+
self
270+
}
271+
242272
/// Add a signature to the document
243-
pub(crate) fn add_signature(
273+
pub fn add_signature(
244274
mut self,
245275
sign_fn: impl FnOnce(Vec<u8>) -> Vec<u8>,
246276
kid: super::CatalystId,
@@ -256,9 +286,15 @@ pub(crate) mod tests {
256286
Ok(self)
257287
}
258288

289+
/// Clear all existing signatures.
290+
pub fn clear_signatures(mut self) -> anyhow::Result<Self> {
291+
self.signatures = Signatures::default();
292+
Ok(self)
293+
}
294+
259295
/// Build a signed document with the collected error report.
260296
/// Could provide an invalid document.
261-
pub(crate) fn build(self) -> super::CatalystSignedDocument {
297+
pub fn build(self) -> super::CatalystSignedDocument {
262298
let metadata_bytes = minicbor::to_vec(self.metadata).unwrap();
263299
let content_bytes = minicbor::to_vec(self.content).unwrap();
264300
let signature_bytes = minicbor::to_vec(self.signatures).unwrap();

rust/signed_doc/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Catalyst documents signing crate
22
3-
mod builder;
3+
pub mod builder;
44
mod content;
55
pub mod decode_context;
66
pub mod doc_types;
@@ -231,12 +231,8 @@ impl CatalystSignedDocument {
231231

232232
/// Returns a signed document `Builder` pre-loaded with the current signed document's
233233
/// data.
234-
///
235-
/// # Errors
236-
/// - If error returned its probably a bug. `CatalystSignedDocument` must be a valid
237-
/// COSE structure.
238-
pub fn into_builder(&self) -> anyhow::Result<SignaturesBuilder> {
239-
self.try_into()
234+
pub fn into_builder(&self) -> SignaturesBuilder {
235+
self.into()
240236
}
241237
}
242238

rust/signed_doc/src/metadata/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,27 @@ impl Metadata {
139139
/// Building metadata by-field with this function doesn't ensure the presence of
140140
/// required fields. Use [`Self::from_fields`] or [`Self::from_json`] if it's
141141
/// important for metadata to be valid.
142-
#[cfg(test)]
143142
pub(crate) fn add_field(
144143
&mut self,
145144
field: SupportedField,
146145
) {
147146
self.0.insert(field.discriminant(), field);
148147
}
149148

149+
/// Removes `SupportedField` from the `Metadata`.
150+
///
151+
/// # Warning
152+
///
153+
/// Building metadata by-field with this function doesn't ensure the presence of
154+
/// required fields. Use [`Self::from_fields`] or [`Self::from_json`] if it's
155+
/// important for metadata to be valid.
156+
pub(crate) fn remove_field(
157+
&mut self,
158+
field_label: SupportedLabel,
159+
) {
160+
self.0.remove(&field_label);
161+
}
162+
150163
/// Build `Metadata` object from the metadata fields, doing all necessary validation.
151164
pub(crate) fn from_fields<E>(
152165
fields: impl Iterator<Item = Result<SupportedField, E>>,

rust/signed_doc/src/metadata/supported_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl fmt::Display for Label<'_> {
8686
)]
8787
#[non_exhaustive]
8888
#[repr(usize)]
89-
pub(crate) enum SupportedField {
89+
pub enum SupportedField {
9090
/// `content-type` field. In COSE it's represented as the signed integer `3` (see [RFC
9191
/// 8949 section 3.1]).
9292
///

rust/signed_doc/src/validator/rules/signature.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ mod tests {
180180
let (another_sk, ..) = create_dummy_key_pair(RoleId::Role0);
181181
let invalid_doc = signed_doc
182182
.into_builder()
183-
.unwrap()
184183
.add_signature(|m| another_sk.sign(&m).to_vec(), kid.clone())
185184
.unwrap()
186185
.build()

0 commit comments

Comments
 (0)