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 #pragma once
18 
19 #include <OffloadControlTestUtils.h>
20 #include <VtsHalHidlTargetCallbackBase.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/unique_fd.h>
23 #include <android/hardware/tetheroffload/config/1.0/IOffloadConfig.h>
24 #include <android/hardware/tetheroffload/control/1.0/IOffloadControl.h>
25 #include <android/hardware/tetheroffload/control/1.0/types.h>
26 #include <gtest/gtest.h>
27 #include <linux/netfilter/nfnetlink.h>
28 #include <log/log.h>
29 
30 using android::sp;
31 using android::base::StringPrintf;
32 using android::base::unique_fd;
33 using android::hardware::hidl_handle;
34 using android::hardware::hidl_string;
35 using android::hardware::hidl_vec;
36 using android::hardware::Return;
37 using android::hardware::Void;
38 using android::hardware::tetheroffload::config::V1_0::IOffloadConfig;
39 using android::hardware::tetheroffload::control::V1_0::ITetheringOffloadCallback;
40 using android::hardware::tetheroffload::control::V1_0::NatTimeoutUpdate;
41 using android::hardware::tetheroffload::control::V1_0::OffloadCallbackEvent;
42 
43 constexpr char kCallbackOnEvent[] = "onEvent";
44 constexpr char kCallbackUpdateTimeout[] = "updateTimeout";
45 
46 enum class ExpectBoolean {
47     Ignored = -1,
48     False = 0,
49     True = 1,
50 };
51 
52 class TetheringOffloadCallbackArgs {
53   public:
54     OffloadCallbackEvent last_event;
55     NatTimeoutUpdate last_params;
56 };
57 
58 class OffloadControlTestBase : public testing::TestWithParam<std::tuple<std::string, std::string>> {
59   public:
60     virtual void SetUp() = 0;
61 
62     virtual void TearDown();
63 
64     // Called once in setup stage to retrieve correct version of
65     // IOffloadControl object.
66     virtual sp<android::hardware::tetheroffload::control::V1_0::IOffloadControl> createControl(
67             const std::string& serviceName) = 0;
68 
69     // The IOffloadConfig HAL is tested more thoroughly elsewhere. Here the
70     // class just setup everything correctly and verify basic readiness.
71     void setupConfigHal();
72 
73     virtual void prepareControlHal() = 0;
74 
75     virtual void initOffload(const bool expected_result) = 0;
76 
setupControlHal()77     void setupControlHal() {
78         prepareControlHal();
79         initOffload(true);
80     };
81 
82     void stopOffload(const ExpectBoolean value);
83 
84     // Callback class for both events and NAT timeout updates.
85     class TetheringOffloadCallback
86         : public testing::VtsHalHidlTargetCallbackBase<TetheringOffloadCallbackArgs>,
87           public ITetheringOffloadCallback {
88       public:
89         TetheringOffloadCallback() = default;
90         virtual ~TetheringOffloadCallback() = default;
91 
onEvent(OffloadCallbackEvent event)92         Return<void> onEvent(OffloadCallbackEvent event) override {
93             const TetheringOffloadCallbackArgs args{.last_event = event};
94             NotifyFromCallback(kCallbackOnEvent, args);
95             return Void();
96         };
97 
updateTimeout(const NatTimeoutUpdate & params)98         Return<void> updateTimeout(const NatTimeoutUpdate& params) override {
99             const TetheringOffloadCallbackArgs args{.last_params = params};
100             NotifyFromCallback(kCallbackUpdateTimeout, args);
101             return Void();
102         };
103     };
104 
105     sp<IOffloadConfig> config;
106     sp<android::hardware::tetheroffload::control::V1_0::IOffloadControl> control;
107     sp<TetheringOffloadCallback> control_cb;
108 };