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 OHOS_TEST_CAPI_SKELETON_H
17 #define OHOS_TEST_CAPI_SKELETON_H
18 
19 #include "ipc_cremote_object.h"
20 #include "test_service_skeleton.h"
21 
22 #include <random>
23 
24 namespace OHOS {
25 
26 enum NativeTestCommand : uint32_t {
27     NATIVE_TEST_CMD_SYNC_ADD = 1,
28     NATIVE_TEST_CMD_ASYNC_ADD = 2,
29     NATIVE_TEST_CMD_SYNC_ADD_REPEAT = 3,
30     NATIVE_TEST_CMD_ASYNC_ADD_REPEAT = 4,
31     NATIVE_TEST_CMD_SEND_AND_ECHO_BASE = 5,
32     NATIVE_TEST_CMD_SEND_AND_ECHO_SRING = 6,
33     NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER = 7,
34     NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR = 8,
35     NATIVE_TEST_CMD_SEND_ERROR_CODE = 9,
36     NATIVE_TEST_CMD_MAX_VALUE,
37 };
38 
39 const std::string NATIVEREMOTESTUBTEST_DESCRIPTOR = "native.remote.stub";
40 const std::string NATIVEREMOTESTUBCALLBACKTEST_DESCRIPTOR = "native.remote.stubCallBack";
41 
42 class NativeRemoteBase {
43 public:
44     explicit NativeRemoteBase(const sptr<ITestService> &testService);
45     virtual ~NativeRemoteBase() = default;
46 
47     virtual int SyncAdd() = 0;
48     virtual int ASyncAdd() = 0;
49     virtual int SendAndEchoBase() = 0;
50     virtual int SendAndEchoString() = 0;
51     virtual int SendAndEchoBuffer() = 0;
52     virtual int SendAndEchoFileDescriptor() = 0;
53     virtual int SendErrorCode() = 0;
54 
55 protected:
56     sptr<ITestService> testService_;
57 };
58 
59 class NativeRemoteProxyTest : public NativeRemoteBase {
60 public:
61     explicit NativeRemoteProxyTest(const sptr<ITestService> &testService);
62     ~NativeRemoteProxyTest() override;
63 
64     int SyncAdd() override;
65     int ASyncAdd() override;
66     int SendAndEchoBase() override;
67     int SendAndEchoString() override;
68     int SendAndEchoBuffer() override;
69     int SendAndEchoFileDescriptor() override;
70     int SendErrorCode() override;
71     int AddParallel(bool isSync);
72 
73 private:
74     void SendBasicDataType(OHIPCParcel *data);
75     int TestBasicDataTypeReply(const OHIPCParcel *reply);
76     void SendAsyncReply(int &replyValue);
77     int WaitForAsyncReply(int timeout);
78     static int OnRemoteRequestStubCallBack(uint32_t code, const OHIPCParcel *data,
79         OHIPCParcel *reply, void *userData);
80 
81 public:
82     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TEST_NATIVE_IPC_SKELETON" };
83 
84 private:
85     int asyncReply_{ 0 };
86     std::mutex mutex_;
87     std::condition_variable cv_;
88     std::random_device randomDevice_;
89     std::uniform_int_distribution<> randomDistribution_{ 1, 10000 };
90 
91     OHIPCRemoteProxy *proxy_{ nullptr };
92     OHIPCRemoteStub *stubCallBack_{ nullptr };
93 };
94 
95 class NativeRemoteStubTest : public NativeRemoteBase {
96 public:
97     explicit NativeRemoteStubTest(const sptr<ITestService> &testService);
98     ~NativeRemoteStubTest() override;
99 
100     int RegisterRemoteStub();
101     int UnRegisterRemoteStub();
102     static int OnRemoteRequest(uint32_t code, const OHIPCParcel *data, OHIPCParcel *reply, void *userData);
103 
104 private:
105     int SyncAdd() override;
106     int ASyncAdd() override;
107     int SendAndEchoBase() override;
108     int SendAndEchoString() override;
109     int SendAndEchoBuffer() override;
110     int SendAndEchoFileDescriptor() override;
111     int SendErrorCode() override;
112 
113 private:
114     [[maybe_unused]] static thread_local const OHIPCParcel *currentData_;
115     [[maybe_unused]] static thread_local OHIPCParcel *currentReply_;
116     OHIPCRemoteStub* stub_{ nullptr };
117     static std::map<int, std::function<int(NativeRemoteStubTest *stub)>> funcMap_;
118     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TEST_NATIVE_IPC_SKELETON" };
119 };
120 
121 } // OHOS
122 
123 #endif // OHOS_TEST_CAPI_SKELETON_H
124