Skip to content

Commit

Permalink
rust: fix unit test link error on Rust 1.70
Browse files Browse the repository at this point in the history
Rust 1.70 appears to now link code on both branches of `if cfg!(test)`
now causing Rust unit tests to fail as that pattern was used to
disable functions only available when linked with the Suricata C code.

To work-around this issue, provide two versions of the `new` function,
one for unit tests and one when running as an application.
  • Loading branch information
jasonish authored and victorjulien committed Jun 12, 2023
1 parent c90f67a commit 68d0d6c
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions rust/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@

use crate::applayer::StreamSlice;
use crate::core::Flow;
#[cfg(not(test))]
use crate::core::STREAM_TOSERVER;
use crate::core::Direction;

#[cfg(not(test))]
#[repr(C)]
struct CFrame {
_private: [u8; 0],
}

// Defined in app-layer-register.h
extern {
#[cfg(not(test))]
fn AppLayerFrameNewByRelativeOffset(
flow: *const Flow, stream_slice: *const StreamSlice, frame_start_rel: u32, len: i64,
dir: i32, frame_type: u8,
) -> *const CFrame;
fn AppLayerFrameAddEventById(flow: *const Flow, dir: i32, id: i64, event: u8);
fn AppLayerFrameSetLengthById(flow: *const Flow, dir: i32, id: i64, len: i64);
fn AppLayerFrameSetTxIdById(flow: *const Flow, dir: i32, id: i64, tx_id: u64);
#[cfg(not(test))]
fn AppLayerFrameGetId(frame: *const CFrame) -> i64;
}

Expand All @@ -49,40 +53,46 @@ impl std::fmt::Debug for Frame {
}

impl Frame {
#[cfg(not(test))]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn new(
flow: *const Flow, stream_slice: &StreamSlice, frame_start: &[u8], frame_len: i64,
frame_type: u8,
) -> Option<Self> {
let offset = frame_start.as_ptr() as usize - stream_slice.as_slice().as_ptr() as usize;
SCLogDebug!("offset {} stream_slice.len() {} frame_start.len() {}", offset, stream_slice.len(), frame_start.len());
// If running Rust unit tests this won't be compiled and None will be returned, as we don't
// have the Suricata C code available for linkage.
if cfg!(not(test)) {
let frame = unsafe {
AppLayerFrameNewByRelativeOffset(
flow,
stream_slice,
offset as u32,
frame_len,
(stream_slice.flags() & STREAM_TOSERVER == 0).into(),
frame_type,
)
};
let id = unsafe { AppLayerFrameGetId(frame) };
if id > 0 {
Some(Self {
id,
direction: Direction::from(stream_slice.flags()),
})
} else {
None
}
let frame = unsafe {
AppLayerFrameNewByRelativeOffset(
flow,
stream_slice,
offset as u32,
frame_len,
(stream_slice.flags() & STREAM_TOSERVER == 0).into(),
frame_type,
)
};
let id = unsafe { AppLayerFrameGetId(frame) };
if id > 0 {
Some(Self {
id,
direction: Direction::from(stream_slice.flags()),
})
} else {
None
}
}

/// A variation of `new` for use when running Rust unit tests as
/// the C functions for building a frame are not available for
/// linkage.
#[cfg(test)]
pub fn new(
_flow: *const Flow, _stream_slice: &StreamSlice, _frame_start: &[u8], _frame_len: i64,
_frame_type: u8,
) -> Option<Self> {
None
}

/// Conversion function to get the direction in the correct form for the
/// C frame methods which takes direction as a u32 value of 0 or 1 rather
/// than the flag value used internally by Frame.
Expand Down

0 comments on commit 68d0d6c

Please sign in to comment.