uselesskey 0.9.1

Deterministic cryptographic key and certificate fixtures for Rust tests. Seed-stable, cached, scanner-safe.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Integration tests for tempfile/sink functionality across all key types.
//!
//! Tests every `write_*` method on RSA, ECDSA, Ed25519, HMAC, Token, X.509
//! key fixtures, verifying content round-trips and cleanup-on-drop.

mod testutil;

use std::collections::HashSet;
use std::path::PathBuf;

use uselesskey::prelude::*;

fn fx() -> Factory {
    testutil::fx()
}

// ===========================================================================
// RSA tempfile writes
// ===========================================================================

#[test]
#[cfg(feature = "rsa")]
fn rsa_write_private_key_pkcs8_pem_round_trip() {
    let kp = fx().rsa("sink-rsa-priv", RsaSpec::rs256());
    let temp = kp.write_private_key_pkcs8_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.private_key_pkcs8_pem());
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));
    assert!(content.contains("-----END PRIVATE KEY-----"));

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".pkcs8.pem"), "filename: {filename}");
}

#[test]
#[cfg(feature = "rsa")]
fn rsa_write_public_key_spki_pem_round_trip() {
    let kp = fx().rsa("sink-rsa-pub", RsaSpec::rs256());
    let temp = kp.write_public_key_spki_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.public_key_spki_pem());
    assert!(content.contains("-----BEGIN PUBLIC KEY-----"));

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".spki.pem"), "filename: {filename}");
}

#[test]
#[cfg(feature = "rsa")]
fn rsa_tempfile_paths_unique_across_invocations() {
    let kp = fx().rsa("sink-rsa-uniq", RsaSpec::rs256());
    let a = kp.write_private_key_pkcs8_pem().unwrap();
    let b = kp.write_private_key_pkcs8_pem().unwrap();
    assert_ne!(a.path(), b.path(), "each call produces a new tempfile");
}

#[test]
#[cfg(feature = "rsa")]
fn rsa_private_and_public_tempfiles_coexist() {
    let kp = fx().rsa("sink-rsa-both", RsaSpec::rs256());
    let priv_temp = kp.write_private_key_pkcs8_pem().unwrap();
    let pub_temp = kp.write_public_key_spki_pem().unwrap();

    assert_ne!(priv_temp.path(), pub_temp.path());
    assert!(priv_temp.path().exists());
    assert!(pub_temp.path().exists());
    assert!(priv_temp.read_to_string().unwrap().contains("PRIVATE KEY"));
    assert!(pub_temp.read_to_string().unwrap().contains("PUBLIC KEY"));
}

// ===========================================================================
// ECDSA tempfile writes
// ===========================================================================

#[test]
#[cfg(feature = "ecdsa")]
fn ecdsa_write_private_key_pkcs8_pem_round_trip() {
    let kp = fx().ecdsa("sink-ec-priv", EcdsaSpec::es256());
    let temp = kp.write_private_key_pkcs8_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.private_key_pkcs8_pem());
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));
}

#[test]
#[cfg(feature = "ecdsa")]
fn ecdsa_write_public_key_spki_pem_round_trip() {
    let kp = fx().ecdsa("sink-ec-pub", EcdsaSpec::es256());
    let temp = kp.write_public_key_spki_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.public_key_spki_pem());
    assert!(content.contains("-----BEGIN PUBLIC KEY-----"));
}

#[test]
#[cfg(feature = "ecdsa")]
fn ecdsa_es384_tempfile_round_trip() {
    let kp = fx().ecdsa("sink-ec384", EcdsaSpec::es384());
    let temp = kp.write_private_key_pkcs8_pem().unwrap();
    assert_eq!(temp.read_to_string().unwrap(), kp.private_key_pkcs8_pem());
}

// ===========================================================================
// Ed25519 tempfile writes
// ===========================================================================

#[test]
#[cfg(feature = "ed25519")]
fn ed25519_write_private_key_pkcs8_pem_round_trip() {
    let kp = fx().ed25519("sink-ed-priv", Ed25519Spec::new());
    let temp = kp.write_private_key_pkcs8_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.private_key_pkcs8_pem());
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));
}

