system/hardware/interfaces
Révision | 065b170e649d593105b3d84c77f95d044372a8c4 (tree) |
---|---|
l'heure | 2018-03-13 13:02:59 |
Auteur | Lorenzo Colitti <lorenzo@goog...> |
Commiter | android-build-merger |
Make the VTS tests for the INetd HAL 1.0 more realistic.
am: 79f693702c
Change-Id: Id951ad6982142a423ffb881979d0ad9e660b4afe
@@ -1,19 +1,37 @@ | ||
1 | +// | |
2 | +// Copyright (C) 2018 The Android Open Source Project | |
3 | +// | |
4 | +// Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +// you may not use this file except in compliance with the License. | |
6 | +// You may obtain a copy of the License at | |
7 | +// | |
8 | +// http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +// | |
10 | +// Unless required by applicable law or agreed to in writing, software | |
11 | +// distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +// See the License for the specific language governing permissions and | |
14 | +// limitations under the License. | |
15 | + | |
1 | 16 | cc_test { |
2 | 17 | name: "VtsHalNetNetdV1_0TargetTest", |
3 | 18 | srcs: [ |
4 | 19 | "VtsHalNetNetdV1_0TargetTest.cpp", |
5 | 20 | ], |
6 | 21 | shared_libs: [ |
7 | - "liblog", | |
22 | + "android.system.net.netd@1.0", | |
23 | + "libandroid_net", | |
24 | + "libbase", | |
8 | 25 | "libhidlbase", |
9 | - "libhidltransport", | |
26 | + "liblog", | |
10 | 27 | "libutils", |
11 | - "android.system.net.netd@1.0", | |
12 | 28 | ], |
13 | - static_libs: ["VtsHalHidlTargetTestBase"], | |
29 | + static_libs: [ | |
30 | + "VtsHalHidlTargetTestBase", | |
31 | + "VtsHalNetNetdTestUtils", | |
32 | + ], | |
14 | 33 | cflags: [ |
15 | - "-O0", | |
16 | - "-g", | |
34 | + "-Og", | |
17 | 35 | "-Wall", |
18 | 36 | "-Werror", |
19 | 37 | ], |
@@ -20,15 +20,33 @@ | ||
20 | 20 | #include <android/system/net/netd/1.0/INetd.h> |
21 | 21 | #include <log/log.h> |
22 | 22 | |
23 | +#include "VtsHalNetNetdTestUtils.h" | |
24 | + | |
23 | 25 | using ::android::system::net::netd::V1_0::INetd; |
24 | 26 | using ::android::hardware::Return; |
25 | 27 | using ::android::sp; |
26 | 28 | |
29 | +// Test environment for Netd HIDL HAL. | |
30 | +class NetdHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { | |
31 | + public: | |
32 | + // get the test environment singleton | |
33 | + static NetdHidlEnvironment* Instance() { | |
34 | + static NetdHidlEnvironment* instance = new NetdHidlEnvironment; | |
35 | + return instance; | |
36 | + } | |
37 | + | |
38 | + virtual void registerTestServices() override { registerTestService<INetd>(); } | |
39 | + | |
40 | + private: | |
41 | + NetdHidlEnvironment() {} | |
42 | +}; | |
43 | + | |
27 | 44 | class NetdHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
28 | 45 | public: |
29 | 46 | virtual void SetUp() override { |
30 | - netd = ::testing::VtsHalHidlTargetTestBase::getService<INetd>(); | |
31 | - ASSERT_NE(nullptr, netd.get()) << "Could not get HIDL instance"; | |
47 | + netd = ::testing::VtsHalHidlTargetTestBase::getService<INetd>( | |
48 | + NetdHidlEnvironment::Instance()->getServiceName<INetd>()); | |
49 | + ASSERT_NE(netd, nullptr) << "Could not get HIDL instance"; | |
32 | 50 | } |
33 | 51 | |
34 | 52 | sp<INetd> netd; |
@@ -36,17 +54,28 @@ class NetdHidlTest : public ::testing::VtsHalHidlTargetTestBase { | ||
36 | 54 | |
37 | 55 | // positive test. Ensure netd creates an oem network and returns valid netHandle, and destroys it. |
38 | 56 | TEST_F(NetdHidlTest, TestCreateAndDestroyOemNetworkOk) { |
39 | - auto cb = [this](uint64_t netHandle, uint32_t packetMark, INetd::StatusCode status) { | |
40 | - ASSERT_EQ(INetd::StatusCode::OK, status); | |
41 | - ASSERT_NE((uint64_t)0, netHandle); | |
42 | - ASSERT_NE((uint32_t)0, packetMark); | |
57 | + net_handle_t netHandle; | |
58 | + uint32_t packetMark; | |
59 | + INetd::StatusCode status; | |
43 | 60 | |
44 | - Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(netHandle); | |
45 | - ASSERT_EQ(INetd::StatusCode::OK, retStatus); | |
46 | - }; | |
61 | + Return<void> ret = netd->createOemNetwork([&](net_handle_t n, uint32_t p, INetd::StatusCode s) { | |
62 | + status = s; | |
63 | + netHandle = n; | |
64 | + packetMark = p; | |
65 | + }); | |
47 | 66 | |
48 | - Return<void> ret = netd->createOemNetwork(cb); | |
49 | 67 | ASSERT_TRUE(ret.isOk()); |
68 | + ASSERT_EQ(INetd::StatusCode::OK, status); | |
69 | + ASSERT_NE(NETWORK_UNSPECIFIED, netHandle); | |
70 | + ASSERT_NE((uint32_t)0, packetMark); | |
71 | + | |
72 | + ASSERT_EQ(0, checkNetworkExists(netHandle)); | |
73 | + ASSERT_EQ(0, countRulesForFwmark(packetMark)); | |
74 | + | |
75 | + Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(netHandle); | |
76 | + ASSERT_EQ(INetd::StatusCode::OK, retStatus); | |
77 | + | |
78 | + ASSERT_EQ(-ENONET, checkNetworkExists(netHandle)); | |
50 | 79 | } |
51 | 80 | |
52 | 81 | // negative test. Ensure destroy for invalid OEM network fails appropriately |
@@ -58,7 +87,9 @@ TEST_F(NetdHidlTest, TestDestroyOemNetworkInvalid) { | ||
58 | 87 | } |
59 | 88 | |
60 | 89 | int main(int argc, char** argv) { |
90 | + ::testing::AddGlobalTestEnvironment(NetdHidlEnvironment::Instance()); | |
61 | 91 | ::testing::InitGoogleTest(&argc, argv); |
92 | + NetdHidlEnvironment::Instance()->init(&argc, argv); | |
62 | 93 | int status = RUN_ALL_TESTS(); |
63 | 94 | ALOGE("Test result with status=%d", status); |
64 | 95 | return status; |
@@ -0,0 +1,31 @@ | ||
1 | +// | |
2 | +// Copyright (C) 2018 The Android Open Source Project | |
3 | +// | |
4 | +// Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +// you may not use this file except in compliance with the License. | |
6 | +// You may obtain a copy of the License at | |
7 | +// | |
8 | +// http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +// | |
10 | +// Unless required by applicable law or agreed to in writing, software | |
11 | +// distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +// See the License for the specific language governing permissions and | |
14 | +// limitations under the License. | |
15 | + | |
16 | +cc_library { | |
17 | + name: "VtsHalNetNetdTestUtils", | |
18 | + srcs: [ | |
19 | + "VtsHalNetNetdTestUtils.cpp", | |
20 | + ], | |
21 | + export_include_dirs: ["."], | |
22 | + shared_libs: [ | |
23 | + "libbase", | |
24 | + "libandroid_net", | |
25 | + ], | |
26 | + cflags: [ | |
27 | + "-Og", | |
28 | + "-Wall", | |
29 | + "-Werror", | |
30 | + ], | |
31 | +} |
@@ -0,0 +1,92 @@ | ||
1 | +/* | |
2 | + * Copyright 2018 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +#include <regex> | |
18 | +#include <string> | |
19 | +#include <vector> | |
20 | + | |
21 | +#include <errno.h> | |
22 | +#include <sys/socket.h> | |
23 | +#include <sys/types.h> | |
24 | +#include <unistd.h> | |
25 | + | |
26 | +#include <android-base/stringprintf.h> | |
27 | +#include <android/multinetwork.h> | |
28 | + | |
29 | +#include "VtsHalNetNetdTestUtils.h" | |
30 | + | |
31 | +using android::base::StringPrintf; | |
32 | + | |
33 | +int checkNetworkExists(net_handle_t netHandle) { | |
34 | + int sock = socket(AF_INET6, SOCK_STREAM, 0); | |
35 | + if (sock == -1) { | |
36 | + return -ENOTSOCK; | |
37 | + } | |
38 | + int ret = android_setsocknetwork(netHandle, sock); | |
39 | + if (ret) { | |
40 | + ret = -errno; | |
41 | + } | |
42 | + close(sock); | |
43 | + return ret; | |
44 | +} | |
45 | + | |
46 | +// TODO: deduplicate this with system/netd/server/binder_test.cpp. | |
47 | +static std::vector<std::string> runCommand(const std::string& command) { | |
48 | + std::vector<std::string> lines; | |
49 | + FILE* f; | |
50 | + | |
51 | + if ((f = popen(command.c_str(), "r")) == nullptr) { | |
52 | + perror("popen"); | |
53 | + return lines; | |
54 | + } | |
55 | + | |
56 | + char* line = nullptr; | |
57 | + size_t bufsize = 0; | |
58 | + ssize_t linelen = 0; | |
59 | + while ((linelen = getline(&line, &bufsize, f)) >= 0) { | |
60 | + lines.push_back(std::string(line, linelen)); | |
61 | + } | |
62 | + | |
63 | + free(line); | |
64 | + pclose(f); | |
65 | + return lines; | |
66 | +} | |
67 | + | |
68 | +static std::vector<std::string> listIpRules(const char* ipVersion) { | |
69 | + std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion); | |
70 | + return runCommand(command); | |
71 | +} | |
72 | + | |
73 | +static int countMatchingIpRules(const std::string& regexString) { | |
74 | + const std::regex regex(regexString, std::regex_constants::extended); | |
75 | + int matches = 0; | |
76 | + | |
77 | + for (const char* version : {"-4", "-6"}) { | |
78 | + std::vector<std::string> rules = listIpRules(version); | |
79 | + for (const auto& rule : rules) { | |
80 | + if (std::regex_search(rule, regex)) { | |
81 | + matches++; | |
82 | + } | |
83 | + } | |
84 | + } | |
85 | + | |
86 | + return matches; | |
87 | +} | |
88 | + | |
89 | +int countRulesForFwmark(const uint32_t fwmark) { | |
90 | + std::string regex = StringPrintf("from all fwmark 0x%x/.* lookup ", fwmark); | |
91 | + return countMatchingIpRules(regex); | |
92 | +} |
@@ -0,0 +1,31 @@ | ||
1 | +/* | |
2 | + * Copyright 2018 The Android Open Source Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +#ifndef VTS_HAL_NET_NETD_TEST_UTILS_H | |
18 | +#define VTS_HAL_NET_NETD_TEST_UTILS_H | |
19 | + | |
20 | +#include <android/multinetwork.h> | |
21 | + | |
22 | +#define IP_PATH "/system/bin/ip" | |
23 | + | |
24 | +// Checks that the given network exists. | |
25 | +// Returns 0 if it exists or -errno if it does not. | |
26 | +int checkNetworkExists(net_handle_t netHandle); | |
27 | + | |
28 | +// Counts the number of IPv4 and IPv6 routing rules that select the specified fwmark. | |
29 | +int countRulesForFwmark(const uint32_t fwmark); | |
30 | + | |
31 | +#endif |