From 4e7adbc290ad0a0ac3d40cfdff39bae7df7c7b8b Mon Sep 17 00:00:00 2001 From: Ximon Eighteen <3304436+ximon18@users.noreply.github.com> Date: Thu, 27 Feb 2025 10:52:12 +0100 Subject: [PATCH] Return more information about the fake server response entry matched by Stelline. --- src/stelline/connection.rs | 5 ++--- src/stelline/dgram.rs | 5 ++--- src/stelline/server.rs | 19 +++++++++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/stelline/connection.rs b/src/stelline/connection.rs index 9c057bc1..3724b44e 100644 --- a/src/stelline/connection.rs +++ b/src/stelline/connection.rs @@ -104,10 +104,9 @@ impl AsyncWrite for Connection { (), ); - let opt_reply = do_server(&req, &self.stelline, &self.step_value); - if opt_reply.is_some() { + if let Some((opt_reply, _indices)) = do_server(&req, &self.stelline, &self.step_value) { // Do we need to support more than one reply? - self.reply = opt_reply; + self.reply = Some(opt_reply); let opt_waker = self.waker.take(); if let Some(waker) = opt_waker { waker.wake(); diff --git a/src/stelline/dgram.rs b/src/stelline/dgram.rs index 4594568a..c4966c43 100644 --- a/src/stelline/dgram.rs +++ b/src/stelline/dgram.rs @@ -114,12 +114,11 @@ impl AsyncDgramSend for DgramConnection { mock_transport_ctx.clone(), (), ); - let opt_reply = do_server(&req, &self.stelline, &self.step_value); let len = buf.len(); - if opt_reply.is_some() { + if let Some((opt_reply, _indices)) = do_server(&req, &self.stelline, &self.step_value) { // Do we need to support more than one reply? let mut reply = self.reply.lock().unwrap(); - *reply = opt_reply; + *reply = Some(opt_reply); drop(reply); let mut waker = self.waker.lock().unwrap(); let opt_waker = (*waker).take(); diff --git a/src/stelline/server.rs b/src/stelline/server.rs index 0f8a316c..84c1f097 100644 --- a/src/stelline/server.rs +++ b/src/stelline/server.rs @@ -16,11 +16,20 @@ use super::matches::match_msg; use super::parse_stelline; use super::parse_stelline::{Adjust, Reply, Stelline}; +/// Gets a matching Stelline range entry. +/// +/// Entries inside a RANGE_BEGIN/RANGE_END block within a Stelline file define +/// queries to match and if matched the response to serve to that query. +/// +/// The _last_ matching entry is returned, as apparently that "works better if +/// the (Stelline) RPL is written with a recursive resolver in mind", along +/// with the zero based index of the range the entry was found in, and the +/// zero based index of the entry within that range. pub fn do_server<'a, Oct, Target>( req: &'a Request, stelline: &Stelline, step_value: &CurrStepValue, -) -> Option> +) -> Option<(AdditionalBuilder, (usize, usize))> where ::Range<'a>: Clone, Oct: Clone + Octets + 'a + Send + Sync, @@ -30,6 +39,7 @@ where let ranges = &stelline.scenario.ranges; let step = step_value.get(); let mut opt_entry = None; + let mut last_found_indices: Option<(usize, usize)> = None; let msg = req.message(); // Take the last entry. That works better if the RPL is written with @@ -39,7 +49,7 @@ where msg.header().opcode(), msg.first_question().unwrap().qtype() ); - for range in ranges { + for (range_idx, range) in ranges.iter().enumerate() { trace!( "Checking against range {} <= {}", range.start_value, @@ -48,10 +58,11 @@ where if step < range.start_value || step > range.end_value { continue; } - for entry in &range.entry { + for (entry_idx, entry) in range.entry.iter().enumerate() { if match_msg(entry, req, true) { trace!("Match found"); opt_entry = Some(entry); + last_found_indices = Some((range_idx, entry_idx)) } } } @@ -59,7 +70,7 @@ where match opt_entry { Some(entry) => { let reply = do_adjust(entry, msg); - Some(reply) + Some((reply, last_found_indices.unwrap())) } None => { trace!("No matching reply found");