summaryrefslogtreecommitdiffstats
path: root/vendor/xz2/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/xz2/tests
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/xz2/tests')
-rw-r--r--vendor/xz2/tests/drop-incomplete.rs31
-rw-r--r--vendor/xz2/tests/tokio.rs124
-rw-r--r--vendor/xz2/tests/xz.rs61
3 files changed, 216 insertions, 0 deletions
diff --git a/vendor/xz2/tests/drop-incomplete.rs b/vendor/xz2/tests/drop-incomplete.rs
new file mode 100644
index 000000000..15815be40
--- /dev/null
+++ b/vendor/xz2/tests/drop-incomplete.rs
@@ -0,0 +1,31 @@
+extern crate xz2;
+
+use std::io::prelude::*;
+use xz2::write::XzDecoder;
+
+// This is a XZ file generated by head -c10 /dev/urandom | xz -c
+const DATA: &'static [u8] =
+ &[253, 55, 122, 88, 90, 0, 0, 4, 230, 214, 180, 70, 2, 0, 33, 1, 22, 0, 0, 0, 116, 47, 229,
+ 163, 1, 0, 9, 7, 122, 65, 14, 253, 214, 121, 128, 230, 115, 0, 0, 0, 158, 47, 174, 196, 175,
+ 10, 34, 254, 0, 1, 34, 10, 21, 26, 225, 103, 31, 182, 243, 125, 1, 0, 0, 0, 0, 4, 89, 90];
+
+
+/// In this test, we drop a write::XzDecoder after supplying it a truncated input stream.
+///
+/// The decoder should detect that it is impossible to decode more data and not
+/// go into an infinite loop waiting for more data.
+#[test]
+fn drop_writer_incomplete_input_no_loop() {
+ let mut decoder = XzDecoder::new(Vec::new());
+ const PREFIX_LEN: usize = 50;
+ decoder.write_all(&DATA[..PREFIX_LEN]).unwrap();
+}
+
+/// Same as above, but verifying that we get an error if we manually call `finish`;
+#[test]
+fn finish_writer_incomplete_input_error() {
+ let mut decoder = XzDecoder::new(Vec::new());
+ const PREFIX_LEN: usize = 50;
+ decoder.write_all(&DATA[..PREFIX_LEN]).unwrap();
+ decoder.finish().err().expect("finish should error because of incomplete input");
+}
diff --git a/vendor/xz2/tests/tokio.rs b/vendor/xz2/tests/tokio.rs
new file mode 100644
index 000000000..be30ed099
--- /dev/null
+++ b/vendor/xz2/tests/tokio.rs
@@ -0,0 +1,124 @@
+#![cfg(feature = "tokio")]
+
+extern crate tokio_core;
+extern crate xz2;
+extern crate tokio_io;
+extern crate futures;
+extern crate rand;
+
+use std::thread;
+use std::net::{Shutdown, TcpListener};
+use std::io::{Read, Write};
+
+use xz2::read;
+use xz2::write;
+use futures::Future;
+use rand::{Rng, thread_rng};
+use tokio_core::net::TcpStream;
+use tokio_core::reactor::Core;
+use tokio_io::AsyncRead;
+use tokio_io::io::{copy, shutdown};
+
+#[test]
+fn tcp_stream_echo_pattern() {
+ const N: u8 = 16;
+ const M: usize = 16 * 1024;
+
+ let mut core = Core::new().unwrap();
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let addr = listener.local_addr().unwrap();
+ let t = thread::spawn(move || {
+ let a = listener.accept().unwrap().0;
+ let b = a.try_clone().unwrap();
+
+ let t = thread::spawn(move || {
+ let mut b = read::XzDecoder::new(b);
+ let mut buf = [0; M];
+ for i in 0..N {
+ b.read_exact(&mut buf).unwrap();
+ for byte in buf.iter() {
+ assert_eq!(*byte, i);
+ }
+ }
+
+ assert_eq!(b.read(&mut buf).unwrap(), 0);
+ });
+
+ let mut a = write::XzEncoder::new(a, 6);
+ for i in 0..N {
+ let buf = [i; M];
+ a.write_all(&buf).unwrap();
+ }
+ a.finish().unwrap()
+ .shutdown(Shutdown::Write).unwrap();
+
+ t.join().unwrap();
+ });
+
+ let handle = core.handle();
+ let stream = TcpStream::connect(&addr, &handle);
+ let copy = stream.and_then(|s| {
+ let (a, b) = s.split();
+ let a = read::XzDecoder::new(a);
+ let b = write::XzEncoder::new(b, 6);
+ copy(a, b)
+ }).then(|result| {
+ let (amt, _a, b) = result.unwrap();
+ assert_eq!(amt, (N as u64) * (M as u64));
+ shutdown(b).map(|_| ())
+ });
+
+ core.run(copy).unwrap();
+ t.join().unwrap();
+}
+
+#[test]
+fn echo_random() {
+ let v = thread_rng().gen_iter::<u8>().take(1024 * 1024).collect::<Vec<_>>();
+ let mut core = Core::new().unwrap();
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let addr = listener.local_addr().unwrap();
+ let v2 = v.clone();
+ let t = thread::spawn(move || {
+ let a = listener.accept().unwrap().0;
+ let b = a.try_clone().unwrap();
+
+ let mut v3 = v2.clone();
+ let t = thread::spawn(move || {
+ let mut b = read::XzDecoder::new(b);
+ let mut buf = [0; 1024];
+ while v3.len() > 0 {
+ let n = b.read(&mut buf).unwrap();
+ for (actual, expected) in buf[..n].iter().zip(&v3) {
+ assert_eq!(*actual, *expected);
+ }
+ v3.drain(..n);
+ }
+
+ assert_eq!(b.read(&mut buf).unwrap(), 0);
+ });
+
+ let mut a = write::XzEncoder::new(a, 6);
+ a.write_all(&v2).unwrap();
+ a.finish().unwrap()
+ .shutdown(Shutdown::Write).unwrap();
+
+ t.join().unwrap();
+ });
+
+ let handle = core.handle();
+ let stream = TcpStream::connect(&addr, &handle);
+ let copy = stream.and_then(|s| {
+ let (a, b) = s.split();
+ let a = read::XzDecoder::new(a);
+ let b = write::XzEncoder::new(b, 6);
+ copy(a, b)
+ }).then(|result| {
+ let (amt, _a, b) = result.unwrap();
+ assert_eq!(amt, v.len() as u64);
+ shutdown(b).map(|_| ())
+ });
+
+ core.run(copy).unwrap();
+ t.join().unwrap();
+}
diff --git a/vendor/xz2/tests/xz.rs b/vendor/xz2/tests/xz.rs
new file mode 100644
index 000000000..5b1dd6bb8
--- /dev/null
+++ b/vendor/xz2/tests/xz.rs
@@ -0,0 +1,61 @@
+extern crate xz2;
+
+use std::fs::File;
+use std::io::prelude::*;
+use std::path::Path;
+
+use xz2::read;
+use xz2::write;
+use xz2::stream;
+
+#[test]
+fn standard_files() {
+ for file in Path::new("lzma-sys/xz-5.2/tests/files").read_dir().unwrap() {
+ let file = file.unwrap();
+ if file.path().extension().and_then(|s| s.to_str()) != Some("xz") {
+ continue
+ }
+
+ let filename = file.file_name().into_string().unwrap();
+
+ // This appears to be implementation-defined how it's handled
+ if filename.contains("unsupported-check") {
+ continue
+ }
+
+ println!("testing {:?}", file.path());
+ let mut contents = Vec::new();
+ File::open(&file.path()).unwrap().read_to_end(&mut contents).unwrap();
+ if filename.starts_with("bad") || filename.starts_with("unsupported") {
+ test_bad(&contents);
+ } else {
+ test_good(&contents);
+ }
+ }
+}
+
+fn test_good(data: &[u8]) {
+ let mut ret = Vec::new();
+ read::XzDecoder::new_multi_decoder(data).read_to_end(&mut ret).unwrap();
+ let mut w = write::XzDecoder::new_multi_decoder(ret);
+ w.write_all(data).unwrap();
+ w.finish().unwrap();
+}
+
+fn test_bad(data: &[u8]) {
+ let mut ret = Vec::new();
+ assert!(read::XzDecoder::new(data).read_to_end(&mut ret).is_err());
+ let mut w = write::XzDecoder::new(ret);
+ assert!(w.write_all(data).is_err() || w.finish().is_err());
+}
+
+fn assert_send_sync<T: Send + Sync>() { }
+
+#[test]
+fn impls_send_and_sync() {
+ assert_send_sync::<stream::Stream>();
+ assert_send_sync::<read::XzDecoder<&[u8]>>();
+ assert_send_sync::<read::XzEncoder<&[u8]>>();
+ assert_send_sync::<write::XzEncoder<&mut [u8]>>();
+ assert_send_sync::<write::XzDecoder<&mut [u8]>>();
+}