misc: Eliminate use of assert!((...).is_ok())

Asserting on .is_ok()/.is_err() leads to hard to debug failures (as if
the test fails, it will only say "assertion failed: false". We replace
these with `.unwrap()`, which also prints the exact error variant that
was unexpectedly encountered (we can to this these days thanks to
efforts to implement Display and Debug for our error types). If the
assert!((...).is_ok()) was followed by an .unwrap() anyway, we just drop
the assert.

Inspired by and quoted from @roypat.

Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
This commit is contained in:
Ruoqing He
2024-09-30 19:07:39 +00:00
committed by Rob Bradford
parent 83bcf2a1ff
commit 297236a7c0
25 changed files with 244 additions and 281 deletions

View File

@@ -146,16 +146,16 @@ mod tests {
#[test]
fn test_mac_addr() {
// too long
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").is_err());
MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").unwrap_err();
// invalid hex
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:ax").is_err());
MacAddr::parse_str("aa:aa:aa:aa:aa:ax").unwrap_err();
// single digit mac address component should be invalid
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:b").is_err());
MacAddr::parse_str("aa:aa:aa:aa:aa:b").unwrap_err();
// components with more than two digits should also be invalid
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").is_err());
MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").unwrap_err();
let mac = MacAddr::parse_str("12:34:56:78:9a:BC").unwrap();
@@ -171,12 +171,12 @@ mod tests {
let src2 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
let src3 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
assert!(MacAddr::from_bytes(&src1[..]).is_err());
MacAddr::from_bytes(&src1[..]).unwrap_err();
let x = MacAddr::from_bytes(&src2[..]).unwrap();
assert_eq!(x.to_string(), String::from("01:02:03:04:05:06"));
assert!(MacAddr::from_bytes(&src3[..]).is_err());
MacAddr::from_bytes(&src3[..]).unwrap_err();
}
#[test]

View File

@@ -605,10 +605,8 @@ mod tests {
let ip_addr: net::Ipv4Addr = (*tap_ip_guard).parse().unwrap();
let netmask: net::Ipv4Addr = SUBNET_MASK.parse().unwrap();
let ret = tap.set_ip_addr(ip_addr);
assert!(ret.is_ok());
let ret = tap.set_netmask(netmask);
assert!(ret.is_ok());
tap.set_ip_addr(ip_addr).unwrap();
tap.set_netmask(netmask).unwrap();
}
#[test]
@@ -626,8 +624,7 @@ mod tests {
let _tap_ip_guard = TAP_IP_LOCK.lock().unwrap();
let tap = Tap::new(1).unwrap();
let ret = tap.enable();
assert!(ret.is_ok());
tap.enable().unwrap();
}
#[test]
@@ -657,10 +654,7 @@ mod tests {
// In theory, this could actually loop forever if something keeps sending data through the
// tap interface, but it's highly unlikely.
while found_packet_sz.is_none() {
let result = tap.read(&mut buf);
assert!(result.is_ok());
let size = result.unwrap();
let size = tap.read(&mut buf).unwrap();
// We skip the first 10 bytes because the IFF_VNET_HDR flag is set when the interface
// is created, and the legacy header is 10 bytes long without a certain flag which
@@ -719,8 +713,8 @@ mod tests {
// leave the vnet hdr as is
pnet_build_packet(&mut buf[10..], mac, payload);
assert!(tap.write(&buf[..]).is_ok());
assert!(tap.flush().is_ok());
tap.write_all(&buf).unwrap();
tap.flush().unwrap();
let (channel_tx, channel_rx) = mpsc::channel();