| 1 | #[macro_use] |
| 2 | extern crate quickcheck; |
| 3 | extern crate zbase32; |
| 4 | |
| 5 | mod common; |
| 6 | use common::*; |
| 7 | |
| 8 | use quickcheck::TestResult; |
| 9 | |
| 10 | quickcheck! { |
| 11 | fn test_recode(data: Vec<u8>) -> bool { |
| 12 | let encoded = zbase32::encode(&data, data.len() as u64 * 8); |
| 13 | let encoded_bytes = zbase32::encode_full_bytes(&data); |
| 14 | assert_eq!(encoded, encoded_bytes); |
| 15 | |
| 16 | let decoded = zbase32::decode(&encoded.as_bytes(), data.len() as u64 * 8).unwrap(); |
| 17 | let decoded_bytes = zbase32::decode_full_bytes(&encoded.as_bytes()).unwrap(); |
| 18 | assert_eq!(decoded, decoded_bytes); |
| 19 | data == decoded |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | quickcheck! { |
| 24 | fn try_decode_ok(data: Vec<u8>) -> TestResult { |
| 25 | if zbase32::validate(&data) { |
| 26 | TestResult::from_bool(zbase32::decode_full_bytes(&data).is_ok()) |
| 27 | } else { |
| 28 | TestResult::discard() |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | quickcheck! { |
| 34 | fn try_decode_err(data: Vec<u8>) -> TestResult { |
| 35 | if zbase32::validate(&data) { |
| 36 | TestResult::discard() |
| 37 | } else { |
| 38 | TestResult::from_bool(zbase32::decode_full_bytes(&data).is_err()) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | quickcheck! { |
| 44 | fn data_len_exceeds_bits_when_encoding(data: Vec<u8>, arbitrary: u8) -> TestResult { |
| 45 | let len = data.len() as u64 * 8 + 1 + arbitrary as u64; |
| 46 | TestResult::must_fail(move || { |
| 47 | zbase32::encode(&data, len); |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | quickcheck! { |
| 53 | fn data_len_exceeds_bits_when_ecoding(data: ZBaseEncodedData, arbitrary: u8) -> TestResult { |
| 54 | let len = data.as_bytes().len() as u64 * 5 + 1 + arbitrary as u64; |
| 55 | TestResult::must_fail(move || { |
| 56 | let _ = zbase32::decode(data.as_bytes(), len); |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | quickcheck! { |
| 62 | fn encode_too_long(data: Vec<u8>) -> () { |
| 63 | let rand_bits = rand_bit_length(data.len(), 8); |
| 64 | zbase32::encode(&data, rand_bits); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | quickcheck! { |
| 69 | fn recode_partial(data: ZBaseEncodedData) -> bool { |
| 70 | let rand_bits = rand_bit_length(data.len(), 5); |
| 71 | let decoded1 = zbase32::decode(&data.as_bytes(), rand_bits).unwrap(); |
| 72 | let encoded = zbase32::encode(&decoded1, rand_bits); |
| 73 | let decoded2 = zbase32::decode_str(&encoded, rand_bits).unwrap(); |
| 74 | decoded1 == decoded2 |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | quickcheck! { |
| 79 | fn decode(data: ZBaseEncodedData) -> bool { |
| 80 | zbase32::decode_full_bytes(&data.as_bytes()).is_ok() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | quickcheck! { |
| 85 | fn validate(data: ZBaseEncodedData) -> bool { |
| 86 | zbase32::validate(&data.as_bytes()) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | quickcheck! { |
| 91 | fn validate_str(data: ZBaseEncodedData) -> bool { |
| 92 | let data = String::from_utf8(data.into_bytes()).unwrap(); |
| 93 | zbase32::validate_str(&data) |
| 94 | } |
| 95 | } |