diff options
author | Stephen Barber <smbarber@chromium.org> | 2018-02-09 15:16:01 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-03-08 00:37:50 -0800 |
commit | a10b2d20a8667a4c730aacc1f4c8d18a6756eaab (patch) | |
tree | c6db02d574746f0a18ec2ee1a868dd795c85e795 /tests | |
parent | 8da6543e74d5e037f08ed4cc49f89ad63963c3dc (diff) | |
download | crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.gz crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.bz2 crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.lz crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.xz crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.tar.zst crosvm-a10b2d20a8667a4c730aacc1f4c8d18a6756eaab.zip |
plugin: add crosvm_net_get_config
BUG=none TEST=sudo cargo test --features plugin Change-Id: Ib38fad250295d73529dff0451345b4274a261073 Reviewed-on: https://chromium-review.googlesource.com/911943 Commit-Ready: Stephen Barber <smbarber@chromium.org> Tested-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/plugin.policy | 2 | ||||
-rw-r--r-- | tests/plugin_net_config.c | 85 | ||||
-rw-r--r-- | tests/plugins.rs | 11 |
3 files changed, 98 insertions, 0 deletions
diff --git a/tests/plugin.policy b/tests/plugin.policy index 960c8e5..773ea24 100644 --- a/tests/plugin.policy +++ b/tests/plugin.policy @@ -33,6 +33,8 @@ fstat: 1 ftruncate: 1 getcwd: 1 getrlimit: 1 +# TUNGETFEATURES +ioctl: arg1 == 0x800454CF madvise: 1 memfd_create: 1 mmap: 1 diff --git a/tests/plugin_net_config.c b/tests/plugin_net_config.c new file mode 100644 index 0000000..f11fa10 --- /dev/null +++ b/tests/plugin_net_config.c @@ -0,0 +1,85 @@ +/* + * Copyright 2018 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <arpa/inet.h> +#include <linux/if_tun.h> +#include <sys/ioctl.h> + +#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "crosvm.h" + +/* + * These must match the network arguments supplied to the plugin in plugins.rs. + * IPv4 addresses here are in host-native byte order. + */ +const uint32_t expected_ip = 0x64735c05; // 100.115.92.5 +const uint32_t expected_netmask = 0xfffffffc; // 255.255.255.252 +const uint8_t expected_mac[] = {0xde, 0x21, 0xe8, 0x47, 0x6b, 0x6a}; + +int main(int argc, char** argv) { + struct crosvm *crosvm; + struct crosvm_net_config net_config; + int ret = crosvm_connect(&crosvm); + + if (ret) { + fprintf(stderr, "failed to connect to crosvm: %d\n", ret); + return 1; + } + + ret = crosvm_net_get_config(crosvm, &net_config); + if (ret) { + fprintf(stderr, "failed to get crosvm net config: %d\n", ret); + return 1; + } + + if (net_config.tap_fd < 0) { + fprintf(stderr, "fd %d is < 0\n", net_config.tap_fd); + return 1; + } + + unsigned int features; + if (ioctl(net_config.tap_fd, TUNGETFEATURES, &features) < 0) { + fprintf(stderr, + "failed to read tap features: %s\n", + strerror(errno)); + return 1; + } + + if (net_config.host_ip != htonl(expected_ip)) { + char ip_addr[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &net_config.host_ip, ip_addr, sizeof(ip_addr)); + fprintf(stderr, "ip %s != 100.115.92.5\n", ip_addr); + return 1; + } + + if (net_config.netmask != htonl(expected_netmask)) { + char netmask[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &net_config.netmask, netmask, sizeof(netmask)); + fprintf(stderr, "netmask %s != 255.255.255.252\n", netmask); + return 1; + } + + if (memcmp(net_config.host_mac_address, + expected_mac, + sizeof(expected_mac)) != 0) { + fprintf(stderr, + "mac %02X:%02X:%02X:%02X:%02X:%02X != de:21:e8:47:6b:6a\n", + net_config.host_mac_address[0], + net_config.host_mac_address[1], + net_config.host_mac_address[2], + net_config.host_mac_address[3], + net_config.host_mac_address[4], + net_config.host_mac_address[5]); + return 1; + } + + return 0; +} diff --git a/tests/plugins.rs b/tests/plugins.rs index d036e97..e858c9a 100644 --- a/tests/plugins.rs +++ b/tests/plugins.rs @@ -83,6 +83,12 @@ fn run_plugin(bin_path: &Path, with_sandbox: bool) { cmd.args(&["run", "-c", "1", + "--host_ip", + "100.115.92.5", + "--netmask", + "255.255.255.252", + "--mac", + "de:21:e8:47:6b:6a", "--seccomp-policy-dir", "tests", "--plugin"]) @@ -241,6 +247,11 @@ fn test_vcpu_pause() { } #[test] +fn test_net_config() { + test_plugin(include_str!("plugin_net_config.c")); +} + +#[test] fn test_debugregs() { let mini_plugin = MiniPlugin { assembly_src: "org 0x1000 |