1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMON_NET_DIAG_CALLBACK_TEST_H
17 #define COMMON_NET_DIAG_CALLBACK_TEST_H
18 
19 #include "net_diag_callback_stub.h"
20 #include "net_manager_constants.h"
21 #include "netnative_log_wrapper.h"
22 #include "netsys_ipc_interface_code.h"
23 #include "netsys_net_diag_data.h"
24 #include "notify_callback_stub.h"
25 
26 namespace OHOS {
27 namespace NetsysNative {
28 namespace {
29 bool g_waitPingSync = false;
30 } // namespace
31 
32 class NetDiagCallbackStubTest : public IRemoteStub<NetsysNative::INetDiagCallback> {
33 public:
NetDiagCallbackStubTest()34     NetDiagCallbackStubTest()
35     {
36         memberFuncMap_[static_cast<uint32_t>(NetsysNative::NetDiagInterfaceCode::ON_NOTIFY_PING_RESULT)] =
37             &NetDiagCallbackStubTest::CmdNotifyPingResult;
38     }
39     virtual ~NetDiagCallbackStubTest() = default;
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
42     {
43         NETNATIVE_LOGI("Stub call start, code:[%{public}d]", code);
44         std::u16string myDescriptor = NetsysNative::NetDiagCallbackStub::GetDescriptor();
45         std::u16string remoteDescriptor = data.ReadInterfaceToken();
46         if (myDescriptor != remoteDescriptor) {
47             NETNATIVE_LOGE("Descriptor checked failed");
48             return NetManagerStandard::NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
49         }
50 
51         auto itFunc = memberFuncMap_.find(code);
52         if (itFunc != memberFuncMap_.end()) {
53             auto requestFunc = itFunc->second;
54             if (requestFunc != nullptr) {
55                 return (this->*requestFunc)(data, reply);
56             }
57         }
58 
59         NETNATIVE_LOGI("Stub default case, need check");
60         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61     }
OnNotifyPingResult(const NetsysNative::NetDiagPingResult & pingResult)62     int32_t OnNotifyPingResult(const NetsysNative::NetDiagPingResult &pingResult) override
63     {
64         g_waitPingSync = false;
65         NETNATIVE_LOGI(
66             "OnNotifyPingResult received dateSize_:%{public}d payloadSize_:%{public}d transCount_:%{public}d "
67             "recvCount_:%{public}d",
68             pingResult.dateSize_, pingResult.payloadSize_, pingResult.transCount_, pingResult.recvCount_);
69         return NetManagerStandard::NETMANAGER_SUCCESS;
70     }
71 
72 private:
73     using NetDiagCallbackFunc = int32_t (NetDiagCallbackStubTest::*)(MessageParcel &, MessageParcel &);
74 
75 private:
CmdNotifyPingResult(MessageParcel & data,MessageParcel & reply)76     int32_t CmdNotifyPingResult(MessageParcel &data, MessageParcel &reply)
77     {
78         NETNATIVE_LOGI("CmdNotifyPingResult received CmdNotifyPingResult");
79 
80         NetsysNative::NetDiagPingResult pingResult;
81         if (!NetsysNative::NetDiagPingResult::Unmarshalling(data, pingResult)) {
82             return NetManagerStandard::NETMANAGER_ERR_READ_DATA_FAIL;
83         }
84 
85         int32_t result = OnNotifyPingResult(pingResult);
86         if (!reply.WriteInt32(result)) {
87             return NetManagerStandard::NETMANAGER_ERR_WRITE_REPLY_FAIL;
88         }
89         return NetManagerStandard::NETMANAGER_SUCCESS;
90     }
91 
92 private:
93     std::map<uint32_t, NetDiagCallbackFunc> memberFuncMap_;
94 };
95 } // namespace NetsysNative
96 } // namespace OHOS
97 #endif // COMMON_NET_DIAG_CALLBACK_TEST_H
98