Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
- Removed the `herodo` binary from the monorepo. This was done as part of the monorepo conversion process. - Updated the `Cargo.toml` file to reflect the removal of `herodo` and adjust dependencies accordingly. - Updated `src/lib.rs` and `src/rhai/mod.rs` to use the new `sal-vault` crate for vault functionality. This improves the modularity and maintainability of the project.
122 lines
3.5 KiB
Rust
122 lines
3.5 KiB
Rust
use sal_vault::error::CryptoError;
|
|
use sal_vault::keyspace::{KeyPair, KeySpace};
|
|
use sal_vault::symmetric::implementation::{
|
|
decrypt_symmetric, encrypt_symmetric, generate_symmetric_key,
|
|
};
|
|
|
|
#[test]
|
|
fn test_symmetric_key_generation() {
|
|
let key1 = generate_symmetric_key();
|
|
let key2 = generate_symmetric_key();
|
|
|
|
// Keys should be different
|
|
assert_ne!(key1, key2);
|
|
|
|
// Keys should be 32 bytes
|
|
assert_eq!(key1.len(), 32);
|
|
assert_eq!(key2.len(), 32);
|
|
}
|
|
|
|
#[test]
|
|
fn test_symmetric_encryption_decryption() {
|
|
let key = generate_symmetric_key();
|
|
let message = b"Hello, World!";
|
|
|
|
// Encrypt the message
|
|
let encrypted = encrypt_symmetric(&key, message).expect("Encryption should succeed");
|
|
|
|
// Encrypted data should be different from original
|
|
assert_ne!(encrypted.as_slice(), message);
|
|
|
|
// Decrypt the message
|
|
let decrypted = decrypt_symmetric(&key, &encrypted).expect("Decryption should succeed");
|
|
|
|
// Decrypted data should match original
|
|
assert_eq!(decrypted.as_slice(), message);
|
|
}
|
|
|
|
#[test]
|
|
fn test_symmetric_encryption_with_wrong_key() {
|
|
let key1 = generate_symmetric_key();
|
|
let key2 = generate_symmetric_key();
|
|
let message = b"Secret message";
|
|
|
|
// Encrypt with key1
|
|
let encrypted = encrypt_symmetric(&key1, message).expect("Encryption should succeed");
|
|
|
|
// Try to decrypt with key2 (should fail)
|
|
let result = decrypt_symmetric(&key2, &encrypted);
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn test_keyspace_creation() {
|
|
let mut keyspace = KeySpace::new("test_space");
|
|
|
|
assert_eq!(keyspace.name, "test_space");
|
|
assert!(keyspace.keypairs.is_empty());
|
|
|
|
// Add a keypair
|
|
keyspace
|
|
.add_keypair("test_key")
|
|
.expect("Adding keypair should succeed");
|
|
|
|
assert_eq!(keyspace.keypairs.len(), 1);
|
|
assert!(keyspace.keypairs.contains_key("test_key"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_keypair_creation() {
|
|
let keypair = KeyPair::new("test_keypair");
|
|
|
|
// Test that we can get the public key
|
|
let public_key = keypair.pub_key();
|
|
assert!(!public_key.is_empty());
|
|
|
|
// Test signing and verification
|
|
let message = b"test message";
|
|
let signature = keypair.sign(message);
|
|
|
|
let is_valid = keypair
|
|
.verify(message, &signature)
|
|
.expect("Verification should succeed");
|
|
assert!(is_valid);
|
|
|
|
// Test with wrong message
|
|
let wrong_message = b"wrong message";
|
|
let is_valid = keypair
|
|
.verify(wrong_message, &signature)
|
|
.expect("Verification should succeed");
|
|
assert!(!is_valid);
|
|
}
|
|
|
|
#[test]
|
|
fn test_keyspace_serialization() {
|
|
let mut keyspace = KeySpace::new("test_space");
|
|
keyspace
|
|
.add_keypair("test_key")
|
|
.expect("Adding keypair should succeed");
|
|
|
|
// Serialize
|
|
let serialized = serde_json::to_string(&keyspace).expect("Serialization should succeed");
|
|
|
|
// Deserialize
|
|
let deserialized: KeySpace =
|
|
serde_json::from_str(&serialized).expect("Deserialization should succeed");
|
|
|
|
assert_eq!(deserialized.name, keyspace.name);
|
|
assert_eq!(deserialized.keypairs.len(), keyspace.keypairs.len());
|
|
}
|
|
|
|
#[test]
|
|
fn test_error_types() {
|
|
let error = CryptoError::InvalidKeyLength;
|
|
assert_eq!(error.to_string(), "Invalid key length");
|
|
|
|
let error = CryptoError::EncryptionFailed("test error".to_string());
|
|
assert_eq!(error.to_string(), "Encryption failed: test error");
|
|
|
|
let error = CryptoError::KeypairNotFound("test_key".to_string());
|
|
assert_eq!(error.to_string(), "Keypair not found: test_key");
|
|
}
|