summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-12-12 17:11:29 +0100
committerJustus Winter <justus@pep-project.org>2017-12-13 13:50:30 +0100
commitc0cab61441df7a0334f817c2cc4817a0910e1193 (patch)
tree746d49a788d2b45a2f9030e87ad028d0ba050d87 /src
parent9db72f10dbe30de3d5613933573ee6ec2c3b2bc1 (diff)
Rework the way we locate test data.
Diffstat (limited to 'src')
-rw-r--r--src/openpgp/parse/parse.rs221
1 files changed, 105 insertions, 116 deletions
diff --git a/src/openpgp/parse/parse.rs b/src/openpgp/parse/parse.rs
index 544930aa..c2b08bc1 100644
--- a/src/openpgp/parse/parse.rs
+++ b/src/openpgp/parse/parse.rs
@@ -453,18 +453,23 @@ fn compressed_data_parser<'a, R: BufferedReader + 'a>(mut bio: R)
});
}
+#[cfg(test)]
+use std::path::PathBuf;
+
+#[cfg(test)]
+fn path_to(artifact: &str) -> PathBuf {
+ [env!("CARGO_MANIFEST_DIR"), "src", "openpgp", "parse", artifact]
+ .iter().collect()
+}
+
#[test]
fn compressed_data_parser_test () {
let expected = include_bytes!("literal-mode-t-partial-body.txt");
for i in 1..4 {
- use std::path::PathBuf;
use std::fs::File;
- let path : PathBuf = [env!("CARGO_MANIFEST_DIR"),
- "src", "openpgp", "parse",
- &format!("compressed-data-algo-{}.asc", i)[..]]
- .iter().collect();
+ let path = path_to(&format!("compressed-data-algo-{}.asc", i)[..]);
let mut f = File::open(&path).expect(&path.to_string_lossy());
let mut bio = BufferedReaderGeneric::new(&mut f, None);
@@ -861,133 +866,117 @@ impl Message {
}
}
-#[test]
-fn deserialize_test_1 () {
- // XXX: This test should be more thorough. Right now, we mostly
- // just rely on the fact that an assertion is not thrown.
-
- // A flat message.
- let data = include_bytes!("public-key.asc");
- let bio = BufferedReaderMemory::new(data);
- let message = Message::deserialize(bio, None).unwrap();
- eprintln!("Message has {} top-level packets.",
- message.packets.len());
- eprintln!("Message: {:?}", message);
-
- let mut count = 0;
- for (i, p) in message.iter().enumerate() {
- eprintln!("{}: {:?}", i, p);
- count += 1;
- }
+#[cfg(test)]
+mod message_test {
+ use super::path_to;
+ use super::{BufferedReaderMemory, BufferedReaderGeneric, Message, PacketParser};
- assert_eq!(count, 61);
-}
-
-#[test]
-fn deserialize_test_2 () {
- // A message containing a compressed packet that contains a
- // literal packet.
- use std::path::PathBuf;
use std::fs::File;
- let path : PathBuf = [env!("CARGO_MANIFEST_DIR"),
- "src", "openpgp", "parse",
- "compressed-data-algo-1.asc"]
- .iter().collect();
- let mut f = File::open(&path).expect(&path.to_string_lossy());
- let bio = BufferedReaderGeneric::new(&mut f, None);
- let message = Message::deserialize(bio, None).unwrap();
- eprintln!("Message has {} top-level packets.", message.packets.len());
- eprintln!("Message: {:?}", message);
-
- let mut count = 0;
- for (i, p) in message.iter().enumerate() {
- eprintln!("{}: {:?}", i, p);
- count += 1;
- }
- assert_eq!(count, 2);
-}
+ #[test]
+ fn deserialize_test_1 () {
+ // XXX: This test should be more thorough. Right now, we mostly
+ // just rely on the fact that an assertion is not thrown.
-#[test]
-fn deserialize_test_3 () {
- use std::path::PathBuf;
- use std::fs::File;
+ // A flat message.
+ let data = include_bytes!("public-key.asc");
+ let bio = BufferedReaderMemory::new(data);
+ let message = Message::deserialize(bio, None).unwrap();
+ eprintln!("Message has {} top-level packets.",
+ message.packets.len());
+ eprintln!("Message: {:?}", message);
+
+ let mut count = 0;
+ for (i, p) in message.iter().enumerate() {
+ eprintln!("{}: {:?}", i, p);
+ count += 1;
+ }
- let path : PathBuf = [env!("CARGO_MANIFEST_DIR"),
- "src", "openpgp", "parse",
- "signed.gpg"]
- .iter().collect();
- let mut f = File::open(&path).expect(&path.to_string_lossy());
- let bio = BufferedReaderGeneric::new(&mut f, None);
- let message = Message::deserialize(bio, None).unwrap();
- eprintln!("Message has {} top-level packets.", message.packets.len());
- eprintln!("Message: {:?}", message);
-
- let mut count = 0;
- for (i, p) in message.iter().enumerate() {
- count += 1;
- eprintln!("{}: {:?}", i, p);
+ assert_eq!(count, 61);
}
- // We expect 6 packets.
- assert_eq!(count, 6);
-}
-#[test]
-fn compression_quine_test_1 () {
- // Use the Message::deserialize interface to parse an OpenPGP
- // quine.
- use std::path::PathBuf;
- use std::fs::File;
-
- let path : PathBuf = [env!("CARGO_MANIFEST_DIR"),
- "src", "openpgp", "parse",
- "compression-quine.gpg"]
- .iter().collect();
- let mut f = File::open(&path).expect(&path.to_string_lossy());
-
- let bio = BufferedReaderGeneric::new(&mut f, None);
- let max_recursion_depth = 128;
- let message =
- Message::deserialize(bio, Some(max_recursion_depth)).unwrap();
-
- let mut count = 0;
- for (i, p) in message.iter().enumerate() {
- count += 1;
- if false {
- eprintln!("{}: p: {:?}", i, p);
+ #[test]
+ fn deserialize_test_2 () {
+ // A message containing a compressed packet that contains a
+ // literal packet.
+ let path = path_to("compressed-data-algo-1.asc");
+ let mut f = File::open(&path).expect(&path.to_string_lossy());
+ let bio = BufferedReaderGeneric::new(&mut f, None);
+ let message = Message::deserialize(bio, None).unwrap();
+ eprintln!("Message has {} top-level packets.", message.packets.len());
+ eprintln!("Message: {:?}", message);
+
+ let mut count = 0;
+ for (i, p) in message.iter().enumerate() {
+ eprintln!("{}: {:?}", i, p);
+ count += 1;
}
+ assert_eq!(count, 2);
}
- assert_eq!(count, 1 + max_recursion_depth);
-}
+ #[test]
+ fn deserialize_test_3 () {
+ let path = path_to("signed.gpg");
+ let mut f = File::open(&path).expect(&path.to_string_lossy());
+ let bio = BufferedReaderGeneric::new(&mut f, None);
+ let message = Message::deserialize(bio, None).unwrap();
+ eprintln!("Message has {} top-level packets.", message.packets.len());
+ eprintln!("Message: {:?}", message);
-#[test]
-fn compression_quine_test_2 () {
- // Use the iterator interface to parse an OpenPGP quine.
- use std::path::PathBuf;
- use std::fs::File;
+ let mut count = 0;
+ for (i, p) in message.iter().enumerate() {
+ count += 1;
+ eprintln!("{}: {:?}", i, p);
+ }
+ // We expect 6 packets.
+ assert_eq!(count, 6);
+ }
- let path : PathBuf = [env!("CARGO_MANIFEST_DIR"),
- "src", "openpgp", "parse",
- "compression-quine.gpg"]
- .iter().collect();
- let mut f = File::open(&path).expect(&path.to_string_lossy());
+ #[test]
+ fn compression_quine_test_1 () {
+ // Use the Message::deserialize interface to parse an OpenPGP
+ // quine.
+ let path = path_to("compression-quine.gpg");
+ let mut f = File::open(&path).expect(&path.to_string_lossy());
- let bio = BufferedReaderGeneric::new(&mut f, None);
- let max_recursion_depth = 255;
- let mut ppo : Option<PacketParser>
- = PacketParser::new(bio, Some(max_recursion_depth)).unwrap();
+ let bio = BufferedReaderGeneric::new(&mut f, None);
+ let max_recursion_depth = 128;
+ let message =
+ Message::deserialize(bio, Some(max_recursion_depth)).unwrap();
- let mut count : usize = 0;
- loop {
- if let Some(pp2) = ppo {
+ let mut count = 0;
+ for (i, p) in message.iter().enumerate() {
count += 1;
+ if false {
+ eprintln!("{}: p: {:?}", i, p);
+ }
+ }
- let (_packet, pp2, _position) = pp2.recurse().unwrap();
- ppo = pp2;
- } else {
- break;
+ assert_eq!(count, 1 + max_recursion_depth);
+ }
+
+ #[test]
+ fn compression_quine_test_2 () {
+ // Use the iterator interface to parse an OpenPGP quine.
+ let path = path_to("compression-quine.gpg");
+ let mut f = File::open(&path).expect(&path.to_string_lossy());
+
+ let bio = BufferedReaderGeneric::new(&mut f, None);
+ let max_recursion_depth = 255;
+ let mut ppo : Option<PacketParser>
+ = PacketParser::new(bio, Some(max_recursion_depth)).unwrap();
+
+ let mut count : usize = 0;
+ loop {
+ if let Some(pp2) = ppo {
+ count += 1;
+
+ let (_packet, pp2, _position) = pp2.recurse().unwrap();
+ ppo = pp2;
+ } else {
+ break;
+ }
}
+ assert_eq!(count, 1 + max_recursion_depth as usize);
}
- assert_eq!(count, 1 + max_recursion_depth as usize);
}