Skip to content

Commit

Permalink
Merge pull request #49 from jymchng/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jymchng authored Apr 24, 2024
2 parents 1bee878 + d97c320 commit b20a2d5
Show file tree
Hide file tree
Showing 49 changed files with 1,021 additions and 54 deletions.
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
- id: cargo-doc-sanity
name: cargo-doc-sanity
description: cargo-doc-sanity
entry: bcargo doc --no-deps --all-features
entry: cargo doc --no-deps --all-features
language: system
types: [rust]
pass_filenames: false
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sosecrets-rs"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["Jim Chng <jimchng@outlook.com>"]
rust-version = "1.70"
Expand All @@ -22,11 +22,12 @@ exclude = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
num-traits = "0.2.18"
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"

[features]
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");
}
51 changes: 1 addition & 50 deletions tests/trybuild_tests_rt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[rustversion::not(stable(1.70.0))]
#[test]
fn test_compile_fails() {
let t = trybuild::TestCases::new();
// t.compile_fail("trybuild_tests/runtime/cannot_impl_minimally_representable_uints.rs");
t.compile_fail("trybuild_tests/runtime/cannot_cross_unwind_if_not_copy.rs");
t.compile_fail("trybuild_tests/runtime/cannot_return_exposed_secret.rs");
t.compile_fail("trybuild_tests/runtime/u0_cannot_call_expose_secret.rs");
Expand All @@ -19,53 +19,4 @@ fn test_compile_fails() {
feature = "alloc"
))]
t.pass("trybuild_tests/runtime/cannot_call_debug_clone_alloc_if_not_use.rs");
// t.compile_fail("trybuild_tests/test_compile_fail_two.rs");
// t.compile_fail("trybuild_tests/test_compile_fail_three.rs");

// #[cfg(all(feature = "cloneable-secret", not(feature = "alloc")))]
// t.compile_fail("trybuild_tests/test_compile_fail_four.rs");

// #[cfg(all(
// feature = "cloneable-secret",
// not(feature = "alloc"),
// not(feature = "zeroize")
// ))]
// t.compile_fail("trybuild_tests/test_compile_fail_five.rs");

// #[cfg(all(feature = "alloc", feature = "cloneable-secret"))]
// t.compile_fail("trybuild_tests/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/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/test_compile_fail_eight.rs");

// #[cfg(all(
// feature = "cloneable-secret",
// not(feature = "alloc"),
// feature = "zeroize"
// ))]
// t.compile_fail("trybuild_tests/test_compile_fail_nine.rs");

// #[cfg(all(
// feature = "cloneable-secret",
// feature = "alloc",
// not(feature = "zeroize")
// ))]
// t.compile_fail("trybuild_tests/test_compile_fail_ten.rs");

// t.compile_fail("trybuild_tests/test_cannot_return_exposed_secret.rs");

// // t.compile_fail("trybuild_tests/test_compile_fail_eleven.rs");

// #[cfg(feature = "cloneable-secret")]
// t.pass("trybuild_tests/test_compile_pass_one.rs");

// // no_std env + no alloc + no cloneable-secret should work
// t.pass("trybuild_tests/test_compile_pass_two.rs");

// t.pass("trybuild_tests/test_compile_pass_three.rs");
}
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)
Loading

0 comments on commit b20a2d5

Please sign in to comment.