silent 2.16.1

Silent Web Framework
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
use anyhow::{Context, Result, anyhow, bail};
use rustls_pemfile::{pkcs8_private_keys, rsa_private_keys};
use rustls_pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer};
use std::fs;
use std::io::Cursor;
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock, RwLock};
use tokio_rustls::TlsAcceptor;

#[derive(Clone)]
enum KeyDer {
    Pkcs8(Vec<u8>),
    Pkcs1(Vec<u8>),
}

impl KeyDer {
    fn to_private_der(&self) -> PrivateKeyDer<'static> {
        match self {
            KeyDer::Pkcs8(bytes) => PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(bytes.clone())),
            KeyDer::Pkcs1(bytes) => PrivateKeyDer::Pkcs1(PrivatePkcs1KeyDer::from(bytes.clone())),
        }
    }
}

fn ensure_crypto_provider() {
    static INIT: OnceLock<()> = OnceLock::new();
    INIT.get_or_init(|| {
        // 尝试安装 ring 提供者;若已安装则忽略错误。
        let _ = rustls::crypto::ring::default_provider().install_default();
    });
}

#[derive(Clone)]
pub struct CertificateStore {
    cert_chain: Vec<Vec<u8>>,
    key_der: KeyDer,
    client_root: Vec<u8>,
}

impl CertificateStore {
    pub fn builder() -> CertificateStoreBuilder {
        CertificateStoreBuilder::default()
    }

    pub fn rustls_server_config(&self, alpn: &[&[u8]]) -> Result<rustls::ServerConfig> {
        ensure_crypto_provider();
        let chain: Vec<CertificateDer<'static>> = self
            .cert_chain
            .iter()
            .cloned()
            .map(CertificateDer::from)
            .collect();
        let key = self.key_der.to_private_der();

        let mut rustls_config = rustls::ServerConfig::builder()
            .with_no_client_auth()
            .with_single_cert(chain, key)?;
        rustls_config.alpn_protocols = alpn.iter().map(|proto| proto.to_vec()).collect();
        Ok(rustls_config)
    }

    pub fn arc_rustls_server_config(&self, alpn: &[&[u8]]) -> Result<Arc<rustls::ServerConfig>> {
        Ok(Arc::new(self.rustls_server_config(alpn)?))
    }

    pub fn tls_acceptor(&self, alpn: &[&[u8]]) -> Result<TlsAcceptor> {
        Ok(TlsAcceptor::from(self.arc_rustls_server_config(alpn)?))
    }

    pub fn https_config(&self) -> Result<Arc<rustls::ServerConfig>> {
        self.arc_rustls_server_config(&[b"h2", b"http/1.1"])
    }

    pub fn client_root_certificate(&self) -> Vec<u8> {
        self.client_root.clone()
    }
}

#[derive(Default)]
pub struct CertificateStoreBuilder {
    cert_path: Option<PathBuf>,
    key_path: Option<PathBuf>,
    root_ca_path: Option<PathBuf>,
}

impl CertificateStoreBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn cert_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.cert_path = Some(path.into());
        self
    }

    pub fn key_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.key_path = Some(path.into());
        self
    }

    pub fn root_ca_path<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.root_ca_path = Some(path.into());
        self
    }

    pub fn build(self) -> Result<CertificateStore> {
        let cert_path = self
            .cert_path
            .ok_or_else(|| anyhow!("未设置证书路径,请调用 cert_path"))?;
        let key_path = self
            .key_path
            .ok_or_else(|| anyhow!("未设置私钥路径,请调用 key_path"))?;

        if !cert_path.exists() {
            bail!("证书文件不存在: {}", cert_path.display());
        }
        if !key_path.exists() {
            bail!("私钥文件不存在: {}", key_path.display());
        }

        let cert_chain = load_cert_chain(&cert_path)?;
        let key_der = load_private_key(&key_path)?;
        let (cert_chain, client_root) =
            load_cert_chain_with_root(cert_chain, self.root_ca_path.as_deref())?;

        tracing::info!(
            cert_path = %cert_path.display(),
            key_path = %key_path.display(),
            root_ca = self.root_ca_path.as_ref().map(|p| p.display().to_string()),
            chain_len = cert_chain.len(),
            "initialized certificate store"
        );

        Ok(CertificateStore {
            cert_chain,
            key_der,
            client_root,
        })
    }
}

/// 支持从文件路径热加载的证书存储。
#[derive(Clone)]
pub struct ReloadableCertificateStore {
    inner: Arc<RwLock<CertificateStore>>,
    cert_path: PathBuf,
    key_path: PathBuf,
    root_ca_path: Option<PathBuf>,
}

