Skip to content

Commit

Permalink
rustfmt the entire project
Browse files Browse the repository at this point in the history
  • Loading branch information
int08h committed May 28, 2024
1 parent 160da01 commit bd083d6
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 102 deletions.
61 changes: 25 additions & 36 deletions benches/roughenough-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
extern crate criterion;
extern crate roughenough;

use std::time::SystemTime;
use criterion::{BenchmarkId, black_box, Criterion, SamplingMode};
use criterion::Throughput::Elements;
use criterion::{black_box, BenchmarkId, Criterion, SamplingMode};
use std::time::SystemTime;

use roughenough::merkle::MerkleTree;
use roughenough::{RtMessage, Tag};
use roughenough::key::OnlineKey;
use roughenough::merkle::MerkleTree;
use roughenough::version::Version;
use roughenough::{RtMessage, Tag};

fn create_signed_srep_tags(c: &mut Criterion) {
let mut group = c.benchmark_group("signing");
Expand All @@ -19,7 +19,7 @@ fn create_signed_srep_tags(c: &mut Criterion) {
group.throughput(Elements(1));
group.bench_function("create signed SREP tag", |b| {
let now = SystemTime::now();
b.iter(|| black_box(key.make_srep(Version::Rfc, now, data.as_ref(), )))
b.iter(|| black_box(key.make_srep(Version::Rfc, now, data.as_ref())))
});
group.finish();
}
Expand Down Expand Up @@ -60,17 +60,15 @@ fn create_new_merkle_tree(c: &mut Criterion) {

for size in SIZES.iter() {
group.throughput(Elements(*size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), size,
|b, &size| {
b.iter(|| {
let mut tree = MerkleTree::new_sha512_classic();
for _ in 0..size {
tree.push_leaf(DATA);
}
black_box(tree.compute_root())
})
}
);
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
b.iter(|| {
let mut tree = MerkleTree::new_sha512_classic();
for _ in 0..size {
tree.push_leaf(DATA);
}
black_box(tree.compute_root())
})
});
}
group.finish();
}
Expand All @@ -83,25 +81,20 @@ fn reuse_merkle_tree(c: &mut Criterion) {

for size in SIZES.iter() {
group.throughput(Elements(*size as u64));
group.bench_with_input(BenchmarkId::from_parameter(size), size,
|b, &size| {
b.iter(|| {
tree.reset();
for _ in 0..size {
tree.push_leaf(DATA);
}
black_box(tree.compute_root())
})
}
);
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
b.iter(|| {
tree.reset();
for _ in 0..size {
tree.push_leaf(DATA);
}
black_box(tree.compute_root())
})
});
}
group.finish();
}

criterion_group!(
message_singing,
create_signed_srep_tags,
);
criterion_group!(message_singing, create_signed_srep_tags,);

