diff options
author | Tri Vo <trong@google.com> | 2017-04-12 08:58:54 -0700 |
---|---|---|
committer | Tri Vo <trong@google.com> | 2017-04-13 10:29:14 -0700 |
commit | 6f1b8bdefdc07b0c35aa9ab80889bbb6b43b2185 (patch) | |
tree | 316c55ad8901f21ad5ce4f34c77a96f7a2aff0d3 | |
parent | f364a195165cb6ed6350f8d264adefebca890818 (diff) | |
download | kernel-6f1b8bdefdc07b0c35aa9ab80889bbb6b43b2185.tar.gz |
Basic test for /dev/tun.
Bug: 36902680
Test: rm -rf out/host/linux-x86/vts && make vts -j64 && vts-tradefed run
commandAndExit vts --skip-all-system-status-check --primary-abi-only
--skip-preconditions -l VERBOSE --module VtsKernelTunTest
Change-Id: I1904b13c8a8d73e40db1de55898d52153c12ca88
-rw-r--r-- | Android.bp | 17 | ||||
-rw-r--r-- | api/tun/Android.bp | 24 | ||||
-rw-r--r-- | api/tun/Android.mk | 22 | ||||
-rw-r--r-- | api/tun/AndroidTest.xml | 29 | ||||
-rw-r--r-- | api/tun/vts_kernel_tun_test.cpp | 95 |
5 files changed, 187 insertions, 0 deletions
diff --git a/Android.bp b/Android.bp new file mode 100644 index 00000000..77862666 --- /dev/null +++ b/Android.bp @@ -0,0 +1,17 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +subdirs = [ + "api/tun", +] diff --git a/api/tun/Android.bp b/api/tun/Android.bp new file mode 100644 index 00000000..f065de24 --- /dev/null +++ b/api/tun/Android.bp @@ -0,0 +1,24 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_test { + name: "vts_kernel_tun_test", + srcs: [ + "vts_kernel_tun_test.cpp", + ], + shared_libs: [ + "libbase", + ], +} + diff --git a/api/tun/Android.mk b/api/tun/Android.mk new file mode 100644 index 00000000..09fc0b14 --- /dev/null +++ b/api/tun/Android.mk @@ -0,0 +1,22 @@ +# +# Copyright (C) 2017 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := VtsKernelTunTest +include test/vts/tools/build/Android.host_config.mk diff --git a/api/tun/AndroidTest.xml b/api/tun/AndroidTest.xml new file mode 100644 index 00000000..87e20c8a --- /dev/null +++ b/api/tun/AndroidTest.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration description="Config for VTS VtsKernelTunTest."> + <target_preparer class="com.android.compatibility.common.tradefed.targetprep.VtsFilePusher"> + <option name="abort-on-push-failure" value="false"/> + <option name="push-group" value="HostDrivenTest.push"/> + </target_preparer> + <target_preparer class="com.android.tradefed.targetprep.VtsPythonVirtualenvPreparer"/> + <test class="com.android.tradefed.testtype.VtsMultiDeviceTest"> + <option name="test-module-name" value="VtsKernelTunTest"/> + <option name="binary-test-source" value="_32bit::DATA/nativetest/vts_kernel_tun_test/vts_kernel_tun_test" /> + <option name="binary-test-source" value="_64bit::DATA/nativetest64/vts_kernel_tun_test/vts_kernel_tun_test" /> + <option name="binary-test-type" value="gtest"/> + <option name="test-timeout" value="10m"/> + </test> +</configuration> diff --git a/api/tun/vts_kernel_tun_test.cpp b/api/tun/vts_kernel_tun_test.cpp new file mode 100644 index 00000000..701a19d9 --- /dev/null +++ b/api/tun/vts_kernel_tun_test.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <iostream> +#include <string> + +#include <fcntl.h> + +#include <android-base/stringprintf.h> +#include <android-base/unique_fd.h> +#include <gtest/gtest.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <linux/if.h> +#include <linux/if_tun.h> + +using std::cout; +using std::endl; +using std::string; +using android::base::StringPrintf; +using android::base::unique_fd; + +static const short kTunModes[] = { + IFF_TUN, + IFF_TAP, +}; + +class VtsKernelTunTest : public ::testing::TestWithParam<short> { + public: + virtual void SetUp() override { + tun_device_ = "/dev/tun"; + uint32_t num = arc4random_uniform(1000); + ifr_name_ = StringPrintf("tun%d", num); + } + + // Used to initialize tun device. + int TunInit(short flags); + + string tun_device_; + string ifr_name_; + unique_fd fd_; +}; + +int VtsKernelTunTest::TunInit(short mode) { + struct ifreq ifr = { + .ifr_flags = mode, + }; + strncpy(ifr.ifr_name, ifr_name_.c_str(), IFNAMSIZ); + int fd = open(tun_device_.c_str(), O_RDWR | O_NONBLOCK); + if (fd < 0) { + return -1; + } + if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { + close(fd); + return -1; + } + return fd; +} + +// Test opening and closing of a tun interface. +TEST_P(VtsKernelTunTest, OpenAndClose) { + fd_ = unique_fd(TunInit(GetParam())); + ASSERT_TRUE(fd_ >= 0); +} + +// Test basic read functionality of a tuen interface. +TEST_P(VtsKernelTunTest, BasicRead) { + fd_ = unique_fd(TunInit(GetParam())); + ASSERT_TRUE(fd_ >= 0); + + uint8_t test_output; + // Expect no packets available on this interface. + ASSERT_TRUE(read(fd_, &test_output, 1) < 0); +} + +INSTANTIATE_TEST_CASE_P(Basic, VtsKernelTunTest, + ::testing::ValuesIn(kTunModes)); + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} |