interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / pkcs8 / tests / traits.rs

102 lines · 2728 bytes Raw Blame History
1//! Tests for PKCS#8 encoding/decoding traits.
2
3#![cfg(any(feature = "pem", feature = "std"))]
4
5use der::Encode;
6use pkcs8::{DecodePrivateKey, EncodePrivateKey, Error, PrivateKeyInfo, Result, SecretDocument};
7
8#[cfg(feature = "pem")]
9use pkcs8::der::pem::LineEnding;
10
11#[cfg(feature = "std")]
12use tempfile::tempdir;
13
14#[cfg(all(feature = "pem", feature = "std"))]
15use std::fs;
16
17/// Ed25519 `PrivateKeyInfo` encoded as ASN.1 DER
18const ED25519_DER_EXAMPLE: &[u8] = include_bytes!("examples/ed25519-priv-pkcs8v1.der");
19
20/// Ed25519 private key encoded as PEM
21#[cfg(feature = "pem")]
22const ED25519_PEM_EXAMPLE: &str = include_str!("examples/ed25519-priv-pkcs8v1.pem");
23
24/// Mock key type for testing trait impls against.
25pub struct MockKey(Vec<u8>);
26
27impl AsRef<[u8]> for MockKey {
28 fn as_ref(&self) -> &[u8] {
29 self.0.as_ref()
30 }
31}
32
33impl EncodePrivateKey for MockKey {
34 fn to_pkcs8_der(&self) -> Result<SecretDocument> {
35 Ok(SecretDocument::try_from(self.as_ref())?)
36 }
37}
38
39impl TryFrom<PrivateKeyInfo<'_>> for MockKey {
40 type Error = Error;
41
42 fn try_from(pkcs8: PrivateKeyInfo<'_>) -> Result<MockKey> {
43 Ok(MockKey(pkcs8.to_der()?))
44 }
45}
46
47#[cfg(feature = "pem")]
48#[test]
49fn from_pkcs8_pem() {
50 let key = MockKey::from_pkcs8_pem(ED25519_PEM_EXAMPLE).unwrap();
51 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE);
52}
53
54#[cfg(feature = "std")]
55#[test]
56fn read_pkcs8_der_file() {
57 let key = MockKey::read_pkcs8_der_file("tests/examples/ed25519-priv-pkcs8v1.der").unwrap();
58 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE);
59}
60
61#[cfg(all(feature = "pem", feature = "std"))]
62#[test]
63fn read_pkcs8_pem_file() {
64 let key = MockKey::read_pkcs8_pem_file("tests/examples/ed25519-priv-pkcs8v1.pem").unwrap();
65 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE);
66}
67
68#[cfg(feature = "pem")]
69#[test]
70fn to_pkcs8_pem() {
71 let pem = MockKey(ED25519_DER_EXAMPLE.to_vec())
72 .to_pkcs8_pem(LineEnding::LF)
73 .unwrap();
74
75 assert_eq!(&*pem, ED25519_PEM_EXAMPLE);
76}
77
78#[cfg(feature = "std")]
79#[test]
80fn write_pkcs8_der_file() {
81 let dir = tempdir().unwrap();
82 let path = dir.path().join("example.der");
83 MockKey(ED25519_DER_EXAMPLE.to_vec())
84 .write_pkcs8_der_file(&path)
85 .unwrap();
86
87 let key = MockKey::read_pkcs8_der_file(&path).unwrap();
88 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE);
89}
90
91#[cfg(all(feature = "pem", feature = "std"))]
92#[test]
93fn write_pkcs8_pem_file() {
94 let dir = tempdir().unwrap();
95 let path = dir.path().join("example.pem");
96 MockKey(ED25519_DER_EXAMPLE.to_vec())
97 .write_pkcs8_pem_file(&path, LineEnding::LF)
98 .unwrap();
99
100 let pem = fs::read_to_string(path).unwrap();
101 assert_eq!(&pem, ED25519_PEM_EXAMPLE);
102}