Skip to content

Commit d10a79a

Browse files
committed
Add netlink socket test
1 parent 9b9c931 commit d10a79a

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ path = "src/bin/ptree.rs"
2323
[[bin]]
2424
name = "epoll_example"
2525
path = "src/bin/testing/epoll.rs"
26+
[[bin]]
27+
name = "netlink_example"
28+
path = "src/bin/testing/netlink.rs"
2629

2730
[profile.release]
2831
lto = true
@@ -34,6 +37,7 @@ panic = "abort" # Save size. We don't need unwinding anyway.
3437
[dependencies]
3538
getopts = "0.2.15"
3639
nix = "0.12.0"
40+
libc = "0.2.48"
3741

3842
[package.metadata.deb]
3943
assets = [

src/bin/testing/netlink.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright 2019 Delphix
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
use nix::sys::socket::{socket, AddressFamily, SockAddr, SockType, SockFlag, bind};
18+
use nix::errno::Errno;
19+
20+
use std::fs::File;
21+
use std::os::raw::c_int;
22+
23+
extern crate nix;
24+
25+
const NETLINK_ROUTE: c_int = 0;
26+
27+
fn main() {
28+
// Use libc crate directly instead of nix because nix's 'socket' method doesn't have ability to
29+
// specify netlink socket protocol.
30+
let fd = unsafe {
31+
libc::socket(AddressFamily::Netlink as c_int,
32+
SockType::Datagram as c_int,
33+
NETLINK_ROUTE)
34+
};
35+
36+
let fd = Errno::result(fd).unwrap();
37+
38+
bind(fd, &SockAddr::new_netlink(0, 0));
39+
40+
// Signal parent process (the test process) that this process is ready to be observed by the
41+
// ptool being tested.
42+
File::create("/tmp/ptools-test-ready").unwrap();
43+
44+
// Wait for the parent finish running the ptool and then kill us.
45+
loop {}
46+
}
47+

tests/netlink_test.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright 2019 Delphix
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
mod common;
18+
19+
#[test]
20+
fn netlink_basic() {
21+
let stdout = common::run_ptool("pfiles", "netlink_example");
22+
let lines = stdout.lines().collect::<Vec<&str>>();
23+
24+
//
25+
// We expect something along the lines of
26+
// ...
27+
// 3: S_IFSOCK mode:777 dev:0,9 ino:725134 uid:65433 gid:50 size:0
28+
// O_RDWR
29+
// SOCK_DGRAM
30+
// sockname: AF_NETLINK
31+
//
32+
let pattern = "3: S_IFSOCK";
33+
let split_lines = lines
34+
.split(|l| l.trim().starts_with(pattern))
35+
.collect::<Vec<_>>();
36+
37+
if split_lines.len() != 2 {
38+
panic!("String '{}' not found in command output:\n\n{}\n\n", pattern, stdout);
39+
}
40+
let fd_info = split_lines[1];
41+
42+
let pattern = "sockname: AF_NETLINK";
43+
if fd_info[2].trim() != pattern {
44+
panic!("String '{}' not found in command output:\n\n{}\n\n", pattern, fd_info.join("\n"));
45+
}
46+
}

0 commit comments

Comments
 (0)