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 / block-buffer / tests / mod.rs
| 1 | use block_buffer::{ |
| 2 | generic_array::typenum::{U10, U16, U24, U4, U8}, |
| 3 | Block, EagerBuffer, LazyBuffer, |
| 4 | }; |
| 5 | |
| 6 | #[test] |
| 7 | fn test_eager_digest_pad() { |
| 8 | let mut buf = EagerBuffer::<U4>::default(); |
| 9 | let inputs = [ |
| 10 | &b"01234567"[..], |
| 11 | &b"89"[..], |
| 12 | &b"abcdefghij"[..], |
| 13 | &b"klmnopqrs"[..], |
| 14 | &b"tuv"[..], |
| 15 | &b"wx"[..], |
| 16 | ]; |
| 17 | let exp_blocks = [ |
| 18 | (0, &[b"0123", b"4567"][..]), |
| 19 | (2, &[b"89ab"][..]), |
| 20 | (2, &[b"cdef", b"ghij"][..]), |
| 21 | (3, &[b"klmn", b"opqr"][..]), |
| 22 | (4, &[b"stuv"][..]), |
| 23 | ]; |
| 24 | let exp_poses = [0, 2, 0, 1, 0, 2]; |
| 25 | |
| 26 | let mut n = 0; |
| 27 | for (i, input) in inputs.iter().enumerate() { |
| 28 | buf.digest_blocks(input, |b| { |
| 29 | let (j, exp) = exp_blocks[n]; |
| 30 | n += 1; |
| 31 | assert_eq!(i, j); |
| 32 | assert_eq!(b.len(), exp.len()); |
| 33 | assert!(b.iter().zip(exp.iter()).all(|v| v.0[..] == v.1[..])); |
| 34 | }); |
| 35 | assert_eq!(exp_poses[i], buf.get_pos()); |
| 36 | } |
| 37 | assert_eq!(buf.pad_with_zeros()[..], b"wx\0\0"[..]); |
| 38 | assert_eq!(buf.get_pos(), 0); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn test_lazy_digest_pad() { |
| 43 | let mut buf = LazyBuffer::<U4>::default(); |
| 44 | let inputs = [ |
| 45 | &b"01234567"[..], |
| 46 | &b"89"[..], |
| 47 | &b"abcdefghij"[..], |
| 48 | &b"klmnopqrs"[..], |
| 49 | ]; |
| 50 | let expected = [ |
| 51 | (0, &[b"0123"][..]), |
| 52 | (1, &[b"4567"][..]), |
| 53 | (2, &[b"89ab"][..]), |
| 54 | (2, &[b"cdef"][..]), |
| 55 | (3, &[b"ghij"][..]), |
| 56 | (3, &[b"klmn", b"opqr"][..]), |
| 57 | ]; |
| 58 | let exp_poses = [4, 2, 4, 1]; |
| 59 | |
| 60 | let mut n = 0; |
| 61 | for (i, input) in inputs.iter().enumerate() { |
| 62 | buf.digest_blocks(input, |b| { |
| 63 | let (j, exp) = expected[n]; |
| 64 | n += 1; |
| 65 | assert_eq!(i, j); |
| 66 | assert_eq!(b.len(), exp.len()); |
| 67 | assert!(b.iter().zip(exp.iter()).all(|v| v.0[..] == v.1[..])); |
| 68 | }); |
| 69 | assert_eq!(exp_poses[i], buf.get_pos()); |
| 70 | } |
| 71 | assert_eq!(buf.pad_with_zeros()[..], b"s\0\0\0"[..]); |
| 72 | assert_eq!(buf.get_pos(), 0); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn test_eager_set_data() { |
| 77 | let mut buf = EagerBuffer::<U4>::default(); |
| 78 | |
| 79 | let mut n = 0u8; |
| 80 | let mut gen = |blocks: &mut [Block<U4>]| { |
| 81 | for block in blocks { |
| 82 | block.iter_mut().for_each(|b| *b = n); |
| 83 | n += 1; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | let mut out = [0u8; 6]; |
| 88 | buf.set_data(&mut out, &mut gen); |
| 89 | assert_eq!(out, [0, 0, 0, 0, 1, 1]); |
| 90 | assert_eq!(buf.get_pos(), 2); |
| 91 | |
| 92 | let mut out = [0u8; 3]; |
| 93 | buf.set_data(&mut out, &mut gen); |
| 94 | assert_eq!(out, [1, 1, 2]); |
| 95 | assert_eq!(buf.get_pos(), 1); |
| 96 | |
| 97 | let mut out = [0u8; 3]; |
| 98 | buf.set_data(&mut out, &mut gen); |
| 99 | assert_eq!(out, [2, 2, 2]); |
| 100 | assert_eq!(n, 3); |
| 101 | assert_eq!(buf.get_pos(), 0); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | #[rustfmt::skip] |
| 106 | fn test_eager_paddings() { |
| 107 | let mut buf_be = EagerBuffer::<U8>::new(&[0x42]); |
| 108 | let mut buf_le = buf_be.clone(); |
| 109 | let mut out_be = Vec::<u8>::new(); |
| 110 | let mut out_le = Vec::<u8>::new(); |
| 111 | let len = 0x0001_0203_0405_0607; |
| 112 | buf_be.len64_padding_be(len, |block| out_be.extend(block)); |
| 113 | buf_le.len64_padding_le(len, |block| out_le.extend(block)); |
| 114 | |
| 115 | assert_eq!( |
| 116 | out_be, |
| 117 | [ |
| 118 | 0x42, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 119 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 120 | ], |
| 121 | ); |
| 122 | assert_eq!( |
| 123 | out_le, |
| 124 | [ |
| 125 | 0x42, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 126 | 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, |
| 127 | ], |
| 128 | ); |
| 129 | |
| 130 | let mut buf_be = EagerBuffer::<U10>::new(&[0x42]); |
| 131 | let mut buf_le = buf_be.clone(); |
| 132 | let mut out_be = Vec::<u8>::new(); |
| 133 | let mut out_le = Vec::<u8>::new(); |
| 134 | buf_be.len64_padding_be(len, |block| out_be.extend(block)); |
| 135 | buf_le.len64_padding_le(len, |block| out_le.extend(block)); |
| 136 | |
| 137 | assert_eq!( |
| 138 | out_be, |
| 139 | [0x42, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], |
| 140 | ); |
| 141 | assert_eq!( |
| 142 | out_le, |
| 143 | [0x42, 0x80, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00], |
| 144 | ); |
| 145 | |
| 146 | let mut buf = EagerBuffer::<U16>::new(&[0x42]); |
| 147 | let mut out = Vec::<u8>::new(); |
| 148 | let len = 0x0001_0203_0405_0607_0809_0a0b_0c0d_0e0f; |
| 149 | buf.len128_padding_be(len, |block| out.extend(block)); |
| 150 | assert_eq!( |
| 151 | out, |
| 152 | [ |
| 153 | 0x42, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 154 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 155 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 156 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 157 | ], |
| 158 | ); |
| 159 | |
| 160 | let mut buf = EagerBuffer::<U24>::new(&[0x42]); |
| 161 | let mut out = Vec::<u8>::new(); |
| 162 | let len = 0x0001_0203_0405_0607_0809_0a0b_0c0d_0e0f; |
| 163 | buf.len128_padding_be(len, |block| out.extend(block)); |
| 164 | assert_eq!( |
| 165 | out, |
| 166 | [ |
| 167 | 0x42, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 168 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 169 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 170 | ], |
| 171 | ); |
| 172 | |
| 173 | let mut buf = EagerBuffer::<U4>::new(&[0x42]); |
| 174 | let mut out = Vec::<u8>::new(); |
| 175 | buf.digest_pad(0xff, &[0x10, 0x11, 0x12], |block| out.extend(block)); |
| 176 | assert_eq!( |
| 177 | out, |
| 178 | [0x42, 0xff, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12], |
| 179 | ); |
| 180 | |
| 181 | let mut buf = EagerBuffer::<U4>::new(&[0x42]); |
| 182 | let mut out = Vec::<u8>::new(); |
| 183 | buf.digest_pad(0xff, &[0x10, 0x11], |block| out.extend(block)); |
| 184 | assert_eq!( |
| 185 | out, |
| 186 | [0x42, 0xff, 0x10, 0x11], |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn test_try_new() { |
| 192 | assert!(EagerBuffer::<U4>::try_new(&[0; 3]).is_ok()); |
| 193 | assert!(EagerBuffer::<U4>::try_new(&[0; 4]).is_err()); |
| 194 | assert!(LazyBuffer::<U4>::try_new(&[0; 4]).is_ok()); |
| 195 | assert!(LazyBuffer::<U4>::try_new(&[0; 5]).is_err()); |
| 196 | } |