Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
main-- committed Oct 5, 2017
1 parent caa8bda commit d79ba6d
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/cfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5333,6 +5333,124 @@ mod tests {
assert_eq!(result.unwrap_err().0, Error::NoUnwindInfoForAddress);
}

#[test]
fn test_eh_frame_hdr_unknown_version() {
let bases = BaseAddresses::default();
let buf = &[42];
let result = EhFrameHdr::new(buf, NativeEndian).parse(&bases, 8);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::UnknownVersion(42));
}

#[test]
fn test_eh_frame_hdr_omit_ehptr() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0xff).L8(0x03).L8(0x0b).L32(2)
.L32(10).L32(1)
.L32(20).L32(2).L32(0);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::UnexpectedNull);
}

#[test]
fn test_eh_frame_hdr_omit_count() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x0b).L8(0xff).L8(0x0b).L32(0x12345);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.eh_frame_ptr(), Pointer::Direct(0x12345));
assert!(result.table().is_none());
}

#[test]
fn test_eh_frame_hdr_omit_table() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x0b).L8(0x03).L8(0xff).L32(0x12345).L32(2);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.eh_frame_ptr(), Pointer::Direct(0x12345));
assert!(result.table().is_none());
}

#[test]
fn test_eh_frame_hdr_varlen_table() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x0b).L8(0x03).L8(0x01).L32(0x12345).L32(2);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.eh_frame_ptr(), Pointer::Direct(0x12345));
let table = result.table();
assert!(table.is_some());
let table = table.unwrap();
assert_eq!(table.lookup(0, &bases), Err(Error::VariableLengthSearchTable));
}

#[test]
fn test_eh_frame_hdr_indirect_length() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x0b).L8(0x83).L8(0x0b).L32(0x12345).L32(2);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::UnsupportedPointerEncoding);
}

#[test]
fn test_eh_frame_hdr_indirect_ptrs() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x8b).L8(0x03).L8(0x8b).L32(0x12345).L32(2)
.L32(10).L32(1)
.L32(20).L32(2);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.eh_frame_ptr(), Pointer::Indirect(0x12345));
let table = result.table();
assert!(table.is_some());
let table = table.unwrap();
assert_eq!(table.lookup(0, &bases), Err(Error::UnsupportedPointerEncoding));
}

#[test]
fn test_eh_frame_hdr_good() {
let section = Section::with_endian(Endian::Little)
.L8(1).L8(0x0b).L8(0x03).L8(0x0b).L32(0x12345).L32(2)
.L32(10).L32(1)
.L32(20).L32(2);
let section = section.get_contents().unwrap();
let bases = BaseAddresses::default();
let result = EhFrameHdr::new(&section, LittleEndian).parse(&bases, 8);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.eh_frame_ptr(), Pointer::Direct(0x12345));
let table = result.table();
assert!(table.is_some());
let table = table.unwrap();
assert_eq!(table.lookup(0, &bases), Ok(Pointer::Direct(1)));
assert_eq!(table.lookup(9, &bases), Ok(Pointer::Direct(1)));
assert_eq!(table.lookup(10, &bases), Ok(Pointer::Direct(1)));
assert_eq!(table.lookup(11, &bases), Ok(Pointer::Direct(1)));
assert_eq!(table.lookup(19, &bases), Ok(Pointer::Direct(1)));
assert_eq!(table.lookup(20, &bases), Ok(Pointer::Direct(2)));
assert_eq!(table.lookup(21, &bases), Ok(Pointer::Direct(2)));
assert_eq!(table.lookup(100000, &bases), Ok(Pointer::Direct(2)));
}

#[test]
fn test_eh_frame_stops_at_zero_length() {
let section = Section::with_endian(Endian::Little).L32(0);
Expand Down

0 comments on commit d79ba6d

Please sign in to comment.