-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.rs
127 lines (103 loc) · 3.29 KB
/
reader.rs
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
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::{self, Result};
use std::path::PathBuf;
use phar::{read, Reader};
#[test]
fn test_plain_no_index() -> Result<()> {
let mut dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push("tests/data");
let mut reader = Reader::<_, read::index::NoIndex>::read(
fs::File::open(dir.join("plain.phar"))?,
read::Options::builder().build(),
)?;
verify_std_header(&mut reader);
Ok(())
}
#[test]
fn test_plain_offset_only() -> Result<()> {
let mut dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push("tests/data");
let mut reader = Reader::<_, read::index::OffsetOnly>::read(
fs::File::open(dir.join("plain.phar"))?,
read::Options::builder().build(),
)?;
verify_std_header(&mut reader);
verify_std_contents(&mut reader);
Ok(())
}
#[test]
fn test_zip_offset_only() -> Result<()> {
let mut dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push("tests/data");
let mut reader = Reader::<_, read::index::OffsetOnly>::read(
fs::File::open(dir.join("zip.phar"))?,
read::Options::builder().build(),
)?;
verify_std_header(&mut reader);
verify_std_contents(&mut reader);
Ok(())
}
#[test]
fn test_zip_name_map() -> Result<()> {
let mut dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push("tests/data");
let mut reader = Reader::<_, read::index::NameHashMap>::read(
fs::File::open(dir.join("zip.phar"))?,
read::Options::builder().build(),
)?;
verify_std_header(&mut reader);
verify_std_contents(&mut reader);
Ok(())
}
#[test]
fn test_zip_metadata_map() -> Result<()> {
let mut dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push("tests/data");
let mut reader = Reader::<_, read::index::MetadataHashMap>::read(
fs::File::open(dir.join("zip.phar"))?,
read::Options::builder().build(),
)?;
verify_std_header(&mut reader);
verify_std_contents(&mut reader);
Ok(())
}
fn verify_std_header<R: io::Read + io::Seek, T: phar::read::FileIndex>(phar: &mut Reader<R, T>) {
assert_eq!(
phar.stub_bytes().expect("cannot read phar stub").as_ref(),
b"<?php __HALT_COMPILER(); ?>\r\n"
);
assert_eq!(
phar.metadata_bytes()
.expect("cannot read phar stub")
.as_ref(),
br#"s:3:"met";"#
);
}
fn verify_std_contents<R: io::Read + io::Seek, T: phar::read::index::Iterable>(
phar: &mut Reader<R, T>,
) {
assert_eq!(
phar.stub_bytes().expect("cannot read phar stub").as_ref(),
b"<?php __HALT_COMPILER(); ?>\r\n"
);
assert_eq!(
phar.metadata_bytes()
.expect("cannot read phar stub")
.as_ref(),
br#"s:3:"met";"#
);
let mut contents = HashMap::new();
phar.for_each_file(|name, read| {
let mut vec = vec![];
read.read_to_end(&mut vec)?;
contents.insert(name.to_vec(), vec);
Ok(())
})
.expect("Failed reading phar contents");
println!("{:?}", contents);
assert_eq!(contents.len(), 2);
assert_eq!(contents.get(&b"foo"[..]), Some(&b"bar".to_vec()));
assert_eq!(contents.get(&b"qux"[..]), Some(&b"corge".to_vec()));
}