Skip to content

Commit

Permalink
Docs (#26)
Browse files Browse the repository at this point in the history
* Add some docs + example

* fmt

* Add docs, change api of publish, fmt.

* Fix benches
  • Loading branch information
pietgeursen authored Jan 17, 2021
1 parent 8275f62 commit 1079bdf
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 104 deletions.
13 changes: 8 additions & 5 deletions bamboo-c/src/publish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bamboo_rs_core::entry::publish::Error as BambooPublishError;
pub enum PublishError {
NoError,

PublishWithoutKeypair,
PublishWithInvalidKeypair,
PublishAfterEndOfFeed,
PublishWithIncorrectLogId,
PublishWithoutSecretKey,
Expand All @@ -23,7 +23,6 @@ pub enum PublishError {
impl From<BambooPublishError> for PublishError {
fn from(err: BambooPublishError) -> PublishError {
match err {
BambooPublishError::PublishWithoutKeypair => PublishError::PublishWithoutKeypair,
BambooPublishError::PublishAfterEndOfFeed => PublishError::PublishAfterEndOfFeed,
BambooPublishError::PublishWithIncorrectBacklinkLogId => {
PublishError::PublishWithIncorrectLogId
Expand All @@ -41,10 +40,14 @@ impl From<BambooPublishError> for PublishError {
}
BambooPublishError::PublishWithoutBacklinkEntry => {
PublishError::PublishWithoutBacklinkEntry
},
}
BambooPublishError::DecodeLipmaaEntry { .. } => PublishError::DecodeLipmaaEntry,
BambooPublishError::PublishKeypairDidNotMatchBacklinkPublicKey => PublishError::PublishKeypairDidNotMatchBacklinkPublicKey,
BambooPublishError::PublishKeypairDidNotMatchLipmaaLinkPublicKey => PublishError::PublishKeypairDidNotMatchLipmaaLinkPublicKey,
BambooPublishError::PublishKeypairDidNotMatchBacklinkPublicKey => {
PublishError::PublishKeypairDidNotMatchBacklinkPublicKey
}
BambooPublishError::PublishKeypairDidNotMatchLipmaaLinkPublicKey => {
PublishError::PublishKeypairDidNotMatchLipmaaLinkPublicKey
}
}
}
}
4 changes: 2 additions & 2 deletions bamboo-c/src/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ pub extern "C" fn publish_ed25519_blake2b_entry(
let key_pair = Keypair::from_bytes(&key_pair_bytes[..]);

if let Err(_) = key_pair {
return PublishError::PublishWithoutKeypair;
return PublishError::PublishWithInvalidKeypair;
}

match publish(
out,
Some(&key_pair.unwrap()),
&key_pair.unwrap(),
args.log_id,
payload,
args.is_end_of_feed,
Expand Down
2 changes: 1 addition & 1 deletion bamboo-c/src/verify/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl From<BambooVerifyError> for VerifyError {
BambooVerifyError::BacklinkAuthorDoesNotMatch => {
VerifyError::BacklinkAuthorDoesNotMatch
}
BambooVerifyError::UnknownError => VerifyError::UnknownError
BambooVerifyError::UnknownError => VerifyError::UnknownError,
}
}
}
2 changes: 1 addition & 1 deletion bamboo-rs-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<()> {

let entry_size = publish(
&mut entry_buff,
Some(&key_pair),
&key_pair,
log_id,
&payload_bytes,
is_end_of_feed,
Expand Down
12 changes: 6 additions & 6 deletions bamboo-rs-core-test/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn encoding_benches(c: &mut Criterion) {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -52,7 +52,7 @@ fn encoding_benches(c: &mut Criterion) {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -78,7 +78,7 @@ fn publish_benches(c: &mut Criterion) {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -92,7 +92,7 @@ fn publish_benches(c: &mut Criterion) {
let mut out2 = [0u8; 512];
let _ = publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -115,7 +115,7 @@ fn verify_signature_benches(c: &mut Criterion) {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -138,7 +138,7 @@ fn verify_signature_benches(c: &mut Criterion) {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down
61 changes: 21 additions & 40 deletions bamboo-rs-core-test/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -112,7 +112,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -125,7 +125,7 @@ mod tests {
let mut out2 = [0u8; 512];
let size2 = publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -148,7 +148,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -162,7 +162,7 @@ mod tests {

match publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -184,7 +184,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -198,7 +198,7 @@ mod tests {

match publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -221,7 +221,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
true,
Expand All @@ -234,7 +234,7 @@ mod tests {
let mut out2 = [0u8; 512];
match publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -256,7 +256,7 @@ mod tests {

match publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -269,25 +269,6 @@ mod tests {
}
}

#[test]
fn publish_without_secret_key_errors() {
let payload = "hello bamboo!";
let mut out = [0u8; 512];

match publish(
&mut out,
None,
0,
payload.as_bytes(),
false,
None,
None,
None,
) {
Err(PublishError::PublishWithoutKeypair) => {}
_ => panic!(),
}
}
#[test]
fn publish_with_different_log_id_to_previous_errors() {
let mut csprng: OsRng = OsRng {};
Expand All @@ -297,7 +278,7 @@ mod tests {
let mut out = [0u8; 512];
let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -310,7 +291,7 @@ mod tests {
let mut out2 = [0u8; 512];
match publish(
&mut out2,
Some(&key_pair),
&key_pair,
1,
payload.as_bytes(),
false,
Expand All @@ -333,7 +314,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down Expand Up @@ -362,7 +343,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -375,7 +356,7 @@ mod tests {
let mut out2 = [0u8; 512];
let size2 = publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down Expand Up @@ -407,7 +388,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -420,7 +401,7 @@ mod tests {
let mut out2 = [0u8; 512];
let size2 = publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down Expand Up @@ -454,7 +435,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -467,7 +448,7 @@ mod tests {
let mut out2 = [0u8; 512];
let size2 = publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down Expand Up @@ -506,7 +487,7 @@ mod tests {

let size = publish(
&mut out,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand All @@ -520,7 +501,7 @@ mod tests {
let mut out2 = [0u8; 512];
publish(
&mut out2,
Some(&key_pair),
&key_pair,
0,
payload.as_bytes(),
false,
Expand Down
4 changes: 4 additions & 0 deletions bamboo-rs-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.0-pre-30] - 2021-1-17
### Changed
- change api of publish to take &Keypair, not Option<&Keypair>

## [0.1.0-pre-29] - 2021-1-13
### Changed
- Move c bindings out into new module bamboo-c. Breaking change to name of libraries.
Expand Down
3 changes: 3 additions & 0 deletions bamboo-rs-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ snafu = { version = "0.6.10", default-features = false }
static_assertions = "0.3"
varu64 = { version ="0.6.2", default-features = false }
yamf-hash = { version = "0.1.2", default-features = false }

[dev-dependencies]
rand = {version = "0.7.0", default-features = false}
8 changes: 2 additions & 6 deletions bamboo-rs-core/src/entry/decode/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ use yamf_hash::error::Error as YamfHashError;
pub enum Error {
#[snafu(display("Could not decode payload hash {}", source))]
DecodePayloadHashError { source: YamfHashError },
#[snafu(display(
"Could not decode payload size, error with varu64 encoding"
))]
#[snafu(display("Could not decode payload size, error with varu64 encoding"))]
DecodePayloadSizeError,
#[snafu(display("Could not decode log_id, error with varu64 encoding"))]
DecodeLogIdError,
#[snafu(display("Could not decode author public key from bytes"))]
DecodeAuthorError,
#[snafu(display(
"Could not decode entry sequence number, error with varu64 encoding"
))]
#[snafu(display("Could not decode entry sequence number, error with varu64 encoding"))]
DecodeSeqError,
#[snafu(display("Entry sequence must be larger than 0 but was {}", seq_num))]
DecodeSeqIsZero { seq_num: u64 },
Expand Down
5 changes: 4 additions & 1 deletion bamboo-rs-core/src/entry/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use crate::signature::Signature;
use crate::yamf_hash::YamfHash;

use super::{is_lipmaa_required, Entry};
use snafu::{ensure, ResultExt, NoneError};
use snafu::{ensure, NoneError, ResultExt};

pub mod error;
pub use error::*;

/// Try and decode `bytes` as an [Entry].
///
/// Returned [Entry] references `bytes`.
pub fn decode<'a>(bytes: &'a [u8]) -> Result<Entry<&'a [u8], &'a [u8]>, Error> {
ensure!(bytes.len() > 0, DecodeInputIsLengthZero);

Expand Down
Loading

0 comments on commit 1079bdf

Please sign in to comment.