1 /*
2 * Copyright (c) 2022-2023 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 #include <gtest/gtest.h>
17
18 #include "dcamera_source_callback_proxy.h"
19 #include "dcamera_source_callback.h"
20 #include "distributed_camera_errno.h"
21 #include "distributed_camera_constants.h"
22 #include "distributed_camera_source_proxy.h"
23 #include "distributed_hardware_log.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 class DcameraSourceCallbackProxyTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
39 namespace {
40 const std::string TEST_DEVICE_ID = "bb536a637105409e904d4da83790a4a7";
41 const std::string TEST_CAMERA_DH_ID_0 = "camera_0";
42 const std::string TEST_REQID = "bb536a637105409e904d4da83790a4a7";
43 std::string TEST_ATTRS = "";
44 }
45
SetUpTestCase(void)46 void DcameraSourceCallbackProxyTest::SetUpTestCase(void)
47 {
48 DHLOGI("DcameraSourceCallbackProxyTest::SetUpTestCase");
49 }
50
TearDownTestCase(void)51 void DcameraSourceCallbackProxyTest::TearDownTestCase(void)
52 {
53 DHLOGI("DcameraSourceCallbackProxyTest::TearDownTestCase");
54 }
55
SetUp(void)56 void DcameraSourceCallbackProxyTest::SetUp(void)
57 {
58 DHLOGI("DcameraSourceCallbackProxyTest::SetUp");
59 }
60
TearDown(void)61 void DcameraSourceCallbackProxyTest::TearDown(void)
62 {
63 DHLOGI("DcameraSourceCallbackProxyTest::TearDown");
64 }
65
66 /**
67 * @tc.name: dcamera_source_callback_proxy_test_001
68 * @tc.desc: Verify the OnNotifyRegResult OnNotifyUnregResult function.
69 * @tc.type: FUNC
70 * @tc.require: issue
71 */
72 HWTEST_F(DcameraSourceCallbackProxyTest, dcamera_source_callback_proxy_test_001, TestSize.Level1)
73 {
74 DHLOGI("DcameraSourceCallbackProxyTest::dcamera_source_callback_proxy_test_001");
75 sptr<ISystemAbilityManager> samgr =
76 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77 ASSERT_NE(samgr, nullptr);
78 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
79 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
80 EXPECT_EQ(false, callbackProxy == nullptr);
81
82 const std::string devId = "";
83 int32_t status = 0;
84 int32_t ret = callbackProxy->OnNotifyRegResult(devId, TEST_CAMERA_DH_ID_0, TEST_REQID, status, TEST_ATTRS);
85 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
86
87 ret = callbackProxy->OnNotifyUnregResult(devId, TEST_CAMERA_DH_ID_0, TEST_REQID, status, TEST_ATTRS);
88 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
89 }
90
91 /**
92 * @tc.name: dcamera_source_callback_proxy_test_002
93 * @tc.desc: Verify the OnNotifyRegResult OnNotifyUnregResult function.
94 * @tc.type: FUNC
95 * @tc.require: issue
96 */
97 HWTEST_F(DcameraSourceCallbackProxyTest, dcamera_source_callback_proxy_test_002, TestSize.Level1)
98 {
99 DHLOGI("DcameraSourceCallbackProxyTest::dcamera_source_callback_proxy_test_002");
100 sptr<ISystemAbilityManager> samgr =
101 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 ASSERT_NE(samgr, nullptr);
103 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
104 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
105 EXPECT_EQ(false, callbackProxy == nullptr);
106
107 const std::string reqId = "";
108 int32_t status = 0;
109 int32_t ret = callbackProxy->OnNotifyRegResult(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, reqId, status, TEST_ATTRS);
110 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
111
112 ret = callbackProxy->OnNotifyUnregResult(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, reqId, status, TEST_ATTRS);
113 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
114 }
115
116 /**
117 * @tc.name: dcamera_source_callback_proxy_test_003
118 * @tc.desc: Verify the OnNotifyRegResult OnNotifyUnregResult function.
119 * @tc.type: FUNC
120 * @tc.require: issue
121 */
122 HWTEST_F(DcameraSourceCallbackProxyTest, dcamera_source_callback_proxy_test_003, TestSize.Level1)
123 {
124 DHLOGI("DcameraSourceCallbackProxyTest::dcamera_source_callback_proxy_test_003");
125 sptr<ISystemAbilityManager> samgr =
126 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127 ASSERT_NE(samgr, nullptr);
128 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
129 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
130 EXPECT_EQ(false, callbackProxy == nullptr);
131
132 int32_t status = 0;
133 int32_t ret = callbackProxy->OnNotifyRegResult(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, TEST_REQID, status, TEST_ATTRS);
134 EXPECT_EQ(DCAMERA_OK, status);
135
136 ret = callbackProxy->OnNotifyUnregResult(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, TEST_REQID, status, TEST_ATTRS);
137 EXPECT_EQ(DCAMERA_OK, status);
138 }
139
140 /**
141 * @tc.name: dcamera_source_callback_proxy_test_004
142 * @tc.desc: Verify the OnNotifyRegResult function.
143 * @tc.type: FUNC
144 * @tc.require: issue
145 */
146 HWTEST_F(DcameraSourceCallbackProxyTest, dcamera_source_callback_proxy_test_004, TestSize.Level1)
147 {
148 DHLOGI("DcameraSourceCallbackProxyTest::dcamera_source_callback_proxy_test_004");
149 sptr<ISystemAbilityManager> samgr =
150 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
151 ASSERT_NE(samgr, nullptr);
152 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
153 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
154 EXPECT_EQ(false, callbackProxy == nullptr);
155
156 int32_t status = 0;
157 const std::string maxdirstr(257, '1');
158 const std::string testemptystr = "";
159 const std::string teststr = "TESTSTR";
160 std::string maxdirstr1(50 * 1024 * 1024 + 1, '1');
161 std::string emptystr = "";
162
163 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
164 testemptystr, testemptystr, testemptystr, status, emptystr));
165 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
166 maxdirstr, testemptystr, testemptystr, status, emptystr));
167 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
168 teststr, testemptystr, testemptystr, status, emptystr));
169 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
170 teststr, maxdirstr, testemptystr, status, emptystr));
171 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
172 teststr, teststr, testemptystr, status, emptystr));
173 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
174 teststr, teststr, maxdirstr, status, emptystr));
175 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnNotifyRegResult(
176 teststr, teststr, teststr, status, maxdirstr1));
177 }
178
179 /**
180 * @tc.name: CheckParams_001
181 * @tc.desc: Verify the CheckParams function.
182 * @tc.type: FUNC
183 * @tc.require: issue
184 */
185 HWTEST_F(DcameraSourceCallbackProxyTest, CheckParams_001, TestSize.Level1)
186 {
187 DHLOGI("DcameraSourceCallbackProxyTest::CheckParams_001");
188 sptr<ISystemAbilityManager> samgr =
189 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
190 ASSERT_NE(samgr, nullptr);
191 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
192 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
193
194 EXPECT_EQ(false, callbackProxy->CheckParams("", "dhId", 0));
195 EXPECT_EQ(false, callbackProxy->CheckParams("devId", "", 0));
196
197 std::string devId(257, '1');
198 EXPECT_EQ(false, callbackProxy->CheckParams(devId, "dhId", 0));
199
200 std::string dhId(257, '1');
201 EXPECT_EQ(false, callbackProxy->CheckParams("devId", dhId, 0));
202
203 EXPECT_EQ(false, callbackProxy->CheckParams("devId", "dhId", -1));
204 EXPECT_EQ(true, callbackProxy->CheckParams("devId", "dhId", 0));
205 }
206
207 /**
208 * @tc.name: OnDataSyncTrigger_001
209 * @tc.desc: Verify the OnDataSyncTrigger function.
210 * @tc.type: FUNC
211 * @tc.require: issue
212 */
213 HWTEST_F(DcameraSourceCallbackProxyTest, OnDataSyncTrigger_001, TestSize.Level1)
214 {
215 DHLOGI("DcameraSourceCallbackProxyTest::OnDataSyncTrigger_001");
216 sptr<ISystemAbilityManager> samgr =
217 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
218 ASSERT_NE(samgr, nullptr);
219 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
220 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
221
222 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnDataSyncTrigger(""));
223 std::string devId(257, '1');
224 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnDataSyncTrigger(devId));
225 }
226
227 /**
228 * @tc.name: OnDataSyncTrigger_002
229 * @tc.desc: Verify the OnDataSyncTrigger function.
230 * @tc.type: FUNC
231 * @tc.require: issue
232 */
233 HWTEST_F(DcameraSourceCallbackProxyTest, OnDataSyncTrigger_002, TestSize.Level1)
234 {
235 DHLOGI("DcameraSourceCallbackProxyTest::OnDataSyncTrigger_002");
236
237 sptr<DCameraSourceCallback> remoteObject = sptr<DCameraSourceCallback>(new DCameraSourceCallback());
238 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
239
240 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnDataSyncTrigger(TEST_DEVICE_ID));
241 }
242
243 /**
244 * @tc.name: OnHardwareStateChanged_001
245 * @tc.desc: Verify the OnHardwareStateChanged function.
246 * @tc.type: FUNC
247 * @tc.require: issue
248 */
249 HWTEST_F(DcameraSourceCallbackProxyTest, OnHardwareStateChanged_001, TestSize.Level1)
250 {
251 DHLOGI("DcameraSourceCallbackProxyTest::OnHardwareStateChanged_001");
252 sptr<ISystemAbilityManager> samgr =
253 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
254 ASSERT_NE(samgr, nullptr);
255 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
256 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
257
258 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnHardwareStateChanged("", "", -1));
259 }
260
261 /**
262 * @tc.name: OnHardwareStateChanged_002
263 * @tc.desc: Verify the OnHardwareStateChanged function.
264 * @tc.type: FUNC
265 * @tc.require: issue
266 */
267 HWTEST_F(DcameraSourceCallbackProxyTest, OnHardwareStateChanged_002, TestSize.Level1)
268 {
269 DHLOGI("DcameraSourceCallbackProxyTest::OnHardwareStateChanged_002");
270
271 sptr<DCameraSourceCallback> remoteObject = sptr<DCameraSourceCallback>(new DCameraSourceCallback());
272 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
273
274 EXPECT_EQ(DCAMERA_BAD_VALUE, callbackProxy->OnHardwareStateChanged(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, 0));
275 }
276 } // namespace DistributedHardware
277 } // namespace OHOS