Skip to content

Commit e0da4b6

Browse files
d-e-s-odanielocfb
authored andcommitted
Move map_info test into test.rs
Let's co-locate the map_info test with the remaining end-to-end tests for easier management. Signed-off-by: Daniel Müller <[email protected]>
1 parent d53fc1f commit e0da4b6

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

libbpf-rs/src/map.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -987,48 +987,3 @@ impl MapInfo {
987987
MapFlags::from_bits_truncate(self.info.map_flags as u64)
988988
}
989989
}
990-
991-
#[cfg(test)]
992-
mod tests {
993-
use super::*;
994-
995-
use crate::Map;
996-
use crate::MapFlags;
997-
use crate::MapInfo;
998-
use crate::MapType;
999-
1000-
/// Test whether `MapInfo` works properly
1001-
#[test]
1002-
pub fn test_map_info() {
1003-
let opts = libbpf_sys::bpf_map_create_opts {
1004-
sz: mem::size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
1005-
map_flags: libbpf_sys::BPF_ANY,
1006-
btf_fd: 0,
1007-
btf_key_type_id: 0,
1008-
btf_value_type_id: 0,
1009-
btf_vmlinux_value_type_id: 0,
1010-
inner_map_fd: 0,
1011-
map_extra: 0,
1012-
numa_node: 0,
1013-
map_ifindex: 0,
1014-
};
1015-
1016-
let map = Map::create(MapType::Hash, Some("simple_map"), 8, 64, 1024, &opts).unwrap();
1017-
let map_info = MapInfo::new(map.fd()).unwrap();
1018-
let name_received = map_info.name().unwrap();
1019-
assert_eq!(name_received, "simple_map");
1020-
assert_eq!(map_info.map_type(), MapType::Hash);
1021-
assert_eq!(map_info.flags() & MapFlags::ANY, MapFlags::ANY);
1022-
1023-
let map_info = &map_info.info;
1024-
assert_eq!(map_info.key_size, 8);
1025-
assert_eq!(map_info.value_size, 64);
1026-
assert_eq!(map_info.max_entries, 1024);
1027-
assert_eq!(map_info.btf_id, 0);
1028-
assert_eq!(map_info.btf_key_type_id, 0);
1029-
assert_eq!(map_info.btf_value_type_id, 0);
1030-
assert_eq!(map_info.btf_vmlinux_value_type_id, 0);
1031-
assert_eq!(map_info.map_extra, 0);
1032-
assert_eq!(map_info.ifindex, 0);
1033-
}
1034-
}

libbpf-rs/tests/test.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ffi::c_void;
33
use std::fs;
44
use std::hint;
55
use std::io::Read;
6+
use std::mem::size_of;
67
use std::os::fd::AsRawFd;
78
use std::path::Path;
89
use std::path::PathBuf;
@@ -23,6 +24,7 @@ use libbpf_rs::Iter;
2324
use libbpf_rs::Linker;
2425
use libbpf_rs::Map;
2526
use libbpf_rs::MapFlags;
27+
use libbpf_rs::MapInfo;
2628
use libbpf_rs::MapType;
2729
use libbpf_rs::Object;
2830
use libbpf_rs::ObjectBuilder;
@@ -226,6 +228,41 @@ fn test_object_map_delete_batch() {
226228
assert!(start.keys().collect::<Vec<_>>().is_empty())
227229
}
228230

