@@ -7,7 +7,7 @@ use cryptoki::slot::Slot;
77use cryptoki:: types:: AuthPin ;
88use std:: env;
99use std:: fs;
10- use std:: path:: { Path , PathBuf } ;
10+ use std:: path:: PathBuf ;
1111
1212fn _setup_common ( ) -> ( Pkcs11 , Slot ) {
1313 let module = env:: var ( "TEST_PKCS11_MODULE" ) . unwrap_or_else ( |_| {
@@ -72,40 +72,57 @@ fn setup_test_dir(name: &str) -> PathBuf {
7272 testdir
7373}
7474
75- pub fn setup_token ( name : & str ) -> ( Pkcs11 , Slot ) {
76- let testdir = setup_test_dir ( name) ;
77- let confname = testdir. join ( format ! ( "{}.sql" , name) ) ;
78-
79- // Set KRYOPTIC_CONF for the C library. Using `std::env::set_var` is not thread-safe.
80- unsafe {
81- env:: set_var ( "KRYOPTIC_CONF" , confname) ;
82- }
83-
84- _setup_common ( )
85- }
86-
87- pub fn setup ( name : & str , common_config_lines : & [ & str ] ) -> ( Pkcs11 , Slot ) {
75+ pub fn setup_token ( name : & str , common_config_lines : & [ & str ] ) -> ( Pkcs11 , Slot ) {
8876 let testdir = setup_test_dir ( name) ;
8977 let confname = testdir. join ( format ! ( "{}.conf" , name) ) ;
90- let sql_path = testdir. join ( format ! ( "{}.sql" , name) ) ;
9178
9279 let mut config_content = String :: new ( ) ;
80+ let mut dbtype = "sqlite" . to_string ( ) ;
9381
94- // Add common section lines
82+ let mut final_config_lines = Vec :: new ( ) ;
9583 for line in common_config_lines {
84+ let line_str = * line;
85+ if line_str. starts_with ( "dbtype=" ) {
86+ dbtype = line_str. splitn ( 2 , '=' ) . nth ( 1 ) . unwrap ( ) . trim ( ) . to_string ( ) ;
87+ } else {
88+ final_config_lines. push ( line_str) ;
89+ }
90+ }
91+
92+ match dbtype. as_str ( ) {
93+ "sqlite" | "nssdb" | "memory" => { }
94+ _ => panic ! ( "Unsupported dbtype: {}" , dbtype) ,
95+ }
96+
97+ // Add common section lines
98+ for line in final_config_lines {
9699 config_content. push_str ( line) ;
97100 config_content. push ( '\n' ) ;
98101 }
99102
100- // Add slot configuration pointing to the sqlite db
103+ // Add slot configuration
104+ let dbargs = match dbtype. as_str ( ) {
105+ "sqlite" => {
106+ let sql_path = testdir. join ( format ! ( "{}.sql" , name) ) ;
107+ sql_path. to_str ( ) . unwrap ( ) . replace ( '\\' , "\\ \\ " )
108+ }
109+ "nssdb" => format ! (
110+ "configDir={}" ,
111+ testdir. to_str( ) . unwrap( ) . replace( '\\' , "\\ \\ " )
112+ ) ,
113+ "memory" => "flags=encrypt" . to_string ( ) ,
114+ _ => unreachable ! ( ) ,
115+ } ;
116+
101117 let slot_config = format ! (
102118 r#"
103119[[slots]]
104120 slot = 0
105- dbtype = "sqlite "
121+ dbtype = "{} "
106122 dbargs = "{}"
107123"# ,
108- sql_path. to_str( ) . unwrap( ) . replace( '\\' , "\\ \\ " )
124+ dbtype,
125+ dbargs. replace( '\\' , "\\ \\ " )
109126 ) ;
110127 config_content. push_str ( & slot_config) ;
111128
@@ -119,6 +136,7 @@ pub fn setup(name: &str, common_config_lines: &[&str]) -> (Pkcs11, Slot) {
119136 _setup_common ( )
120137}
121138
139+ #[ allow( dead_code) ]
122140pub fn modify_setup (
123141 name : & str ,
124142 common_config_lines : & [ & str ] ,
@@ -130,25 +148,53 @@ pub fn modify_setup(
130148 )
131149 . join ( name) ;
132150 let confname = testdir. join ( format ! ( "{}.conf" , name) ) ;
133- let sql_path = testdir. join ( format ! ( "{}.sql" , name) ) ;
134151
135152 let mut config_content = String :: new ( ) ;
153+ let mut dbtype = "sqlite" . to_string ( ) ;
136154
137- // Add common section lines
155+ let mut final_config_lines = Vec :: new ( ) ;
138156 for line in common_config_lines {
157+ let line_str = * line;
158+ if line_str. starts_with ( "dbtype=" ) {
159+ dbtype = line_str. splitn ( 2 , '=' ) . nth ( 1 ) . unwrap ( ) . trim ( ) . to_string ( ) ;
160+ } else {
161+ final_config_lines. push ( line_str) ;
162+ }
163+ }
164+
165+ match dbtype. as_str ( ) {
166+ "sqlite" | "nssdb" | "memory" => { }
167+ _ => panic ! ( "Unsupported dbtype: {}" , dbtype) ,
168+ }
169+
170+ // Add common section lines
171+ for line in final_config_lines {
139172 config_content. push_str ( line) ;
140173 config_content. push ( '\n' ) ;
141174 }
142175
143- // Add slot configuration pointing to the sqlite db
176+ // Add slot configuration
177+ let dbargs = match dbtype. as_str ( ) {
178+ "sqlite" => {
179+ let sql_path = testdir. join ( format ! ( "{}.sql" , name) ) ;
180+ sql_path. to_str ( ) . unwrap ( ) . replace ( '\\' , "\\ \\ " )
181+ }
182+ "nssdb" => format ! (
183+ "configDir={}" ,
184+ testdir. to_str( ) . unwrap( ) . replace( '\\' , "\\ \\ " )
185+ ) ,
186+ "memory" => "flags=encrypt" . to_string ( ) ,
187+ _ => unreachable ! ( ) ,
188+ } ;
189+
144190 let slot_config = format ! (
145191 r#"
146192[[slots]]
147193 slot = 0
148- dbtype = "sqlite "
194+ dbtype = "{} "
149195 dbargs = "{}"
150196"# ,
151- sql_path . to_str ( ) . unwrap ( ) . replace ( '\\' , " \\ \\ " )
197+ dbtype , dbargs
152198 ) ;
153199 config_content. push_str ( & slot_config) ;
154200
0 commit comments