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