diff --git a/fhevm-engine/coprocessor/src/tests/mod.rs b/fhevm-engine/coprocessor/src/tests/mod.rs index 71d3b6c6..64a6ea10 100644 --- a/fhevm-engine/coprocessor/src/tests/mod.rs +++ b/fhevm-engine/coprocessor/src/tests/mod.rs @@ -1,19 +1,19 @@ use std::str::FromStr; +use crate::server::common::FheOperation; use crate::server::coprocessor::async_computation_input::Input; use crate::server::coprocessor::fhevm_coprocessor_client::FhevmCoprocessorClient; use crate::server::coprocessor::{ AsyncComputation, AsyncComputationInput, AsyncComputeRequest, DebugDecryptRequest, DebugEncryptRequest, DebugEncryptRequestSingle, }; -use crate::server::common::FheOperation; use tonic::metadata::MetadataValue; -use utils::{default_api_key, wait_until_all_ciphertexts_computed}; +use utils::{default_api_key, random_handle, wait_until_all_ciphertexts_computed}; +mod errors; +mod inputs; mod operators; mod utils; -mod inputs; -mod errors; #[tokio::test] async fn test_smoke() -> Result<(), Box> { @@ -24,17 +24,22 @@ async fn test_smoke() -> Result<(), Box> { let api_key_header = format!("bearer {}", default_api_key()); let ct_type = 4; // i32 + let h1 = random_handle().to_be_bytes(); + let h2 = random_handle().to_be_bytes(); + let h3 = random_handle().to_be_bytes(); + let h4 = random_handle().to_be_bytes(); + // encrypt two ciphertexts { let mut encrypt_request = tonic::Request::new(DebugEncryptRequest { values: vec![ DebugEncryptRequestSingle { - handle: vec![0x0a, 0xbc], + handle: h1.to_vec(), be_value: vec![123], output_type: ct_type, }, DebugEncryptRequestSingle { - handle: vec![0x0a, 0xbd], + handle: h2.to_vec(), be_value: vec![124], output_type: ct_type, }, @@ -54,10 +59,10 @@ async fn test_smoke() -> Result<(), Box> { computations: vec![ AsyncComputation { operation: FheOperation::FheAdd.into(), - output_handle: vec![0x0a, 0xbf], + output_handle: h3.to_vec(), inputs: vec![ AsyncComputationInput { - input: Some(Input::InputHandle(vec![0x0a, 0xbe])), + input: Some(Input::InputHandle(h4.to_vec())), }, AsyncComputationInput { input: Some(Input::Scalar(vec![0x00, 0x10])), @@ -66,13 +71,13 @@ async fn test_smoke() -> Result<(), Box> { }, AsyncComputation { operation: FheOperation::FheAdd.into(), - output_handle: vec![0x0a, 0xbe], + output_handle: h4.to_vec(), inputs: vec![ AsyncComputationInput { - input: Some(Input::InputHandle(vec![0x0a, 0xbc])), + input: Some(Input::InputHandle(h1.to_vec())), }, AsyncComputationInput { - input: Some(Input::InputHandle(vec![0x0a, 0xbd])), + input: Some(Input::InputHandle(h2.to_vec())), }, ], }, @@ -92,7 +97,7 @@ async fn test_smoke() -> Result<(), Box> { // decrypt values { let mut decrypt_request = tonic::Request::new(DebugDecryptRequest { - handles: vec![vec![0x0a, 0xbe], vec![0x0a, 0xbf]], + handles: vec![h4.to_vec(), h3.to_vec()], }); decrypt_request.metadata_mut().append( "authorization", @@ -110,4 +115,4 @@ async fn test_smoke() -> Result<(), Box> { } Ok(()) -} \ No newline at end of file +} diff --git a/fhevm-engine/coprocessor/src/tests/operators.rs b/fhevm-engine/coprocessor/src/tests/operators.rs index bc4f0ede..3ffc9dac 100644 --- a/fhevm-engine/coprocessor/src/tests/operators.rs +++ b/fhevm-engine/coprocessor/src/tests/operators.rs @@ -4,7 +4,7 @@ use crate::server::coprocessor::{ AsyncComputation, AsyncComputeRequest, DebugDecryptRequest, DebugEncryptRequest, DebugEncryptRequestSingle, }; -use crate::tests::utils::wait_until_all_ciphertexts_computed; +use crate::tests::utils::{random_handle, wait_until_all_ciphertexts_computed}; use crate::{ server::coprocessor::{async_computation_input::Input, AsyncComputationInput}, tests::utils::{default_api_key, setup_test_app}, @@ -14,7 +14,6 @@ use fhevm_engine_common::tfhe_ops::{ does_fhe_operation_support_both_encrypted_operands, does_fhe_operation_support_scalar, }; use fhevm_engine_common::types::{FheOperationType, SupportedFheOperations}; -use rand::Rng; use std::{ops::Not, str::FromStr}; use strum::IntoEnumIterator; use tonic::metadata::MetadataValue; @@ -75,17 +74,13 @@ fn supported_bits_to_bit_type_in_db(inp: i32) -> i32 { } } -fn random_handle_start() -> u64 { - rand::thread_rng().gen() -} - #[tokio::test] async fn test_fhe_binary_operands() -> Result<(), Box> { let ops = generate_binary_test_cases(); let app = setup_test_app().await?; let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?; - let mut handle_counter: u64 = random_handle_start(); + let mut handle_counter: u64 = random_handle(); let mut next_handle = || { let out: u64 = handle_counter; handle_counter += 1; @@ -223,7 +218,7 @@ async fn test_fhe_unary_operands() -> Result<(), Box> { let app = setup_test_app().await?; let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?; - let mut handle_counter: u64 = random_handle_start(); + let mut handle_counter: u64 = random_handle(); let mut next_handle = || { let out: u64 = handle_counter; handle_counter += 1; @@ -336,7 +331,7 @@ async fn test_fhe_casts() -> Result<(), Box> { let app = setup_test_app().await?; let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?; - let mut handle_counter = random_handle_start(); + let mut handle_counter = random_handle(); let mut next_handle = || { let out: u64 = handle_counter; handle_counter += 1; @@ -471,7 +466,7 @@ async fn test_fhe_if_then_else() -> Result<(), Box> { let app = setup_test_app().await?; let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?; - let mut handle_counter = random_handle_start(); + let mut handle_counter = random_handle(); let mut next_handle = || { let out: u64 = handle_counter; handle_counter += 1; diff --git a/fhevm-engine/coprocessor/src/tests/utils.rs b/fhevm-engine/coprocessor/src/tests/utils.rs index 81644cc4..c98194f4 100644 --- a/fhevm-engine/coprocessor/src/tests/utils.rs +++ b/fhevm-engine/coprocessor/src/tests/utils.rs @@ -1,4 +1,5 @@ use crate::cli::Args; +use rand::Rng; use std::sync::atomic::{AtomicU16, Ordering}; use testcontainers::{core::WaitFor, runners::AsyncRunner, GenericImage, ImageExt}; use tokio::sync::watch::Receiver; @@ -39,6 +40,10 @@ pub fn default_tenant_id() -> i32 { 1 } +pub fn random_handle() -> u64 { + rand::thread_rng().gen() +} + pub async fn setup_test_app() -> Result> { if std::env::var("COPROCESSOR_TEST_LOCALHOST").is_ok() { setup_test_app_existing_localhost().await @@ -60,7 +65,7 @@ async fn setup_test_app_existing_localhost() -> Result Result> { +async fn setup_test_app_existing_db() -> Result> { let app_port = get_app_port(); let (app_close_channel, rx) = tokio::sync::watch::channel(false); start_coprocessor(rx, app_port, &LOCAL_DB_URL).await;