Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustls_version() integration test #434

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ capi = []

[dependencies]
# Keep in sync with RUSTLS_CRATE_VERSION in build.rs
rustls = { version = "0.23.4", default-features = false, features = [ "ring", "std", "tls12" ]}
rustls = { version = "0.23.4", default-features = false, features = ["ring", "std", "tls12"] }
pki-types = { package = "rustls-pki-types", version = "1", features = ["std"] }
webpki = { package = "rustls-webpki", version = "0.102.0", default-features = false, features = [ "ring", "std" ] }
webpki = { package = "rustls-webpki", version = "0.102.0", default-features = false, features = ["ring", "std"] }
libc = "0.2"
rustls-pemfile = "2"
log = "0.4.17"
Expand All @@ -38,6 +38,7 @@ crate-type = ["lib", "staticlib"]

[dev-dependencies]
regex = "1.9.6"
toml = { version = "0.6.0", default-features = false, features = ["parse"] }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: the version here, and in the Cargo.lock was arranged based on this crate's MSRV. The toml stack of crates (toml, toml-edit, serde_spanned, etc) have aggressive MSRV reqs for newer versions. (Another reason I didn't want them to be primary dependencies!)


[package.metadata.capi.header]
name = "rustls"
Expand All @@ -54,4 +55,4 @@ name = "rustls"
filename = "rustls"

[package.metadata.capi.install.include]
asset = [{from = "src/rustls.h", to = "" }]
asset = [{ from = "src/rustls.h", to = "" }]
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use std::io::Write;
use std::{env, fs, path::PathBuf};

// Keep in sync with Cargo.toml.
//
// We don't populate this automatically from the Cargo.toml at build time
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shower thought:

If the rust crate's build.rs did println!("cargo:version_number={}", env!("CARGO_PKG_VERSION")); then we could simply use env!("DEP_RUSTLS_VERSION_NUMBER") here.

Haven't tried that, and it's probably a worse overall solution than what we have in this PR, and also I should have learned my lesson by now rather than relying on underused parts of cargo.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo, I forgot that build.rs can forward information like that. Seems worth playing with but we'd need a rustls release with that magic in-place to be usable here so I think I'll leave it for a "One Day" follow-up.

// because doing so would require a heavy-weight deserialization lib dependency
// (and it couldn't be a _dev_ dep for use in a build script) or doing brittle
// by-hand parsing.
const RUSTLS_CRATE_VERSION: &str = "0.23.4";

fn main() {
Expand Down
211 changes: 101 additions & 110 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,109 +197,6 @@ pub(crate) fn log_callback_get() -> Result<(rustls_log_callback, *mut c_void), U
.unwrap_or(Err(UserdataError::AccessError))
}

#[cfg(test)]
mod tests {
use super::*;
use std::thread;

#[test]
fn guard_try_pop() {
let data = "hello";
let data_ptr = data as *const _ as _;
let mut guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
guard.try_pop().unwrap();
assert!(guard.try_pop().is_err())
}

#[test]
fn guard_try_drop() {
let data = "hello";
let data_ptr = data as *const _ as _;
let guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
guard.try_drop().unwrap();
assert!(userdata_get().is_err())
}

#[test]
fn guard_drop() {
let data = "hello";
let data_ptr = data as *const _ as _;
{
let _guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
}
assert!(userdata_get().is_err())
}

#[test]
fn nested_guards() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
{
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);
{
let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);
guard2.try_drop().unwrap();
}
assert_eq!(userdata_get().unwrap(), hello_ptr);
guard.try_drop().unwrap();
}
assert!(userdata_get().is_err())
}

#[test]
fn out_of_order_drop() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);

let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);

assert!(matches!(guard.try_drop(), Err(UserdataError::WrongData)));
assert!(matches!(guard2.try_drop(), Err(UserdataError::WrongData)));
}

#[test]
fn userdata_multi_threads() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);

let thread1 = thread::spawn(|| {
let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);

let greetz = "greetz";
let greetz_ptr = greetz as *const _ as _;

let guard3 = userdata_push(greetz_ptr, None).unwrap();

assert_eq!(userdata_get().unwrap(), greetz_ptr);
guard3.try_drop().unwrap();

assert_eq!(userdata_get().unwrap(), yo_ptr);
guard2.try_drop().unwrap();
});

assert_eq!(userdata_get().unwrap(), hello_ptr);
guard.try_drop().unwrap();
thread1.join().unwrap();
}
}

/// Used to mark that pointer to a [`Castable`]'s underlying `Castable::RustType` is provided
/// to C code as a pointer to a `Box<Castable::RustType>`.
pub(crate) struct OwnershipBox;
Expand Down Expand Up @@ -808,11 +705,105 @@ pub extern "C" fn rustls_version() -> rustls_str<'static> {
rustls_str::from_str_unchecked(RUSTLS_FFI_VERSION)
}

#[test]
fn test_rustls_version() {
// very rough check that the version number is being interpolated into the
// variable
assert!(RUSTLS_FFI_VERSION.contains("/0."));
let vsn = rustls_version();
assert!(vsn.len > 4)
#[cfg(test)]
mod tests {
use super::*;
use std::thread;

#[test]
fn guard_try_pop() {
let data = "hello";
let data_ptr = data as *const _ as _;
let mut guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
guard.try_pop().unwrap();
assert!(guard.try_pop().is_err())
}

#[test]
fn guard_try_drop() {
let data = "hello";
let data_ptr = data as *const _ as _;
let guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
guard.try_drop().unwrap();
assert!(userdata_get().is_err())
}

#[test]
fn guard_drop() {
let data = "hello";
let data_ptr = data as *const _ as _;
{
let _guard = userdata_push(data_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), data_ptr);
}
assert!(userdata_get().is_err())
}

#[test]
fn nested_guards() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
{
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);
{
let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);
guard2.try_drop().unwrap();
}
assert_eq!(userdata_get().unwrap(), hello_ptr);
guard.try_drop().unwrap();
}
assert!(userdata_get().is_err())
}

#[test]
fn out_of_order_drop() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);

let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);

assert!(matches!(guard.try_drop(), Err(UserdataError::WrongData)));
assert!(matches!(guard2.try_drop(), Err(UserdataError::WrongData)));
}

#[test]
fn userdata_multi_threads() {
let hello = "hello";
let hello_ptr = hello as *const _ as _;
let guard = userdata_push(hello_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), hello_ptr);

let thread1 = thread::spawn(|| {
let yo = "yo";
let yo_ptr = yo as *const _ as _;
let guard2 = userdata_push(yo_ptr, None).unwrap();
assert_eq!(userdata_get().unwrap(), yo_ptr);

let greetz = "greetz";
let greetz_ptr = greetz as *const _ as _;

let guard3 = userdata_push(greetz_ptr, None).unwrap();

assert_eq!(userdata_get().unwrap(), greetz_ptr);
guard3.try_drop().unwrap();

assert_eq!(userdata_get().unwrap(), yo_ptr);
guard2.try_drop().unwrap();
});

assert_eq!(userdata_get().unwrap(), hello_ptr);
guard.try_drop().unwrap();
thread1.join().unwrap();
}
}
Loading