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 <android/system/net/netd/1.0/INetd.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 #include <log/log.h>
24 
25 #include "VtsHalNetNetdTestUtils.h"
26 
27 using ::android::system::net::netd::V1_0::INetd;
28 using ::android::hardware::Return;
29 using ::android::sp;
30 
31 class NetdHidlTest : public ::testing::TestWithParam<std::string> {
32    public:
SetUp()33     virtual void SetUp() override {
34         netd = INetd::getService(GetParam());
35         ASSERT_NE(netd, nullptr) << "Could not get HIDL instance";
36     }
37 
38     sp<INetd> netd;
39 };
40 
41 // positive test. Ensure netd creates an oem network and returns valid netHandle, and destroys it.
TEST_P(NetdHidlTest,TestCreateAndDestroyOemNetworkOk)42 TEST_P(NetdHidlTest, TestCreateAndDestroyOemNetworkOk) {
43     net_handle_t netHandle;
44     uint32_t packetMark;
45     INetd::StatusCode status;
46 
47     Return<void> ret = netd->createOemNetwork([&](net_handle_t n, uint32_t p, INetd::StatusCode s) {
48         status = s;
49         netHandle = n;
50         packetMark = p;
51     });
52 
53     ASSERT_TRUE(ret.isOk());
54     ASSERT_EQ(INetd::StatusCode::OK, status);
55     ASSERT_NE(NETWORK_UNSPECIFIED, netHandle);
56     ASSERT_NE((uint32_t)0, packetMark);
57 
58     ASSERT_EQ(0, checkNetworkExists(netHandle));
59     ASSERT_EQ(0, countRulesForFwmark(packetMark));
60 
61     Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(netHandle);
62     ASSERT_EQ(INetd::StatusCode::OK, retStatus);
63 
64     ASSERT_EQ(-ENONET, checkNetworkExists(netHandle));
65 }
66 
67 // negative test. Ensure destroy for invalid OEM network fails appropriately
TEST_P(NetdHidlTest,TestDestroyOemNetworkInvalid)68 TEST_P(NetdHidlTest, TestDestroyOemNetworkInvalid) {
69     const uint64_t nh = 0x6600FACADE;
70 
71     Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(nh);
72     ASSERT_EQ(INetd::StatusCode::INVALID_ARGUMENTS, retStatus);
73 }
74 
75 INSTANTIATE_TEST_SUITE_P(
76     PerInstance, NetdHidlTest,
77     testing::ValuesIn(android::hardware::getAllHalInstanceNames(INetd::descriptor)),
78     android::hardware::PrintInstanceNameToString);
79