Skip to content

Commit 41d9a56

Browse files
committed
Unit tests
Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
1 parent 93ae874 commit 41d9a56

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

src/lib/efivar/efivar/efi_variables.rs

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ impl<const SIZE: usize> TryFrom<File> for EfiVariableBuffer<SIZE> {
9494
});
9595
match handle.read_vectored(io_vectors.as_mut_slice()) {
9696
Ok(bytes_read) => {
97-
eprintln!("read bytes {}", bytes_read);
9897
if bytes_read == 0 {
9998
return Err(format!(
10099
"Corrupt variable. Read {} byte(s) but expected to read {}.",
@@ -333,29 +332,41 @@ mod tests {
333332
static STRIO_BUFFER: RefCell<VecDeque<u8>> = RefCell::new(VecDeque::new());
334333
}
335334

336-
pub struct File {}
335+
pub struct File {
336+
read_: fn(&mut [u8]) -> std::io::Result<usize>,
337+
}
337338

338339
impl File {
340+
fn new() -> Self {
341+
Self {
342+
read_: |dst: &mut [u8]| STRIO_BUFFER.with(|b| (*b).borrow_mut().read(dst)),
343+
}
344+
}
345+
339346
pub fn open<P: AsRef<Path>>(_path: P) -> std::io::Result<Self> {
340-
Ok(File {})
347+
Ok(File::new())
348+
}
349+
}
350+
351+
impl Default for File {
352+
fn default() -> Self {
353+
Self::new()
341354
}
342355
}
343356

344357
impl Read for File {
345358
fn read(&mut self, dst: &mut [u8]) -> std::io::Result<usize> {
346-
STRIO_BUFFER.with(|b| (*b).borrow_mut().read(dst))
359+
(self.read_)(dst)
347360
}
348361

349362
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> std::io::Result<usize> {
350363
let mut total_bytes_read: usize = 0;
351364
STRIO_BUFFER.with(|tl_b| {
352-
let mut sb = (*tl_b).borrow_mut();
353365
bufs.iter_mut().filter(|db| !db.is_empty()).for_each(|db| {
354-
let bytes_read = min(db.len(), sb.len());
355-
db[..bytes_read]
356-
.copy_from_slice(sb.drain(..bytes_read).collect::<Vec<u8>>().as_slice());
366+
let bytes_read = min(db.len(), (*tl_b).borrow().len());
367+
(self.read_)(&mut db[..bytes_read]).unwrap();
357368
total_bytes_read += bytes_read;
358-
});
369+
})
359370
});
360371
Ok(total_bytes_read)
361372
}
@@ -422,7 +433,7 @@ mod tests {
422433

423434
#[test]
424435
fn efi_variable_buffer_32_read_empty() {
425-
let file = File {};
436+
let file = File::new();
426437
let var = Efi32VariableBuffer::try_from(file);
427438

428439
assert_eq!(
@@ -439,7 +450,7 @@ mod tests {
439450
sb.write_all("1".as_bytes()).unwrap();
440451
});
441452
}
442-
let file = File {};
453+
let file = File::new();
443454
let var = Efi32VariableBuffer::try_from(file);
444455

445456
assert_eq!(
@@ -456,7 +467,33 @@ mod tests {
456467
sb.write_all(&[0xff; 2077]).unwrap();
457468
});
458469
}
459-
let file = File {};
470+
let file = File::new();
471+
let var = Efi32VariableBuffer::try_from(file);
472+
473+
assert_eq!(
474+
(*var.err().unwrap()).to_string(),
475+
"Corrupt variable. Read 2077 byte(s) but expected to read 2076."
476+
);
477+
}
478+
479+
#[test]
480+
fn efi_variable_buffer_32_multiple_reads() {
481+
{
482+
STRIO_BUFFER.with(|tl_b| {
483+
let mut sb = (*tl_b).borrow_mut();
484+
sb.write_all(&[0xff; 2077]).unwrap();
485+
});
486+
}
487+
let file = File {
488+
read_: |dst: &mut [u8]| {
489+
if dst.len() > 0 {
490+
dst[0] = 0xff;
491+
Ok(1)
492+
} else {
493+
Ok(0)
494+
}
495+
},
496+
};
460497
let var = Efi32VariableBuffer::try_from(file);
461498

462499
assert_eq!(
@@ -473,7 +510,7 @@ mod tests {
473510
sb.write_all("1".as_bytes()).unwrap();
474511
});
475512
}
476-
let file = File {};
513+
let file = File::new();
477514
let var = Efi64VariableBuffer::try_from(file);
478515

479516
assert_eq!(
@@ -490,7 +527,33 @@ mod tests {
490527
sb.write_all(&[0xff; 2085]).unwrap();
491528
});
492529
}
493-
let file = File {};
530+
let file = File::new();
531+
let var = Efi64VariableBuffer::try_from(file);
532+
533+
assert_eq!(
534+
(*var.err().unwrap()).to_string(),
535+
"Corrupt variable. Read 2085 byte(s) but expected to read 2084."
536+
);
537+
}
538+
539+
#[test]
540+
fn efi_variable_buffer_64_multiple_reads() {
541+
{
542+
STRIO_BUFFER.with(|tl_b| {
543+
let mut sb = (*tl_b).borrow_mut();
544+
sb.write_all(&[0xff; 2084]).unwrap();
545+
});
546+
}
547+
let file = File {
548+
read_: |dst: &mut [u8]| {
549+
if dst.len() > 0 {
550+
dst[0] = 0xff;
551+
Ok(1)
552+
} else {
553+
Ok(0)
554+
}
555+
},
556+
};
494557
let var = Efi64VariableBuffer::try_from(file);
495558

496559
assert_eq!(

0 commit comments

Comments
 (0)