Debian Codegraph @

src/tests/read/zip64/mod.rs

raw symbols SCIP
1 // Copyright (c) 2023 Harry [Majored] [[email protected]]
2 // Copyright (c) 2023 Cognite AS
3 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
4
5 use futures_literust-analyzer cargo futures-lite 2.1.0 crate/::iorust-analyzer cargo futures-lite 2.1.0 io/::AsyncReadExtrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#;
6
7 use craterust-analyzer cargo async_zip 0.0.18 crate/::testsrust-analyzer cargo async_zip 0.0.18 tests/::init_loggerrust-analyzer cargo async_zip 0.0.18 tests/init_logger().;
8
9 const ZIP64_ZIP_CONTENTSrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/ZIP64_ZIP_CONTENTS.: &str = "Hello World!\n";
10
11 /// Tests opening and reading a zip64 archive.
12 /// It contains one file named "-" with a zip 64 extended field header.
13 #[tokiorust-analyzer cargo tokio 1.25.0 crate/::testrust-analyzer cargo tokio-macros 1.8.0 test:]
14 async fn test_read_zip64_archive_memrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/test_read_zip64_archive_mem().() {
15 use craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::memrust-analyzer cargo async_zip 0.0.18 base/read/mem/::ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#;
16 init_loggerrust-analyzer cargo async_zip 0.0.18 tests/init_logger().();
17
18 let data = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("zip64.zip").to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().();
19
20 let reader = ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/mem/ZipFileReader#::newrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]new().(data).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
21 let mut entry_reader = reader.reader_without_entryrust-analyzer cargo async_zip 0.0.18 base/read/mem/impl#[ZipFileReader]reader_without_entry().(0).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
22
23 let mut read_data = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
24 entry_reader.read_to_stringrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#read_to_string().(&mut read_data).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll()..expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("read failed");
25
26 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
27 read_data.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(),
28 ZIP64_ZIP_CONTENTSrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/ZIP64_ZIP_CONTENTS..charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(),
29 "{read_data:?} != {ZIP64_ZIP_CONTENTS:?}"
30 );
31 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(read_data, ZIP64_ZIP_CONTENTSrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/ZIP64_ZIP_CONTENTS.);
32 }
33
34 /// Like test_read_zip64_archive_mem() but for the streaming version
35 #[tokiorust-analyzer cargo tokio 1.25.0 crate/::testrust-analyzer cargo tokio-macros 1.8.0 test:]
36 async fn test_read_zip64_archive_streamrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/test_read_zip64_archive_stream().() {
37 use craterust-analyzer cargo async_zip 0.0.18 crate/::baserust-analyzer cargo async_zip 0.0.18 base/::readrust-analyzer cargo async_zip 0.0.18 base/read/::streamrust-analyzer cargo async_zip 0.0.18 base/read/stream/::ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/stream/ZipFileReader#;
38 init_loggerrust-analyzer cargo async_zip 0.0.18 tests/init_logger().();
39
40 let data = include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("zip64.zip").to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().();
41
42 let reader = ZipFileReaderrust-analyzer cargo async_zip 0.0.18 base/read/stream/ZipFileReader#::newrust-analyzer cargo async_zip 0.0.18 base/read/stream/impl#[`ZipFileReader<Ready<R>>`]new().(data.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
43 let mut entry_reader = reader.next_without_entryrust-analyzer cargo async_zip 0.0.18 base/read/stream/impl#[`ZipFileReader<Ready<R>>`]next_without_entry().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
44
45 let mut read_data = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
46 entry_reader.reader_mutrust-analyzer cargo async_zip 0.0.18 base/read/stream/impl#[`ZipFileReader<Reading<'a, R, E>>`]reader_mut().().read_to_stringrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#read_to_string().(&mut read_data).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll()..expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("read failed");
47
48 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
49 read_data.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(),
50 ZIP64_ZIP_CONTENTSrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/ZIP64_ZIP_CONTENTS..charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(),
51 "{read_data:?} != {ZIP64_ZIP_CONTENTS:?}"
52 );
53 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(read_data, ZIP64_ZIP_CONTENTSrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/ZIP64_ZIP_CONTENTS.);
54 }
55
56 /// Generate an example file only if it doesn't exist already.
57 /// The file is placed adjacent to this rs file.
58 #[cfg(feature = "tokio")]
59 fn generate_zip64many_ziprust-analyzer cargo async_zip 0.0.18 tests/read/zip64/generate_zip64many_zip().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# {
60 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
61 use ziprust-analyzer cargo zip 5.1.1 crate/::writerust-analyzer cargo zip 5.1.1 write/::{ExtendedFileOptionsrust-analyzer cargo zip 5.1.1 write/ExtendedFileOptions#, FileOptionsrust-analyzer cargo zip 5.1.1 write/FileOptions#};
62
63 let mut path = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_MANIFEST_DIR"));
64 path.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().("src/tests/read/zip64/zip64many.zip");
65
66 // Only recreate the zip if it doesnt already exist.
67 if path.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
68 return path;
69 }
70
71 let zip_file = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
72 let mut zip = ziprust-analyzer cargo zip 5.1.1 crate/::ZipWriterrust-analyzer cargo zip 5.1.1 write/zip_writer/ZipWriter#::newrust-analyzer cargo zip 5.1.1 write/impl#[`ZipWriter<W>`]new().(zip_file);
73 let options: FileOptionsrust-analyzer cargo zip 5.1.1 write/FileOptions#<'_, ExtendedFileOptionsrust-analyzer cargo zip 5.1.1 write/ExtendedFileOptions#> =
74 FileOptionsrust-analyzer cargo zip 5.1.1 write/FileOptions#::defaultrust-analyzer cargo zip 5.1.1 write/impl#[`FileOptions<'_, T>`][Default]default().().compression_methodrust-analyzer cargo zip 5.1.1 write/impl#[`FileOptions<'_, T>`]compression_method().(ziprust-analyzer cargo zip 5.1.1 crate/::CompressionMethodrust-analyzer cargo zip 5.1.1 compression/CompressionMethod#::Storedrust-analyzer cargo zip 5.1.1 compression/CompressionMethod#Stored#);
75
76 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#2_u32.powrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]pow().(16) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().1 {
77 zip.start_filerust-analyzer cargo zip 5.1.1 write/impl#[`ZipWriter<W>`]start_file().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{i}.txt"), options.clonerust-analyzer cargo zip 5.1.1 write/impl#[`FileOptions<'k, T>`][Clone]clone().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
78 zip.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"\n").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
79 }
80
81 zip.finishrust-analyzer cargo zip 5.1.1 write/impl#[`ZipWriter<W>`]finish().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
82
83 path
84 }
85
86 /// Test reading a generated zip64 archive that contains more than 2^16 entries.
87 #[cfg(feature = "tokio-fs")]
88 #[tokiorust-analyzer cargo tokio 1.25.0 crate/::testrust-analyzer cargo tokio-macros 1.8.0 test:]
89 async fn test_read_zip64_archive_many_entriesrust-analyzer cargo async_zip 0.0.18 tests/read/zip64/test_read_zip64_archive_many_entries().() {
90 use craterust-analyzer cargo async_zip 0.0.18 crate/::tokiorust-analyzer cargo async_zip 0.0.18 tokio/::readrust-analyzer cargo async_zip 0.0.18 tokio/read/::fsrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/::ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#;
91
92 init_loggerrust-analyzer cargo async_zip 0.0.18 tests/init_logger().();
93
94 let path = generate_zip64many_ziprust-analyzer cargo async_zip 0.0.18 tests/read/zip64/generate_zip64many_zip().();
95
96 let reader = ZipFileReaderrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/ZipFileReader#::newrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]new().(path).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
97
98 // Verify that each entry exists and is has the contents "\n"
99 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#2_u32.powrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]pow().(16) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). 1 {
100 let entry = reader.filerust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]file().().entriesrust-analyzer cargo async_zip 0.0.18 file/impl#[ZipFile]entries().().getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(i as usize).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
101 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("{:?}", entry.filenamerust-analyzer cargo async_zip 0.0.18 entry/impl#[ZipEntry]filename().().as_bytesrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]as_bytes().());
102 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(entry.filenamerust-analyzer cargo async_zip 0.0.18 entry/ZipEntry#filename..as_strrust-analyzer cargo async_zip 0.0.18 string/impl#[ZipString]as_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{i}.txt"));
103 let mut entry = reader.reader_without_entryrust-analyzer cargo async_zip 0.0.18 tokio/read/fs/impl#[ZipFileReader]reader_without_entry().(i as usize).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
104 let mut contents = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
105 entry.read_to_stringrust-analyzer cargo futures-lite 2.1.0 io/AsyncReadExt#read_to_string().(&mut contents).rust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll().awaitrust-analyzer cargo futures-lite 2.1.0 io/impl#[`ReadToStringFuture<'_, R>`][Future]poll()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(contents, "\n");
107 }
108 }