Skip to content

Commit

Permalink
add test module
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Dec 19, 2024
1 parent 3497bb1 commit 475736d
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 264 deletions.
3 changes: 3 additions & 0 deletions rust/src/nasl/builtin/raw_ip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use packet_forgery::PacketForgery;
pub use packet_forgery::PacketForgeryError;
use thiserror::Error;

#[cfg(test)]
mod tests;

#[derive(Debug, Error)]
pub enum RawIpError {
#[error("Failed to get local MAC address.")]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/raw_ip/packet_forgery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn display_packet(vector: &[u8]) {
}

/// Copy from a slice in safe way, performing the necessary test to avoid panicking
fn safe_copy_from_slice(
pub fn safe_copy_from_slice(
d_buf: &mut [u8],
d_init: usize,
d_fin: usize,
Expand Down
117 changes: 52 additions & 65 deletions rust/src/nasl/builtin/raw_ip/tests/frame_forgery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,59 @@
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

//! Defines NASL frame forgery and arp functions
#[cfg(test)]
mod tests {
use nasl_builtin_raw_ip::RawIp;
use nasl_builtin_std::{nasl_std_functions, ContextFactory};
use crate::nasl::interpreter::test_utils::TestBuilder;
use crate::nasl::syntax::NaslValue;
use crate::nasl::test_prelude::*;

fn setup() -> TestBuilder<crate::nasl::syntax::NoOpLoader, storage::DefaultDispatcher> {
let t = TestBuilder::default();
let mut context = ContextFactory::default();
context.functions = nasl_std_functions();
context.functions.add_set(RawIp);
t.with_context(context)
}

#[test]
fn get_local_mac_address_from_ip() {
let mut t = setup();
t.ok(
"get_local_mac_address_from_ip(127.0.0.1);",
"00:00:00:00:00:00",
);
t.ok(
r#"get_local_mac_address_from_ip("127.0.0.1");"#,
"00:00:00:00:00:00",
);
t.ok(
r#"get_local_mac_address_from_ip("::1");"#,
"00:00:00:00:00:00",
);
}
#[test]
fn get_local_mac_address_from_ip() {
let mut t = TestBuilder::default();
t.ok(
"get_local_mac_address_from_ip(127.0.0.1);",
"00:00:00:00:00:00",
);
t.ok(
r#"get_local_mac_address_from_ip("127.0.0.1");"#,
"00:00:00:00:00:00",
);
t.ok(
r#"get_local_mac_address_from_ip("::1");"#,
"00:00:00:00:00:00",
);
}

#[test]
fn forge_frame() {
let mut t = setup();
t.run(r#"src = raw_string(0x01, 0x02, 0x03, 0x04, 0x05, 0x06);"#);
t.run(r#"dst = "0a:0b:0c:0d:0e:0f";"#);
t.ok(r#"a = forge_frame(src_haddr: src , dst_haddr: dst,ether_proto: 0x0806, payload: "abcd" );"#
, vec![
0x0au8, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x06,
0x61, 0x62, 0x63, 0x64
]);
t.run(r#"dump_frame(frame:a);"#);
}
#[test]
fn forge_frame() {
let mut t = TestBuilder::default();
t.run(r#"src = raw_string(0x01, 0x02, 0x03, 0x04, 0x05, 0x06);"#);
t.run(r#"dst = "0a:0b:0c:0d:0e:0f";"#);
t.ok(r#"a = forge_frame(src_haddr: src , dst_haddr: dst,ether_proto: 0x0806, payload: "abcd" );"#
, vec![
0x0au8, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x06,
0x61, 0x62, 0x63, 0x64
]);
t.run(r#"dump_frame(frame:a);"#);
}

#[test]
fn send_frame() {
let mut t = setup();
t.run(r#"src = raw_string(0x01, 0x02, 0x03, 0x04, 0x05, 0x06);"#);
t.run(r#"dst = "0a:0b:0c:0d:0e:0f";"#);
t.ok(r#"a = forge_frame(src_haddr: src , dst_haddr: dst, ether_proto: 0x0806, payload: "abcd");"#
, vec![
0x0au8, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x06,
0x61, 0x62, 0x63, 0x64
]);
t.ok(
r#"send_frame(frame: a, pcap_active: FALSE);"#,
NaslValue::Null,
);
t.ok(
r#"send_frame(frame: a, pcap_active: TRUE);"#,
NaslValue::Null,
);
t.ok(
r#"send_frame(frame: a, pcap_active: TRUE, filter: "arp", timeout: 2);"#,
NaslValue::Null,
);
}
#[test]
fn send_frame() {
let mut t = TestBuilder::default();
t.run(r#"src = raw_string(0x01, 0x02, 0x03, 0x04, 0x05, 0x06);"#);
t.run(r#"dst = "0a:0b:0c:0d:0e:0f";"#);
t.ok(r#"a = forge_frame(src_haddr: src , dst_haddr: dst, ether_proto: 0x0806, payload: "abcd");"#
, vec![
0x0au8, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x06,
0x61, 0x62, 0x63, 0x64
]);
t.ok(
r#"send_frame(frame: a, pcap_active: FALSE);"#,
NaslValue::Null,
);
t.ok(
r#"send_frame(frame: a, pcap_active: TRUE);"#,
NaslValue::Null,
);
t.ok(
r#"send_frame(frame: a, pcap_active: TRUE, filter: "arp", timeout: 2);"#,
NaslValue::Null,
);
}

Loading

0 comments on commit 475736d

Please sign in to comment.