Skip to content

Commit 1c9e29d

Browse files
Add no leak test.
1 parent c395658 commit 1c9e29d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ build = "build.rs"
1414
hashbrown = "0.15"
1515
log = "0.4"
1616

17+
[dev-dependencies]
18+
mockalloc = "0.1.2"
19+
1720
[profile.release]
1821
lto = true
1922
opt-level = 3

src/hostcalls.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub fn set_buffer(
137137
}
138138
}
139139

140+
#[cfg(not(test))]
140141
extern "C" {
141142
fn proxy_get_header_map_pairs(
142143
map_type: MapType,
@@ -145,6 +146,15 @@ extern "C" {
145146
) -> Status;
146147
}
147148

149+
#[cfg(test)]
150+
fn proxy_get_header_map_pairs(
151+
map_type: MapType,
152+
return_map_data: *mut *mut u8,
153+
return_map_size: *mut usize,
154+
) -> Status {
155+
alloc_tests::proxy_get_header_map_pairs(map_type, return_map_data, return_map_size)
156+
}
157+
148158
pub fn get_map(map_type: MapType) -> Result<Vec<(String, String)>, Status> {
149159
unsafe {
150160
let mut return_data: *mut u8 = null_mut();
@@ -1147,6 +1157,78 @@ pub fn increment_metric(metric_id: u32, offset: i64) -> Result<(), Status> {
11471157
}
11481158
}
11491159

1160+
#[cfg(test)]
1161+
mod alloc_tests {
1162+
use crate::types::*;
1163+
1164+
use mockalloc::Mockalloc;
1165+
use std::alloc::{alloc, Layout, System};
1166+
1167+
#[global_allocator]
1168+
static ALLOCATOR: Mockalloc<System> = Mockalloc(System);
1169+
1170+
// mock extern call
1171+
pub fn proxy_get_header_map_pairs(
1172+
_map_type: MapType,
1173+
return_map_data: *mut *mut u8,
1174+
return_map_size: *mut usize,
1175+
) -> Status {
1176+
let layout = Layout::array::<u8>(SERIALIZED_MAP.len()).unwrap();
1177+
unsafe {
1178+
*return_map_data = alloc(layout);
1179+
*return_map_size = SERIALIZED_MAP.len();
1180+
std::ptr::copy(
1181+
SERIALIZED_MAP.as_ptr(),
1182+
*return_map_data,
1183+
SERIALIZED_MAP.len(),
1184+
);
1185+
}
1186+
Status::Ok
1187+
}
1188+
1189+
#[rustfmt::skip]
1190+
static SERIALIZED_MAP: &[u8] = &[
1191+
// num entries
1192+
4, 0, 0, 0,
1193+
// len (":method", "GET")
1194+
7, 0, 0, 0, 3, 0, 0, 0,
1195+
// len (":path", "/bytes/1")
1196+
5, 0, 0, 0, 8, 0, 0, 0,
1197+
// len (":authority", "httpbin.org")
1198+
10, 0, 0, 0, 11, 0, 0, 0,
1199+
// len ("Powered-By", "proxy-wasm")
1200+
10, 0, 0, 0, 10, 0, 0, 0,
1201+
// ":method"
1202+
58, 109, 101, 116, 104, 111, 100, 0,
1203+
// "GET"
1204+
71, 69, 84, 0,
1205+
// ":path"
1206+
58, 112, 97, 116, 104, 0,
1207+
// "/bytes/1"
1208+
47, 98, 121, 116, 101, 115, 47, 49, 0,
1209+
// ":authority"
1210+
58, 97, 117, 116, 104, 111, 114, 105, 116, 121, 0,
1211+
// "httpbin.org"
1212+
104, 116, 116, 112, 98, 105, 110, 46, 111, 114, 103, 0,
1213+
// "Powered-By"
1214+
80, 111, 119, 101, 114, 101, 100, 45, 66, 121, 0,
1215+
// "proxy-wasm"
1216+
112, 114, 111, 120, 121, 45, 119, 97, 115, 109, 0,
1217+
];
1218+
1219+
#[mockalloc::test]
1220+
fn get_map_no_leaks() {
1221+
let result = super::get_map(MapType::HttpRequestHeaders);
1222+
assert!(result.is_ok());
1223+
}
1224+
1225+
#[mockalloc::test]
1226+
fn get_map_bytes_no_leaks() {
1227+
let result = super::get_map_bytes(MapType::HttpRequestHeaders);
1228+
assert!(result.is_ok());
1229+
}
1230+
}
1231+
11501232
mod utils {
11511233
use crate::types::Bytes;
11521234
use std::convert::TryFrom;

0 commit comments

Comments
 (0)