1 /*
2 * Copyright 2017 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 #define LOG_TAG "netd_hidl_test"
18
19 #include <VtsHalHidlTargetTestBase.h>
20 #include <android/system/net/netd/1.0/INetd.h>
21 #include <log/log.h>
22
23 #include "VtsHalNetNetdTestUtils.h"
24
25 using ::android::system::net::netd::V1_0::INetd;
26 using ::android::hardware::Return;
27 using ::android::sp;
28
29 // Test environment for Netd HIDL HAL.
30 class NetdHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
31 public:
32 // get the test environment singleton
Instance()33 static NetdHidlEnvironment* Instance() {
34 static NetdHidlEnvironment* instance = new NetdHidlEnvironment;
35 return instance;
36 }
37
registerTestServices()38 virtual void registerTestServices() override { registerTestService<INetd>(); }
39
40 private:
NetdHidlEnvironment()41 NetdHidlEnvironment() {}
42 };
43
44 class NetdHidlTest : public ::testing::VtsHalHidlTargetTestBase {
45 public:
SetUp()46 virtual void SetUp() override {
47 netd = ::testing::VtsHalHidlTargetTestBase::getService<INetd>(
48 NetdHidlEnvironment::Instance()->getServiceName<INetd>());
49 ASSERT_NE(netd, nullptr) << "Could not get HIDL instance";
50 }
51
52 sp<INetd> netd;
53 };
54
55 // positive test. Ensure netd creates an oem network and returns valid netHandle, and destroys it.
TEST_F(NetdHidlTest,TestCreateAndDestroyOemNetworkOk)56 TEST_F(NetdHidlTest, TestCreateAndDestroyOemNetworkOk) {
57 net_handle_t netHandle;
58 uint32_t packetMark;
59 INetd::StatusCode status;
60
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 });
66
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));
79 }
80
81 // negative test. Ensure destroy for invalid OEM network fails appropriately
TEST_F(NetdHidlTest,TestDestroyOemNetworkInvalid)82 TEST_F(NetdHidlTest, TestDestroyOemNetworkInvalid) {
83 const uint64_t nh = 0x6600FACADE;
84
85 Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(nh);
86 ASSERT_EQ(INetd::StatusCode::INVALID_ARGUMENTS, retStatus);
87 }
88
main(int argc,char ** argv)89 int main(int argc, char** argv) {
90 ::testing::AddGlobalTestEnvironment(NetdHidlEnvironment::Instance());
91 ::testing::InitGoogleTest(&argc, argv);
92 NetdHidlEnvironment::Instance()->init(&argc, argv);
93 int status = RUN_ALL_TESTS();
94 ALOGE("Test result with status=%d", status);
95 return status;
96 }
97