Skip to content

Commit fa32136

Browse files
committed
Add pre-commit git hook
1 parent 25518f5 commit fa32136

File tree

5 files changed

+122
-83
lines changed

5 files changed

+122
-83
lines changed

.githooks/pre-commit

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
HAS_ISSUES=0
4+
FIRST_FILE=1
5+
6+
for file in $(git diff --name-only --staged); do
7+
FMT_RESULT="$(rustfmt --edition 2021 --color auto --files-with-diff $file 2>/dev/null || true)"
8+
if [ "$FMT_RESULT" != "" ]; then
9+
if [ $FIRST_FILE -eq 0 ]; then
10+
echo -n ", "
11+
fi
12+
echo -n "$file"
13+
HAS_ISSUES=1
14+
FIRST_FILE=0
15+
fi
16+
done
17+
18+
if [ $HAS_ISSUES -eq 0 ]; then
19+
exit 0
20+
fi
21+
22+
echo ". Your code has formatting issues in files listed above. Format your code with \`make format\` or call rustfmt manually."
23+
exit 1

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
init:
2+
rustup component add rustfmt && git config core.hooksPath .githooks
3+
4+
format:
5+
cargo fmt -- --color auto --files-with-diff --verbose

src/test.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extern crate alloc;
121121

122122
use alloc::{format, string::String, vec::Vec};
123123

124-
use crate::{error};
124+
use crate::error;
125125

126126
#[cfg(any(feature = "std", feature = "test_logging"))]
127127
extern crate std;
@@ -534,6 +534,8 @@ mod tests {
534534
#[test]
535535
#[should_panic(expected = "Syntax error: Expected Key = Value.")]
536536
fn syntax_error() {
537-
test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| Ok(()));
537+
test::run(test_file!("test/test_1_syntax_error_tests.txt"), |_, _| {
538+
Ok(())
539+
});
538540
}
539541
}

tests/aes_test.rs

+90-79
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,114 @@
1-
2-
31
//use ring::{aead, error};
42

