1 /*
2 * Copyright (c) 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <thread>
19
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "parameters.h"
23 #include "token_setproc.h"
24
25 #include "sensors_errors.h"
26 #include "vibrator.h"
27 #include "vibrator_agent.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "NativeVibratorTest"
31
32 namespace OHOS {
33 namespace Sensors {
34 using namespace testing::ext;
35 using namespace Security::AccessToken;
36 using Security::AccessToken::AccessTokenID;
37
38 namespace {
39 uint32_t g_duration = 300;
40 constexpr int32_t TIME_WAIT_FOR_OP = 2;
41
42 PermissionStateFull infoManagerTestState_ = {
43 .grantFlags = {1},
44 .grantStatus = {PermissionState::PERMISSION_GRANTED},
45 .isGeneral = true,
46 .permissionName = "ohos.permission.VIBRATE",
47 .resDeviceID = {"local"}
48 };
49
50 HapPolicyParams infoManagerTestPolicyPrams_ = {
51 .apl = APL_NORMAL,
52 .domain = "test.domain",
53 .permList = {},
54 .permStateList = {infoManagerTestState_}
55 };
56
57 HapInfoParams infoManagerTestInfoParms_ = {
58 .bundleName = "vibratoragent_test",
59 .userID = 1,
60 .instIndex = 0,
61 .appIDDesc = "NativeVibratorTest"
62 };
63 } // namespace
64
65 class NativeVibratorTest : public testing::Test {
66 public:
67 static void SetUpTestCase();
68 static void TearDownTestCase();
69 void SetUp();
70 void TearDown();
71
72 private:
73 static AccessTokenID tokenID_;
74 };
75
76 struct FileDescriptor {
FileDescriptorOHOS::Sensors::FileDescriptor77 explicit FileDescriptor(const std::string &path)
78 {
79 fd = open(path.c_str(), O_RDONLY);
80 }
~FileDescriptorOHOS::Sensors::FileDescriptor81 ~FileDescriptor()
82 {
83 close(fd);
84 }
85 int32_t fd;
86 };
87
88 AccessTokenID NativeVibratorTest::tokenID_ = 0;
89
SetUpTestCase()90 void NativeVibratorTest::SetUpTestCase()
91 {
92 AccessTokenIDEx tokenIdEx = {0};
93 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_);
94 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
95 ASSERT_NE(0, tokenID_);
96 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
97 }
98
TearDownTestCase()99 void NativeVibratorTest::TearDownTestCase()
100 {
101 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
102 if (tokenID_ != 0) {
103 ASSERT_EQ(RET_SUCCESS, ret);
104 }
105 }
106
SetUp()107 void NativeVibratorTest::SetUp()
108 {}
109
TearDown()110 void NativeVibratorTest::TearDown()
111 {}
112
113 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_001, TestSize.Level1)
114 {
115 CALL_LOG_ENTER;
116 Vibrator_Attribute vibrateAttribute;
117 vibrateAttribute.usage = VIBRATOR_USAGE_ALARM;
118
119 int32_t ret = OH_Vibrator_PlayVibration(0, vibrateAttribute);
120 ASSERT_EQ(ret, PARAMETER_ERROR);
121 }
122
123 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_002, TestSize.Level1)
124 {
125 CALL_LOG_ENTER;
126 Vibrator_Attribute vibrateAttribute = {
127 .usage = VIBRATOR_USAGE_RING
128 };
129 int32_t ret = OH_Vibrator_PlayVibration(g_duration, vibrateAttribute);
130 ASSERT_EQ(ret, RET_SUCCESS);
131 ret = OH_Vibrator_Cancel();
132 ASSERT_EQ(ret, RET_SUCCESS);
133 }
134
135 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_003, TestSize.Level1)
136 {
137 CALL_LOG_ENTER;
138 Vibrator_Attribute vibrateAttribute = {
139 .usage = VIBRATOR_USAGE_MAX
140 };
141 int32_t ret = OH_Vibrator_PlayVibration(g_duration, vibrateAttribute);
142 ASSERT_EQ(ret, PARAMETER_ERROR);
143 }
144
145 HWTEST_F(NativeVibratorTest, OH_Vibrator_CancelTest_002, TestSize.Level1)
146 {
147 CALL_LOG_ENTER;
148 int32_t ret = OH_Vibrator_Cancel();
149 MISC_HILOGI("ret is %{public}d", ret);
150 ASSERT_NE(ret, RET_SUCCESS);
151 }
152
153 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_001, TestSize.Level1)
154 {
155 CALL_LOG_ENTER;
156 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
157 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
158 struct stat64 statbuf = { 0 };
159 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
160 Vibrator_FileDescription fileDescription = {
161 .fd = fileDescriptor.fd,
162 .offset = 0,
163 .length = statbuf.st_size
164 };
165 Vibrator_Attribute vibrateAttribute = {
166 .usage = VIBRATOR_USAGE_RING
167 };
168 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
169 bool isSuccess = ((ret == 0) || (ret == UNSUPPORTED));
170 ASSERT_EQ(isSuccess, true);
171 }
172 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
173 OH_Vibrator_Cancel();
174 }
175
176 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_002, TestSize.Level1)
177 {
178 CALL_LOG_ENTER;
179 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_type.json");
180 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
181 struct stat64 statbuf = { 0 };
182 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
183 Vibrator_FileDescription fileDescription = {
184 .fd = fileDescriptor.fd,
185 .offset = 0,
186 .length = statbuf.st_size
187 };
188 Vibrator_Attribute vibrateAttribute = {
189 .usage = VIBRATOR_USAGE_RING
190 };
191 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
192 ASSERT_NE(ret, 0);
193 }
194 }
195
196 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_003, TestSize.Level1)
197 {
198 CALL_LOG_ENTER;
199 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_startTime.json");
200 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
201 struct stat64 statbuf = { 0 };
202 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
203 Vibrator_FileDescription fileDescription = {
204 .fd = fileDescriptor.fd,
205 .offset = 0,
206 .length = statbuf.st_size
207 };
208 Vibrator_Attribute vibrateAttribute = {
209 .usage = VIBRATOR_USAGE_RING
210 };
211 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
212 ASSERT_NE(ret, 0);
213 }
214 }
215
216 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_004, TestSize.Level1)
217 {
218 CALL_LOG_ENTER;
219 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_duration.json");
220 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
221 struct stat64 statbuf = { 0 };
222 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
223 Vibrator_FileDescription fileDescription = {
224 .fd = fileDescriptor.fd,
225 .offset = 0,
226 .length = statbuf.st_size
227 };
228 Vibrator_Attribute vibrateAttribute = {
229 .usage = VIBRATOR_USAGE_RING
230 };
231 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
232 ASSERT_NE(ret, 0);
233 }
234 }
235
236 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_005, TestSize.Level1)
237 {
238 CALL_LOG_ENTER;
239 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_intensity.json");
240 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
241 struct stat64 statbuf = { 0 };
242 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
243 Vibrator_FileDescription fileDescription = {
244 .fd = fileDescriptor.fd,
245 .offset = 0,
246 .length = statbuf.st_size
247 };
248 Vibrator_Attribute vibrateAttribute = {
249 .usage = VIBRATOR_USAGE_RING
250 };
251 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
252 ASSERT_NE(ret, 0);
253 }
254 }
255
256 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_006, TestSize.Level1)
257 {
258 CALL_LOG_ENTER;
259 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_frequency.json");
260 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
261 struct stat64 statbuf = { 0 };
262 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
263 Vibrator_FileDescription fileDescription = {
264 .fd = fileDescriptor.fd,
265 .offset = 0,
266 .length = statbuf.st_size
267 };
268 Vibrator_Attribute vibrateAttribute = {
269 .usage = VIBRATOR_USAGE_RING
270 };
271 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
272 ASSERT_NE(ret, 0);
273 }
274 }
275
276 HWTEST_F(NativeVibratorTest, PlayVibratorCustom_018, TestSize.Level1)
277 {
278 CALL_LOG_ENTER;
279 FileDescriptor fileDescriptor("/data/test/vibrator/test_129_event.json");
280 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
281 struct stat64 statbuf = { 0 };
282 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
283 Vibrator_FileDescription fileDescription = {
284 .fd = fileDescriptor.fd,
285 .offset = 0,
286 .length = statbuf.st_size
287 };
288 Vibrator_Attribute vibrateAttribute = {
289 .usage = VIBRATOR_USAGE_RING
290 };
291 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
292 ASSERT_NE(ret, 0);
293 }
294 }
295
296 HWTEST_F(NativeVibratorTest, PlayVibratorCustom_019, TestSize.Level1)
297 {
298 CALL_LOG_ENTER;
299 FileDescriptor fileDescriptor("/data/test/vibrator/test_big_file_size.json");
300 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
301 struct stat64 statbuf = { 0 };
302 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
303 Vibrator_FileDescription fileDescription = {
304 .fd = fileDescriptor.fd,
305 .offset = 0,
306 .length = statbuf.st_size
307 };
308 Vibrator_Attribute vibrateAttribute = {
309 .usage = VIBRATOR_USAGE_RING
310 };
311 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
312 ASSERT_NE(ret, 0);
313 }
314 }
315 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_020, TestSize.Level1)
316 {
317 CALL_LOG_ENTER;
318 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
319 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
320 struct stat64 statbuf = { 0 };
321 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
322 Vibrator_FileDescription fileDescription = {
323 .fd = fileDescriptor.fd,
324 .offset = 0,
325 .length = statbuf.st_size
326 };
327 Vibrator_Attribute vibrateAttribute = {
328 .usage = VIBRATOR_USAGE_MAX
329 };
330 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
331 bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
332 ASSERT_TRUE(isSuccess);
333 }
334 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
335 OH_Vibrator_Cancel();
336 }
337
338 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_021, TestSize.Level1)
339 {
340 CALL_LOG_ENTER;
341 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
342 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
343 struct stat64 statbuf = { 0 };
344 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
345 Vibrator_FileDescription fileDescription = {
346 .fd = -1,
347 .offset = 0,
348 .length = statbuf.st_size
349 };
350 Vibrator_Attribute vibrateAttribute = {
351 .usage = VIBRATOR_USAGE_RING
352 };
353 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
354 bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
355 ASSERT_TRUE(isSuccess);
356 }
357 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
358 OH_Vibrator_Cancel();
359 }
360
361 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_022, TestSize.Level1)
362 {
363 CALL_LOG_ENTER;
364 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
365 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
366 struct stat64 statbuf = { 0 };
367 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
368 Vibrator_FileDescription fileDescription = {
369 .fd = fileDescriptor.fd,
370 .offset = -1,
371 .length = statbuf.st_size
372 };
373 Vibrator_Attribute vibrateAttribute = {
374 .usage = VIBRATOR_USAGE_RING
375 };
376 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
377 bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
378 ASSERT_TRUE(isSuccess);
379 }
380 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
381 OH_Vibrator_Cancel();
382 }
383 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_023, TestSize.Level1)
384 {
385 CALL_LOG_ENTER;
386 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
387 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
388 struct stat64 statbuf = { 0 };
389 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
390 Vibrator_FileDescription fileDescription = {
391 .fd = fileDescriptor.fd,
392 .offset = 0,
393 .length = -1
394 };
395 Vibrator_Attribute vibrateAttribute = {
396 .usage = VIBRATOR_USAGE_RING
397 };
398 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
399 bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
400 ASSERT_TRUE(isSuccess);
401 }
402 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
403 OH_Vibrator_Cancel();
404 }
405
406 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_024, TestSize.Level1)
407 {
408 CALL_LOG_ENTER;
409 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
410 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
411 struct stat64 statbuf = { 0 };
412 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
413 Vibrator_FileDescription fileDescription = {
414 .fd = fileDescriptor.fd,
415 .offset = 0,
416 .length = statbuf.st_size
417 };
418 Vibrator_Attribute vibrateAttribute = {
419 .usage = VIBRATOR_USAGE_MAX
420 };
421 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
422 bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
423 ASSERT_TRUE(isSuccess);
424 }
425 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
426 OH_Vibrator_Cancel();
427 }
428 } // namespace Sensors
429 } // namespace OHOS
430