Skip to content

Commit 913e38c

Browse files
inikulinRReverser
authored andcommitted
Get rid of callbacks in tests. Remove panic tests due to rust-lang/rust#48088
1 parent 991c536 commit 913e38c

File tree

1 file changed

+82
-79
lines changed

1 file changed

+82
-79
lines changed

ffi/src/lib.rs

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub unsafe extern "C" fn wirefilter_create_scheme() -> *mut Scheme {
8282
}
8383

8484
#[no_mangle]
85-
pub extern "C" fn wirefilter_free_scheme(scheme: &mut Scheme) {
85+
pub extern "C" fn wirefilter_free_scheme(scheme: *mut Scheme) {
8686
drop(unsafe { Box::from_raw(scheme) });
8787
}
8888

@@ -136,7 +136,7 @@ pub unsafe extern "C" fn wirefilter_create_execution_context<'a>() -> *mut Execu
136136
}
137137

138138
#[no_mangle]
139-
pub extern "C" fn wirefilter_free_execution_context(exec_context: &mut ExecutionContext) {
139+
pub extern "C" fn wirefilter_free_execution_context(exec_context: *mut ExecutionContext) {
140140
drop(unsafe { Box::from_raw(exec_context) });
141141
}
142142

@@ -203,89 +203,109 @@ mod ffi_test {
203203
use wirefilter::op::{CombiningOp, OrderingOp};
204204
use wirefilter::types::RhsValue;
205205

206-
fn test_with_scheme<F: Fn(&mut Scheme)>(test_fn: F) {
207-
let scheme = unsafe { &mut *wirefilter_create_scheme() };
206+
fn create_scheme() -> Box<Scheme> {
207+
let mut scheme = unsafe { Box::from_raw(wirefilter_create_scheme()) };
208208

209-
wirefilter_add_ip_type_field_to_scheme(scheme, ExternallyAllocatedByteArr::from("ip1"));
210-
wirefilter_add_ip_type_field_to_scheme(scheme, ExternallyAllocatedByteArr::from("ip2"));
209+
wirefilter_add_ip_type_field_to_scheme(
210+
&mut scheme,
211+
ExternallyAllocatedByteArr::from("ip1"),
212+
);
213+
wirefilter_add_ip_type_field_to_scheme(
214+
&mut scheme,
215+
ExternallyAllocatedByteArr::from("ip2"),
216+
);
211217

212-
wirefilter_add_bytes_type_field_to_scheme(scheme, ExternallyAllocatedByteArr::from("str1"));
213-
wirefilter_add_bytes_type_field_to_scheme(scheme, ExternallyAllocatedByteArr::from("str2"));
218+
wirefilter_add_bytes_type_field_to_scheme(
219+
&mut scheme,
220+
ExternallyAllocatedByteArr::from("str1"),
221+
);
222+
wirefilter_add_bytes_type_field_to_scheme(
223+
&mut scheme,
224+
ExternallyAllocatedByteArr::from("str2"),
225+
);
214226

215227
wirefilter_add_unsigned_type_field_to_scheme(
216-
scheme,
228+
&mut scheme,
217229
ExternallyAllocatedByteArr::from("num1"),
218230
);
219231
wirefilter_add_unsigned_type_field_to_scheme(
220-
scheme,
232+
&mut scheme,
221233
ExternallyAllocatedByteArr::from("num2"),
222234
);
223235

224-
test_fn(scheme);
225-
226-
wirefilter_free_scheme(scheme);
236+
scheme
227237
}
228238

229-
fn create_execution_context<'a>() -> &'a mut ExecutionContext<'a> {
230-
let exec_context = unsafe { &mut *wirefilter_create_execution_context() };
239+
fn create_execution_context() -> Box<ExecutionContext<'static>> {
240+
let mut exec_context = unsafe { Box::from_raw(wirefilter_create_execution_context()) };
231241

232242
wirefilter_add_ipv4_value_to_execution_context(
233-
exec_context,
243+
&mut exec_context,
234244
ExternallyAllocatedByteArr::from("ip1"),
235245
&[127, 0, 0, 1],
236246
);
237247

238248
wirefilter_add_ipv6_value_to_execution_context(
239-
exec_context,
249+
&mut exec_context,
240250
ExternallyAllocatedByteArr::from("ip2"),
241251
&[0, 0, 0, 0, 0, 0xffff, 0xc0a8, 1],
242252
);
243253

244254
wirefilter_add_string_bytes_value_to_execution_context(
245-
exec_context,
255+
&mut exec_context,
246256
ExternallyAllocatedByteArr::from("str1"),
247257
ExternallyAllocatedByteArr::from("Hey"),
248258
);
249259

250260
wirefilter_add_bytes_value_to_execution_context(
251-
exec_context,
261+
&mut exec_context,
252262
ExternallyAllocatedByteArr::from("str2"),
253263
ExternallyAllocatedByteArr::from("yo123"),
254264
);
255265

256266
wirefilter_add_unsigned_value_to_execution_context(
257-
exec_context,
267+
&mut exec_context,
258268
ExternallyAllocatedByteArr::from("num1"),
259269
42,
260270
);
261271

262272
wirefilter_add_unsigned_value_to_execution_context(
263-
exec_context,
273+
&mut exec_context,
264274
ExternallyAllocatedByteArr::from("num2"),
265275
1337,
266276
);
267277

268278
exec_context
269279
}
270280

271-
fn test_with_filter<T: Fn(&Filter)>(input: &'static str, func: T) {
272-
test_with_scheme(|scheme| {
273-
let result = wirefilter_parse_filter(scheme, ExternallyAllocatedByteArr::from(input));
281+
fn create_filter<'a>(
282+
scheme: &'a Scheme,
283+
input: &'static str,
284+
) -> (&'a Filter<'a>, ParsingResult<'a>) {
285+
let result = wirefilter_parse_filter(scheme, ExternallyAllocatedByteArr::from(input));
274286

275-
match result {
276-
ParsingResult::Ok(filter) => func(unsafe { &*filter }),
277-
ParsingResult::Err(ref err) => panic!("{}", err.as_str()),
278-
}
287+
match result {
288+
ParsingResult::Ok(filter) => (unsafe { &*filter }, result),
289+
ParsingResult::Err(ref err) => panic!("{}", err.as_str()),
290+
}
291+
}
279292

280-
wirefilter_free_parsing_result(result);
281-
});
293+
fn match_filter(input: &'static str, scheme: &Scheme, exec_context: &ExecutionContext) -> bool {
294+
let (filter, parsing_result) = create_filter(scheme, input);
295+
let result = wirefilter_match(filter, exec_context);
296+
297+
wirefilter_free_parsing_result(parsing_result);
298+
299+
result
282300
}
283301

284302
#[test]
285303
fn parse_error() {
286-
test_with_scheme(|scheme| {
287-
let src = r#"num1 == "abc""#;
288-
let result = wirefilter_parse_filter(scheme, ExternallyAllocatedByteArr::from(src));
304+
let src = r#"num1 == "abc""#;
305+
let scheme = create_scheme();
306+
307+
{
308+
let result = wirefilter_parse_filter(&scheme, ExternallyAllocatedByteArr::from(src));
289309

290310
match result {
291311
ParsingResult::Ok(_) => panic!("Error expected"),
@@ -296,12 +316,18 @@ mod ffi_test {
296316
}
297317

298318
wirefilter_free_parsing_result(result);
299-
});
319+
}
320+
321+
wirefilter_free_scheme(Box::into_raw(scheme));
300322
}
301323

302324
#[test]
303325
fn parse_filter() {
304-
test_with_filter(r#"num1 > 3 && str2 == "abc""#, |filter| {
326+
let scheme = create_scheme();
327+
328+
{
329+
let (filter, parsing_result) = create_filter(&scheme, r#"num1 > 3 && str2 == "abc""#);
330+
305331
assert_eq!(
306332
*filter,
307333
Filter::Combine(
@@ -321,60 +347,37 @@ mod ffi_test {
321347
]
322348
)
323349
);
324-
});
350+
351+
wirefilter_free_parsing_result(parsing_result);
352+
}
353+
354+
wirefilter_free_scheme(Box::into_raw(scheme));
325355
}
326356

327357
#[test]
328-
fn match_filter() {
358+
fn filter_matching() {
359+
let scheme = create_scheme();
329360
let exec_context = create_execution_context();
330361

331-
test_with_filter(
362+
assert!(match_filter(
332363
r#"num1 > 41 && num2 == 1337 && ip1 != 192.168.0.1 && str2 ~ "yo\d+""#,
333-
|filter| {
334-
assert!(wirefilter_match(filter, exec_context));
335-
},
336-
);
364+
&scheme,
365+
&exec_context
366+
));
337367

338-
test_with_filter(
368+
assert!(match_filter(
339369
r#"ip2 == 0:0:0:0:0:ffff:c0a8:1 && (str1 == "Hey" || str2 == "ya")"#,
340-
|filter| {
341-
assert!(wirefilter_match(filter, exec_context));
342-
},
343-
);
370+
&scheme,
371+
&exec_context
372+
));
344373

345-
test_with_filter(
374+
assert!(!match_filter(
346375
"ip1 == 127.0.0.1 && ip2 == 0:0:0:0:0:ffff:c0a8:2",
347-
|filter| {
348-
assert!(!wirefilter_match(filter, exec_context));
349-
},
350-
);
351-
352-
wirefilter_free_execution_context(exec_context);
353-
}
354-
355-
#[test]
356-
#[should_panic(expected = "Could not find previously registered field num1")]
357-
fn panic_on_missing_value() {
358-
let exec_context = unsafe { &mut *wirefilter_create_execution_context() };
359-
360-
test_with_filter("num1 == 42", |filter| {
361-
wirefilter_match(filter, exec_context);
362-
});
363-
}
364-
365-
#[test]
366-
#[should_panic(expected="Field num1 was previously registered with type Unsigned but now contains Bytes")]
367-
fn panic_on_wrong_exec_context_type() {
368-
let exec_context = create_execution_context();
369-
370-
wirefilter_add_string_bytes_value_to_execution_context(
371-
exec_context,
372-
ExternallyAllocatedByteArr::from("num1"),
373-
ExternallyAllocatedByteArr::from("Hey"),
374-
);
376+
&scheme,
377+
&exec_context
378+
));
375379

376-
test_with_filter("num1 == 42", |filter| {
377-
wirefilter_match(filter, exec_context);
378-
});
380+
wirefilter_free_execution_context(Box::into_raw(exec_context));
381+
wirefilter_free_scheme(Box::into_raw(scheme));
379382
}
380383
}

0 commit comments

Comments
 (0)