diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000000..2b7d7a7b506 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,23 @@ +#!/bin/bash + +HAS_ISSUES=0 +FIRST_FILE=1 + +for file in $(git diff --name-only --staged); do + FMT_RESULT="$(rustfmt --edition 2021 --color auto --files-with-diff $file 2>/dev/null || true)" + if [ "$FMT_RESULT" != "" ]; then + if [ $FIRST_FILE -eq 0 ]; then + echo -n ", " + fi + echo -n "$file" + HAS_ISSUES=1 + FIRST_FILE=0 + fi +done + +if [ $HAS_ISSUES -eq 0 ]; then + exit 0 +fi + +echo ". Your code has formatting issues in files listed above. Format your code with \`make format\` or call rustfmt manually." +exit 1 diff --git a/Makefile b/Makefile new file mode 100644 index 00000000000..83f1a7fae84 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +init: + rustup component add rustfmt && git config core.hooksPath .githooks + +format: + cargo fmt -- --color auto --files-with-diff --verbose diff --git a/src/test.rs b/src/test.rs index fa6027fcdc5..00d31e26e26 100644 --- a/src/test.rs +++ b/src/test.rs @@ -121,7 +121,7 @@ extern crate alloc; use alloc::{format, string::String, vec::Vec}; -use crate::{error}; +use crate::error; #[cfg(any(feature = "std", feature = "test_logging"))] extern crate std; @@ -534,6 +534,8 @@ mod tests { #[test] #[should_panic(expected = "Syntax error: Expected Key = Value.")] fn syntax_error() { - test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| Ok(())); + test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| { + Ok(()) + }); } } diff --git a/tests/aes_test.rs b/tests/aes_test.rs index 1c5ec182ce4..4962c2931ab 100644 --- a/tests/aes_test.rs +++ b/tests/aes_test.rs @@ -1,103 +1,114 @@ - - //use ring::{aead, error}; -macro_rules! test_aead -{( $pkg:ident ) => -{ -mod $pkg { - - use $pkg::{aead, error}; - - use aead::{AES_128_GCM, Algorithm, NonceSequence, OpeningKey, UnboundKey, BoundKey, SealingKey, Nonce, Aad}; - use error::Unspecified; +macro_rules! test_aead { + ( $pkg:ident ) => { + mod $pkg { + use $pkg::{aead, error}; - const AES_128_TEST_KEY: [u8; 16] = [12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192 ]; - const TEST_NONCE: [u8; aead::NONCE_LEN] = [ 12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31 ]; - const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes(); + use aead::{ + Aad, Algorithm, BoundKey, Nonce, NonceSequence, OpeningKey, SealingKey, UnboundKey, + AES_128_GCM, + }; + use error::Unspecified; - struct NotANonce(Vec); + const AES_128_TEST_KEY: [u8; 16] = [ + 12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192, + ]; + const TEST_NONCE: [u8; aead::NONCE_LEN] = + [12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31]; + const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes(); - impl NotANonce { - fn from(value: Vec) -> Self { - NotANonce(value) - } - } + struct NotANonce(Vec); - impl NonceSequence for NotANonce { - fn advance(&mut self) -> Result { - let mut nonce = [0u8; aead::NONCE_LEN]; - nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]); - Ok(Nonce::assume_unique_for_key(nonce)) - } - } - - struct AeadConfig { - algorithm: &'static Algorithm, - key: Vec, - nonce: Vec, - aad: String - } - - impl AeadConfig { - fn new(algorithm: &'static Algorithm, key: &[u8], nonce: &[u8], aad: &str) -> AeadConfig { - AeadConfig { - algorithm: algorithm, - key: Vec::from(key), - nonce: Vec::from(nonce), - aad: String::from(aad) + impl NotANonce { + fn from(value: Vec) -> Self { + NotANonce(value) + } } - } - fn key(&self) -> UnboundKey { - UnboundKey::new(self.algorithm, &self.key).unwrap() - } - fn aad(&self) -> Aad { - Aad::from(self.aad.clone()) - } - fn nonce(&self) -> impl NonceSequence { - //RngNonce{} - //NotANonce::new() - NotANonce::from( self.nonce.clone()) - } - } + impl NonceSequence for NotANonce { + fn advance(&mut self) -> Result { + let mut nonce = [0u8; aead::NONCE_LEN]; + nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]); + Ok(Nonce::assume_unique_for_key(nonce)) + } + } - #[test] - fn test_aes_128_gcm() -> Result<(), String> { - let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test"); - let mut in_out = Vec::from(PLAINTEXT); + struct AeadConfig { + algorithm: &'static Algorithm, + key: Vec, + nonce: Vec, + aad: String, + } - test_aead(config, &mut in_out)?; + impl AeadConfig { + fn new( + algorithm: &'static Algorithm, + key: &[u8], + nonce: &[u8], + aad: &str, + ) -> AeadConfig { + AeadConfig { + algorithm: algorithm, + key: Vec::from(key), + nonce: Vec::from(nonce), + aad: String::from(aad), + } + } + + fn key(&self) -> UnboundKey { + UnboundKey::new(self.algorithm, &self.key).unwrap() + } + fn aad(&self) -> Aad { + Aad::from(self.aad.clone()) + } + fn nonce(&self) -> impl NonceSequence { + //RngNonce{} + //NotANonce::new() + NotANonce::from(self.nonce.clone()) + } + } + #[test] + fn test_aes_128_gcm() -> Result<(), String> { + let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test"); + let mut in_out = Vec::from(PLAINTEXT); - Ok(()) - } + test_aead(config, &mut in_out)?; - fn test_aead(config: AeadConfig, in_out: &mut Vec) -> Result, String> { - let mut sealing_key = SealingKey::new(config.key(), config.nonce()); - let mut opening_key = OpeningKey::new(config.key(), config.nonce()); + Ok(()) + } - let plaintext = in_out.clone(); - println!("Plaintext: {:?}", plaintext); + fn test_aead(config: AeadConfig, in_out: &mut Vec) -> Result, String> { + let mut sealing_key = SealingKey::new(config.key(), config.nonce()); + let mut opening_key = OpeningKey::new(config.key(), config.nonce()); - let tag = sealing_key.seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice()).map_err(|x| x.to_string() )?; - let cipher_text = in_out.clone(); - println!("Ciphertext: {:?}", cipher_text); - assert_ne!(plaintext, cipher_text); + let plaintext = in_out.clone(); + println!("Plaintext: {:?}", plaintext); - in_out.extend(tag.as_ref()); + let tag = sealing_key + .seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice()) + .map_err(|x| x.to_string())?; + let cipher_text = in_out.clone(); + println!("Ciphertext: {:?}", cipher_text); + assert_ne!(plaintext, cipher_text); - let result_plaintext = opening_key.open_in_place(config.aad(), in_out).map_err(|x| x.to_string() )?; - assert_eq!(plaintext, result_plaintext); + in_out.extend(tag.as_ref()); - println!("Roundtrip: {:?}", result_plaintext); + let result_plaintext = opening_key + .open_in_place(config.aad(), in_out) + .map_err(|x| x.to_string())?; + assert_eq!(plaintext, result_plaintext); + println!("Roundtrip: {:?}", result_plaintext); - Ok(Vec::from(result_plaintext)) - } -}}} + Ok(Vec::from(result_plaintext)) + } + } + }; +} mod test_aead { test_aead!(ring); -} \ No newline at end of file +} diff --git a/tests/digest_test.rs b/tests/digest_test.rs index 7240d345f4f..f9617e30022 100644 --- a/tests/digest_test.rs +++ b/tests/digest_test.rs @@ -1,5 +1,3 @@ - - // Copyright 2015-2017 Brian Smith. // // Permission to use, copy, modify, and/or distribute this software for any