Skip to content

Commit 1914f61

Browse files
authored
ci: run examples on SoftHSM and added README.md for guidance on new examples
- run examples on SoftHSM during CI builds - added README for guidance on how to write new examples
1 parent 6fdde48 commit 1914f61

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ jobs:
107107
- target: aarch64-unknown-linux-gnu
108108
runner: ubuntu-24.04-arm
109109
runs-on: ${{ matrix.runner }}
110+
env:
111+
TEST_PKCS11_MODULE: /usr/lib/softhsm/libsofthsm2.so
112+
SOFTHSM2_CONF: /tmp/softhsm2.conf
113+
RUSTFLAGS: "-D warnings"
114+
RUST_BACKTRACE: 1
110115
steps:
111116
- uses: actions/checkout@v4
112117
- name: Setup Rust toolchain
@@ -134,12 +139,18 @@ jobs:
134139
- name: Check
135140
run: cargo check --target ${{ matrix.target }} --workspace --all-targets
136141
- name: Test script
137-
env:
138-
TEST_PKCS11_MODULE: /usr/lib/softhsm/libsofthsm2.so
139-
SOFTHSM2_CONF: /tmp/softhsm2.conf
140-
RUSTFLAGS: "-D warnings"
141-
RUST_BACKTRACE: 1
142142
run: cargo test --target ${{ matrix.target }}
143+
# "Run examples" assumes that:
144+
# - there is one example per file.
145+
# - the run does not take any arguments.
146+
# - the run is relatively fast.
147+
# - the return code is non-zero if the example fails.
148+
- name: Run examples
149+
run: |
150+
EXAMPLES=$(ls cryptoki/examples/*.rs | sed 's/\.rs$//' | xargs -n 1 basename)
151+
for example in $EXAMPLES; do
152+
cargo run --target ${{ matrix.target }} --example "$example"
153+
done
143154
144155
build-windows:
145156
name: Build on Windows

cryptoki/examples/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Writing new examples
2+
3+
Examples are run during CI, so new examples must follow these rules:
4+
5+
- Use the same test credentials as other examples and tests.
6+
- Keep each example to a single file (1 example = 1 file).
7+
- Do not expect command-line arguments.
8+
- Do not require environment variables other than `TEST_PKCS11_MODULE`.
9+
- Keep runtime relatively fast; verbose output is fine.
10+
- Ensure it runs on the MSRV.
11+
- Exit with status 0 on normal execution; any non-zero status is treated as an error.
12+
- Use `testresult::TestResult` as the return type of `main` for easier error handling.
13+
14+
In addition, examples should be extensively documented and designed to be educative.
15+
16+
Suggested best practices:
17+
18+
- Reference the same SoftHSM setup used by CI; avoid introducing new credentials.
19+
- Clean up any tokens, keys, or objects created by the example. When possible, use session (i.e. non-persistent) objects.
20+
- Ensure the example works with SoftHSM2.
21+

cryptoki/examples/generate_key_pair.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use cryptoki::types::AuthPin;
88
use std::env;
99

1010
// The default user pin
11-
pub static USER_PIN: &str = "fedcba";
11+
pub static USER_PIN: &str = "fedcba123456";
1212
// The default SO pin
13-
pub static SO_PIN: &str = "abcdef";
13+
pub static SO_PIN: &str = "abcdef654321";
1414

1515
fn main() -> testresult::TestResult {
1616
// initialize a new Pkcs11 object using the module from the env variable
@@ -24,10 +24,10 @@ fn main() -> testresult::TestResult {
2424
let slot = pkcs11.get_slots_with_token()?[0];
2525

2626
// initialize a test token
27-
let so_pin = AuthPin::new("abcdef".into());
27+
let so_pin = AuthPin::new(SO_PIN.into());
2828
pkcs11.init_token(slot, &so_pin, "Test Token")?;
2929

30-
let user_pin = AuthPin::new("fedcba".into());
30+
let user_pin = AuthPin::new(USER_PIN.into());
3131

3232
// initialize user PIN
3333
{

cryptoki/examples/thread_local_session.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ use cryptoki::object::Attribute;
4747
use cryptoki::session::{Session, UserType};
4848
use cryptoki::types::AuthPin;
4949

50-
const USER_PIN: &str = "fedcba";
51-
const SO_PIN: &str = "abcdef";
50+
// The default user pin
51+
pub static USER_PIN: &str = "fedcba123456";
52+
// The default SO pin
53+
pub static SO_PIN: &str = "abcdef654321";
5254

5355
// Global PKCS11 context shared across all threads using Arc for cheap cloning
5456
static PKCS11_CTX: OnceLock<Pkcs11> = OnceLock::new();

0 commit comments

Comments
 (0)