5-
macro_rules! test_aead
6-
{( $pkg:ident ) =>
7-
{
8-
mod $pkg {
9-
10-
use $pkg::{aead, error};
11-
12-
use aead::{AES_128_GCM, Algorithm, NonceSequence, OpeningKey, UnboundKey, BoundKey, SealingKey, Nonce, Aad};
13-
use error::Unspecified;
3+
macro_rules! test_aead {
4+
( $pkg:ident ) => {
5+
mod $pkg {
146

7+
use $pkg::{aead, error};
158

16-
const AES_128_TEST_KEY: [u8; 16] = [12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192 ];
17-
const TEST_NONCE: [u8; aead::NONCE_LEN] = [ 12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31 ];
18-
const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes();
9+
use aead::{
10+
Aad, Algorithm, BoundKey, Nonce, NonceSequence, OpeningKey, SealingKey, UnboundKey,
11+
AES_128_GCM,
12+
};
13+
use error::Unspecified;
1914

20-
struct NotANonce(Vec<u8>);
15+
const AES_128_TEST_KEY: [u8; 16] = [
16+
12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31, 226, 11, 135, 192,
17+
];
18+
const TEST_NONCE: [u8; aead::NONCE_LEN] =
19+
[12, 124, 200, 31, 226, 11, 135, 192, 12, 124, 200, 31];
20+
const PLAINTEXT: &[u8] = "plaintext to be encrypted".as_bytes();
2121

22-
impl NotANonce {
23-
fn from(value: Vec<u8>) -> Self {
24-
NotANonce(value)
25-
}
26-
}
22+
struct NotANonce(Vec<u8>);
2723

28-
impl NonceSequence for NotANonce {
29-
fn advance(&mut self) -> Result<Nonce, Unspecified> {
30-
let mut nonce = [0u8; aead::NONCE_LEN];
31-
nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]);
32-
Ok(Nonce::assume_unique_for_key(nonce))
33-
}
34-
}
35-
36-
struct AeadConfig {
37-
algorithm: &'static Algorithm,
38-
key: Vec<u8>,
39-
nonce: Vec<u8>,
40-
aad: String
41-
}
42-
43-
impl AeadConfig {
44-
fn new(algorithm: &'static Algorithm, key: &[u8], nonce: &[u8], aad: &str) -> AeadConfig {
45-
AeadConfig {
46-
algorithm: algorithm,
47-
key: Vec::from(key),
48-
nonce: Vec::from(nonce),
49-
aad: String::from(aad)
24+
impl NotANonce {
25+
fn from(value: Vec<u8>) -> Self {
26+
NotANonce(value)
27+
}
5028
}
51-
}
5229

53-
fn key(&self) -> UnboundKey {
54-
UnboundKey::new(self.algorithm, &self.key).unwrap()
55-
}
56-
fn aad(&self) -> Aad<String> {
57-
Aad::from(self.aad.clone())
58-
}
59-
fn nonce(&self) -> impl NonceSequence {
60-
//RngNonce{}
61-
//NotANonce::new()
62-
NotANonce::from( self.nonce.clone())
63-
}
64-
}
30+
impl NonceSequence for NotANonce {
31+
fn advance(&mut self) -> Result<Nonce, Unspecified> {
32+
let mut nonce = [0u8; aead::NONCE_LEN];
33+
nonce.copy_from_slice(&self.0[0..aead::NONCE_LEN]);
34+
Ok(Nonce::assume_unique_for_key(nonce))
35+
}
36+
}
6537

66-
#[test]
67-
fn test_aes_128_gcm() -> Result<(), String> {
68-
let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test");
69-
let mut in_out = Vec::from(PLAINTEXT);
38+
struct AeadConfig {
39+
algorithm: &'static Algorithm,
40+
key: Vec<u8>,
41+
nonce: Vec<u8>,
42+
aad: String,
43+
}
7044

71-
test_aead(config, &mut in_out)?;
45+
impl AeadConfig {
46+
fn new(
47+
algorithm: &'static Algorithm,
48+
key: &[u8],
49+
nonce: &[u8],
50+
aad: &str,
51+
) -> AeadConfig {
52+
AeadConfig {
53+
algorithm: algorithm,
54+
key: Vec::from(key),
55+
nonce: Vec::from(nonce),
56+
aad: String::from(aad),
57+
}
58+
}
59+
60+
fn key(&self) -> UnboundKey {
61+
UnboundKey::new(self.algorithm, &self.key).unwrap()
62+
}
63+
fn aad(&self) -> Aad<String> {
64+
Aad::from(self.aad.clone())
65+
}
66+
fn nonce(&self) -> impl NonceSequence {
67+
//RngNonce{}
68+
//NotANonce::new()
69+
NotANonce::from(self.nonce.clone())
70+
}
71+
}
7272

73+
#[test]
74+
fn test_aes_128_gcm() -> Result<(), String> {
75+
let config = AeadConfig::new(&AES_128_GCM, &AES_128_TEST_KEY, &TEST_NONCE, "test");
76+
let mut in_out = Vec::from(PLAINTEXT);
7377

74-
Ok(())
75-
}
78+
test_aead(config, &mut in_out)?;
7679

77-
fn test_aead(config: AeadConfig, in_out: &mut Vec<u8>) -> Result<Vec<u8>, String> {
78-
let mut sealing_key = SealingKey::new(config.key(), config.nonce());
79-
let mut opening_key = OpeningKey::new(config.key(), config.nonce());
80+
Ok(())
81+
}
8082

81-
let plaintext = in_out.clone();
82-
println!("Plaintext: {:?}", plaintext);
83+
fn test_aead(config: AeadConfig, in_out: &mut Vec<u8>) -> Result<Vec<u8>, String> {
84+
let mut sealing_key = SealingKey::new(config.key(), config.nonce());
85+
let mut opening_key = OpeningKey::new(config.key(), config.nonce());
8386

84-
let tag = sealing_key.seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice()).map_err(|x| x.to_string() )?;
85-
let cipher_text = in_out.clone();
86-
println!("Ciphertext: {:?}", cipher_text);
87-
assert_ne!(plaintext, cipher_text);
87+
let plaintext = in_out.clone();
88+
println!("Plaintext: {:?}", plaintext);
8889

89-
in_out.extend(tag.as_ref());
90+
let tag = sealing_key
91+
.seal_in_place_separate_tag(config.aad(), in_out.as_mut_slice())
92+
.map_err(|x| x.to_string())?;
93+
let cipher_text = in_out.clone();
94+
println!("Ciphertext: {:?}", cipher_text);
95+
assert_ne!(plaintext, cipher_text);
9096

91-
let result_plaintext = opening_key.open_in_place(config.aad(), in_out).map_err(|x| x.to_string() )?;
92-
assert_eq!(plaintext, result_plaintext);
97+
in_out.extend(tag.as_ref());
9398

94-
println!("Roundtrip: {:?}", result_plaintext);
99+
let result_plaintext = opening_key
100+
.open_in_place(config.aad(), in_out)
101+
.map_err(|x| x.to_string())?;
102+
assert_eq!(plaintext, result_plaintext);
95103

104+
println!("Roundtrip: {:?}", result_plaintext);
96105

97-
Ok(Vec::from(result_plaintext))
98-
}
99-
}}}
106+
Ok(Vec::from(result_plaintext))
107+
}
108+
}
109+
};
110+
}
100111

101112
mod test_aead {
102113
test_aead!(ring);
103-
}
114+
}

tests/digest_test.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
// Copyright 2015-2017 Brian Smith.
42
//
53
// Permission to use, copy, modify, and/or distribute this software for any

0 commit comments

Comments
 (0)