Skip to content

Commit

Permalink
build: added ENABLE_MOCK_TOR_PROVIDER cmake build flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pospesel committed Apr 2, 2024
1 parent d7dbaf2 commit b45c712
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 29 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.17)
project(gosling)

# Feature flags
option(ENABLE_MOCK_TOR_PROVIDER "Enable mocked tor support for tests" ON)
option(ENABLE_LEGACY_TOR_PROVIDER "Enable legacy c-tor daemon support" ON)

# Test options
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ Cargo will automatically download and build the required Rust crates. The list o

Build-time configuration options for features which may be conditionally enabled or disabled.

### ENABLE_MOCK_TOR_PROVIDER

```shell
cmake -DENABLE_MOCK_TOR_PROVIDER=ON
```

Enable the mock TorProvider implementation. This TorProvider is in-process and local only; it does not connect to the internet or the real Tor Network. It is only useful for testing. This option is **ON** by default.

### ENABLE_LEGACY_TOR_PROVIDER

```shell
Expand Down
1 change: 1 addition & 0 deletions source/gosling/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions source/gosling/crates/cgosling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ set(cgosling_impl_outputs
# cgosling crate feature flags
#
set(CGOSLING_FEATURES_LIST)
if (ENABLE_MOCK_TOR_PROVIDER)
list(APPEND CGOSLING_FEATURES_LIST "mock-tor-provider")
endif()
if (ENABLE_LEGACY_TOR_PROVIDER)
list(APPEND CGOSLING_FEATURES_LIST "legacy-tor-provider")
endif()
Expand Down
4 changes: 4 additions & 0 deletions source/gosling/crates/cgosling/Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ static_assertions = "1.1"
tor-interface = { path = "../tor-interface" }
which = "4.4"

[dev-dependencies]
serial_test = "0.9"

[lib]
name = "cgosling"
crate-type = ["staticlib" ,"rlib"]

[features]
impl-lib = []
mock-tor-provider = ["gosling/mock-tor-provider"]
legacy-tor-provider = ["gosling/legacy-tor-provider"]
1 change: 1 addition & 0 deletions source/gosling/crates/cgosling/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }

[dependencies.cgosling]
path = ".."
features = ["mock-tor-provider"]

# Prevent this from interfering with workspaces
[workspace]
Expand Down
2 changes: 2 additions & 0 deletions source/gosling/crates/cgosling/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use cgosling_proc_macros::*;
use gosling::context::*;
#[cfg(feature = "legacy-tor-provider")]
use tor_interface::legacy_tor_client::*;
#[cfg(feature = "mock-tor-provider")]
use tor_interface::mock_tor_client::*;
use tor_interface::tor_crypto::*;
use tor_interface::*;
Expand Down Expand Up @@ -1033,6 +1034,7 @@ pub unsafe extern "C" fn gosling_tor_provider_new_legacy_client(
/// @param out_tor_provider: returned tor provider
/// @param error: filled on error
#[no_mangle]
#[cfg(feature = "mock-tor-provider")]
#[cfg_attr(feature = "impl-lib", rename_impl)]
pub unsafe extern "C" fn gosling_tor_provider_new_mock_client(
out_tor_provider: *mut *mut GoslingTorProvider,
Expand Down
117 changes: 89 additions & 28 deletions source/gosling/crates/cgosling/tests/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::sync::atomic::{AtomicBool, Ordering};

// external crates
use anyhow::bail;
#[cfg(test)]
use serial_test::serial;

// internal crates
use cgosling::ffi::*;
Expand Down Expand Up @@ -234,26 +236,29 @@ fn create_server_endpoint_handshake(context: *mut GoslingContext) -> anyhow::Res
}

#[test]
fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
// init libary
#[serial]
#[cfg(feature = "mock-tor-provider")]
fn test_gosling_ffi_handshake_mock_client() -> anyhow::Result<()> {
let library = test_gosling_ffi_handshake_preamble()?;

println!("--- init gosling library");
let mut library: *mut GoslingLibrary = ptr::null_mut();
require_noerror!(gosling_library_init(&mut library));
// construct tor providers
let mut alice_tor_provider: *mut GoslingTorProvider = ptr::null_mut();
require_noerror!(gosling_tor_provider_new_mock_client(&mut alice_tor_provider));

println!("--- library: {:?}", library);
let mut pat_tor_provider: *mut GoslingTorProvider = ptr::null_mut();
require_noerror!(gosling_tor_provider_new_mock_client(&mut pat_tor_provider));

// init alice
// do test
test_gosling_ffi_handshake_impl(library, alice_tor_provider, pat_tor_provider)
}

println!("--- init alice");
let mut alice_private_key: *mut GoslingEd25519PrivateKey = ptr::null_mut();
require_noerror!(gosling_ed25519_private_key_generate(&mut alice_private_key));
#[test]
#[serial]
#[cfg(feature = "legacy-tor-provider")]
fn test_gosling_ffi_handshake_legacy_client() -> anyhow::Result<()> {
let library = test_gosling_ffi_handshake_preamble()?;

let mut alice_identity: *mut GoslingV3OnionServiceId = ptr::null_mut();
require_noerror!(gosling_v3_onion_service_id_from_ed25519_private_key(
&mut alice_identity,
alice_private_key
));
// construct tor providers

let mut alice_working_dir = std::env::temp_dir();
alice_working_dir.push("gosling_context_test_alice");
Expand All @@ -268,6 +273,50 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
alice_working_dir.as_bytes().len()
));

let mut pat_working_dir = std::env::temp_dir();
pat_working_dir.push("gosling_context_test_pat");
let pat_working_dir: CString = CString::new(pat_working_dir.to_str().unwrap())?;

let mut pat_tor_provider: *mut GoslingTorProvider = ptr::null_mut();
require_noerror!(gosling_tor_provider_new_legacy_client(
&mut pat_tor_provider,
ptr::null(),
0usize,
pat_working_dir.as_ptr(),
pat_working_dir.as_bytes().len()
));

// do test
test_gosling_ffi_handshake_impl(library, alice_tor_provider, pat_tor_provider)
}

fn test_gosling_ffi_handshake_preamble() -> anyhow::Result<*mut GoslingLibrary> {
// init libary

println!("--- init gosling library");
let mut library: *mut GoslingLibrary = ptr::null_mut();
require_noerror!(gosling_library_init(&mut library));

println!("--- library: {:?}", library);

Ok(library)
}

fn test_gosling_ffi_handshake_impl(library: *mut GoslingLibrary, alice_tor_provider: *mut GoslingTorProvider, pat_tor_provider: *mut GoslingTorProvider) -> anyhow::Result<()> {


// init alice

println!("--- init alice");
let mut alice_private_key: *mut GoslingEd25519PrivateKey = ptr::null_mut();
require_noerror!(gosling_ed25519_private_key_generate(&mut alice_private_key));

let mut alice_identity: *mut GoslingV3OnionServiceId = ptr::null_mut();
require_noerror!(gosling_v3_onion_service_id_from_ed25519_private_key(
&mut alice_identity,
alice_private_key
));

let mut alice_context: *mut GoslingContext = ptr::null_mut();
require_noerror!(gosling_context_init(
&mut alice_context,
Expand All @@ -291,19 +340,6 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
pat_private_key
));

let mut pat_working_dir = std::env::temp_dir();
pat_working_dir.push("gosling_context_test_pat");
let pat_working_dir: CString = CString::new(pat_working_dir.to_str().unwrap())?;

let mut pat_tor_provider: *mut GoslingTorProvider = ptr::null_mut();
require_noerror!(gosling_tor_provider_new_legacy_client(
&mut pat_tor_provider,
ptr::null(),
0usize,
pat_working_dir.as_ptr(),
pat_working_dir.as_bytes().len()
));

let mut pat_context: *mut GoslingContext = ptr::null_mut();
require_noerror!(gosling_context_init(
&mut pat_context,
Expand All @@ -318,6 +354,8 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
// bootstrap alice

static ALICE_BOOTSTRAP_COMPLETE: AtomicBool = AtomicBool::new(false);
ALICE_BOOTSTRAP_COMPLETE.store(false, Ordering::Relaxed);

extern "C" fn alice_bootstrap_complete_callback(context: *mut GoslingContext) -> () {
assert!(!context.is_null());
ALICE_BOOTSTRAP_COMPLETE.store(true, Ordering::Relaxed);
Expand All @@ -336,6 +374,8 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {

// init alice's identity server
static ALICE_IDENTITY_SERVER_READY: AtomicBool = AtomicBool::new(false);
ALICE_IDENTITY_SERVER_READY.store(false, Ordering::Relaxed);

extern "C" fn alice_identity_server_published_callback(context: *mut GoslingContext) -> () {
assert!(!context.is_null());
println!("--- alice identity server published");
Expand All @@ -357,6 +397,8 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
// bootstrap pat

static PAT_BOOTSTRAP_COMPLETE: AtomicBool = AtomicBool::new(false);
PAT_BOOTSTRAP_COMPLETE.store(false, Ordering::Relaxed);

extern "C" fn pat_bootstrap_complete_callback(context: *mut GoslingContext) -> () {
assert!(!context.is_null());

Expand All @@ -380,6 +422,12 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
static mut PAT_ENDPOINT_REQUEST_COMPLETE: bool = false;
static mut ALICE_ENDPOINT_SERVICE_ID: *mut GoslingV3OnionServiceId = ptr::null_mut();
static mut PAT_ONION_AUTH_PRIVATE_KEY: *mut GoslingX25519PrivateKey = ptr::null_mut();
unsafe {
PAT_ENDPOINT_REQUEST_COMPLETE = false;
ALICE_ENDPOINT_SERVICE_ID = ptr::null_mut();
PAT_ONION_AUTH_PRIVATE_KEY = ptr::null_mut();
}

extern "C" fn pat_identity_client_handshake_completed_callback(
context: *mut GoslingContext,
_handshake_handle: usize,
Expand Down Expand Up @@ -467,6 +515,12 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
static mut ALICE_ENDPOINT_PRIVATE_KEY: *mut GoslingEd25519PrivateKey = ptr::null_mut();
static mut PAT_IDENTITY_SERVICE_ID: *mut GoslingV3OnionServiceId = ptr::null_mut();
static mut PAT_ONION_AUTH_PUBLIC_KEY: *mut GoslingX25519PublicKey = ptr::null_mut();
unsafe {
ALICE_ENDPOINT_REQUEST_COMPLETE = false;
ALICE_ENDPOINT_PRIVATE_KEY = ptr::null_mut();
PAT_IDENTITY_SERVICE_ID = ptr::null_mut();
PAT_ONION_AUTH_PUBLIC_KEY = ptr::null_mut();
}

extern "C" fn alice_identity_server_handshake_completed_callback(
context: *mut GoslingContext,
Expand Down Expand Up @@ -601,6 +655,9 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {
// start alice's enddpoint server

static mut ALICE_ENDPOINT_PUBLISHED: bool = false;
unsafe {
ALICE_ENDPOINT_PUBLISHED = false;
}

extern "C" fn alice_endpoint_server_published_callback(
context: *mut GoslingContext,
Expand Down Expand Up @@ -646,6 +703,10 @@ fn test_gosling_ffi_handshake() -> anyhow::Result<()> {

static mut PAT_SOCKET: Option<TcpSocket> = None;
static mut ALICE_SOCKET: Option<TcpSocket> = None;
unsafe {
PAT_SOCKET = None;
ALICE_SOCKET = None;
}

extern "C" fn pat_enpdoint_client_handshake_completed_callback(
context: *mut GoslingContext,
Expand Down
3 changes: 3 additions & 0 deletions source/gosling/crates/gosling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ set(gosling_outputs
# gosling crate feature flags
#
set(GOSLING_FEATURES_LIST)
if (ENABLE_MOCK_TOR_PROVIDER)
list(APPEND GOSLING_FEATURES_LIST "mock-tor-provider")
endif()
if (ENABLE_LEGACY_TOR_PROVIDER)
list(APPEND GOSLING_FEATURES_LIST "legacy-tor-provider")
endif()
Expand Down
1 change: 1 addition & 0 deletions source/gosling/crates/gosling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ serial_test = "0.9"
which = "4.4"

[features]
mock-tor-provider = ["tor-interface/mock-tor-provider"]
legacy-tor-provider = ["tor-interface/legacy-tor-provider"]
2 changes: 1 addition & 1 deletion source/gosling/crates/gosling/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ curve25519-dalek = "4.1"
data-encoding = "2.0"
honk-rpc = { path = "../../honk-rpc" }
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
tor-interface = { path = "../../tor-interface" }
tor-interface = { path = "../../tor-interface", features = ["mock-tor-provider"] }

[dependencies.gosling]
path = ".."
Expand Down
2 changes: 2 additions & 0 deletions source/gosling/crates/gosling/tests/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bson::doc;
use serial_test::serial;
#[cfg(feature = "legacy-tor-provider")]
use tor_interface::legacy_tor_client::*;
#[cfg(feature = "mock-tor-provider")]
use tor_interface::mock_tor_client::*;
use tor_interface::tor_crypto::*;
use tor_interface::tor_provider::*;
Expand All @@ -16,6 +17,7 @@ use tor_interface::tor_provider::*;
use gosling::context::*;

#[test]
#[cfg(feature = "mock-tor-provider")]
fn test_mock_client_gosling_context() -> anyhow::Result<()> {
let alice_tor_client = Box::new(MockTorClient::new());
let pat_tor_client = Box::new(MockTorClient::new());
Expand Down
3 changes: 3 additions & 0 deletions source/gosling/crates/tor-interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ set(tor_interface_outputs
#

set(TOR_INTERFACE_FEATURE_LIST)
if (ENABLE_MOCK_TOR_PROVIDER)
list(APPEND TOR_INTERFACE_FEATURE_LIST "mock-tor-provider")
endif()
if (ENABLE_LEGACY_TOR_PROVIDER)
list(APPEND TOR_INTERFACE_FEATURE_LIST "legacy-tor-provider")
endif()
Expand Down
2 changes: 2 additions & 0 deletions source/gosling/crates/tor-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ tor-llcrypto = { version = "0.7", features = ["relay"] }
anyhow = "1.0"
serial_test = "0.9"
which = "4.4"

[features]
mock-tor-provider = []
legacy-tor-provider = []
1 change: 1 addition & 0 deletions source/gosling/crates/tor-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod legacy_tor_controller;
mod legacy_tor_process;
#[cfg(feature = "legacy-tor-provider")]
mod legacy_tor_version;
#[cfg(feature = "mock-tor-provider")]
pub mod mock_tor_client;
pub mod tor_crypto;
pub mod tor_provider;
3 changes: 3 additions & 0 deletions source/gosling/crates/tor-interface/tests/tor_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serial_test::serial;
// internal crates
#[cfg(feature = "legacy-tor-provider")]
use tor_interface::legacy_tor_client::*;
#[cfg(feature = "mock-tor-provider")]
use tor_interface::mock_tor_client::*;
use tor_interface::tor_crypto::*;
use tor_interface::tor_provider::*;
Expand Down Expand Up @@ -204,11 +205,13 @@ pub(crate) fn onion_service_test(mut tor: Box<dyn TorProvider>) -> anyhow::Resul
}

#[test]
#[cfg(feature = "mock-tor-provider")]
fn test_mock_bootstrap() -> anyhow::Result<()> {
bootstrap_test(Box::new(MockTorClient::new()))
}

#[test]
#[cfg(feature = "mock-tor-provider")]
fn test_mock_onion_service() -> anyhow::Result<()> {
onion_service_test(Box::new(MockTorClient::new()))
}
Expand Down

0 comments on commit b45c712

Please sign in to comment.