Skip to content

Commit

Permalink
update to see if testing on 1.70 works or not
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Apr 24, 2024
1 parent ee8c7ec commit e71c329
Show file tree
Hide file tree
Showing 48 changed files with 1,018 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- dev

jobs:
msrv_solo:
Expand All @@ -16,7 +17,7 @@ jobs:
uses: dtolnay/rust-toolchain@1.70

- name: Check tests
run: cargo test --all-features --no-run && cargo test
run: bash scripts/tests-all-features-1.70.sh

tests:
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 25 April 2024

1. Added directory `1_70` under `trybuild_tests` directory to ensure the tests pass and make sense for Minimum Supported Rust Version 1.70.
2. Wrote a build script `scripts/build-ver-tests.rs.rs` to copy the existing tests into the `1_70` directory.
3. Made sure that this build script is only ran during testing phase by trigger it using `cargo eval scripts/build-ver-tests.rs`.
4. GitHub Actions updated to run `scripts/tests-all-features-1.70.sh`, which is a bash script that wraps `scripts/tests-all-features.sh` to do testing on Rust version 1.70.

## 04 April 2024
<center><h1>V0.2.1 IS RELEASED</h1></center>

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typenum = "1.17.0"
zeroize = { version = "1.6.0", optional = true}

[dev-dependencies]
fs_extra = "1.3.0"
rustversion = "1.0.15"
trybuild = "1.0.85"

Expand Down
80 changes: 80 additions & 0 deletions scripts/build-ver-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// build.rs
use std::cfg;

fn main() {
build::main();
}

mod build {
use std::fs;
use std::io::{self, BufRead, Write};
use std::path::Path;

pub fn main() {
write_test_files_for_version_1_70("tests/trybuild_tests_rt.rs");
write_test_files_for_version_1_70("tests/trybuild_tests.rs");
let original_runtime_path = "trybuild_tests/runtime";
let new_runtime_path = "trybuild_tests/1_70/runtime";
fs::create_dir_all(new_runtime_path).expect("Unable to create directory");

let original_tests_path = "trybuild_tests";
let new_tests_path = "trybuild_tests/1_70";
fs::create_dir_all(new_tests_path).expect("Unable to create directory");

// Copy all .rs files from original_tests_path to new_tests_path
for (old_test_dir, new_test_dir) in [original_runtime_path, original_tests_path]
.iter()
.zip([new_runtime_path, new_tests_path])
{
for entry in fs::read_dir(old_test_dir).expect("Unable to read directory") {
let entry = entry.expect("Unable to get entry");
let path = entry.path();
if let Some(extension) = path.extension() {
if extension == "rs" {
let new_path = Path::new(new_test_dir)
.join(path.file_name().expect("Unable to get file name"));
fs::copy(&path, new_path).expect("Unable to copy file");
}
}
}
}
}

fn generate_new_file_path(original_file_path: &str, suffix: &str) -> String {
let file_name = Path::new(original_file_path)
.file_stem()
.unwrap()
.to_str()
.unwrap();
let new_file_name = format!("{}_{}", file_name, suffix);
let new_file_path = Path::new(original_file_path)
.with_file_name(new_file_name)
.with_extension("rs");
new_file_path.to_str().unwrap().to_string()
}

fn write_test_files_for_version_1_70(original_tests_dir_file_path: &str) {
let suffix = "1_70";
let new_file_path = generate_new_file_path(original_tests_dir_file_path, suffix);

let file = fs::File::open(original_tests_dir_file_path).expect("Unable to open file");
let new_file = fs::File::create(new_file_path).expect("Unable to create file");
let reader = io::BufReader::new(file);
let mut writer = io::BufWriter::new(new_file);

let first_line = "#[rustversion::stable(1.70.0)]";
writeln!(writer, "{}", first_line).expect("Unable to write line");

for line in reader.lines() {
let line = line.expect("Unable to read line");
if line == "#[rustversion::not(stable(1.70.0))]".to_owned() || line.is_empty() {
continue;
}
let modified_line = line.replace(
"trybuild_tests",
format!("trybuild_tests/{suffix}").as_str(),
);
writeln!(writer, "{}", modified_line).expect("Unable to write line");
}
}
}
28 changes: 28 additions & 0 deletions scripts/tests-all-features-1.70.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

cargo eval --help &> /dev/null

if [ $? -ne 0 ]; then
echo "cargo eval is NOT installed, installing it now"
cargo install --force cargo-eval
echo "installed cargo eval"
else
echo "cargo eval is installed"
fi

rust_version=$(rustc --version | cut -d ' ' -f 2)

if [ "$rust_version" != "1.70.0" ]; then
echo "Rust version is not 1.70.0, now setting it to 1.70.0; the previous version is $rust_version"
rustup override set 1.70
fi

bash scripts/tests-all-features.sh

rustup override unset

