upload android base code part6

This commit is contained in:
August 2018-08-08 17:48:24 +08:00
parent 421e214c7d
commit 4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions

View file

@ -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",
],
}

View file

@ -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

View file

@ -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>

View file

@ -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();
}