From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 7791366AF4 for ; Tue, 28 Jul 2020 12:33:26 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 74FD3237B2 for ; Tue, 28 Jul 2020 12:33:26 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4A34D2375D for ; Tue, 28 Jul 2020 12:33:23 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 11EA6433C5 for ; Tue, 28 Jul 2020 12:33:23 +0200 (CEST) From: Wolfgang Bumiller To: pbs-devel@lists.proxmox.com Date: Tue, 28 Jul 2020 12:33:20 +0200 Message-Id: <20200728103321.16843-7-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200728103321.16843-1-w.bumiller@proxmox.com> References: <20200728103321.16843-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.038 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [compat.rs] Subject: [pbs-devel] [PATCH pxar 5/6] add entry v1 compatiblity test X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Jul 2020 10:33:26 -0000 Signed-off-by: Wolfgang Bumiller --- tests/compat.rs | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/compat.rs diff --git a/tests/compat.rs b/tests/compat.rs new file mode 100644 index 0000000..53e2d09 --- /dev/null +++ b/tests/compat.rs @@ -0,0 +1,136 @@ +//! Test for old timestamp format compatibility. + +use std::io::{self, Read}; + +use endian_trait::Endian; + +use pxar::{decoder, format, EntryKind}; + +fn write_raw_struct(output: &mut W, data: T) -> io::Result<()> { + let data = data.to_le(); + output.write_all(unsafe { + std::slice::from_raw_parts(&data as *const T as *const u8, std::mem::size_of::()) + }) +} + +fn write_data(output: &mut W, htype: u64, data: &[u8]) -> io::Result<()> +where + W: io::Write + ?Sized, +{ + let header = format::Header::with_content_size(htype, data.len() as u64); + header.check_header_size()?; + write_raw_struct(output, header)?; + output.write_all(data) +} + +fn write_entry(output: &mut W, htype: u64, data: T) -> io::Result<()> +where + T: Endian, + W: io::Write + ?Sized, +{ + let data = data.to_le(); + let data = unsafe { + std::slice::from_raw_parts(&data as *const T as *const u8, std::mem::size_of::()) + }; + write_data(output, htype, data) +} + +const MAY_1_2015_1530: u64 = 1430487000u64; + +const FILE_NAME: &str = "file.txt"; +const FILE_NAME_BYTES: &[u8] = b"file.txt\0"; +const FILE_CONTENT: &[u8] = b"This is a small text file.\n"; +const ROOT_STAT: format::Entry_V1 = format::Entry_V1 { + mode: format::mode::IFDIR | 0o755, + flags: 0, + uid: 1000, + gid: 1000, + mtime: MAY_1_2015_1530 * 1_000_000_000u64, +}; +const FILE_STAT: format::Entry_V1 = format::Entry_V1 { + mode: format::mode::IFREG | 0o644, + flags: 0, + uid: 1000, + gid: 1000, + mtime: MAY_1_2015_1530 * 1_000_000_000u64, +}; + +fn create_archive() -> io::Result> { + let mut out = Vec::new(); + + write_entry(&mut out, format::PXAR_ENTRY_V1, ROOT_STAT.clone())?; + + let file_offset = out.len(); + write_data(&mut out, format::PXAR_FILENAME, FILE_NAME_BYTES)?; + write_entry(&mut out, format::PXAR_ENTRY_V1, FILE_STAT.clone())?; + write_data(&mut out, format::PXAR_PAYLOAD, FILE_CONTENT)?; + + let mut gbt = Vec::new(); + write_raw_struct( + &mut gbt, + format::GoodbyeItem::new( + FILE_NAME.as_bytes(), + file_offset as u64, + FILE_CONTENT.len() as u64, + ), + )?; + + let gbt_size = gbt.len(); + write_raw_struct( + &mut gbt, + format::GoodbyeItem { + hash: format::PXAR_GOODBYE_TAIL_MARKER, + offset: out.len() as u64, + size: gbt_size as u64, + }, + )?; + + write_data(&mut out, format::PXAR_GOODBYE, &{ gbt })?; + + Ok(out) +} + +#[test] +fn test_archive() { + let archive = create_archive().expect("failed to create test archive"); + let mut input = &archive[..]; + let mut decoder = decoder::Decoder::from_std(&mut input).expect("failed to create decoder"); + + let item = decoder + .next() + .expect("missing root directory in test archive") + .expect("failed to extract root directory from test archive"); + match item.kind() { + EntryKind::Directory => (), + other => panic!("unexpected root entry in archive: {:?}", other), + } + assert_eq!(item.file_name(), ""); + assert_eq!(item.metadata().stat, ROOT_STAT.into()); + assert_eq!( + item.metadata().stat.mtime, + format::StatxTimestamp { + secs: MAY_1_2015_1530 as i64, + nanos: 0, + }, + ); + + let item = decoder + .next() + .expect("missing file entry in test archive") + .expect("failed to extract file entry from test archive"); + match item.kind() { + EntryKind::File { size, .. } => assert_eq!(*size, FILE_CONTENT.len() as u64), + other => panic!("unexpected file entry in archive: {:?}", other), + } + assert_eq!(item.file_name(), FILE_NAME); + assert_eq!(item.metadata().stat, FILE_STAT.into()); + let mut content = Vec::new(); + decoder + .contents() + .expect("failed to get contents for file entry") + .read_to_end(&mut content) + .expect("failed to read test file contents"); + assert_eq!(&content[..], FILE_CONTENT); + + assert!(decoder.next().is_none(), "expected end of test archive"); +} -- 2.20.1