/src/suricata7/rust/src/feature.rs
Line | Count | Source |
1 | | /* Copyright (C) 2023 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | //! Rust bindings to the "feature" API. |
19 | | //! |
20 | | //! As this feature module is a binding to a Suricata C module it is |
21 | | //! not available to Rust unit tests. Instead when running Rust unit |
22 | | //! tests and "mock" version is provided that will return true for any |
23 | | //! feature starting with "true" and false for any other feature name. |
24 | | |
25 | | #[cfg(test)] |
26 | | mod mock { |
27 | | /// Check for a feature returning true if found. |
28 | | /// |
29 | | /// This a "mock" variant of `requires` that will return true for |
30 | | /// any feature starting with string `true`, and false for |
31 | | /// anything else. |
32 | | pub fn requires(feature: &str) -> bool { |
33 | | return feature.starts_with("true"); |
34 | | } |
35 | | } |
36 | | |
37 | | #[cfg(not(test))] |
38 | | mod real { |
39 | | use std::ffi::CString; |
40 | | use std::os::raw::c_char; |
41 | | |
42 | | extern "C" { |
43 | | fn RequiresFeature(feature: *const c_char) -> bool; |
44 | | } |
45 | | |
46 | | /// Check for a feature returning true if found. |
47 | 416 | pub fn requires(feature: &str) -> bool { |
48 | 416 | if let Ok(feature) = CString::new(feature) { |
49 | 416 | unsafe { RequiresFeature(feature.as_ptr()) } |
50 | | } else { |
51 | 0 | false |
52 | | } |
53 | 416 | } |
54 | | } |
55 | | |
56 | | #[cfg(not(test))] |
57 | | pub use real::*; |
58 | | |
59 | | #[cfg(test)] |
60 | | pub use mock::*; |