zbase32-0.1.2/.gitignore010064400017500001750000000000271330752073000131730ustar0000000000000000target Cargo.lock *.bk zbase32-0.1.2/.gitlab-ci.yml010064400017500001750000000016251340552156600136530ustar0000000000000000image: rust:latest variables: QUICKCHECK_GENERATOR_SIZE: 4096 QUICKCHECK_MAX_TESTS: 100000 QUICKCHECK_TESTS: 5000 QUICKCHECK_MIN_TESTS_PASSED: 3 stable: &default before_script: - &pybase32 | apt-get -y update \ && apt-get -y install python3-dev \ && git clone https://github.com/tobyn/zbase32.git pyzbase32 \ && cd pyzbase32 \ && python3 ./setup.py install --user script: - cargo rustc --lib --verbose -- -D warnings - rustup component add clippy - cargo clippy -- -D clippy::all -D clippy::pedantic -D warnings - cargo test --verbose --features python_tests beta: <<: *default before_script: - *pybase32 - rustup toolchain install beta - rustup default beta nightly: before_script: - *pybase32 - rustup toolchain install nightly - rustup default nightly script: - cargo test --verbose --features unstable,python_tests allow_failure: true zbase32-0.1.2/Cargo.toml.orig010064400017500001750000000007011340552621200140710ustar0000000000000000[package] name = "zbase32" version = "0.1.2" description = "Implementation of zbase32." documentation = "https://docs.rs/zbase32" repository = "https://gitlab.com/pgerber/zbase32-rust" authors = ["Peter Gerber "] keywords = ["zbase32"] license = "LGPL-3.0+" [badges] gitlab = { repository = "pgerber/zbase32-rust" } [dev-dependencies] cpython = "0.2" quickcheck = "0.7" rand = "0.6" [features] python_tests = [] unstable = [] zbase32-0.1.2/Cargo.toml0000644000000020050000000000000103370ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "zbase32" version = "0.1.2" authors = ["Peter Gerber "] description = "Implementation of zbase32." documentation = "https://docs.rs/zbase32" keywords = ["zbase32"] license = "LGPL-3.0+" repository = "https://gitlab.com/pgerber/zbase32-rust" [dev-dependencies.cpython] version = "0.2" [dev-dependencies.quickcheck] version = "0.7" [dev-dependencies.rand] version = "0.6" [features] python_tests = [] unstable = [] [badges.gitlab] repository = "pgerber/zbase32-rust" zbase32-0.1.2/LICENSE010064400017500001750000000167431330752073000122240ustar0000000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. zbase32-0.1.2/README.md010064400017500001750000000005361330752073000124670ustar0000000000000000# Implementation of zbase32 in Rust [![crates.io](https://meritbadge.herokuapp.com/zbase32)](https://crates.io/crates/zbase32) This is an implementation of the human-oriented base-32 encoding called [zbase32](https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt). [The Documentation has some more details.](https://docs.rs/zbase32) zbase32-0.1.2/src/lib.rs010064400017500001750000000362031340552574600131260ustar0000000000000000//! Encoding and decoding of zbase32. //! //! This is an implementation of the human-oriented base-32 encoding called //! [zbase32](https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt). #![cfg_attr(all(test, feature = "unstable"), feature(test))] /// Alphabet used by zbase32 pub const ALPHABET: &[u8; 32] = b"ybndrfg8ejkmcpqxot1uwisza345h769"; #[rustfmt::skip] const CONVERSION_TABLE: &[Option; 256] = &[ /* 0 */ None, None, None, None, None, /* 5 */ None, None, None, None, None, /* 10 */ None, None, None, None, None, /* 15 */ None, None, None, None, None, /* 20 */ None, None, None, None, None, /* 25 */ None, None, None, None, None, /* 30 */ None, None, None, None, None, /* 35 */ None, None, None, None, None, /* 40 */ None, None, None, None, None, /* 45 */ None, None, None, None, Some(0x12), /* 50 */ None, Some(0x19), Some(0x1a), Some(0x1b), Some(0x1e), /* 55 */ Some(0x1d), Some(0x07), Some(0x1f), None, None, /* 60 */ None, None, None, None, None, /* 65 */ None, None, None, None, None, /* 70 */ None, None, None, None, None, /* 75 */ None, None, None, None, None, /* 80 */ None, None, None, None, None, /* 85 */ None, None, None, None, None, /* 90 */ None, None, None, None, None, /* 95 */ None, None, Some(0x18), Some(0x01), Some(0x0c), /* 100 */ Some(0x03), Some(0x08), Some(0x05), Some(0x06), Some(0x1c), /* 105 */ Some(0x15), Some(0x09), Some(0x0a), None, Some(0x0b), /* 110 */ Some(0x02), Some(0x10), Some(0x0d), Some(0x0e), Some(0x04), /* 115 */ Some(0x16), Some(0x11), Some(0x13), None, Some(0x14), /* 120 */ Some(0x0f), Some(0x00), Some(0x17), None, None, /* 125 */ None, None, None, None, None, /* 130 */ None, None, None, None, None, /* 135 */ None, None, None, None, None, /* 140 */ None, None, None, None, None, /* 145 */ None, None, None, None, None, /* 150 */ None, None, None, None, None, /* 155 */ None, None, None, None, None, /* 160 */ None, None, None, None, None, /* 165 */ None, None, None, None, None, /* 170 */ None, None, None, None, None, /* 175 */ None, None, None, None, None, /* 180 */ None, None, None, None, None, /* 185 */ None, None, None, None, None, /* 190 */ None, None, None, None, None, /* 195 */ None, None, None, None, None, /* 200 */ None, None, None, None, None, /* 205 */ None, None, None, None, None, /* 210 */ None, None, None, None, None, /* 215 */ None, None, None, None, None, /* 220 */ None, None, None, None, None, /* 225 */ None, None, None, None, None, /* 230 */ None, None, None, None, None, /* 235 */ None, None, None, None, None, /* 240 */ None, None, None, None, None, /* 245 */ None, None, None, None, None, /* 250 */ None, None, None, None, None, /* 255 */ None ]; #[inline] fn value_of_digit(digit: u8) -> Result { match CONVERSION_TABLE[digit as usize] { Some(v) => Ok(v), None => Err("not a zbase32 digit"), } } /// Decode first N `bits` of given zbase32 encoded data /// /// # Panic /// /// Panics if `zbase32` decoded is shorter than N `bits`. /// /// # Examples /// /// ``` /// use zbase32; /// /// assert_eq!(zbase32::decode(b"o", 1).unwrap(), &[0x80]); /// ``` pub fn decode(zbase32: &[u8], bits: u64) -> Result, &'static str> { assert!(zbase32.len() as u64 * 5 >= bits, "zbase32 slice too short"); #[allow(clippy::cast_possible_truncation)] let capacity = if bits % 8 == 0 { bits / 8 } else { bits / 8 + 1 } as usize; let mut result = Vec::with_capacity(capacity); let mut bits_remaining = bits; let mut buffer_size: u8 = 0; let mut buffer: u16 = !0; for digit in zbase32 { let value = value_of_digit(*digit)?; buffer = (buffer << 5) | u16::from(value); buffer_size += 5; if bits_remaining < 8 && u64::from(buffer_size) >= bits_remaining { break; } if buffer_size >= 8 { #[allow(clippy::cast_possible_truncation)] let byte = (buffer >> (buffer_size - 8)) as u8; result.push(byte); bits_remaining -= 8; buffer_size -= 8; } } if bits_remaining > 0 { #[allow(clippy::cast_possible_truncation)] let trim_right = buffer_size - bits_remaining as u8; buffer >>= trim_right; buffer_size -= trim_right; #[allow(clippy::cast_possible_truncation)] let byte = (buffer << (8_u8 - buffer_size)) as u8; result.push(byte); } debug_assert_eq!(capacity, result.len()); Ok(result) } /// Decode given zbase32 encoded string /// /// Just like `decode` but doesn't allow decoding with bit precision. /// /// This decodes full bytes. For instance, if you have `b"yy"`, you'll get one /// byte back. `b"yy"` can enode 10 bits (2 * 5) which is truncated at the next /// lower byte boundary. /// /// # Examples /// /// ``` /// use zbase32; /// /// assert_eq!(zbase32::decode_full_bytes(b"qb1ze3m1").unwrap(), b"peter"); /// ``` #[inline] pub fn decode_full_bytes(zbase: &[u8]) -> Result, &'static str> { let size = zbase.len() as u64 * 5; decode(zbase, size / 8 * 8) } /// Decode first N `bits` of given zbase32 encoded string /// /// # Panic /// /// Panics if `zbase32` decoded is shorter than N `bits`. /// /// # Examples /// /// ``` /// use zbase32; /// /// assert_eq!(zbase32::decode_str("o", 1).unwrap(), &[0x80]); /// ``` #[inline] pub fn decode_str(zbase32: &str, bits: u64) -> Result, &'static str> { decode(zbase32.as_bytes(), bits) } /// Decode given zbase32 encoded string /// /// Just like `decode_str` but doesn't allow decoding with bit precision. /// /// # Examples /// /// ``` /// use zbase32; /// /// assert_eq!(zbase32::decode_full_bytes_str("qb1ze3m1").unwrap(), b"peter"); /// ``` #[inline] pub fn decode_full_bytes_str(zbase32: &str) -> Result, &'static str> { decode_full_bytes(zbase32.as_bytes()) } /// Encode first N `bits` with zbase32. /// /// # Panics /// /// Panics if `data` is shorter than N `bits`. /// /// # Examples /// /// ``` /// use zbase32; /// /// assert_eq!(zbase32::encode(b"testdata", 64), "qt1zg7drcf4gn"); /// ``` /// pub fn encode(data: &[u8], bits: u64) -> String { assert!(data.len() as u64 * 8 >= bits, "slice too short"); #[allow(clippy::cast_possible_truncation)] let capacity = if bits % 5 == 0 { bits / 5 } else { bits / 5 + 1 } as usize; let mut result = Vec::with_capacity(capacity); let mut bits_remaining = bits; let mut bit_offset: u8 = 16; let mut remaining = data; let mut buffer = !0; while bits_remaining > 0 { if bit_offset >= 8 { if let Some((first, others)) = remaining.split_first() { buffer = buffer << 8 | u16::from(*first); remaining = others; bit_offset -= 8; } } let unused_bits = 5_u64.saturating_sub(bits_remaining); #[allow(clippy::cast_possible_truncation)] let index = (buffer >> (unused_bits as u8 + 16 - 5 - bit_offset) << unused_bits) & 0x1f; result.push(ALPHABET[index as usize]); bit_offset += 5; bits_remaining -= 5 - unused_bits; } debug_assert_eq!(capacity, result.len()); unsafe { String::from_utf8_unchecked(result) } } /// Encode full bytes using zbase32. /// /// Just like `encode` but doesn't allow encoding with bit precision. /// /// # Examples /// /// ``` /// use zbase32; /// /// let data = "Just an arbitrary sentence."; /// assert_eq!(zbase32::encode_full_bytes(data.as_bytes()), /// "jj4zg7bycfznyam1cjwzehubqjh1yh5fp34gk5udcwzy"); /// ``` #[inline] pub fn encode_full_bytes(data: &[u8]) -> String { encode(data, data.len() as u64 * 8) } /// Check if `data` is valid zbase32 encoded bytes /// /// # Examples /// /// ``` /// use zbase32; /// /// assert!(zbase32::validate(b"y1")); /// assert!(!zbase32::validate(b"A")); /// ``` pub fn validate(data: &[u8]) -> bool { data.iter().all(|i| value_of_digit(*i).is_ok()) } /// Check if `data` is valid zbase32 encoded string /// /// # Examples /// /// ``` /// use zbase32; /// /// assert!(zbase32::validate_str("y1")); /// assert!(!zbase32::validate_str("A")); /// ``` #[inline(always)] pub fn validate_str(data: &str) -> bool { validate(data.as_bytes()) } #[cfg(test)] mod tests { #[cfg(feature = "unstable")] extern crate test; extern crate rand; use super::*; #[cfg(feature = "unstable")] use tests::rand::seq::SliceRandom; #[cfg(feature = "unstable")] use tests::rand::Rng; #[rustfmt::skip] const TEST_DATA: &[(u64, &str, &[u8])] = &[ (0, "", &[]), (1, "y", &[0x00]), (1, "o", &[0x80]), (2, "e", &[0x40]), (2, "a", &[0xc0]), (8, "yy", &[0x00]), (10, "yy", &[0x00, 0x00]), (10, "on", &[0x80, 0x80]), (20, "tqre", &[0x8b, 0x88, 0x80]), (24, "6n9hq", &[0xf0, 0xbf, 0xc7]), (24, "4t7ye", &[0xd4, 0x7a, 0x04]), (30, "6im5sd", &[0xf5, 0x57, 0xbb, 0x0c]), (160, "ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6, 0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6, 0x75, 0xbe, 0x77, 0xdf]) ]; const INVALID_TEST_DATA: &[&str] = &["ybndrfg8ejkmcpqxot1uwisza345H769", "bnℕe", "uv", "l"]; #[cfg(feature = "unstable")] const ONE_MIB: usize = 1048576; #[test] fn test_decode() { for &(bits, zbase32, data) in TEST_DATA { assert_eq!(decode(zbase32.as_bytes(), bits).unwrap(), data); } } #[test] #[rustfmt::skip] fn test_decode_full_bytes() { let test_data: &[(&str, &[u8])] = &[ ("9", &[]), ("y9", &[0x07]), ("6n9hq", &[0xf0, 0xbf, 0xc7]), ("4t7ye", &[0xd4, 0x7a, 0x04]), ("ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6, 0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6, 0x75, 0xbe, 0x77, 0xdf]) ]; for &(zbase32, data) in test_data { assert_eq!(decode_full_bytes_str(zbase32).unwrap(), data); } } #[test] fn test_decode_invalid_digits() { for string in INVALID_TEST_DATA.iter() { assert!(decode(string.as_bytes(), string.as_bytes().len() as u64 * 5).is_err()); assert!(decode_full_bytes(string.as_bytes()).is_err()); } } #[test] fn test_decode_superfluous_bits() { assert_eq!(decode(b"999", 1).unwrap(), &[0x80]); assert_eq!(decode(b"4t7yj", 24).unwrap(), &[0xd4, 0x7a, 0x04]); assert_eq!(decode(b"4t7ye9", 25).unwrap(), &[0xd4, 0x7a, 0x04, 0x00]); assert_eq!(decode(b"gyh3", 18).unwrap(), &[0x30, 0x39, 0x80]); } #[test] #[should_panic(expected = "zbase32 slice too short")] fn test_decode_short_slice() { decode(b"oyoy", 4 * 5 + 1).unwrap(); } #[test] fn test_encode() { for &(bits, zbase32, data) in TEST_DATA { assert_eq!(encode(data, bits), zbase32); } } #[test] fn test_encode_superfluous_bits() { assert_eq!(encode(&[0xff, 0xff], 1), "o"); assert_eq!(encode(&[0xd4, 0x7a, 0x04, 0xff], 24), "4t7ye"); } #[test] #[should_panic(expected = "slice too short")] fn test_encode_short_slice() { encode(b"1234", 4 * 8 + 1); } #[test] fn test_encode_full_bytes() { for &(_, zbase32, data) in TEST_DATA.iter().filter(|&&(i, _, _)| i % 8 == 0) { assert_eq!(encode_full_bytes(data), zbase32); } } #[test] fn test_validate() { for &(_, zbase32, _) in TEST_DATA { assert!(validate(zbase32.as_bytes())); assert!(validate_str(zbase32)); } } #[test] fn test_validate_invalid() { for string in INVALID_TEST_DATA.iter() { assert!(!validate(string.as_bytes())); assert!(!validate_str(string)); } } #[test] fn test_valid_and_invalid_chars() { for char in 0..=255 { let bytes = &[char]; if ALPHABET.contains(&char) { assert_eq!(ALPHABET[value_of_digit(char).unwrap() as usize], char); assert_eq!(encode(&decode(bytes, 5).unwrap(), 5).as_bytes(), bytes); assert!(validate(bytes)); } else { assert!(value_of_digit(char).is_err()); assert!(decode_full_bytes(bytes).is_err()); assert!(!validate(bytes)); } } } #[cfg(feature = "unstable")] #[bench] fn decode_one_mib(b: &mut test::Bencher) { let data = random_encoded_data(ONE_MIB); b.iter(|| decode_full_bytes(&data).unwrap()) } #[cfg(feature = "unstable")] #[bench] fn encode_one_mib(b: &mut test::Bencher) { let data = random_data(ONE_MIB); b.iter(|| encode_full_bytes(&data)) } #[cfg(feature = "unstable")] #[bench] fn decode_five_bytes(b: &mut test::Bencher) { let data = random_encoded_data(5); b.iter(|| decode_full_bytes(&data).unwrap()) } #[cfg(feature = "unstable")] #[bench] fn encode_five_bytes(b: &mut test::Bencher) { let data = random_data(5); b.iter(|| encode_full_bytes(&data)) } #[cfg(feature = "unstable")] #[bench] fn validate_one_mib(b: &mut test::Bencher) { let data = random_encoded_data(ONE_MIB); b.iter(|| validate(&data)) } #[cfg(feature = "unstable")] fn random_data(bytes: usize) -> Vec { let mut data = vec![0_u8; bytes]; rand::thread_rng().fill(&mut data[..]); data } #[cfg(feature = "unstable")] fn random_encoded_data(bytes: usize) -> Vec { let mut gen = rand::thread_rng(); (0..bytes * 8 / 5) .map(|_| *ALPHABET.choose(&mut gen).unwrap()) .collect() } } zbase32-0.1.2/tests/common/mod.rs010064400017500001750000000017371340552457300150030ustar0000000000000000extern crate rand; extern crate quickcheck; extern crate zbase32; use self::quickcheck::{Arbitrary, Gen}; use self::rand::seq::SliceRandom; use self::rand::Rng; #[derive(Clone, Debug)] pub struct ZBaseEncodedData(Vec); impl Arbitrary for ZBaseEncodedData { fn arbitrary(g: &mut G) -> Self { let len = g.gen_range(0, 256); let content = (0..len) .map(|_| *zbase32::ALPHABET.choose(g).unwrap()) .collect(); ZBaseEncodedData(content) } } impl ZBaseEncodedData { pub fn as_bytes(&self) -> &[u8] { &self.0 } #[allow(unused)] pub fn into_bytes(self) -> Vec { self.0 } pub fn len(&self) -> usize { self.0.len() } } pub fn rand_bit_length(units: usize, bits_per_unit: u64) -> u64 { let bits = if units > 0 { rand::thread_rng().gen_range(0, units as u64 * bits_per_unit) } else { 0 }; println!("random bit length: {}", bits); bits } zbase32-0.1.2/tests/fuzz.rs010064400017500001750000000051421330752073000137140ustar0000000000000000#[macro_use] extern crate quickcheck; extern crate zbase32; mod common; use common::*; use quickcheck::TestResult; quickcheck! { fn test_recode(data: Vec) -> bool { let encoded = zbase32::encode(&data, data.len() as u64 * 8); let encoded_bytes = zbase32::encode_full_bytes(&data); assert_eq!(encoded, encoded_bytes); let decoded = zbase32::decode(&encoded.as_bytes(), data.len() as u64 * 8).unwrap(); let decoded_bytes = zbase32::decode_full_bytes(&encoded.as_bytes()).unwrap(); assert_eq!(decoded, decoded_bytes); data == decoded } } quickcheck! { fn try_decode_ok(data: Vec) -> TestResult { if zbase32::validate(&data) { TestResult::from_bool(zbase32::decode_full_bytes(&data).is_ok()) } else { TestResult::discard() } } } quickcheck! { fn try_decode_err(data: Vec) -> TestResult { if zbase32::validate(&data) { TestResult::discard() } else { TestResult::from_bool(zbase32::decode_full_bytes(&data).is_err()) } } } quickcheck! { fn data_len_exceeds_bits_when_encoding(data: Vec, arbitrary: u8) -> TestResult { let len = data.len() as u64 * 8 + 1 + arbitrary as u64; TestResult::must_fail(move || { zbase32::encode(&data, len); }) } } quickcheck! { fn data_len_exceeds_bits_when_ecoding(data: ZBaseEncodedData, arbitrary: u8) -> TestResult { let len = data.as_bytes().len() as u64 * 5 + 1 + arbitrary as u64; TestResult::must_fail(move || { let _ = zbase32::decode(data.as_bytes(), len); }) } } quickcheck! { fn encode_too_long(data: Vec) -> () { let rand_bits = rand_bit_length(data.len(), 8); zbase32::encode(&data, rand_bits); } } quickcheck! { fn recode_partial(data: ZBaseEncodedData) -> bool { let rand_bits = rand_bit_length(data.len(), 5); let decoded1 = zbase32::decode(&data.as_bytes(), rand_bits).unwrap(); let encoded = zbase32::encode(&decoded1, rand_bits); let decoded2 = zbase32::decode_str(&encoded, rand_bits).unwrap(); decoded1 == decoded2 } } quickcheck! { fn decode(data: ZBaseEncodedData) -> bool { zbase32::decode_full_bytes(&data.as_bytes()).is_ok() } } quickcheck! { fn validate(data: ZBaseEncodedData) -> bool { zbase32::validate(&data.as_bytes()) } } quickcheck! { fn validate_str(data: ZBaseEncodedData) -> bool { let data = String::from_utf8(data.into_bytes()).unwrap(); zbase32::validate_str(&data) } } zbase32-0.1.2/tests/pyzbase32.rs010064400017500001750000000047041330752073000145430ustar0000000000000000#![cfg(feature = "python_tests")] extern crate cpython; #[macro_use] extern crate quickcheck; extern crate zbase32; mod common; use common::*; use cpython::{Python, PyDict, PyResult, PyBytes}; quickcheck! { fn encode(input: Vec) -> bool { let bits = rand_bit_length(input.len(), 8); let rust = zbase32::encode(&input, bits); let python = py_encode(&input[..], Some(bits)).unwrap(); rust == python } } quickcheck! { fn decode(input: ZBaseEncodedData) -> bool { let bits = rand_bit_length(input.len(), 5); let rust = zbase32::decode(input.as_bytes(), bits).unwrap(); let python = py_decode(&input.as_bytes()[..], Some(bits)).unwrap(); rust == python } } quickcheck! { fn encode_bytes(input: Vec) -> bool { let rust = zbase32::encode_full_bytes(&input); let python = py_encode(&input[..], None).unwrap(); rust == python } } quickcheck! { fn decode_bytes(input: ZBaseEncodedData) -> bool { let rust = zbase32::decode_full_bytes(input.as_bytes()).unwrap(); let python = py_decode(&input.as_bytes()[..], None).unwrap(); rust == python } } fn py_encode(input: &[u8], bits: Option) -> PyResult { let gil = Python::acquire_gil(); let py = gil.python(); let locals = PyDict::new(py); locals.set_item(py, "pyzbase32", py.import("pyzbase32")?)?; locals.set_item(py, "input", PyBytes::new(py, input))?; let result: String = if let Some(bits) = bits { locals.set_item(py, "bits", bits)?; py.eval( "pyzbase32.encode(input, bits).decode()", None, Some(&locals), ) } else { py.eval( "pyzbase32.encode_bytes(input).decode()", None, Some(&locals), ) }? .extract(py)?; Ok(result) } fn py_decode(input: &[u8], bits: Option) -> PyResult> { let gil = Python::acquire_gil(); let py = gil.python(); let locals = PyDict::new(py); locals.set_item(py, "pyzbase32", py.import("pyzbase32")?)?; locals.set_item(py, "input", PyBytes::new(py, input))?; let result: PyBytes = if let Some(bits) = bits { locals.set_item(py, "bits", bits)?; py.eval("pyzbase32.decode(input, bits)", None, Some(&locals)) } else { py.eval("pyzbase32.decode_bytes(input)", None, Some(&locals)) }? .extract(py)?; Ok(Vec::from(result.data(py))) } zbase32-0.1.2/.cargo_vcs_info.json0000644000000001120000000000000123360ustar00{ "git": { "sha1": "7d3c27e1f860c08e46202349c8625df6a62f9fdd" } }