#[test]
#[cfg(feature = "ed25519")]
fn ed25519_write_public_key_spki_pem_round_trip() {
    let kp = fx().ed25519("sink-ed-pub", Ed25519Spec::new());
    let temp = kp.write_public_key_spki_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, kp.public_key_spki_pem());
    assert!(content.contains("-----BEGIN PUBLIC KEY-----"));
}

// ===========================================================================
// X.509 tempfile writes (certificates for TLS)
// ===========================================================================

#[test]
#[cfg(feature = "x509")]
fn x509_write_cert_pem_round_trip() {
    let cert = fx().x509_self_signed("sink-x509-cert", X509Spec::self_signed("sink.example.com"));
    let temp = cert.write_cert_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, cert.cert_pem());
    assert!(content.contains("-----BEGIN CERTIFICATE-----"));

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".crt.pem"), "filename: {filename}");
}

#[test]
#[cfg(feature = "x509")]
fn x509_write_cert_der_round_trip() {
    let cert = fx().x509_self_signed("sink-x509-der", X509Spec::self_signed("sink.example.com"));
    let temp = cert.write_cert_der().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_bytes().unwrap();
    assert_eq!(content, cert.cert_der());
    assert_eq!(content[0], 0x30, "DER starts with SEQUENCE tag");

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".crt.der"), "filename: {filename}");
}

#[test]
#[cfg(feature = "x509")]
fn x509_write_private_key_pem_round_trip() {
    let cert = fx().x509_self_signed("sink-x509-key", X509Spec::self_signed("sink.example.com"));
    let temp = cert.write_private_key_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, cert.private_key_pkcs8_pem());
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".key.pem"), "filename: {filename}");
}

#[test]
#[cfg(feature = "x509")]
fn x509_write_identity_pem_contains_cert_and_key() {
    let cert = fx().x509_self_signed("sink-x509-id", X509Spec::self_signed("sink.example.com"));
    let temp = cert.write_identity_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert!(content.contains("-----BEGIN CERTIFICATE-----"));
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));

    let filename = temp.path().file_name().unwrap().to_string_lossy();
    assert!(filename.ends_with(".identity.pem"), "filename: {filename}");
}

#[test]
#[cfg(feature = "x509")]
fn x509_all_tempfile_paths_are_unique() {
    let cert = fx().x509_self_signed(
        "sink-x509-unique",
        X509Spec::self_signed("sink.example.com"),
    );
    let paths: HashSet<PathBuf> = [
        cert.write_cert_pem().unwrap().path().to_path_buf(),
        cert.write_cert_der().unwrap().path().to_path_buf(),
        cert.write_private_key_pem().unwrap().path().to_path_buf(),
        cert.write_identity_pem().unwrap().path().to_path_buf(),
    ]
    .into_iter()
    .collect();

    assert_eq!(paths.len(), 4, "each tempfile must have a unique path");
}