impl ReloadableCertificateStore {
    pub fn from_paths<P: Into<PathBuf>>(
        cert_path: P,
        key_path: P,
        root_ca_path: Option<PathBuf>,
    ) -> Result<Self> {
        let cert_path = cert_path.into();
        let key_path = key_path.into();
        let mut builder = CertificateStoreBuilder::new()
            .cert_path(cert_path.clone())
            .key_path(key_path.clone());
        if let Some(root) = root_ca_path.clone() {
            builder = builder.root_ca_path(root);
        }
        let store = builder.build()?;
        Ok(Self {
            inner: Arc::new(RwLock::new(store)),
            cert_path,
            key_path,
            root_ca_path,
        })
    }

    /// 重新从磁盘加载证书与私钥。
    pub fn reload(&self) -> Result<()> {
        let mut builder = CertificateStoreBuilder::new()
            .cert_path(self.cert_path.clone())
            .key_path(self.key_path.clone());
        if let Some(root) = self.root_ca_path.clone() {
            builder = builder.root_ca_path(root);
        }
        let store = builder.build()?;
        if let Ok(mut guard) = self.inner.write() {
            *guard = store;
        }
        Ok(())
    }

    pub fn tls_acceptor(&self, alpn: &[&[u8]]) -> Result<TlsAcceptor> {
        let guard = self.inner.read().expect("certificate store poisoned");
        guard.tls_acceptor(alpn)
    }

    pub fn https_acceptor(&self) -> Result<TlsAcceptor> {
        self.tls_acceptor(&[b"h2", b"http/1.1"])
    }
}

fn load_cert_chain(cert_path: &Path) -> Result<Vec<Vec<u8>>> {
    let data = fs::read(cert_path)
        .with_context(|| format!("读取证书文件失败: {}", cert_path.display()))?;
    if looks_like_pem(&data) || is_pem_path(cert_path) {
        let mut reader = Cursor::new(&data);
        let certs = rustls_pemfile::certs(&mut reader)
            .collect::<Result<Vec<_>, _>>()
            .context("解析 PEM 证书失败")?;
        if certs.is_empty() {
            bail!("PEM 证书文件为空: {}", cert_path.display());
        }
        Ok(certs.into_iter().map(|c| c.to_vec()).collect())
    } else {
        Ok(vec![data])
    }
}

fn load_private_key(key_path: &Path) -> Result<KeyDer> {
    let data =
        fs::read(key_path).with_context(|| format!("读取私钥文件失败: {}", key_path.display()))?;
    if looks_like_pem(&data) || is_pem_path(key_path) {
        let mut cursor = Cursor::new(&data);
        let mut keys = pkcs8_private_keys(&mut cursor)
            .collect::<Result<Vec<_>, _>>()
            .context("解析 PKCS8 私钥失败")?;
        if let Some(key) = keys.pop() {
            return Ok(KeyDer::Pkcs8(key.secret_pkcs8_der().to_vec()));
        }

        let mut cursor = Cursor::new(&data);
        let mut rsa_keys = rsa_private_keys(&mut cursor)
            .collect::<Result<Vec<_>, _>>()
            .context("解析 RSA 私钥失败")?;
        if let Some(rsa_key) = rsa_keys.pop() {
            return Ok(KeyDer::Pkcs1(rsa_key.secret_pkcs1_der().to_vec()));
        }

        bail!(
            "PEM 私钥文件不包含 PKCS8 或 RSA 私钥: {}",
            key_path.display()
        );
    }

    Ok(KeyDer::Pkcs8(data))
}

fn load_cert_chain_with_root(
    mut chain: Vec<Vec<u8>>,
    root_ca_path: Option<&Path>,
) -> Result<(Vec<Vec<u8>>, Vec<u8>)> {
    if chain.is_empty() {
        bail!("证书链为空");
    }

    let mut client_root = chain[0].clone();

    if let Some(path) = root_ca_path {
        if path.exists() {
            let root_chain = load_cert_chain(path)?;
            if let Some(first) = root_chain.first() {
                client_root = first.clone();
            }
            for cert in root_chain {
                if !chain.iter().any(|existing| existing == &cert) {
                    chain.push(cert);
                }
            }
        } else {
            tracing::warn!(
                path = %path.display(),
                "根证书文件不存在,将使用服务器证书作为客户端根证书"
            );
        }
    }

    Ok((chain, client_root))
}

fn looks_like_pem(data: &[u8]) -> bool {
    data.starts_with(b"-----BEGIN")
}

