aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / base64ct / tests / common / mod.rs
| 1 | //! Common testing functionality |
| 2 | |
| 3 | /// Base64 test vector |
| 4 | pub struct TestVector { |
| 5 | pub raw: &'static [u8], |
| 6 | pub b64: &'static str, |
| 7 | } |
| 8 | |
| 9 | /// Generate test suite for a particular Base64 flavor |
| 10 | #[macro_export] |
| 11 | macro_rules! impl_tests { |
| 12 | ($encoding:ty) => { |
| 13 | use base64ct::{Encoding, Error}; |
| 14 | |
| 15 | #[test] |
| 16 | fn encode_test_vectors() { |
| 17 | let mut buf = [0u8; 1024]; |
| 18 | |
| 19 | for vector in TEST_VECTORS { |
| 20 | let out = <$encoding>::encode(vector.raw, &mut buf).unwrap(); |
| 21 | assert_eq!(<$encoding>::encoded_len(vector.raw), vector.b64.len()); |
| 22 | assert_eq!(vector.b64, &out[..]); |
| 23 | |
| 24 | #[cfg(feature = "alloc")] |
| 25 | { |
| 26 | let out = <$encoding>::encode_string(vector.raw); |
| 27 | assert_eq!(vector.b64, &out[..]); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn decode_test_vectors() { |
| 34 | let mut buf = [0u8; 1024]; |
| 35 | |
| 36 | for vector in TEST_VECTORS { |
| 37 | let out = <$encoding>::decode(vector.b64, &mut buf).unwrap(); |
| 38 | assert_eq!(vector.raw, &out[..]); |
| 39 | |
| 40 | let n = vector.b64.len(); |
| 41 | buf[..n].copy_from_slice(vector.b64.as_bytes()); |
| 42 | let out = <$encoding>::decode_in_place(&mut buf[..n]).unwrap(); |
| 43 | assert_eq!(vector.raw, out); |
| 44 | |
| 45 | #[cfg(feature = "alloc")] |
| 46 | { |
| 47 | let out = <$encoding>::decode_vec(vector.b64).unwrap(); |
| 48 | assert_eq!(vector.raw, &out[..]); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn encode_and_decode_various_lengths() { |
| 55 | let data = [b'X'; 64]; |
| 56 | let mut inbuf = [0u8; 1024]; |
| 57 | let mut outbuf = [0u8; 1024]; |
| 58 | |
| 59 | for i in 0..data.len() { |
| 60 | let encoded = <$encoding>::encode(&data[..i], &mut inbuf).unwrap(); |
| 61 | |
| 62 | // Make sure it round trips |
| 63 | let decoded = <$encoding>::decode(encoded, &mut outbuf).unwrap(); |
| 64 | assert_eq!(decoded, &data[..i]); |
| 65 | |
| 66 | let elen = <$encoding>::encode(&data[..i], &mut inbuf).unwrap().len(); |
| 67 | let buf = &mut inbuf[..elen]; |
| 68 | let decoded = <$encoding>::decode_in_place(buf).unwrap(); |
| 69 | assert_eq!(decoded, &data[..i]); |
| 70 | |
| 71 | #[cfg(feature = "alloc")] |
| 72 | { |
| 73 | let encoded = <$encoding>::encode_string(&data[..i]); |
| 74 | let decoded = <$encoding>::decode_vec(&encoded).unwrap(); |
| 75 | assert_eq!(decoded, &data[..i]); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | } |