diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-05-14 14:05:24 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-05-14 15:31:31 +0200 |
commit | 09f470d2762dc8aeff2b97369285c9cfb97d03ea (patch) | |
tree | af275c2882bcdd764c6d1c00a35a6ea3a9f4d0a5 /openpgp/src/tests.rs | |
parent | 0e74cf1b42b239e26d21b531e6ba3694e6f9361c (diff) |
openpgp: Add a filesystem-like framework for test data.
- Fixes #267.
Diffstat (limited to 'openpgp/src/tests.rs')
-rw-r--r-- | openpgp/src/tests.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/openpgp/src/tests.rs b/openpgp/src/tests.rs index cc8aa569..ba34f617 100644 --- a/openpgp/src/tests.rs +++ b/openpgp/src/tests.rs @@ -4,6 +4,7 @@ //! structured way. use std::fmt; +use std::collections::BTreeMap; pub struct Test { path: &'static str, @@ -51,3 +52,43 @@ pub const TSKS: &[&Test] = &[ t!("keys/testy-nistp521-private.pgp"), t!("keys/testy-private.pgp"), ]; + +/// Returns the content of the given file below `openpgp/tests/data`. +pub fn file(name: &str) -> &'static [u8] { + lazy_static! { + static ref FILES: BTreeMap<&'static str, &'static [u8]> = { + let mut m: BTreeMap<&'static str, &'static [u8]> = + Default::default(); + + macro_rules! add { + ( $key: expr, $path: expr ) => { + m.insert($key, include_bytes!($path)) + } + } + include!(concat!(env!("OUT_DIR"), "/tests.index.rs.inc")); + + // Sanity checks. + assert!(m.contains_key("messages/a-cypherpunks-manifesto.txt")); + assert!(m.contains_key("keys/testy.pgp")); + assert!(m.contains_key("keys/testy-private.pgp")); + m + }; + } + + FILES.get(name).unwrap_or_else(|| panic!("No such file {:?}", name)) +} + +/// Returns the content of the given file below `openpgp/tests/data/keys`. +pub fn key(name: &str) -> &'static [u8] { + file(&format!("keys/{}", name)) +} + +/// Returns the content of the given file below `openpgp/tests/data/messages`. +pub fn message(name: &str) -> &'static [u8] { + file(&format!("messages/{}", name)) +} + +/// Returns the cypherpunks manifesto. +pub fn manifesto() -> &'static [u8] { + message("a-cypherpunks-manifesto.txt") +} |