fn is_pem_path(path: &Path) -> bool {
    matches!(path.extension().and_then(|ext| ext.to_str()), Some("pem"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn test_looks_like_pem_and_ext() {
        assert!(looks_like_pem(b"-----BEGIN CERTIFICATE-----\n..."));
        assert!(!looks_like_pem(b"random bytes"));
        assert!(is_pem_path(Path::new("/tmp/test.pem")));
        assert!(!is_pem_path(Path::new("/tmp/test.der")));
        // is_pem_path 只检查小写扩展名
        assert!(!is_pem_path(Path::new("/tmp/test.PEM")));
        assert!(!is_pem_path(Path::new("/tmp/test.crt")));
    }

    #[test]
    fn test_key_der_pkcs8_to_private_der() {
        let key_der = KeyDer::Pkcs8(vec![1, 2, 3, 4]);
        let private_der = key_der.to_private_der();
        match private_der {
            PrivateKeyDer::Pkcs8(_) => {}
            _ => panic!("Expected Pkcs8 variant"),
        }
    }

    #[test]
    fn test_key_der_pkcs1_to_private_der() {
        let key_der = KeyDer::Pkcs1(vec![5, 6, 7, 8]);
        let private_der = key_der.to_private_der();
        match private_der {
            PrivateKeyDer::Pkcs1(_) => {}
            _ => panic!("Expected Pkcs1 variant"),
        }
    }

    #[test]
    fn test_ensure_crypto_provider_multiple_calls() {
        // 测试多次调用 ensure_crypto_provider 不会 panic
        ensure_crypto_provider();
        ensure_crypto_provider();
        ensure_crypto_provider();
    }

    #[test]
    fn test_certificate_store_builder_new() {
        let builder = CertificateStoreBuilder::new();
        assert!(builder.cert_path.is_none());
        assert!(builder.key_path.is_none());
        assert!(builder.root_ca_path.is_none());
    }

    #[test]
    fn test_certificate_store_builder_default() {
        let builder = CertificateStoreBuilder::default();
        assert!(builder.cert_path.is_none());
        assert!(builder.key_path.is_none());
        assert!(builder.root_ca_path.is_none());
    }

    #[test]
    fn test_certificate_store_builder_chain() {
        use std::path::PathBuf;
        let builder = CertificateStoreBuilder::new()
            .cert_path("/tmp/test.crt")
            .key_path("/tmp/test.key")
            .root_ca_path("/tmp/ca.crt");

        assert_eq!(builder.cert_path, Some(PathBuf::from("/tmp/test.crt")));
        assert_eq!(builder.key_path, Some(PathBuf::from("/tmp/test.key")));
        assert_eq!(builder.root_ca_path, Some(PathBuf::from("/tmp/ca.crt")));
    }

    #[test]
    fn test_builder_missing_paths_errors() {
        // 仅设置 key_path,缺少 cert_path
        let err = CertificateStoreBuilder::new()
            .key_path("/tmp/missing.key")
            .build()
            .err()
            .expect("should error when cert_path is missing");
        let msg = format!("{err:#}");
        assert!(msg.contains("未设置证书路径"));

        // 仅设置 cert_path,缺少 key_path
        let err = CertificateStoreBuilder::new()
            .cert_path("/tmp/missing.crt")
            .build()
            .err()
            .expect("should error when key_path is missing");
        let msg = format!("{err:#}");
        assert!(msg.contains("未设置私钥路径"));
    }

    #[test]
    fn test_builder_nonexistent_files_errors() {
        // 同时设置证书与私钥,但文件不存在
        let err = CertificateStoreBuilder::new()
            .cert_path("/tmp/not-exist.crt")
            .key_path("/tmp/not-exist.key")
            .build()
            .err()
            .expect("should error on non-existent files");
        let msg = format!("{err:#}");
        assert!(msg.contains("证书文件不存在") || msg.contains("私钥文件不存在"));
    }

    #[test]
    fn test_builder_success_with_raw_der_bytes() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_{}.key", unique));

        // 写入原始字节(非 PEM),builder 会将其视为 DER 字节并成功构建
        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .expect("builder should succeed with raw bytes");

        // 能返回客户端根证书字节(即我们写入的第一段)
        let root = store.client_root_certificate();
        assert!(!root.is_empty());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_builder_with_root_ca_path_not_exists() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_ca_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_ca_{}.key", unique));
        let ca_path = base.join(format!("silent_tls_test_ca_{}.ca", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        // root_ca 不存在时应该仍然成功(会使用服务器证书作为客户端根证书)
        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .root_ca_path(&ca_path)
            .build()
            .expect("builder should succeed even if root CA doesn't exist");

        let root = store.client_root_certificate();
        assert!(!root.is_empty());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_certificate_store_client_root_certificate() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_root_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_root_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .unwrap();

        let root1 = store.client_root_certificate();
        let root2 = store.client_root_certificate();
        assert_eq!(root1, root2);
        assert_eq!(root1, b"CERTBYTES");

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_certificate_store_rustls_server_config() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_conf_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_conf_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .unwrap();

        // 测试不同的 ALPN 协议配置
        let alpn_slice: &[&[u8]] = &[b"h2", b"http/1.1"];

        let result = store.rustls_server_config(alpn_slice);
        // 由于使用的是无效的 DER 字节,rustls 配置会失败
        assert!(result.is_err());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_certificate_store_arc_rustls_server_config() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_arc_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_arc_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .unwrap();

        let result = store.arc_rustls_server_config(&[b"h2", b"http/1.1"]);
        // 由于使用的是无效的 DER 字节,rustls 配置会失败
        assert!(result.is_err());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_certificate_store_tls_acceptor() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_acceptor_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_acceptor_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .unwrap();

        let result = store.tls_acceptor(&[b"h2", b"http/1.1"]);
        // 由于使用的是无效的 DER 字节,rustls 配置会失败
        assert!(result.is_err());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_https_config_error_on_invalid_der() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_bad_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_bad_{}.key", unique));

        // 无效原始字节:将导致 https_config() 失败
        fs::write(&cert_path, b"BAD_CERT").unwrap();
        fs::write(&key_path, b"BAD_KEY").unwrap();

        let store = CertificateStore::builder()
            .cert_path(&cert_path)
            .key_path(&key_path)
            .build()
            .expect("builder should still construct store with raw bytes");

        let err = store
            .https_config()
            .expect_err("https_config should fail on invalid der");
        let msg = format!("{err:#}");
        assert!(!msg.is_empty());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_reloadable_certificate_store_from_paths() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_reload_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_reload_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let _store = ReloadableCertificateStore::from_paths(&cert_path, &key_path, None)
            .expect("should create reloadable store");

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_reloadable_certificate_store_reload() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_reload2_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_reload2_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = ReloadableCertificateStore::from_paths(&cert_path, &key_path, None)
            .expect("should create reloadable store");

        // 测试 reload 方法
        let result = store.reload();
        assert!(result.is_ok());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_reloadable_certificate_store_with_root_ca() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_reload_ca_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_reload_ca_{}.key", unique));
        let ca_path = base.join(format!("silent_tls_test_reload_ca_{}.ca", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store =
            ReloadableCertificateStore::from_paths(&cert_path, &key_path, Some(ca_path.clone()))
                .expect("should create reloadable store with root CA");

        // 验证 root_ca_path 被正确保存
        assert_eq!(store.root_ca_path, Some(ca_path));

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_reloadable_certificate_store_tls_acceptor() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_acceptor2_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_acceptor2_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = ReloadableCertificateStore::from_paths(&cert_path, &key_path, None)
            .expect("should create reloadable store");

        let result = store.tls_acceptor(&[b"h2", b"http/1.1"]);
        // 由于使用的是无效的 DER 字节,rustls 配置会失败
        assert!(result.is_err());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_reloadable_certificate_store_https_acceptor() {
        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let cert_path = base.join(format!("silent_tls_test_https_{}.crt", unique));
        let key_path = base.join(format!("silent_tls_test_https_{}.key", unique));

        fs::write(&cert_path, b"CERTBYTES").unwrap();
        fs::write(&key_path, b"KEYBYTES").unwrap();

        let store = ReloadableCertificateStore::from_paths(&cert_path, &key_path, None)
            .expect("should create reloadable store");

        let result = store.https_acceptor();
        // 由于使用的是无效的 DER 字节,rustls 配置会失败
        assert!(result.is_err());

        let _ = fs::remove_file(&cert_path);
        let _ = fs::remove_file(&key_path);
    }

    #[test]
    fn test_load_cert_chain_with_root_empty_chain() {
        let result = load_cert_chain_with_root(vec![], None);
        assert!(result.is_err());
        if let Err(e) = result {
            let msg = format!("{e:#}");
            assert!(msg.contains("证书链为空"));
        }
    }

    #[test]
    fn test_load_cert_chain_with_root_without_root_ca() {
        let cert_chain = vec![vec![1, 2, 3], vec![4, 5, 6]];
        let (chain, client_root) = load_cert_chain_with_root(cert_chain.clone(), None)
            .expect("should succeed without root CA");

        assert_eq!(chain, cert_chain);
        assert_eq!(client_root, vec![1, 2, 3]);
    }

    #[test]
    fn test_load_cert_chain_with_root_with_root_ca() {
        let cert_chain = vec![vec![1, 2, 3]];
        let root_ca_chain = [vec![7, 8, 9], vec![10, 11, 12]];

        let base = std::env::temp_dir();
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let ca_path = base.join(format!("silent_tls_test_root_ca_{}.ca", unique));

        // 写入非 PEM 格式的数据
        fs::write(&ca_path, &root_ca_chain[0]).unwrap();

        let result = load_cert_chain_with_root(cert_chain, Some(&ca_path));
        // 由于数据格式不正确,会读取为单个 DER 证书
        assert!(result.is_ok());

        let _ = fs::remove_file(&ca_path);
    }
}