-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
237 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Copyright (c) The Holo Core Contributors | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
|
||
// the ipv4 Packet header details that will be used in IPV4 tests | ||
// - version: 4 | ||
// - header length: 20 | ||
// - tos: 0 | ||
// - total length: 52 | ||
// - identification: 0x6cb8 | ||
// - flags: 0b0100000000000000 | ||
// - ttl: 51 | ||
// - protocol: 6 [TCP] | ||
// - checksum: 0xfe74 | ||
// - source_address: 208.115.231.106 | ||
// - destination_address: 192.168.100.16 | ||
fn valid_pkt_data() -> [u8; 20] { | ||
[ | ||
0x45, 0x00, 0x00, 0x34, | ||
0x6c, 0xb8, 0x40, 0x00, | ||
0x33, 0x06, 0xfe, 0x74, | ||
0xd0, 0x73, 0xe7, 0x6a, | ||
0xc0, 0xa8, 0x61, 0x10 | ||
] | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// Copyright (c) The Holo Core Contributors | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
use bytes::Buf; | ||
use holo_vrrp::packet::{VRRPPacket, DecodeError, PacketLengthError}; | ||
|
||
/* | ||
generally in the packet tests we will use the following packet structure | ||
with slight modifications to be done on a per test basis(the changes will | ||
be specified) | ||
Valid VRRP packet with the following params: | ||
- ver_type: 21 [version: 2, header_type: 1] | ||
- vrid: 51 | ||
- priority: 101 | ||
- count_ip: 1 | ||
- auth_type: 0 | ||
- adver_int: 1 | ||
- checksum: 0x54db | ||
- ip_addresses: [192.168.100.100] | ||
- auth_data: 0 | ||
- auth_data2: 0 | ||
*/ | ||
fn valid_pkt_data() -> [u8; 20] { | ||
[ | ||
0x21, 0x33, 0x65, 0x01, | ||
0x00, 0x01, 0x54, 0xbd, | ||
0xc0, 0xa8, 0x64, 0x64, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00 | ||
] | ||
} | ||
|
||
// test the valid packet being decoded | ||
#[test] | ||
fn test_valid_decoding(){ | ||
let vrrp_pkt = VRRPPacket::decode(&valid_pkt_data()); | ||
assert!(vrrp_pkt.is_ok()); | ||
} | ||
|
||
|
||
// make the VRRP packet too short. We will use 10 bytes. | ||
#[test] | ||
fn test_pkt_too_short() { | ||
let vrrp_pkt = VRRPPacket::decode(&[0x00; 10]); | ||
assert_eq!( | ||
vrrp_pkt, | ||
Err(DecodeError::PacketLengthError(PacketLengthError::TooShort(10))) | ||
); | ||
} | ||
|
||
// the length of the entire packet is too long | ||
#[test] | ||
fn test_pkt_too_long() { | ||
let vrrp_pkt = VRRPPacket::decode(&[0x00; 100]); | ||
assert_eq!( | ||
vrrp_pkt, | ||
Err(DecodeError::PacketLengthError(PacketLengthError::TooLong(100))) | ||
); | ||
} | ||
|
||
// test when the packet is too long in length | ||
// we set count_ip as 17 | ||
#[test] | ||
fn test_count_ip_too_high() { | ||
let data: &mut [u8] = &mut valid_pkt_data(); | ||
data[3] = 17; | ||
let vrrp_pkt = VRRPPacket::decode(data); | ||
assert_eq!( | ||
vrrp_pkt, | ||
Err(DecodeError::PacketLengthError(PacketLengthError::AddressCount(17))) | ||
); | ||
} | ||
|
||
|
||
|
||
// let us claim we have 3 ip addresses yet we have only one | ||
// we set count_ip as 17 | ||
#[test] | ||
fn test_count_ip_corrupted() { | ||
let data: &mut [u8] = &mut valid_pkt_data(); | ||
data[3] = 3; | ||
let vrrp_pkt = VRRPPacket::decode(data); | ||
assert_eq!( | ||
vrrp_pkt, | ||
Err(DecodeError::PacketLengthError(PacketLengthError::CorruptedLength)) | ||
); | ||
} |