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
28 #undef LOG_TAG
29 #define LOG_TAG "NativeVibratorTest"
30
31 namespace OHOS {
32 namespace Sensors {
33 using namespace testing::ext;
34 using namespace Security::AccessToken;
35 using Security::AccessToken::AccessTokenID;
36
37 namespace {
38 uint32_t g_duration = 300;
39 constexpr int32_t TIME_WAIT_FOR_OP = 2;
40 PermissionDef infoManagerTestPermDef_ = {
41 .permissionName = "ohos.permission.VIBRATE",
42 .bundleName = "accesstoken_test",
43 .grantMode = 1,
44 .label = "label",
45 .labelId = 1,
46 .description = "native vibrator test",
47 .descriptionId = 1,
48 .availableLevel = APL_NORMAL
49 };
50
51 PermissionStateFull infoManagerTestState_ = {
52 .grantFlags = {1},
53 .grantStatus = {PermissionState::PERMISSION_GRANTED},
54 .isGeneral = true,
55 .permissionName = "ohos.permission.VIBRATE",
56 .resDeviceID = {"local"}
57 };
58
59 HapPolicyParams infoManagerTestPolicyPrams_ = {
60 .apl = APL_NORMAL,
61 .domain = "test.domain",
62 .permList = {infoManagerTestPermDef_},
63 .permStateList = {infoManagerTestState_}
64 };
65
66 HapInfoParams infoManagerTestInfoParms_ = {
67 .bundleName = "vibratoragent_test",
68 .userID = 1,
69 .instIndex = 0,
70 .appIDDesc = "NativeVibratorTest"
71 };
72 } // namespace
73
74 class NativeVibratorTest : public testing::Test {
75 public:
76 static void SetUpTestCase();
77 static void TearDownTestCase();
78 void SetUp();
79 void TearDown();
80
81 private:
82 static AccessTokenID tokenID_;
83 };
84
85 struct FileDescriptor {
FileDescriptorOHOS::Sensors::FileDescriptor86 explicit FileDescriptor(const std::string &path)
87 {
88 fd = open(path.c_str(), O_RDONLY);
89 }
~FileDescriptorOHOS::Sensors::FileDescriptor90 ~FileDescriptor()
91 {
92 close(fd);
93 }
94 int32_t fd;
95 };
96
97 AccessTokenID NativeVibratorTest::tokenID_ = 0;
98
SetUpTestCase()99 void NativeVibratorTest::SetUpTestCase()
100 {
101 AccessTokenIDEx tokenIdEx = {0};
102 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_);
103 tokenID_ = tokenIdEx.tokenIdExStruct.tokenID;
104 ASSERT_NE(0, tokenID_);
105 ASSERT_EQ(0, SetSelfTokenID(tokenID_));
106 }
107
TearDownTestCase()108 void NativeVibratorTest::TearDownTestCase()
109 {
110 int32_t ret = AccessTokenKit::DeleteToken(tokenID_);
111 if (tokenID_ != 0) {
112 ASSERT_EQ(RET_SUCCESS, ret);
113 }
114 }
115
SetUp()116 void NativeVibratorTest::SetUp()
117 {}
118
TearDown()119 void NativeVibratorTest::TearDown()
120 {}
121
122 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_001, TestSize.Level1)
123 {
124 CALL_LOG_ENTER;
125 Vibrator_Attribute vibrateAttribute;
126 vibrateAttribute.usage = VIBRATOR_USAGE_ALARM;
127
128 int32_t ret = OH_Vibrator_PlayVibration(0, vibrateAttribute);
129 ASSERT_EQ(ret, PARAMETER_ERROR);
130 }
131
132 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationTest_002, TestSize.Level1)
133 {
134 CALL_LOG_ENTER;
135 Vibrator_Attribute vibrateAttribute = {
136 .usage = VIBRATOR_USAGE_RING
137 };
138 int32_t ret = OH_Vibrator_PlayVibration(g_duration, vibrateAttribute);
139 ASSERT_EQ(ret, RET_SUCCESS);
140 ret = OH_Vibrator_Cancel();
141 ASSERT_EQ(ret, RET_SUCCESS);
142 }
143
144 HWTEST_F(NativeVibratorTest, OH_Vibrator_CancelTest_002, TestSize.Level1)
145 {
146 CALL_LOG_ENTER;
147 int32_t ret = OH_Vibrator_Cancel();
148 MISC_HILOGI("ret is %{public}d", ret);
149 ASSERT_NE(ret, RET_SUCCESS);
150 }
151
152 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_001, TestSize.Level1)
153 {
154 CALL_LOG_ENTER;
155 FileDescriptor fileDescriptor("/data/test/vibrator/coin_drop.json");
156 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
157 struct stat64 statbuf = { 0 };
158 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
159 Vibrator_FileDescription fileDescription = {
160 .fd = fileDescriptor.fd,
161 .offset = 0,
162 .length = statbuf.st_size
163 };
164 Vibrator_Attribute vibrateAttribute = {
165 .usage = VIBRATOR_USAGE_RING
166 };
167 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
168 bool isSuccess = ((ret == 0) || (ret == UNSUPPORTED));
169 ASSERT_EQ(isSuccess, true);
170 }
171 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
172 OH_Vibrator_Cancel();
173 }
174
175 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_002, TestSize.Level1)
176 {
177 CALL_LOG_ENTER;
178 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_type.json");
179 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
180 struct stat64 statbuf = { 0 };
181 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
182 Vibrator_FileDescription fileDescription = {
183 .fd = fileDescriptor.fd,
184 .offset = 0,
185 .length = statbuf.st_size
186 };
187 Vibrator_Attribute vibrateAttribute = {
188 .usage = VIBRATOR_USAGE_RING
189 };
190 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
191 ASSERT_NE(ret, 0);
192 }
193 }
194
195 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_003, TestSize.Level1)
196 {
197 CALL_LOG_ENTER;
198 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_startTime.json");
199 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
200 struct stat64 statbuf = { 0 };
201 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
202 Vibrator_FileDescription fileDescription = {
203 .fd = fileDescriptor.fd,
204 .offset = 0,
205 .length = statbuf.st_size
206 };
207 Vibrator_Attribute vibrateAttribute = {
208 .usage = VIBRATOR_USAGE_RING
209 };
210 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
211 ASSERT_NE(ret, 0);
212 }
213 }
214
215 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_004, TestSize.Level1)
216 {
217 CALL_LOG_ENTER;
218 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_duration.json");
219 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
220 struct stat64 statbuf = { 0 };
221 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
222 Vibrator_FileDescription fileDescription = {
223 .fd = fileDescriptor.fd,
224 .offset = 0,
225 .length = statbuf.st_size
226 };
227 Vibrator_Attribute vibrateAttribute = {
228 .usage = VIBRATOR_USAGE_RING
229 };
230 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
231 ASSERT_NE(ret, 0);
232 }
233 }
234
235 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_005, TestSize.Level1)
236 {
237 CALL_LOG_ENTER;
238 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_intensity.json");
239 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
240 struct stat64 statbuf = { 0 };
241 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
242 Vibrator_FileDescription fileDescription = {
243 .fd = fileDescriptor.fd,
244 .offset = 0,
245 .length = statbuf.st_size
246 };
247 Vibrator_Attribute vibrateAttribute = {
248 .usage = VIBRATOR_USAGE_RING
249 };
250 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
251 ASSERT_NE(ret, 0);
252 }
253 }
254
255 HWTEST_F(NativeVibratorTest, OH_Vibrator_PlayVibrationCustom_006, TestSize.Level1)
256 {
257 CALL_LOG_ENTER;
258 FileDescriptor fileDescriptor("/data/test/vibrator/test_invalid_frequency.json");
259 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
260 struct stat64 statbuf = { 0 };
261 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
262 Vibrator_FileDescription fileDescription = {
263 .fd = fileDescriptor.fd,
264 .offset = 0,
265 .length = statbuf.st_size
266 };
267 Vibrator_Attribute vibrateAttribute = {
268 .usage = VIBRATOR_USAGE_RING
269 };
270 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
271 ASSERT_NE(ret, 0);
272 }
273 }
274
275 HWTEST_F(NativeVibratorTest, PlayVibratorCustom_018, TestSize.Level1)
276 {
277 CALL_LOG_ENTER;
278 FileDescriptor fileDescriptor("/data/test/vibrator/test_129_event.json");
279 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
280 struct stat64 statbuf = { 0 };
281 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
282 Vibrator_FileDescription fileDescription = {
283 .fd = fileDescriptor.fd,
284 .offset = 0,
285 .length = statbuf.st_size
286 };
287 Vibrator_Attribute vibrateAttribute = {
288 .usage = VIBRATOR_USAGE_RING
289 };
290 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
291 ASSERT_NE(ret, 0);
292 }
293 }
294
295 HWTEST_F(NativeVibratorTest, PlayVibratorCustom_019, TestSize.Level1)
296 {
297 CALL_LOG_ENTER;
298 FileDescriptor fileDescriptor("/data/test/vibrator/test_big_file_size.json");
299 MISC_HILOGD("Test fd:%{public}d", fileDescriptor.fd);
300 struct stat64 statbuf = { 0 };
301 if (fstat64(fileDescriptor.fd, &statbuf) == 0) {
302 Vibrator_FileDescription fileDescription = {
303 .fd = fileDescriptor.fd,
304 .offset = 0,
305 .length = statbuf.st_size
306 };
307 Vibrator_Attribute vibrateAttribute = {
308 .usage = VIBRATOR_USAGE_RING
309 };
310 int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
311 ASSERT_NE(ret, 0);
312 }
313 }
314 } // namespace Sensors
315 } // namespace OHOS
316