Skip to content

Commit

Permalink
#58 : Adding tests for crypto::read crypto::write (#187)
Browse files Browse the repository at this point in the history
* resolved diverging upstream branch and adding back in the tests
* merging old changes with new changes for read tests
* fixing merge issues in write tests
* more merge fixes
* removing redundant test
  • Loading branch information
raman4793 authored Aug 28, 2024
1 parent 59efec4 commit 02d7dab
Show file tree
Hide file tree
Showing 2 changed files with 458 additions and 83 deletions.
177 changes: 161 additions & 16 deletions src/crypto/read/test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,167 @@
#[allow(unused_imports)]
use std::io::{self, Seek};

use super::CryptoRead;
#[allow(unused_imports)]
use ring::aead::AES_256_GCM;
#[allow(unused_imports)]
use secrecy::SecretVec;
#[allow(unused_imports)]
use std::io::{self, Seek};
#[allow(unused_imports)]
use tracing_test::traced_test;
#[allow(dead_code)]
fn create_secret_key(key_len: usize) -> SecretVec<u8> {
use rand::RngCore;
use secrecy::SecretVec;
let mut key = vec![0; key_len];
rand::thread_rng().fill_bytes(&mut key);
SecretVec::new(key)
}
#[allow(dead_code)]
fn create_encrypted_data(data: &[u8], key: &SecretVec<u8>) -> Vec<u8> {
use crate::crypto;
use crate::crypto::write::CryptoWrite;
use crate::crypto::Cipher;
use std::io::{Cursor, Write};
let writer = Cursor::new(Vec::new());
let cipher = Cipher::ChaCha20Poly1305;

#[allow(unused_imports)]
use crate::crypto::read::{CryptoRead, RingCryptoRead};
let mut crypto_writer = crypto::create_write(writer, cipher, key);

crypto_writer.write_all(data).unwrap();

crypto_writer.finish().unwrap().into_inner()
}

#[test]
#[traced_test]
fn test_read_empty() {
use super::RingCryptoRead;
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;
use std::io::Read;
let reader = Cursor::new(vec![]);
let mut buf = [0u8; 10];
let cipher = &CHACHA20_POLY1305;
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let mut crypto_reader = RingCryptoRead::new(reader, cipher, &key);
let result = &crypto_reader.read(&mut buf).unwrap();
let expected: usize = 0;
assert_eq!(*result, expected);
}

#[test]
#[traced_test]
fn test_read_single_block() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;

use std::io::Read;
let binding = "h".repeat(BLOCK_SIZE);
let data = binding.as_bytes();
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let encrypted_data = create_encrypted_data(data, &key);
let mut reader = RingCryptoRead::new(Cursor::new(encrypted_data), &CHACHA20_POLY1305, &key);
let mut buf = vec![0u8; BLOCK_SIZE];
assert_eq!(reader.read(&mut buf).unwrap(), BLOCK_SIZE);
}

#[test]
#[traced_test]
fn test_read_multiple_blocks() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;

use std::io::Read;
let num_blocks = 5;

let block_size = BLOCK_SIZE * num_blocks;

let binding = "h".repeat(block_size);
let data = binding.as_bytes();
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let encrypted_data = create_encrypted_data(data, &key);
let mut reader = RingCryptoRead::new(Cursor::new(encrypted_data), &CHACHA20_POLY1305, &key);
let mut buf = vec![0u8; block_size];
for _ in 0..num_blocks {
assert_eq!(reader.read(&mut buf).unwrap(), BLOCK_SIZE);
}
assert_eq!(reader.read(&mut buf).unwrap(), 0);
}

#[test]
#[traced_test]
fn test_partial_read() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;

use std::io::Read;
let binding = "h".repeat(BLOCK_SIZE);
let data = binding.as_bytes();
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let encrypted_data = create_encrypted_data(data, &key);
let mut reader = RingCryptoRead::new(Cursor::new(encrypted_data), &CHACHA20_POLY1305, &key);
let mut buf = vec![0u8; BLOCK_SIZE / 2];
assert_eq!(reader.read(&mut buf).unwrap(), BLOCK_SIZE / 2);
}

#[test]
#[traced_test]
fn test_read_one_byte_less_than_block() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE, NONCE_LEN};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;
use std::io::Read;
let data = vec![0u8; NONCE_LEN + BLOCK_SIZE + CHACHA20_POLY1305.tag_len() - 1];
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let mut reader = RingCryptoRead::new(Cursor::new(data), &CHACHA20_POLY1305, &key);
let mut buf = vec![0u8; BLOCK_SIZE];
assert!(reader.read(&mut buf).is_err());
}

#[test]
#[traced_test]
fn test_alternating_small_and_large_reads() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;

use std::io::Read;
let num_blocks = 5;

let block_size = BLOCK_SIZE + num_blocks;

let binding = "h".repeat(block_size);
let data = binding.as_bytes();
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let encrypted_data = create_encrypted_data(data, &key);
let mut reader = RingCryptoRead::new(Cursor::new(encrypted_data), &CHACHA20_POLY1305, &key);
let mut small_buf = vec![0u8; 10];
let mut large_buf = vec![0u8; 40];
assert_eq!(reader.read(&mut small_buf).unwrap(), 10);
assert_eq!(reader.read(&mut large_buf).unwrap(), 40);
assert_eq!(reader.read(&mut small_buf).unwrap(), 10);
assert_eq!(reader.read(&mut large_buf).unwrap(), 40);
assert_eq!(reader.read(&mut small_buf).unwrap(), 5);
assert_eq!(reader.read(&mut large_buf).unwrap(), 0);
assert_eq!(reader.read(&mut small_buf).unwrap(), 0);
}

