Pārlūkot izejas kodu

Add struct size tests.

Caduser2020 4 gadi atpakaļ
vecāks
revīzija
20f25f2398
2 mainītis faili ar 51 papildinājumiem un 1 dzēšanām
  1. 1 1
      src/elf_sections.rs
  2. 50 0
      src/lib.rs

+ 1 - 1
src/elf_sections.rs

@@ -13,7 +13,7 @@ pub unsafe fn elf_sections_tag(tag: &Tag, offset: usize) -> ElfSectionsTag {
     assert_eq!(9, tag.typ);
     let es = ElfSectionsTag {
         inner: (tag as *const Tag).offset(1) as *const ElfSectionsTagInner,
-        offset: offset,
+        offset,
     };
     assert!((es.get().entry_size * es.get().shndx) <= tag.size);
     es

+ 50 - 0
src/lib.rs

@@ -466,6 +466,15 @@ mod tests {
         assert!(bi.command_line_tag().is_none());
     }
 
+    #[test]
+    /// Compile time test for `BootLoaderNameTag`.
+    fn name_tag_size() {
+        use BootLoaderNameTag;
+        unsafe {
+            core::mem::transmute::<[u8; 9], BootLoaderNameTag>([0u8; 9]);
+        }
+    }
+
     #[test]
     fn framebuffer_tag_rgb() {
         // direct RGB mode test:
@@ -587,6 +596,16 @@ mod tests {
         }
     }
 
+    #[test]
+    /// Compile time test for `FramebufferTag`.
+    fn framebuffer_tag_size() {
+        use crate::FramebufferTag;
+        unsafe {
+            // 24 for the start + 24 for `FramebufferType`.
+            core::mem::transmute::<[u8; 48], FramebufferTag>([0u8; 48]);
+        }
+    }
+
     #[test]
     fn vbe_info_tag() {
         //Taken from GRUB2 running in QEMU.
@@ -745,6 +764,16 @@ mod tests {
         }
     }
 
+    #[test]
+    /// Compile time test for `VBEInfoTag`.
+    fn vbe_info_tag_size() {
+        use VBEInfoTag;
+        unsafe {
+            // 16 for the start + 512 from `VBEControlInfo` + 256 from `VBEModeInfo`.
+            core::mem::transmute::<[u8; 784], VBEInfoTag>([0u8; 784]);
+        }
+    }
+
     #[test]
     fn grub2() {
         #[repr(C, align(8))]
@@ -1225,6 +1254,16 @@ mod tests {
         assert!(s.next().is_none());
     }
 
+    #[test]
+    /// Compile time test for `ElfSectionsTag`.
+    fn elf_sections_tag_size() {
+        use super::ElfSectionsTag;
+        unsafe {
+            // `ElfSectionsTagInner` is 12 bytes + 4 in the offset.
+            core::mem::transmute::<[u8; 16], ElfSectionsTag>([0u8; 16]);
+        }
+    }
+
     #[test]
     fn efi_memory_map() {
         use memory_map::EFIMemoryAreaType;
@@ -1291,4 +1330,15 @@ mod tests {
         let efi_mmap = boot_info.efi_memory_map_tag();
         assert!(efi_mmap.is_none());
     }
+
+    #[test]
+    /// Compile time test for `EFIMemoryMapTag`.
+    fn efi_memory_map_tag_size() {
+        use super::EFIMemoryMapTag;
+        unsafe {
+            // `EFIMemoryMapTag` is 16 bytes + `EFIMemoryDesc` is 40 bytes.
+            core::mem::transmute::<[u8; 56], EFIMemoryMapTag>([0u8; 56]);
+        }
+    }
+
 }