summaryrefslogtreecommitdiff
path: root/src/rust
diff options
context:
space:
mode:
Diffstat (limited to 'src/rust')
-rw-r--r--src/rust/Cargo.toml9
-rw-r--r--src/rust/build.rs12
-rw-r--r--src/rust/crypto/Cargo.toml4
-rw-r--r--src/rust/crypto/digests/sha2.rs6
-rw-r--r--src/rust/protover/ffi.rs62
-rw-r--r--src/rust/tor_allocate/tor_allocate.rs12
6 files changed, 62 insertions, 43 deletions
diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml
index c3e44d2a79..4bbadbe535 100644
--- a/src/rust/Cargo.toml
+++ b/src/rust/Cargo.toml
@@ -14,3 +14,12 @@ members = [
debug = true
panic = "abort"
+[features]
+default = []
+# If this feature is enabled, test code which calls Tor C code from Rust will
+# execute with `cargo test`. Due to numerous linker issues (#25386), this is
+# currently disabled by default. Crates listed here are those which, in their
+# unittests, doctests, and/or integration tests, call C code.
+test-c-from-rust = [
+ "crypto/test-c-from-rust",
+]
diff --git a/src/rust/build.rs b/src/rust/build.rs
index a35e6868c6..78773915a5 100644
--- a/src/rust/build.rs
+++ b/src/rust/build.rs
@@ -149,14 +149,16 @@ pub fn main() {
// tor uses. We must be careful with factoring and dependencies
// moving forward!
cfg.component("tor-crypt-ops-testing");
- cfg.component("tor-sandbox");
+ cfg.component("tor-sandbox-testing");
cfg.component("tor-encoding-testing");
- cfg.component("tor-net");
+ cfg.component("tor-fs-testing");
+ cfg.component("tor-time-testing");
+ cfg.component("tor-net-testing");
cfg.component("tor-thread-testing");
cfg.component("tor-memarea-testing");
- cfg.component("tor-log");
- cfg.component("tor-lock");
- cfg.component("tor-fdio");
+ cfg.component("tor-log-testing");
+ cfg.component("tor-lock-testing");
+ cfg.component("tor-fdio-testing");
cfg.component("tor-container-testing");
cfg.component("tor-smartlist-core-testing");
cfg.component("tor-string-testing");
diff --git a/src/rust/crypto/Cargo.toml b/src/rust/crypto/Cargo.toml
index 869e0d6256..d68ac48e28 100644
--- a/src/rust/crypto/Cargo.toml
+++ b/src/rust/crypto/Cargo.toml
@@ -26,3 +26,7 @@ rand = { version = "=0.5.0-pre.2", default-features = false }
rand_core = { version = "=0.2.0-pre.0", default-features = false }
[features]
+# If this feature is enabled, test code which calls Tor C code from Rust will
+# execute with `cargo test`. Due to numerous linker issues (#25386), this is
+# currently disabled by default.
+test-c-from-rust = []
diff --git a/src/rust/crypto/digests/sha2.rs b/src/rust/crypto/digests/sha2.rs
index 03e0843dc0..d0246eeb94 100644
--- a/src/rust/crypto/digests/sha2.rs
+++ b/src/rust/crypto/digests/sha2.rs
@@ -165,15 +165,19 @@ impl FixedOutput for Sha512 {
#[cfg(test)]
mod test {
+ #[cfg(feature = "test-c-from-rust")]
use digest::Digest;
+ #[cfg(feature = "test-c-from-rust")]
use super::*;
+ #[cfg(feature = "test-c-from-rust")]
#[test]
fn sha256_default() {
let _: Sha256 = Sha256::default();
}
+ #[cfg(feature = "test-c-from-rust")]
#[test]
fn sha256_digest() {
let mut h: Sha256 = Sha256::new();
@@ -193,11 +197,13 @@ mod test {
assert_eq!(result, expected);
}
+ #[cfg(feature = "test-c-from-rust")]
#[test]
fn sha512_default() {
let _: Sha512 = Sha512::default();
}
+ #[cfg(feature = "test-c-from-rust")]
#[test]
fn sha512_digest() {
let mut h: Sha512 = Sha512::new();
diff --git a/src/rust/protover/ffi.rs b/src/rust/protover/ffi.rs
index 3055893d43..91bd83addf 100644
--- a/src/rust/protover/ffi.rs
+++ b/src/rust/protover/ffi.rs
@@ -42,7 +42,6 @@ pub extern "C" fn protover_all_supported(
c_relay_version: *const c_char,
missing_out: *mut *mut c_char,
) -> c_int {
-
if c_relay_version.is_null() {
return 1;
}
@@ -58,14 +57,13 @@ pub extern "C" fn protover_all_supported(
let relay_proto_entry: UnvalidatedProtoEntry =
match UnvalidatedProtoEntry::from_str_any_len(relay_version) {
- Ok(n) => n,
- Err(_) => return 1,
- };
- let maybe_unsupported: Option<UnvalidatedProtoEntry> = relay_proto_entry.all_supported();
+ Ok(n) => n,
+ Err(_) => return 1,
+ };
- if maybe_unsupported.is_some() {
- let unsupported: UnvalidatedProtoEntry = maybe_unsupported.unwrap();
- let c_unsupported: CString = match CString::new(unsupported.to_string()) {
+ if let Some(unsupported) = relay_proto_entry.all_supported() {
+ let c_unsupported: CString = match CString::new(unsupported.to_string())
+ {
Ok(n) => n,
Err(_) => return 1,
};
@@ -100,22 +98,23 @@ pub extern "C" fn protocol_list_supports_protocol(
Err(_) => return 1,
};
let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
- Ok(n) => n,
+ Ok(n) => n,
Err(_) => return 0,
};
let protocol: UnknownProtocol = match translate_to_rust(c_protocol) {
Ok(n) => n.into(),
Err(_) => return 0,
};
- match proto_entry.supports_protocol(&protocol, &version) {
- false => return 0,
- true => return 1,
+ if proto_entry.supports_protocol(&protocol, &version) {
+ 1
+ } else {
+ 0
}
}
#[no_mangle]
pub extern "C" fn protover_contains_long_protocol_names_(
- c_protocol_list: *const c_char
+ c_protocol_list: *const c_char,
) -> c_int {
if c_protocol_list.is_null() {
return 1;
@@ -127,13 +126,10 @@ pub extern "C" fn protover_contains_long_protocol_names_(
let protocol_list = match c_str.to_str() {
Ok(n) => n,
- Err(_) => return 1
+ Err(_) => return 1,
};
- let protocol_entry : Result<UnvalidatedProtoEntry,_> =
- protocol_list.parse();
-
- match protocol_entry {
+ match protocol_list.parse::<UnvalidatedProtoEntry>() {
Ok(_) => 0,
Err(_) => 1,
}
@@ -166,7 +162,7 @@ pub extern "C" fn protocol_list_supports_protocol_or_later(
};
let proto_entry: UnvalidatedProtoEntry = match protocol_list.parse() {
- Ok(n) => n,
+ Ok(n) => n,
Err(_) => return 1,
};
@@ -196,10 +192,8 @@ pub extern "C" fn protover_compute_vote(
threshold: c_int,
allow_long_proto_names: bool,
) -> *mut c_char {
-
if list.is_null() {
- let empty = String::new();
- return allocate_and_copy_string(&empty);
+ return allocate_and_copy_string("");
}
// Dereference of raw pointer requires an unsafe block. The pointer is
@@ -209,17 +203,21 @@ pub extern "C" fn protover_compute_vote(
let mut proto_entries: Vec<UnvalidatedProtoEntry> = Vec::new();
for datum in data {
- let entry: UnvalidatedProtoEntry = match allow_long_proto_names {
- true => match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
- Ok(n) => n,
- Err(_) => continue},
- false => match datum.parse() {
- Ok(n) => n,
- Err(_) => continue},
+ let entry: UnvalidatedProtoEntry = if allow_long_proto_names {
+ match UnvalidatedProtoEntry::from_str_any_len(datum.as_str()) {
+ Ok(n) => n,
+ Err(_) => continue,
+ }
+ } else {
+ match datum.parse() {
+ Ok(n) => n,
+ Err(_) => continue,
+ }
};
proto_entries.push(entry);
}
- let vote: UnvalidatedProtoEntry = ProtoverVote::compute(&proto_entries, &hold);
+ let vote: UnvalidatedProtoEntry =
+ ProtoverVote::compute(&proto_entries, &hold);
allocate_and_copy_string(&vote.to_string())
}
@@ -244,7 +242,9 @@ pub extern "C" fn protover_is_supported_here(
/// Provide an interface for C to translate arguments and return types for
/// protover::compute_for_old_tor
#[no_mangle]
-pub extern "C" fn protover_compute_for_old_tor(version: *const c_char) -> *const c_char {
+pub extern "C" fn protover_compute_for_old_tor(
+ version: *const c_char,
+) -> *const c_char {
let supported: &'static CStr;
let empty: &'static CStr;
diff --git a/src/rust/tor_allocate/tor_allocate.rs b/src/rust/tor_allocate/tor_allocate.rs
index 47fa5fc593..d0c0d79943 100644
--- a/src/rust/tor_allocate/tor_allocate.rs
+++ b/src/rust/tor_allocate/tor_allocate.rs
@@ -9,9 +9,9 @@ use libc::c_void;
// Define a no-op implementation for testing Rust modules without linking to C
#[cfg(feature = "testing")]
-pub fn allocate_and_copy_string(s: &String) -> *mut c_char {
+pub fn allocate_and_copy_string(s: &str) -> *mut c_char {
use std::ffi::CString;
- CString::new(s.as_str()).unwrap().into_raw()
+ CString::new(s).unwrap().into_raw()
}
// Defined only for tests, used for testing purposes, so that we don't need
@@ -39,7 +39,7 @@ extern "C" {
/// A `*mut c_char` that should be freed by tor_free in C
///
#[cfg(not(feature = "testing"))]
-pub fn allocate_and_copy_string(src: &String) -> *mut c_char {
+pub fn allocate_and_copy_string(src: &str) -> *mut c_char {
let bytes: &[u8] = src.as_bytes();
let size = mem::size_of_val::<[u8]>(bytes);
@@ -77,8 +77,7 @@ mod test {
use tor_allocate::allocate_and_copy_string;
- let empty = String::new();
- let allocated_empty = allocate_and_copy_string(&empty);
+ let allocated_empty = allocate_and_copy_string("");
let allocated_empty_rust =
unsafe { CStr::from_ptr(allocated_empty).to_str().unwrap() };
@@ -95,8 +94,7 @@ mod test {
use tor_allocate::allocate_and_copy_string;
- let empty = String::from("foo bar biz");
- let allocated_empty = allocate_and_copy_string(&empty);
+ let allocated_empty = allocate_and_copy_string("foo bar biz");
let allocated_empty_rust =
unsafe { CStr::from_ptr(allocated_empty).to_str().unwrap() };