1 /*
2  * Copyright (C) 2020 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 <OffloadControlTestBase.h>
18 
TearDown()19 void OffloadControlTestBase::TearDown() {
20     // For good measure, the teardown should try stopOffload() once more, since
21     // different HAL call test cycles might enter this function. Also the
22     // return code cannot be actually expected for all cases, hence ignore it.
23     stopOffload(ExpectBoolean::Ignored);
24 }
25 
26 // The IOffloadConfig HAL is tested more thoroughly elsewhere. Here the class
27 // just setup everything correctly and verify basic readiness.
setupConfigHal()28 void OffloadControlTestBase::setupConfigHal() {
29     config = IOffloadConfig::getService(std::get<0>(GetParam()));
30     ASSERT_NE(nullptr, config.get()) << "Could not get HIDL instance";
31 
32     unique_fd fd1(conntrackSocket(NF_NETLINK_CONNTRACK_NEW | NF_NETLINK_CONNTRACK_DESTROY));
33     if (fd1.get() < 0) {
34         ALOGE("Unable to create conntrack handles: %d/%s", errno, strerror(errno));
35         FAIL();
36     }
37     native_handle_t* const nativeHandle1 = native_handle_create(1, 0);
38     nativeHandle1->data[0] = fd1.release();
39     hidl_handle h1;
40     h1.setTo(nativeHandle1, true);
41 
42     unique_fd fd2(conntrackSocket(NF_NETLINK_CONNTRACK_UPDATE | NF_NETLINK_CONNTRACK_DESTROY));
43     if (fd2.get() < 0) {
44         ALOGE("Unable to create conntrack handles: %d/%s", errno, strerror(errno));
45         FAIL();
46     }
47     native_handle_t* const nativeHandle2 = native_handle_create(1, 0);
48     nativeHandle2->data[0] = fd2.release();
49     hidl_handle h2;
50     h2.setTo(nativeHandle2, true);
51 
52     const Return<void> ret = config->setHandles(h1, h2, ASSERT_TRUE_CALLBACK);
53     ASSERT_TRUE(ret.isOk());
54 }
55 
stopOffload(const ExpectBoolean value)56 void OffloadControlTestBase::stopOffload(const ExpectBoolean value) {
57     auto cb = [&](bool success, const hidl_string& errMsg) {
58         switch (value) {
59             case ExpectBoolean::False:
60                 ASSERT_EQ(false, success) << "Unexpectedly able to stop offload: " << errMsg;
61                 break;
62             case ExpectBoolean::True:
63                 ASSERT_EQ(true, success) << "Unexpectedly failed to stop offload: " << errMsg;
64                 break;
65             case ExpectBoolean::Ignored:
66                 break;
67         }
68     };
69     const Return<void> ret = control->stopOffload(cb);
70     ASSERT_TRUE(ret.isOk());
71 }
72