From 3a9e2dc2324886590a1ac4135528ad9bd55bb013 Mon Sep 17 00:00:00 2001 From: Chenbo Feng Date: Fri, 8 Dec 2017 12:56:25 -0800 Subject: [PATCH] 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 --- .../native/qtaguid/src/NativeQtaguidTest.cpp | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/cts/net/native/qtaguid/src/NativeQtaguidTest.cpp b/tests/cts/net/native/qtaguid/src/NativeQtaguidTest.cpp index 9009c248ca..7f790fb7c7 100644 --- a/tests/cts/net/native/qtaguid/src/NativeQtaguidTest.cpp +++ b/tests/cts/net/native/qtaguid/src/NativeQtaguidTest.cpp @@ -15,14 +15,39 @@ */ #include +#include #include #include #include #include +#include #include #include +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(); }