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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_001, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_002, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_003, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_CancelTest_002, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_001, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_002, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_003, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_004, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_005, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_006, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, PlayVibratorCustom_018, TestSize.Level1)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 
HWTEST_F(NativeVibratorTest, PlayVibratorCustom_019, TestSize.Level1)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 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_020, TestSize.Level1)
317 {
318     CALL_LOG_ENTER;
319     FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
320     MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
321     struct stat64 statbuf = { 0 };
322     if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
323         Vibrator_FileDescription fileDescription = {
324             .fd = fileDescriptor.fd,
325             .offset = 0,
326             .length = statbuf.st_size
327         };
328         Vibrator_Attribute vibrateAttribute = {
329             .usage = VIBRATOR_USAGE_MAX
330         };
331         int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
332         bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
333         ASSERT_TRUE(isSuccess);
334     }
335     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
336     OH_Vibrator_Cancel();
337 }
338 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_021, TestSize.Level1)339 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_021, TestSize.Level1)
340 {
341     CALL_LOG_ENTER;
342     FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
343     MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
344     struct stat64 statbuf = { 0 };
345     if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
346         Vibrator_FileDescription fileDescription = {
347             .fd = -1,
348             .offset = 0,
349             .length = statbuf.st_size
350         };
351         Vibrator_Attribute vibrateAttribute = {
352             .usage = VIBRATOR_USAGE_RING
353         };
354         int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
355         bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
356         ASSERT_TRUE(isSuccess);
357     }
358     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
359     OH_Vibrator_Cancel();
360 }
361 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_022, TestSize.Level1)362 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_022, TestSize.Level1)
363 {
364     CALL_LOG_ENTER;
365     FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
366     MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
367     struct stat64 statbuf = { 0 };
368     if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
369         Vibrator_FileDescription fileDescription = {
370             .fd = fileDescriptor.fd,
371             .offset = -1,
372             .length = statbuf.st_size
373         };
374         Vibrator_Attribute vibrateAttribute = {
375             .usage = VIBRATOR_USAGE_RING
376         };
377         int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
378         bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
379         ASSERT_TRUE(isSuccess);
380     }
381     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
382     OH_Vibrator_Cancel();
383 }
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_023, TestSize.Level1)384 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_023, TestSize.Level1)
385 {
386     CALL_LOG_ENTER;
387     FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
388     MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
389     struct stat64 statbuf = { 0 };
390     if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
391         Vibrator_FileDescription fileDescription = {
392             .fd = fileDescriptor.fd,
393             .offset = 0,
394             .length = -1
395         };
396         Vibrator_Attribute vibrateAttribute = {
397             .usage = VIBRATOR_USAGE_RING
398         };
399         int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
400         bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
401         ASSERT_TRUE(isSuccess);
402     }
403     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
404     OH_Vibrator_Cancel();
405 }
406 
HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_024, TestSize.Level1)407 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_024, TestSize.Level1)
408 {
409     CALL_LOG_ENTER;
410     FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
411     MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
412     struct stat64 statbuf = { 0 };
413     if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
414         Vibrator_FileDescription fileDescription = {
415             .fd = fileDescriptor.fd,
416             .offset = 0,
417             .length = statbuf.st_size
418         };
419         Vibrator_Attribute vibrateAttribute = {
420             .usage = VIBRATOR_USAGE_MAX
421         };
422         int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
423         bool isSuccess = ((ret != 0) || (ret == UNSUPPORTED));
424         ASSERT_TRUE(isSuccess);
425     }
426     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
427     OH_Vibrator_Cancel();
428 }
429 } // namespace Sensors
430 } // namespace OHOS
431