Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration_tests_sv2 docs cleanup #1340

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions roles/tests-integration/lib/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,26 @@ type MsgType = u8;
enum SnifferError {
DownstreamClosed,
UpstreamClosed,
MessageInterrupted,
}

/// Allows to intercept messages sent between two roles.
///
/// Can be useful for testing purposes, as it allows to assert that the roles have sent specific
/// messages in a specific order and to inspect the messages details.
///
Comment on lines +38 to +40
Copy link
Collaborator Author

@plebhash plebhash Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this paragraph was just moved from the bottom to the top, as it feels more like an introduction about Sniffer

it was a bit confusing to read it on the bottom, as it wasn't clear what it was referring to (Sniffer in general vs some internal detail)

/// The downstream (or client) role connects to the [`Sniffer`] `listening_address` and the
/// [`Sniffer`] connects to the `upstream` server. This way, the Sniffer can intercept messages sent
/// between the downstream and upstream roles. The downstream will send its messages to the
/// [`Sniffer`] which will save those in the `messages_from_downstream` aggregator and forward them
/// to the upstream role. When a response is received it is saved in `messages_from_upstream` and
/// forwarded to the downstream role. Both `messages_from_downstream` and `messages_from_upstream`
/// can be accessed as FIFO queues.
Comment on lines -41 to -45
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was re-rewitten below, as it implies that messages from upstream are always a response, which is not necessarily true

///
/// In order to alter the messages sent between the roles, the [`Sniffer::intercept_messages`]
/// field can be used. It will look for the [`InterceptMessage::expected_message_type`] in the
/// specified [`InterceptMessage::direction`] and replace it with
/// [`InterceptMessage::response_message`].
/// between the downstream and upstream roles.
///
/// If `break_on` is set to `true`, the [`Sniffer`] will stop the communication after sending the
/// response message.
/// Messages received from downstream are stored in the `messages_from_downstream` aggregator and
/// forward them to the upstream role. Alternatively, messages received from upstream are stored in
/// the `messages_from_upstream` and forwarded to the downstream role. Both
/// `messages_from_downstream` and `messages_from_upstream` aggregators can be accessed as FIFO
/// queues via [`Sniffer::next_message_from_downstream`] and
/// [`Sniffer::next_message_from_upstream`], respectively.
///
/// Can be useful for testing purposes, as it allows to assert that the roles have sent specific
/// messages in a specific order and to inspect the messages details.
/// In order to replace the messages sent between the roles, a set of [`InterceptMessage`] can be
/// used in [`Sniffer::new`].
#[derive(Debug, Clone)]
pub struct Sniffer {
identifier: String,
Expand All @@ -65,29 +62,27 @@ pub struct Sniffer {
intercept_messages: Vec<InterceptMessage>,
}

/// Allows [`Sniffer`] to replace some intercepted message before forwarding it.
#[derive(Debug, Clone)]
pub struct InterceptMessage {
direction: MessageDirection,
expected_message_type: MsgType,
response_message: PoolMessages<'static>,
response_message_type: MsgType,
break_on: bool,
replacement_message: PoolMessages<'static>,
replacement_message_type: MsgType,
}

impl InterceptMessage {
pub fn new(
direction: MessageDirection,
expected_message_type: MsgType,
response_message: PoolMessages<'static>,
response_message_type: MsgType,
break_on: bool,
replacement_message: PoolMessages<'static>,
replacement_message_type: MsgType,
) -> Self {
Self {
direction,
expected_message_type,
response_message,
response_message_type,
break_on,
replacement_message,
replacement_message_type,
}
}
}
Expand Down Expand Up @@ -234,21 +229,16 @@ impl Sniffer {
let channel_msg = false;
let frame = StandardEitherFrame::<AnyMessage<'_>>::Sv2(
Sv2Frame::from_message(
intercept_message.response_message.clone(),
intercept_message.response_message_type,
intercept_message.replacement_message.clone(),
intercept_message.replacement_message_type,
extension_type,
channel_msg,
)
.expect("Failed to create the frame"),
);
downstream_messages
.add_message(msg_type, intercept_message.response_message.clone());
.add_message(msg_type, intercept_message.replacement_message.clone());
let _ = send.send(frame).await;
if intercept_message.break_on {
return Err(SnifferError::MessageInterrupted);
} else {
continue;
}
}
}

Expand Down Expand Up @@ -276,21 +266,16 @@ impl Sniffer {
let channel_msg = false;
let frame = StandardEitherFrame::<AnyMessage<'_>>::Sv2(
Sv2Frame::from_message(
intercept_message.response_message.clone(),
intercept_message.response_message_type,
intercept_message.replacement_message.clone(),
intercept_message.replacement_message_type,
extension_type,
channel_msg,
)
.expect("Failed to create the frame"),
);
upstream_messages
.add_message(msg_type, intercept_message.response_message.clone());
.add_message(msg_type, intercept_message.replacement_message.clone());
let _ = send.send(frame).await;
if intercept_message.break_on {
return Err(SnifferError::MessageInterrupted);
} else {
continue;
}
}
}
if send.send(frame).await.is_err() {
Expand Down
38 changes: 27 additions & 11 deletions roles/tests-integration/tests/sniffer_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ use roles_logic_sv2::{
use sniffer::{InterceptMessage, MessageDirection};
use std::convert::TryInto;

// this test aims to assert that Sniffer is able to intercept and replace some message
// sniffer_a replaces a SetupConnectionSuccess from TP with a SetupConnectionError directed at Pool
// sniffer_b asserts that Pool is about to receive a SetupConnectionError
// TP -> sniffer_a -> sniffer_b -> Pool
#[tokio::test]
async fn test_sniffer_interrupter() {
let (_tp, tp_addr) = start_template_provider(None).await;
async fn test_sniffer_intercept() {
use const_sv2::MESSAGE_TYPE_SETUP_CONNECTION_SUCCESS;
let message =

let (_tp, tp_addr) = start_template_provider(None).await;
let message_replacement =
PoolMessages::Common(CommonMessages::SetupConnectionError(SetupConnectionError {
flags: 0,
error_code: "unsupported-feature-flags"
Expand All @@ -20,16 +25,27 @@ async fn test_sniffer_interrupter() {
.try_into()
.unwrap(),
}));
let interrupt_msgs = InterceptMessage::new(
let intercept = InterceptMessage::new(
MessageDirection::ToDownstream,
MESSAGE_TYPE_SETUP_CONNECTION_SUCCESS,
message,
message_replacement,
MESSAGE_TYPE_SETUP_CONNECTION_ERROR,
true,
);
let (sniffer, sniffer_addr) =
start_sniffer("".to_string(), tp_addr, false, Some(vec![interrupt_msgs])).await;
let _ = start_pool(Some(sniffer_addr)).await;
assert_common_message!(&sniffer.next_message_from_downstream(), SetupConnection);
assert_common_message!(&sniffer.next_message_from_upstream(), SetupConnectionError);

// this sniffer will replace SetupConnectionSuccess with SetupConnectionError
let (_sniffer_a, sniffer_a_addr) =
start_sniffer("A".to_string(), tp_addr, false, Some(vec![intercept])).await;

// this sniffer will assert SetupConnectionSuccess was correctly replaced with
// SetupConnectionError
let (sniffer_b, sniffer_b_addr) =
start_sniffer("B".to_string(), sniffer_a_addr, false, None).await;

let _ = start_pool(Some(sniffer_b_addr)).await;

// assert sniffer_a functionality of replacing messages work as expected (goal of this test)
assert_common_message!(
&sniffer_b.next_message_from_upstream(),
SetupConnectionError
);
}
Loading