#[test]
#[traced_test]
fn test_read_one_byte_more_than_block() {
use crate::crypto::read::{RingCryptoRead, BLOCK_SIZE, NONCE_LEN};
use ring::aead::CHACHA20_POLY1305;
use std::io::Cursor;
use std::io::Read;
let data = vec![0u8; NONCE_LEN + BLOCK_SIZE + CHACHA20_POLY1305.tag_len() + 1];
let key = create_secret_key(CHACHA20_POLY1305.key_len());
let mut reader = RingCryptoRead::new(Cursor::new(data), &CHACHA20_POLY1305, &key);
let mut buf = vec![0u8; BLOCK_SIZE];
assert!(reader.read(&mut buf).is_err());
}

#[test]
#[traced_test]
Expand Down Expand Up @@ -37,7 +189,7 @@ fn test_ring_crypto_read_seek_chacha() {

// Create a RingCryptoReaderSeek
cursor.seek(SeekFrom::Start(0)).unwrap();
let mut reader = RingCryptoRead::new(cursor, algorithm, &key);
let mut reader = RingCryptoRead::new(&mut cursor, algorithm, &key);

// Seek to the middle of the data
reader.seek(SeekFrom::Start(7)).unwrap();
Expand Down Expand Up @@ -66,7 +218,6 @@ fn test_ring_crypto_read_seek_aes() {
use std::io::{Cursor, Read, Seek, SeekFrom, Write};

use ring::aead::AES_256_GCM;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite};
Expand Down Expand Up @@ -116,7 +267,6 @@ fn test_ring_crypto_read_seek_blocks_chacha() {

use rand::Rng;
use ring::aead::CHACHA20_POLY1305;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand All @@ -138,7 +288,7 @@ fn test_ring_crypto_read_seek_blocks_chacha() {

// Create a RingCryptoReaderSeek
cursor.seek(SeekFrom::Start(0)).unwrap();
let mut reader = RingCryptoRead::new_seek(cursor, algorithm, &key);
let mut reader = RingCryptoRead::new_seek(&mut cursor, algorithm, &key);

// Seek in the second block
reader.seek(SeekFrom::Start(BLOCK_SIZE as u64)).unwrap();
Expand Down Expand Up @@ -168,7 +318,6 @@ fn test_ring_crypto_read_seek_blocks_aes() {

use rand::Rng;
use ring::aead::AES_256_GCM;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand Down Expand Up @@ -220,7 +369,6 @@ fn test_ring_crypto_read_seek_blocks_boundary_chacha() {

use rand::Rng;
use ring::aead::CHACHA20_POLY1305;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand All @@ -242,7 +390,7 @@ fn test_ring_crypto_read_seek_blocks_boundary_chacha() {

// Create a RingCryptoReaderSeek
cursor.seek(SeekFrom::Start(0)).unwrap();
let mut reader = RingCryptoRead::new_seek(cursor, algorithm, &key);
let mut reader = RingCryptoRead::new_seek(&mut cursor, algorithm, &key);

reader.read_exact(&mut [0; 1]).unwrap();
// Seek to the second block boundary
Expand All @@ -269,7 +417,6 @@ fn test_ring_crypto_read_seek_blocks_boundary_aes() {

use rand::Rng;
use ring::aead::AES_256_GCM;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand Down Expand Up @@ -318,7 +465,6 @@ fn test_ring_crypto_read_seek_skip_blocks_chacha() {

use rand::Rng;
use ring::aead::CHACHA20_POLY1305;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand Down Expand Up @@ -355,7 +501,6 @@ fn test_ring_crypto_read_seek_skip_blocks_aes() {

use rand::Rng;
use ring::aead::AES_256_GCM;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand Down Expand Up @@ -392,7 +537,6 @@ fn test_ring_crypto_read_seek_in_second_block() {

use rand::Rng;
use ring::aead::AES_256_GCM;
use secrecy::SecretVec;

use crate::crypto::read::RingCryptoRead;
use crate::crypto::write::{CryptoWrite, RingCryptoWrite, BLOCK_SIZE};
Expand All @@ -414,7 +558,7 @@ fn test_ring_crypto_read_seek_in_second_block() {

// Create a RingCryptoReaderSeek
cursor.seek(SeekFrom::Start(0)).unwrap();
let mut reader = RingCryptoRead::new_seek(&mut cursor, algorithm, &key);
let mut reader = RingCryptoRead::new_seek(cursor, algorithm, &key);

assert_eq!(
reader.seek(SeekFrom::Start(BLOCK_SIZE as u64)).unwrap(),
Expand All @@ -425,6 +569,7 @@ fn test_ring_crypto_read_seek_in_second_block() {
#[test]
#[traced_test]
fn finish_seek() {
use super::RingCryptoRead;
let reader = io::Cursor::new(vec![0; 10]);
let mut reader = RingCryptoRead::new_seek(reader, &AES_256_GCM, &SecretVec::new(vec![0; 32]));
let mut reader = reader.into_inner();
Expand Down
Loading

0 comments on commit 02d7dab

Please sign in to comment.