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
|
use std::str;
use pdf::file::FileOptions;
use pdf::object::*;
use pdf::parser::{parse, ParseFlags};
use glob::glob;
macro_rules! file_path {
( $subdir:expr ) => { concat!("../files/", $subdir) }
}
macro_rules! run {
($e:expr) => (
match $e {
Ok(v) => v,
Err(e) => {
e.trace();
panic!("{}", e);
}
}
)
}
#[ignore]
#[test]
fn open_file() {
let _ = run!(FileOptions::uncached().open(file_path!("example.pdf")));
#[cfg(all(feature = "mmap", feature = "cache"))]
let _ = run!({
use memmap2::Mmap;
let file = std::fs::File::open(file_path!("example.pdf")).expect("can't open file");
let mmap = unsafe { Mmap::map(&file).expect("can't mmap file") };
FileOptions::cached().load(mmap)
});
}
#[cfg(feature="cache")]
#[test]
fn read_pages() {
for entry in glob(file_path!("*.pdf")).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
println!("\n == Now testing `{}` ==", path.to_str().unwrap());
let path = path.to_str().unwrap();
let file = run!(FileOptions::cached().open(path));
for i in 0 .. file.num_pages() {
println!("Read page {}", i);
let _ = file.get_page(i);
}
}
Err(e) => println!("{:?}", e)
}
}
}
#[test]
fn user_password() {
for entry in glob(file_path!("password_protected/*.pdf"))
.expect("Failed to read glob pattern")
{
match entry {
Ok(path) => {
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
let path = path.to_str().unwrap();
let file = run!(FileOptions::uncached().password(b"userpassword").open(path));
for i in 0 .. file.num_pages() {
println!("\nRead page {}", i);
let _ = file.get_page(i);
}
}
Err(e) => println!("{:?}", e)
}
}
}
#[ignore]
#[test]
fn owner_password() {
for entry in glob(file_path!("password_protected/*.pdf"))
.expect("Failed to read glob pattern")
{
match entry {
Ok(path) => {
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
let path = path.to_str().unwrap();
let file = run!(FileOptions::uncached().password(b"ownerpassword").open(path));
for i in 0 .. file.num_pages() {
println!("\nRead page {}", i);
let _ = file.get_page(i);
}
}
Err(e) => println!("{:?}", e)
}
}
}
// Test for invalid PDFs found by fuzzing.
// We don't care if they give an Err or Ok, as long as they don't panic.
#[cfg(feature="cache")]
#[test]
fn invalid_pdfs() {
for entry in glob(file_path!("invalid/*.pdf"))
.expect("Failed to read glob pattern")
{
match entry {
Ok(path) => {
let path = path.to_str().unwrap();
println!("\n\n == Now testing `{}` ==\n", path);
match FileOptions::cached().open(path) {
Ok(file) => {
for i in 0 .. file.num_pages() {
let _ = file.get_page(i);
}
}
Err(_) => {
continue;
}
}
}
Err(e) => panic!("error when reading glob patterns: {:?}", e),
}
}
}
#[cfg(feature="cache")]
#[ignore]
#[test]
fn parse_objects_from_stream() {
use pdf::object::NoResolve;
let file = run!(FileOptions::cached().open(file_path!("xelatex.pdf")));
let resolver = file.resolver();
// .. we know that object 13 of that file is an ObjectStream
let obj_stream: RcRef<ObjectStream> = run!(resolver.get(Ref::new(PlainRef {id: 13, gen: 0})));
for i in 0..obj_stream.n_objects() {
let (data, range) = run!(obj_stream.get_object_slice(i, &resolver));
let slice = &data[range];
println!("Object slice #{}: {}\n", i, str::from_utf8(slice).unwrap());
run!(parse(slice, &NoResolve, ParseFlags::ANY));
}
}
// TODO test decoding
|