231+
/// Test whether `MapInfo` works properly
232+
#[test]
233+
pub fn test_object_map_info() {
234+
let opts = libbpf_sys::bpf_map_create_opts {
235+
sz: size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
236+
map_flags: libbpf_sys::BPF_ANY,
237+
btf_fd: 0,
238+
btf_key_type_id: 0,
239+
btf_value_type_id: 0,
240+
btf_vmlinux_value_type_id: 0,
241+
inner_map_fd: 0,
242+
map_extra: 0,
243+
numa_node: 0,
244+
map_ifindex: 0,
245+
};
246+
247+
let map = Map::create(MapType::Hash, Some("simple_map"), 8, 64, 1024, &opts).unwrap();
248+
let map_info = MapInfo::new(map.fd()).unwrap();
249+
let name_received = map_info.name().unwrap();
250+
assert_eq!(name_received, "simple_map");
251+
assert_eq!(map_info.map_type(), MapType::Hash);
252+
assert_eq!(map_info.flags() & MapFlags::ANY, MapFlags::ANY);
253+
254+
let map_info = &map_info.info;
255+
assert_eq!(map_info.key_size, 8);
256+
assert_eq!(map_info.value_size, 64);
257+
assert_eq!(map_info.max_entries, 1024);
258+
assert_eq!(map_info.btf_id, 0);
259+
assert_eq!(map_info.btf_key_type_id, 0);
260+
assert_eq!(map_info.btf_value_type_id, 0);
261+
assert_eq!(map_info.btf_vmlinux_value_type_id, 0);
262+
assert_eq!(map_info.map_extra, 0);
263+
assert_eq!(map_info.ifindex, 0);
264+
}
265+
229266
#[test]
230267
fn test_object_percpu_lookup() {
231268
bump_rlimit_mlock();
@@ -242,7 +279,7 @@ fn test_object_percpu_lookup() {
242279
res.len(),
243280
num_possible_cpus().expect("must be one value per cpu")
244281
);
245-
assert_eq!(res[0].len(), std::mem::size_of::<u32>());
282+
assert_eq!(res[0].len(), size_of::<u32>());
246283
}
247284

248285
#[test]
@@ -783,7 +820,7 @@ fn test_object_task_iter() {
783820
.expect("Failed to read from iterator");
784821

785822
assert!(bytes_read > 0);
786-
assert_eq!(bytes_read % std::mem::size_of::<IndexPidPair>(), 0);
823+
assert_eq!(bytes_read % size_of::<IndexPidPair>(), 0);
787824
let items: &[IndexPidPair] =
788825
plain::slice_from_bytes(buf.as_slice()).expect("Input slice cannot satisfy length");
789826

@@ -800,7 +837,7 @@ fn test_object_map_iter() {
800837

801838
// Create a map for iteration test.
802839
let opts = libbpf_sys::bpf_map_create_opts {
803-
sz: std::mem::size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
840+
sz: size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
804841
map_flags: libbpf_sys::BPF_F_NO_PREALLOC,
805842
..Default::default()
806843
};
@@ -836,7 +873,7 @@ fn test_object_map_iter() {
836873
.expect("Failed to read from iterator");
837874

838875
assert!(bytes_read > 0);
839-
assert_eq!(bytes_read % std::mem::size_of::<u32>(), 0);
876+
assert_eq!(bytes_read % size_of::<u32>(), 0);
840877
// Convert buf to &[u32]
841878
let buf =
842879
plain::slice_from_bytes::<u32>(buf.as_slice()).expect("Input slice cannot satisfy length");
@@ -850,7 +887,7 @@ fn test_object_map_create_and_pin() {
850887
bump_rlimit_mlock();
851888

852889
let opts = libbpf_sys::bpf_map_create_opts {
853-
sz: std::mem::size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
890+
sz: size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
854891
map_flags: libbpf_sys::BPF_F_NO_PREALLOC,
855892
..Default::default()
856893
};
@@ -895,7 +932,7 @@ fn test_object_map_create_without_name() {
895932
bump_rlimit_mlock();
896933

897934
let opts = libbpf_sys::bpf_map_create_opts {
898-
sz: std::mem::size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
935+
sz: size_of::<libbpf_sys::bpf_map_create_opts>() as libbpf_sys::size_t,
899936
map_flags: libbpf_sys::BPF_F_NO_PREALLOC,
900937
btf_fd: 0,
901938
btf_key_type_id: 0,

0 commit comments

Comments
 (0)