echo "Rustup toolchain resetted to default; COMMAND RAN: rustup override unset"

echo "========== RUST TOOLCHAIN =========="
rustup show
echo "===================================="
4 changes: 4 additions & 0 deletions scripts/tests-all-features.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

echo "========== RUST TOOLCHAIN =========="
rustup show
echo "===================================="

# Array of feature names
features=("cloneable-secret", "alloc", "zeroize", "debug-secret")

Expand Down
1 change: 1 addition & 0 deletions tests/trybuild_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[rustversion::not(stable(1.70.0))]
#[test]
fn test_compile_fails() {
let t = trybuild::TestCases::new();
Expand Down
46 changes: 46 additions & 0 deletions tests/trybuild_tests_1_70.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#[rustversion::stable(1.70.0)]
#[test]
fn test_compile_fails() {
let t = trybuild::TestCases::new();
t.compile_fail("trybuild_tests/1_70/test_compile_fail_one.rs");
t.compile_fail("trybuild_tests/1_70/test_compile_fail_two.rs");
t.compile_fail("trybuild_tests/1_70/test_compile_fail_three.rs");
#[cfg(all(feature = "cloneable-secret", not(feature = "alloc")))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_four.rs");
#[cfg(all(
feature = "cloneable-secret",
not(feature = "alloc"),
not(feature = "zeroize")
))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_five.rs");
#[cfg(all(feature = "alloc", feature = "cloneable-secret"))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_six.rs");
// std env + alloc + no clone, no clone should error
#[cfg(all(feature = "alloc", not(feature = "cloneable-secret")))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_seven.rs");
// no_std env + alloc + extern crate alloc::vec::Vec in main()
#[cfg(all(feature = "alloc", not(feature = "cloneable-secret")))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_eight.rs");
#[cfg(all(
feature = "cloneable-secret",
not(feature = "alloc"),
feature = "zeroize"
))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_nine.rs");
#[cfg(all(
feature = "cloneable-secret",
feature = "alloc",
not(feature = "zeroize")
))]
t.compile_fail("trybuild_tests/1_70/test_compile_fail_ten.rs");
t.compile_fail("trybuild_tests/1_70/test_cannot_return_exposed_secret.rs");
t.compile_fail("trybuild_tests/1_70/test_panic_cannot_return_exposed.rs");
#[cfg(not(feature = "zeroize"))]
t.compile_fail("trybuild_tests/1_70/test_ref_cannot_leak_secret.rs");
// t.compile_fail("trybuild_tests/1_70/test_compile_fail_eleven.rs");
#[cfg(feature = "cloneable-secret")]
t.pass("trybuild_tests/1_70/test_compile_pass_one.rs");
// no_std env + no alloc + no cloneable-secret should work
t.pass("trybuild_tests/1_70/test_compile_pass_two.rs");
t.pass("trybuild_tests/1_70/test_compile_pass_three.rs");
}
1 change: 1 addition & 0 deletions tests/trybuild_tests_rt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[rustversion::not(stable(1.70.0))]
#[test]
fn test_compile_fails() {
let t = trybuild::TestCases::new();
Expand Down
20 changes: 20 additions & 0 deletions tests/trybuild_tests_rt_1_70.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[rustversion::stable(1.70.0)]
#[test]
fn test_compile_fails() {
let t = trybuild::TestCases::new();
t.compile_fail("trybuild_tests/1_70/runtime/cannot_cross_unwind_if_not_copy.rs");
t.compile_fail("trybuild_tests/1_70/runtime/cannot_return_exposed_secret.rs");
t.compile_fail("trybuild_tests/1_70/runtime/u0_cannot_call_expose_secret.rs");
#[cfg(all(
not(feature = "debug-secret"),
not(feature = "cloneable-secret"),
not(feature = "alloc")
))]
t.compile_fail("trybuild_tests/1_70/runtime/cannot_call_debug_clone_alloc_if_not_use.rs");
#[cfg(all(
feature = "debug-secret",
feature = "cloneable-secret",
feature = "alloc"
))]
t.pass("trybuild_tests/1_70/runtime/cannot_call_debug_clone_alloc_if_not_use.rs");
}
9 changes: 9 additions & 0 deletions trybuild_tests/1_70/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[derive(Debug)]
pub struct UseSecret<T> {
pub inner: T,
}
impl<T> UseSecret<T> {
pub fn new(value: T) -> Self {
Self { inner: value }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn main() {
#[cfg(all(
feature = "debug-secret",
feature = "cloneable-secret",
feature = "alloc"
))]
use sosecrets_rs::runtime::traits::RTExposeSecret;
use sosecrets_rs::{prelude::typenum::U5, runtime::secret::RTSecret};

let vec_one = vec![1, 2, 3];

let secret = RTSecret::<Vec<i32>, U5>::new(vec_one);

let cloned_secret = secret.clone();
let debug_secret = format!("{:?}", secret);

