Check kernel version before running qtaguid test

The qtaguid module is required for devices running kernel earlier then
4.9, but is no longer avaliable after that. Add a check before running
the kernel qtaguid native test so it doesn't fail on new devices.

Bug: 30950746
Test: run cts -m CtsNativeNetTestCases
Change-Id: I32bc77a4c51d8b64ac1a5411c75c56a51b84ea77
This commit is contained in:
Chenbo Feng
2017-12-08 12:56:25 -08:00
parent eca595be58
commit 3a9e2dc232

View File

@@ -15,14 +15,39 @@
*/
#include <arpa/inet.h>
#include <error.h>
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <gtest/gtest.h>
#include <cutils/qtaguid.h>
int hasQtaguidKernelSupport() {
struct utsname buf;
int kernel_version_major;
int kernel_version_minor;
int ret = uname(&buf);
if (ret) {
ret = -errno;
return ret;
}
char dummy;
ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
if (ret < 3)
return -EINVAL;
if ((kernel_version_major == 4 && kernel_version_minor < 9) ||
(kernel_version_major < 4)) {
return 1;
} else {
return access("/proc/net/xt_qtaguid/ctrl", F_OK) != -1;
}
}
int getCtrlSkInfo(int tag, uid_t uid, uint64_t* sk_addr, int* ref_cnt) {
FILE *fp;
fp = fopen("/proc/net/xt_qtaguid/ctrl", "r");
@@ -70,6 +95,12 @@ void checkNoSocketPointerLeaks(int family) {
}
TEST (NativeQtaguidTest, close_socket_without_untag) {
int res = hasQtaguidKernelSupport();
ASSERT_LE(0, res);
if (!res) {
GTEST_LOG_(INFO) << "This test is skipped since kernel may not have the module\n";
return;
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
uid_t uid = getuid();
int tag = arc4random();
@@ -83,6 +114,12 @@ TEST (NativeQtaguidTest, close_socket_without_untag) {
}
TEST (NativeQtaguidTest, close_socket_without_untag_ipv6) {
int res = hasQtaguidKernelSupport();
ASSERT_LE(0, res);
if (!res) {
GTEST_LOG_(INFO) << "This test is skipped since kernel may not have the module\n";
return;
}
int sockfd = socket(AF_INET6, SOCK_STREAM, 0);
uid_t uid = getuid();
int tag = arc4random();
@@ -96,12 +133,17 @@ TEST (NativeQtaguidTest, close_socket_without_untag_ipv6) {
}
TEST (NativeQtaguidTest, no_socket_addr_leak) {
int res = hasQtaguidKernelSupport();
ASSERT_LE(0, res);
if (!res) {
GTEST_LOG_(INFO) << "This test is skipped since kernel may not have the module\n";
return;
}
checkNoSocketPointerLeaks(AF_INET);
checkNoSocketPointerLeaks(AF_INET6);
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}