criterion_group!(
message_creation,
Expand All @@ -110,10 +103,6 @@ criterion_group!(
create_four_field_message,
);

criterion_group!(
merkle_tree,
create_new_merkle_tree,
reuse_merkle_tree
);
criterion_group!(merkle_tree, create_new_merkle_tree, reuse_merkle_tree);

criterion_main!(message_singing, message_creation, merkle_tree);
40 changes: 27 additions & 13 deletions src/bin/roughenough-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ extern crate clap;

use std::collections::HashMap;
use std::fs::File;
use std::io::{Cursor, Write};
use std::io::ErrorKind::WouldBlock;
use std::io::{Cursor, Write};
use std::iter::Iterator;
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
use std::time;

use byteorder::{LittleEndian, ReadBytesExt};
use chrono::{Local, TimeZone};
use chrono::offset::Utc;
use chrono::{Local, TimeZone};
use clap::{App, Arg};
use data_encoding::{Encoding, HEXLOWER_PERMISSIVE, BASE64};
use data_encoding::{Encoding, BASE64, HEXLOWER_PERMISSIVE};
use ring::rand;
use ring::rand::SecureRandom;

use roughenough::{
CERTIFICATE_CONTEXT, Error, RFC_REQUEST_FRAME_BYTES, roughenough_version, RtMessage, SIGNED_RESPONSE_CONTEXT,
Tag,
};
use roughenough::merkle::MerkleTree;
use roughenough::sign::Verifier;
use roughenough::version::Version;
use roughenough::{
roughenough_version, Error, RtMessage, Tag, CERTIFICATE_CONTEXT, RFC_REQUEST_FRAME_BYTES,
SIGNED_RESPONSE_CONTEXT,
};

const HEX: Encoding = HEXLOWER_PERMISSIVE;

Expand Down Expand Up @@ -141,7 +141,12 @@ fn stress_test_forever(ver: Version, addr: &SocketAddr) -> ! {
println!("Stress testing!");

let nonce = create_nonce(ver);
let socket = UdpSocket::bind(if addr.is_ipv6() { "[::]:0" } else { "0.0.0.0:0" }).expect("Couldn't open UDP socket");
let socket = UdpSocket::bind(if addr.is_ipv6() {
"[::]:0"
} else {
"0.0.0.0:0"
})
.expect("Couldn't open UDP socket");
let request = make_request(ver, &nonce, false);
loop {
socket.send_to(&request, addr).unwrap();
Expand Down Expand Up @@ -408,7 +413,10 @@ fn main() {
0 => Version::Classic,
1 => Version::Rfc,
8 => Version::RfcDraft8,
_ => panic!("Invalid protocol '{}'; valid values are 0, 1, or 8", protocol),
_ => panic!(
"Invalid protocol '{}'; valid values are 0, 1, or 8",
protocol
),
};

let addr = (host, port).to_socket_addrs().unwrap().next().unwrap();
Expand All @@ -425,7 +433,12 @@ fn main() {

for _ in 0..num_requests {
let nonce = create_nonce(version);
let socket = UdpSocket::bind(if addr.is_ipv6() { "[::]:0" } else { "0.0.0.0:0" }).expect("Couldn't open UDP socket");
let socket = UdpSocket::bind(if addr.is_ipv6() {
"[::]:0"
} else {
"0.0.0.0:0"
})
.expect("Couldn't open UDP socket");
let request = make_request(version, &nonce, text_dump);

if let Some(f) = file_for_requests.as_mut() {
Expand All @@ -441,7 +454,8 @@ fn main() {

for (nonce, _, socket) in requests {
let duration = time::Duration::from_secs(timeout_secs);
socket.set_read_timeout(Some(duration))
socket
.set_read_timeout(Some(duration))
.expect("Failed setting send timeout");

let mut buf = [0u8; 4096];
Expand All @@ -452,7 +466,7 @@ fn main() {
eprintln!("Timeout waiting for response");
return;
}
Err(e) => panic!("{}", e),
Err(e) => panic!("{}", e),
};

if let Some(f) = file_for_responses.as_mut() {
Expand Down Expand Up @@ -484,7 +498,7 @@ fn main() {
let seconds = midpoint / 10_u64.pow(6);
let nsecs = (midpoint - (seconds * 10_u64.pow(6))) * 10_u64.pow(3);
(seconds, nsecs as u32)
},
}
Version::Rfc | Version::RfcDraft8 => (midpoint, 0),
};

Expand Down
8 changes: 6 additions & 2 deletions src/bin/roughenough-kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ fn encrypt_seed(_: &str, _: &str) {
fn encrypt_seed(kms_key: &str, hex_seed: &str) {
let kms_client = get_kms(kms_key);

let plaintext_seed = HEX.decode(hex_seed.as_ref()).expect("Error decoding hex seed value");
let plaintext_seed = HEX
.decode(hex_seed.as_ref())
.expect("Error decoding hex seed value");

if plaintext_seed.len() != 32 {
panic!(
Expand Down Expand Up @@ -71,7 +73,9 @@ fn decrypt_blob(_: &str, _: &str) {
#[cfg(any(feature = "awskms", feature = "gcpkms"))]
fn decrypt_blob(kms_key: &str, hex_blob: &str) {
let kms_client = get_kms(kms_key);
let ciphertext = HEX.decode(hex_blob.as_ref()).expect("Error decoding hex blob value");
let ciphertext = HEX
.decode(hex_blob.as_ref())
.expect("Error decoding hex blob value");

match EnvelopeEncryption::decrypt_seed(&kms_client, ciphertext.as_ref()) {
Ok(plaintext) => {
Expand Down
20 changes: 13 additions & 7 deletions src/bin/roughenough-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
#[macro_use]
extern crate log;

use std::{env, thread};
use std::process;
use std::sync::{Arc, Mutex};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::{env, thread};

use log::LevelFilter;
use mio::Events;
use mio::net::UdpSocket;
use mio::Events;
use once_cell::sync::Lazy;
use simple_logger::SimpleLogger;

Expand Down Expand Up @@ -74,7 +74,10 @@ fn display_config(server: &Server, cfg: &dyn ServerConfig) {
info!("Number of workers : {}", cfg.num_workers());
info!("Long-term public key : {}", server.get_public_key());
info!("Max response batch size : {}", cfg.batch_size());
info!("Status updates every : {} seconds", cfg.status_interval().as_secs());
info!(
"Status updates every : {} seconds",
cfg.status_interval().as_secs()
);

info!(
"Server listening on : {}:{}",
Expand Down Expand Up @@ -103,7 +106,6 @@ fn display_config(server: &Server, cfg: &dyn ServerConfig) {
} else {
info!("Deliberate response errors : disabled");
}

}

pub fn main() {
Expand Down Expand Up @@ -132,7 +134,11 @@ pub fn main() {
};

let socket = {
let sock_addr = config.lock().unwrap().udp_socket_addr().expect("udp sock addr");
let sock_addr = config
.lock()
.unwrap()
.udp_socket_addr()
.expect("udp sock addr");
let sock = UdpSocket::bind(&sock_addr).expect("failed to bind to socket");
Arc::new(sock)
};
Expand All @@ -142,7 +148,7 @@ pub fn main() {
// TODO(stuart) move TCP healthcheck out of worker threads as it currently conflicts
let mut threads = Vec::new();

for i in 0 .. config.lock().unwrap().num_workers() {
for i in 0..config.lock().unwrap().num_workers() {
let cfg = config.clone();
let sock = socket.try_clone().unwrap();
let thread = thread::Builder::new()
Expand Down
6 changes: 3 additions & 3 deletions src/config/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{env, thread};
use std::time::Duration;
use std::{env, thread};

use data_encoding::{Encoding, HEXLOWER_PERMISSIVE};

use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::config::ServerConfig;
use crate::Error;
use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::key::KmsProtection;
use crate::Error;

const HEX: Encoding = HEXLOWER_PERMISSIVE;

Expand Down
4 changes: 2 additions & 2 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use std::time::Duration;
use data_encoding::{Encoding, HEXLOWER_PERMISSIVE};
use yaml_rust::YamlLoader;

use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::config::ServerConfig;
use crate::Error;
use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::key::KmsProtection;
use crate::Error;

const HEX: Encoding = HEXLOWER_PERMISSIVE;

Expand Down
2 changes: 1 addition & 1 deletion src/config/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::time::Duration;

use data_encoding::{Encoding, HEXLOWER_PERMISSIVE};

use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::config::ServerConfig;
use crate::config::{DEFAULT_BATCH_SIZE, DEFAULT_STATUS_INTERVAL};
use crate::key::KmsProtection;

const HEX: Encoding = HEXLOWER_PERMISSIVE;
Expand Down
4 changes: 2 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use std::net::SocketAddr;
use std::time::Duration;

use crate::Error;
use crate::key::KmsProtection;
use crate::Error;
use crate::SEED_LENGTH;

pub use self::environment::EnvironmentConfig;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub const DEFAULT_STATUS_INTERVAL: Duration = Duration::from_secs(600);
/// * [EnvironmentConfig](struct.EnvironmentConfig.html) - configure via environment variables
/// * [MemoryConfig](struct.MemoryConfig.html) - in-memory configuration for testing
///
pub trait ServerConfig : Send {
pub trait ServerConfig: Send {
/// [Required] IP address or interface name to listen for client requests
fn interface(&self) -> &str;

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub enum Error {
NoCompatibleVersion,

/// Sending response to a client request has failed
SendingResponseFailed
SendingResponseFailed,
}

impl From<std::io::Error> for Error {
Expand Down
6 changes: 3 additions & 3 deletions src/grease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
//! on how to enable.
//!
use rand::{FromEntropy, Rng};
use rand::distributions::Bernoulli;
use rand::rngs::SmallRng;
use rand::seq::index::sample as index_sample;
use rand::seq::SliceRandom;
use rand::{FromEntropy, Rng};

use crate::grease::Pathologies::*;
use crate::tag::Tag;
use crate::RtMessage;
use crate::SIGNATURE_LENGTH;
use crate::tag::Tag;

///
/// Ways that a message can be made invalid.
Expand Down Expand Up @@ -149,8 +149,8 @@ impl Grease {
#[cfg(test)]
mod test {
use crate::grease::Grease;
use crate::RtMessage;
use crate::tag::Tag;
use crate::RtMessage;

#[test]
fn verify_error_probability() {
Expand Down
2 changes: 1 addition & 1 deletion src/key/longterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
use std::fmt;
use std::fmt::Formatter;

use crate::CERTIFICATE_CONTEXT;
use crate::key::OnlineKey;
use crate::message::RtMessage;
use crate::sign::Signer;
use crate::tag::Tag;
use crate::CERTIFICATE_CONTEXT;

///
/// Represents the server's long-term identity.
Expand Down
Loading

0 comments on commit bd083d6

Please sign in to comment.