Skip to content

Commit 301acb8

Browse files
hug-devmematthias
andcommitted
Derive common traits to struct
Co-authored-by: mematthias <[email protected]>
1 parent a855f7b commit 301acb8

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

cryptoki/src/mechanism/mechanism_info.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cryptoki_sys::*;
77
use std::fmt::{Debug, Formatter};
88

99
bitflags! {
10+
#[derive(Debug, Clone, Copy)]
1011
struct MechanismInfoFlags: CK_FLAGS {
1112
const HW = CKF_HW;
1213
const ENCRYPT = CKF_ENCRYPT;
@@ -305,12 +306,13 @@ mod test {
305306

306307
#[test]
307308
fn debug_flags_all() {
308-
let expected = "\
309-
HW | ENCRYPT | DECRYPT | DIGEST | SIGN | SIGN_RECOVER | VERIFY | \
310-
VERIFY_RECOVER | GENERATE | GENERATE_KEY_PAIR | WRAP | UNWRAP | DERIVE | \
311-
EXTENSION | EC_F_P | EC_F_2M | EC_ECPARAMETERS | EC_NAMEDCURVE | \
312-
EC_OID | EC_UNCOMPRESS | EC_COMPRESS | MESSAGE_ENCRYPT | MESSAGE_DECRYPT | \
313-
MULTI_MESSAGE | ENCAPSULATE | DECAPSULATE";
309+
let expected = "MechanismInfoFlags(
310+
HW | ENCRYPT | DECRYPT | DIGEST | SIGN | SIGN_RECOVER | VERIFY | \
311+
VERIFY_RECOVER | GENERATE | GENERATE_KEY_PAIR | WRAP | UNWRAP | DERIVE | \
312+
EXTENSION | EC_F_P | EC_F_2M | EC_ECPARAMETERS | EC_OID | EC_UNCOMPRESS | \
313+
EC_COMPRESS | MESSAGE_ENCRYPT | MESSAGE_DECRYPT | MULTI_MESSAGE | ENCAPSULATE | \
314+
DECAPSULATE,
315+
)";
314316
let all = MechanismInfoFlags::all();
315317
let observed = format!("{all:#?}");
316318
println!("{observed}");
@@ -327,7 +329,9 @@ MULTI_MESSAGE | ENCAPSULATE | DECAPSULATE";
327329
let expected = r#"MechanismInfo {
328330
min_key_size: 16,
329331
max_key_size: 4096,
330-
flags: (empty),
332+
flags: MechanismInfoFlags(
333+
0x0,
334+
),
331335
}"#;
332336
let observed = format!("{info:#?}");
333337
assert_eq!(observed, expected);

cryptoki/src/session/session_info.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt::Debug;
1111

1212
bitflags! {
1313
/// Collection of flags defined for [`CK_SESSION_INFO`]
14+
#[derive(Debug, Clone, Copy)]
1415
struct SessionInfoFlags: CK_FLAGS {
1516
const RW_SESSION = CKF_RW_SESSION;
1617
const SERIAL_SESSION = CKF_SERIAL_SESSION;
@@ -109,7 +110,9 @@ mod test {
109110

110111
#[test]
111112
fn debug_flags_all() {
112-
let expected = "RW_SESSION | SERIAL_SESSION";
113+
let expected = "SessionInfoFlags(
114+
RW_SESSION | SERIAL_SESSION,
115+
)";
113116
let all = SessionInfoFlags::all();
114117
let observed = format!("{all:#?}");
115118
assert_eq!(observed, expected);
@@ -128,7 +131,9 @@ mod test {
128131
slot_id: 100,
129132
},
130133
state: RoPublic,
131-
flags: (empty),
134+
flags: SessionInfoFlags(
135+
0x0,
136+
),
132137
device_error: 0,
133138
}"#;
134139
let observed = format!("{info:#?}");

cryptoki/src/slot/slot_info.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::fmt::Debug;
99

1010
bitflags! {
1111
/// Collection of flags defined for [`CK_SLOT_INFO`]
12+
#[derive(Debug, Clone, Copy)]
1213
struct SlotInfoFlags: CK_FLAGS {
1314
const TOKEN_PRESENT = CKF_TOKEN_PRESENT;
1415
const REMOVABLE_DEVICE = CKF_REMOVABLE_DEVICE;
@@ -98,7 +99,9 @@ mod test {
9899

99100
#[test]
100101
fn debug_flags_all() {
101-
let expected = "TOKEN_PRESENT | REMOVABLE_DEVICE | HW_SLOT";
102+
let expected = "SlotInfoFlags(
103+
TOKEN_PRESENT | REMOVABLE_DEVICE | HW_SLOT,
104+
)";
102105
let all = SlotInfoFlags::all();
103106
let observed = format!("{all:#?}");
104107
assert_eq!(observed, expected);
@@ -116,7 +119,9 @@ mod test {
116119
let expected = r#"SlotInfo {
117120
slot_description: "Slot Description",
118121
manufacturer_id: "Manufacturer ID",
119-
flags: (empty),
122+
flags: SlotInfoFlags(
123+
0x0,
124+
),
120125
hardware_version: Version {
121126
major: 0,
122127
minor: 255,

cryptoki/src/slot/token_info.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::fmt::Debug;
1313

1414
bitflags! {
1515
/// Collection of flags defined for [`CK_TOKEN_INFO`]
16+
#[derive(Debug, Clone, Copy)]
1617
struct TokenInfoFlags: CK_FLAGS {
1718
const RNG = CKF_RNG;
1819
const WRITE_PROTECTED = CKF_WRITE_PROTECTED;
@@ -463,13 +464,14 @@ mod test {
463464

464465
#[test]
465466
fn debug_flags_all() {
466-
let expected = "\
467-
RNG | WRITE_PROTECTED | LOGIN_REQUIRED | USER_PIN_INITIALIZED | \
468-
RESTORE_KEY_NOT_NEEDED | CLOCK_ON_TOKEN | PROTECTED_AUTHENTICATION_PATH | \
469-
DUAL_CRYPTO_OPERATIONS | TOKEN_INITIALIZED | SECONDARY_AUTHENTICATION | \
470-
USER_PIN_COUNT_LOW | USER_PIN_FINAL_TRY | USER_PIN_LOCKED | \
471-
USER_PIN_TO_BE_CHANGED | SO_PIN_COUNT_LOW | SO_PIN_FINAL_TRY | SO_PIN_LOCKED | \
472-
SO_PIN_TO_BE_CHANGED | ERROR_STATE";
467+
let expected = "TokenInfoFlags(
468+
RNG | WRITE_PROTECTED | LOGIN_REQUIRED | USER_PIN_INITIALIZED | \
469+
RESTORE_KEY_NOT_NEEDED | CLOCK_ON_TOKEN | PROTECTED_AUTHENTICATION_PATH | \
470+
DUAL_CRYPTO_OPERATIONS | TOKEN_INITIALIZED | SECONDARY_AUTHENTICATION | \
471+
USER_PIN_COUNT_LOW | USER_PIN_FINAL_TRY | USER_PIN_LOCKED | \
472+
USER_PIN_TO_BE_CHANGED | SO_PIN_COUNT_LOW | SO_PIN_FINAL_TRY | SO_PIN_LOCKED | \
473+
SO_PIN_TO_BE_CHANGED | ERROR_STATE,
474+
)";
473475
let all = TokenInfoFlags::all();
474476
let observed = format!("{all:#?}");
475477
assert_eq!(observed, expected);
@@ -509,7 +511,9 @@ SO_PIN_TO_BE_CHANGED | ERROR_STATE";
509511
manufacturer_id: "Manufacturer ID",
510512
model: "Token Model",
511513
serial_number: "Serial Number",
512-
flags: (empty),
514+
flags: TokenInfoFlags(
515+
0x0,
516+
),
513517
max_session_count: Max(
514518
100,
515519
),

cryptoki/tests/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ fn get_session_info_test() -> TestResult {
15791579
let session_info = session.get_session_info()?;
15801580
assert!(session_info.read_write());
15811581
assert_eq!(session_info.slot_id(), slot);
1582-
assert!(matches!(session_info.session_state(), SessionState::RwUser,));
1582+
assert!(matches!(session_info.session_state(), SessionState::RwUser));
15831583
session.logout()?;
15841584
session.login(UserType::So, Some(&AuthPin::new(SO_PIN.into())))?;
15851585
let session_info = session.get_session_info()?;

0 commit comments

Comments
 (0)