assert_eq!("RTSecret<[REDACTED]>", debug_secret);

let _ = cloned_secret.expose_secret(|exposed_secret| {
assert_eq!(*exposed_secret, vec![1, 2, 3]);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0599]: no method named `clone` found for struct `RTSecret` in the current scope
--> trybuild_tests/1_70/runtime/cannot_call_debug_clone_alloc_if_not_use.rs:14:32
|
14 | let cloned_secret = secret.clone();
| ^^^^^ method not found in `RTSecret<Vec<i32>, UInt<UInt<UInt<UTerm, B1>, B0>, B1>>`

error[E0277]: `RTSecret<Vec<i32>, UInt<UInt<UInt<UTerm, B1>, B0>, B1>>` doesn't implement `Debug`
--> trybuild_tests/1_70/runtime/cannot_call_debug_clone_alloc_if_not_use.rs:15:40
|
15 | let debug_secret = format!("{:?}", secret);
| ^^^^^^ `RTSecret<Vec<i32>, UInt<UInt<UInt<UTerm, B1>, B0>, B1>>` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= help: the trait `Debug` is not implemented for `RTSecret<Vec<i32>, UInt<UInt<UInt<UTerm, B1>, B0>, B1>>`
= note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
36 changes: 36 additions & 0 deletions trybuild_tests/1_70/runtime/cannot_cross_unwind_if_not_copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
fn main() {
use core::panic::AssertUnwindSafe;

extern crate std;
use sosecrets_rs::{
prelude::typenum::U2,
runtime::{secret::RTSecret, traits::RTExposeSecret},
};
use std::panic::catch_unwind;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
fn zeroize(&mut self) {
self.inner.zeroize()
}
}

let mut opt_a: Option<A> = Option::<A>::None;

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });

let _ = catch_unwind(AssertUnwindSafe(|| {
secret_one.expose_secret(|exposed_secret| {
opt_a.replace(*exposed_secret);
panic!();
});
}));
assert_eq!(opt_a.unwrap().inner, 69);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error[E0507]: cannot move out of dereference of `RTExposedSecret<'_, &A>`
--> trybuild_tests/1_70/runtime/cannot_cross_unwind_if_not_copy.rs:31:27
|
31 | opt_a.replace(*exposed_secret);
| ^^^^^^^^^^^^^^^ move occurs because value has type `A`, which does not implement the `Copy` trait
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// use sosecrets_rs::{
// prelude::typenum::{Unsigned, U69, U8},
// runtime::traits::{MinimallyRepresentableUInt, __private},
// };

// fn main() {
// #[derive(Copy, Default, Clone)]
// struct A;

// // cannot impl Unsigned
// impl Unsigned for A {}

// // cannot impl MinimallyRepresentableUInt
// impl MinimallyRepresentableUInt for A {}

// // cannot call the method `cast_unsigned_to_self_type`
// let a = <U8 as MinimallyRepresentableUInt>::cast_unsigned_to_self_type::<U69>(
// __private::SealedToken {},
// );
// }
26 changes: 26 additions & 0 deletions trybuild_tests/1_70/runtime/cannot_return_exposed_secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main() {
use sosecrets_rs::{
prelude::typenum::U2,
runtime::{secret::RTSecret, traits::RTExposeSecret},
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

struct A {
inner: i32,
}

#[cfg(feature = "zeroize")]
impl Zeroize for A {
fn zeroize(&mut self) {
self.inner.zeroize()
}
}

let secret_one = RTSecret::<A, U2>::new(A { inner: 69 });

let _ = secret_one.expose_secret(|exposed_secret| exposed_secret);

let _ = secret_one.expose_secret(|exposed_secret| *exposed_secret);
}
18 changes: 18 additions & 0 deletions trybuild_tests/1_70/runtime/cannot_return_exposed_secret.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: lifetime may not live long enough
--> trybuild_tests/1_70/runtime/cannot_return_exposed_secret.rs:23:55
|
23 | let _ = secret_one.expose_secret(|exposed_secret| exposed_secret);
| --------------- ^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
| | |
| | return type of closure is RTExposedSecret<'2, &A>
| has type `RTExposedSecret<'1, &'1 A>`
|
= note: requirement occurs because of the type `RTExposedSecret<'_, &A>`, which makes the generic argument `'_` invariant
= note: the struct `RTExposedSecret<'brand, T>` is invariant over the parameter `'brand`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error[E0507]: cannot move out of dereference of `RTExposedSecret<'_, &A>`
--> trybuild_tests/1_70/runtime/cannot_return_exposed_secret.rs:25:55
|
25 | let _ = secret_one.expose_secret(|exposed_secret| *exposed_secret);
| ^^^^^^^^^^^^^^^ move occurs because value has type `A`, which does not implement the `Copy` trait
Loading

0 comments on commit e71c329

Please sign in to comment.