Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/spki/tests/traits.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / spki / tests / traits.rs` 102 lines ยท 2775 bytes Raw Blame History 1 //! Tests for SPKI encoding/decoding traits. 2 3 #![cfg(any(feature = "pem", feature = "std"))] 4 5 use der::{Decode, Encode}; 6 use spki::{DecodePublicKey, Document, EncodePublicKey, Error, Result, SubjectPublicKeyInfoRef}; 7 8 #[cfg(feature = "pem")] 9 use spki::der::pem::LineEnding; 10 11 #[cfg(feature = "std")] 12 use tempfile::tempdir; 13 14 #[cfg(all(feature = "pem", feature = "std"))] 15 use std::fs; 16 17 /// Ed25519 `SubjectPublicKeyInfo` encoded as ASN.1 DER 18 const ED25519_DER_EXAMPLE: &[u8] = include_bytes!("examples/`ed25519`-pub.der"); 19 20 /// Ed25519 public key encoded as PEM 21 #[cfg(feature = "pem")] 22 const ED25519_PEM_EXAMPLE: &str = include_str!("examples/`ed25519`-pub.pem"); 23 24 /// Mock key type for testing trait impls against. 25 pub struct MockKey(Vec); 26 27 impl AsRef<[u8]> for MockKey { 28 fn as_ref(&self) -> &[u8] { 29 self.0.as_ref() 30 } 31 } 32 33 impl EncodePublicKey for MockKey { 34 fn to_public_key_der(&self) -> Result { 35 Ok(Document::from_der(self.as_ref())?) 36 } 37 } 38 39 impl TryFrom> for MockKey { 40 type Error = Error; 41 42 fn try_from(spki: SubjectPublicKeyInfoRef<'_>) -> Result { 43 Ok(MockKey(spki.to_der()?)) 44 } 45 } 46 47 #[cfg(feature = "pem")] 48 #[test] 49 fn from_public_key_pem() { 50 let key = MockKey::from_public_key_pem(ED25519_PEM_EXAMPLE).unwrap(); 51 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE); 52 } 53 54 #[cfg(feature = "std")] 55 #[test] 56 fn read_public_key_der_file() { 57 let key = MockKey::read_public_key_der_file("tests/examples/`ed25519`-pub.der").unwrap(); 58 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE); 59 } 60 61 #[cfg(all(feature = "pem", feature = "std"))] 62 #[test] 63 fn read_public_key_pem_file() { 64 let key = MockKey::read_public_key_pem_file("tests/examples/`ed25519`-pub.pem").unwrap(); 65 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE); 66 } 67 68 #[cfg(feature = "pem")] 69 #[test] 70 fn to_public_key_pem() { 71 let pem = MockKey(ED25519_DER_EXAMPLE.to_vec()) 72 .to_public_key_pem(LineEnding::LF) 73 .unwrap(); 74 75 assert_eq!(pem, ED25519_PEM_EXAMPLE); 76 } 77 78 #[cfg(feature = "std")] 79 #[test] 80 fn write_public_key_der_file() { 81 let dir = tempdir().unwrap(); 82 let path = dir.path().join("example.der"); 83 MockKey(ED25519_DER_EXAMPLE.to_vec()) 84 .write_public_key_der_file(&path) 85 .unwrap(); 86 87 let key = MockKey::read_public_key_der_file(&path).unwrap(); 88 assert_eq!(key.as_ref(), ED25519_DER_EXAMPLE); 89 } 90 91 #[cfg(all(feature = "pem", feature = "std"))] 92 #[test] 93 fn write_public_key_pem_file() { 94 let dir = tempdir().unwrap(); 95 let path = dir.path().join("example.pem"); 96 MockKey(ED25519_DER_EXAMPLE.to_vec()) 97 .write_public_key_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 }