// ===========================================================================
// X.509 chain tempfile writes
// ===========================================================================

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_leaf_cert_pem() {
    let chain = fx().x509_chain("sink-chain", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_leaf_cert_pem().unwrap();

    assert!(temp.path().exists());
    let content = temp.read_to_string().unwrap();
    assert_eq!(content, chain.leaf_cert_pem());
    assert!(content.contains("-----BEGIN CERTIFICATE-----"));
}

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_leaf_cert_der() {
    let chain = fx().x509_chain("sink-chain-der", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_leaf_cert_der().unwrap();

    assert!(temp.path().exists());
    assert_eq!(temp.read_to_bytes().unwrap(), chain.leaf_cert_der());
}

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_chain_pem() {
    let chain = fx().x509_chain("sink-chain-chain", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_chain_pem().unwrap();

    let content = temp.read_to_string().unwrap();
    // Chain PEM should contain multiple certificates
    let cert_count = content.matches("-----BEGIN CERTIFICATE-----").count();
    assert!(
        cert_count >= 2,
        "chain PEM should have at least 2 certs, got {cert_count}"
    );
}

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_full_chain_pem() {
    let chain = fx().x509_chain("sink-chain-full", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_full_chain_pem().unwrap();

    let content = temp.read_to_string().unwrap();
    let cert_count = content.matches("-----BEGIN CERTIFICATE-----").count();
    assert!(
        cert_count >= 3,
        "full chain PEM should have at least 3 certs (leaf+intermediate+root), got {cert_count}"
    );
}

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_root_cert_pem() {
    let chain = fx().x509_chain("sink-chain-root", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_root_cert_pem().unwrap();

    let content = temp.read_to_string().unwrap();
    assert_eq!(content, chain.root_cert_pem());
    assert!(content.contains("-----BEGIN CERTIFICATE-----"));
}

#[test]
#[cfg(feature = "x509")]
fn x509_chain_write_leaf_private_key_pem() {
    let chain = fx().x509_chain("sink-chain-key", ChainSpec::new("leaf.example.com"));
    let temp = chain.write_leaf_private_key_pem().unwrap();

    let content = temp.read_to_string().unwrap();
    assert_eq!(content, chain.leaf_private_key_pkcs8_pem());
    assert!(content.contains("-----BEGIN PRIVATE KEY-----"));
}

// ===========================================================================
// Tempfiles for TLS configuration scenario
// ===========================================================================

#[test]
#[cfg(feature = "x509")]
fn x509_tempfiles_for_tls_config() {
    let cert = fx().x509_self_signed("tls-server", X509Spec::self_signed("localhost"));

    // A typical TLS setup needs cert + key as files
    let cert_file = cert.write_cert_pem().unwrap();
    let key_file = cert.write_private_key_pem().unwrap();

    // Both files exist and have content
    assert!(cert_file.path().exists());
    assert!(key_file.path().exists());

    let cert_content = cert_file.read_to_string().unwrap();
    let key_content = key_file.read_to_string().unwrap();

    assert!(cert_content.contains("CERTIFICATE"));
    assert!(key_content.contains("PRIVATE KEY"));

    // Paths are different
    assert_ne!(cert_file.path(), key_file.path());
}

// ===========================================================================
// Cleanup verification across key types
// ===========================================================================

#[test]
#[cfg(feature = "rsa")]
fn rsa_tempfile_cleaned_up_on_drop() {
    let path = {
        let kp = fx().rsa("sink-rsa-drop", RsaSpec::rs256());
        let temp = kp.write_private_key_pkcs8_pem().unwrap();
        let p = temp.path().to_path_buf();
        assert!(p.exists());
        p
    };
    std::thread::sleep(std::time::Duration::from_millis(50));
    assert!(!path.exists(), "RSA tempfile should be cleaned up on drop");
}

#[test]
#[cfg(feature = "ecdsa")]
fn ecdsa_tempfile_cleaned_up_on_drop() {
    let path = {
        let kp = fx().ecdsa("sink-ec-drop", EcdsaSpec::es256());
        let temp = kp.write_private_key_pkcs8_pem().unwrap();
        let p = temp.path().to_path_buf();
        assert!(p.exists());
        p
    };
    std::thread::sleep(std::time::Duration::from_millis(50));
    assert!(
        !path.exists(),
        "ECDSA tempfile should be cleaned up on drop"
    );
}

#[test]
#[cfg(feature = "ed25519")]
fn ed25519_tempfile_cleaned_up_on_drop() {
    let path = {
        let kp = fx().ed25519("sink-ed-drop", Ed25519Spec::new());
        let temp = kp.write_private_key_pkcs8_pem().unwrap();
        let p = temp.path().to_path_buf();
        assert!(p.exists());
        p
    };
    std::thread::sleep(std::time::Duration::from_millis(50));
    assert!(
        !path.exists(),
        "Ed25519 tempfile should be cleaned up on drop"
    );
}

#[test]
#[cfg(feature = "x509")]
fn x509_tempfile_cleaned_up_on_drop() {
    let paths: Vec<PathBuf> = {
        let cert =
            fx().x509_self_signed("sink-x509-drop", X509Spec::self_signed("drop.example.com"));
        let cert_pem = cert.write_cert_pem().unwrap();
        let cert_der = cert.write_cert_der().unwrap();
        let key_pem = cert.write_private_key_pem().unwrap();
        let identity = cert.write_identity_pem().unwrap();
        vec![
            cert_pem.path().to_path_buf(),
            cert_der.path().to_path_buf(),
            key_pem.path().to_path_buf(),
            identity.path().to_path_buf(),
        ]
    };
    std::thread::sleep(std::time::Duration::from_millis(50));
    for p in &paths {
        assert!(
            !p.exists(),
            "X.509 tempfile should be cleaned up on drop: {p:?}"
        );
    }
}

// ===========================================================================
// Concurrent tempfile writes from multiple threads (all key types)
// ===========================================================================

#[test]
#[cfg(feature = "rsa")]
fn rsa_concurrent_tempfile_writes() {
    let factory = fx();
    let handles: Vec<_> = (0..4)
        .map(|i| {
            let fx = factory.clone();
            std::thread::spawn(move || {
                let label = format!("sink-rsa-conc-{i}");
                let kp = fx.rsa(&label, RsaSpec::rs256());
                let priv_temp = kp.write_private_key_pkcs8_pem().unwrap();
                let pub_temp = kp.write_public_key_spki_pem().unwrap();
                assert!(priv_temp.path().exists());
                assert!(pub_temp.path().exists());
                (
                    priv_temp.path().to_path_buf(),
                    pub_temp.path().to_path_buf(),
                    priv_temp,
                    pub_temp,
                )
            })
        })
        .collect();

    let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
    let all_paths: HashSet<_> = results
        .iter()
        .flat_map(|(a, b, _, _)| [a.clone(), b.clone()])
        .collect();

    assert_eq!(
        all_paths.len(),
        8,
        "all concurrent tempfile paths must be unique"
    );
}

#[test]
#[cfg(all(feature = "ecdsa", feature = "ed25519"))]
fn mixed_key_types_concurrent_tempfile_writes() {
    let factory = fx();

    let ec_handle = {
        let fx = factory.clone();
        std::thread::spawn(move || {
            let kp = fx.ecdsa("sink-mixed-ec", EcdsaSpec::es256());
            let temp = kp.write_private_key_pkcs8_pem().unwrap();
            (temp.path().to_path_buf(), temp)
        })
    };

    let ed_handle = {
        let fx = factory.clone();
        std::thread::spawn(move || {
            let kp = fx.ed25519("sink-mixed-ed", Ed25519Spec::new());
            let temp = kp.write_private_key_pkcs8_pem().unwrap();
            (temp.path().to_path_buf(), temp)
        })
    };

    let (ec_path, _ec_temp) = ec_handle.join().unwrap();
    let (ed_path, _ed_temp) = ed_handle.join().unwrap();

    assert_ne!(
        ec_path, ed_path,
        "different key type tempfiles must be distinct"
    );
}

// ===========================================================================
// Direct TempArtifact usage (re-exported from facade)
// ===========================================================================

#[test]
fn temp_artifact_reexported_from_facade() {
    let temp = TempArtifact::new_string("uk-facade-", ".pem", "facade-test-data").unwrap();
    assert!(temp.path().exists());
    assert_eq!(temp.read_to_string().unwrap(), "facade-test-data");
}

#[test]
fn temp_artifact_debug_does_not_leak() {
    let temp = TempArtifact::new_string("uk-facade-", ".pem", "SUPER_SECRET_KEY_MATERIAL").unwrap();
    let dbg = format!("{temp:?}");
    assert!(dbg.contains("TempArtifact"));
    assert!(
        !dbg.contains("SUPER_SECRET_KEY_MATERIAL"),
        "debug must not leak key content"
    );
}