1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "UserHalHelper.h"
18
19 #include <gtest/gtest.h>
20
21 #include <cstdint>
22
23 #include "gmock/gmock.h"
24
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace vehicle {
29 namespace V2_0 {
30
31 namespace user_hal_helper {
32
33 namespace {
34
35 using testing::Eq;
36 using testing::Gt;
37 using testing::IsNull;
38 using testing::NotNull;
39 using testing::Pointee;
40
41 constexpr int32_t INITIAL_USER_INFO = static_cast<int32_t>(VehicleProperty::INITIAL_USER_INFO);
42 constexpr int32_t SWITCH_USER = static_cast<int32_t>(VehicleProperty::SWITCH_USER);
43 constexpr int32_t CREATE_USER = static_cast<int32_t>(VehicleProperty::CREATE_USER);
44 constexpr int32_t REMOVE_USER = static_cast<int32_t>(VehicleProperty::REMOVE_USER);
45 constexpr int32_t USER_IDENTIFICATION_ASSOCIATION =
46 static_cast<int32_t>(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION);
47
48 constexpr int32_t FIRST_BOOT_AFTER_OTA =
49 static_cast<int32_t>(InitialUserInfoRequestType::FIRST_BOOT_AFTER_OTA);
50 constexpr int32_t LEGACY_ANDROID_SWITCH =
51 static_cast<int32_t>(SwitchUserMessageType::LEGACY_ANDROID_SWITCH);
52 constexpr int32_t VEHICLE_REQUEST = static_cast<int32_t>(SwitchUserMessageType::VEHICLE_REQUEST);
53
54 constexpr int32_t GUEST_USER = static_cast<int32_t>(UserFlags::GUEST);
55 constexpr int32_t NONE_USER = static_cast<int32_t>(UserFlags::NONE);
56 constexpr int32_t SYSTEM_USER = static_cast<int32_t>(UserFlags::SYSTEM);
57
58 constexpr int32_t USER_ID_ASSOC_KEY_FOB =
59 static_cast<int32_t>(UserIdentificationAssociationType::KEY_FOB);
60 constexpr int32_t USER_ID_ASSOC_CUSTOM_1 =
61 static_cast<int32_t>(UserIdentificationAssociationType::CUSTOM_1);
62
63 constexpr int32_t USER_ID_ASSOC_SET_CURRENT_USER =
64 static_cast<int32_t>(UserIdentificationAssociationSetValue::ASSOCIATE_CURRENT_USER);
65 constexpr int32_t USER_ID_ASSOC_UNSET_CURRENT_USER =
66 static_cast<int32_t>(UserIdentificationAssociationSetValue::DISASSOCIATE_CURRENT_USER);
67
68 constexpr int32_t USER_ID_ASSOC_CURRENT_USER =
69 static_cast<int32_t>(UserIdentificationAssociationValue::ASSOCIATED_CURRENT_USER);
70 constexpr int32_t USER_ID_ASSOC_NO_USER =
71 static_cast<int32_t>(UserIdentificationAssociationValue::NOT_ASSOCIATED_ANY_USER);
72
73 } // namespace
74
TEST(UserHalHelperTest,TestToInitialUserInfoRequest)75 TEST(UserHalHelperTest, TestToInitialUserInfoRequest) {
76 VehiclePropValue propValue{
77 .prop = INITIAL_USER_INFO,
78 .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
79 10, NONE_USER}},
80 };
81 InitialUserInfoRequest expected{
82 .requestId = 23,
83 .requestType = InitialUserInfoRequestType::FIRST_BOOT_AFTER_OTA,
84 .usersInfo = {{10, UserFlags::NONE},
85 2,
86 {{0, UserFlags::SYSTEM}, {10, UserFlags::NONE}}},
87 };
88
89 auto actual = toInitialUserInfoRequest(propValue);
90
91 ASSERT_TRUE(actual.ok()) << actual.error().message();
92 EXPECT_THAT(actual.value(), Eq(expected));
93 }
94
TEST(UserHalHelperTest,TestFailsToInitialUserInfoRequestWithMismatchingPropType)95 TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithMismatchingPropType) {
96 VehiclePropValue propValue{
97 .prop = INT32_MAX,
98 .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
99 10, NONE_USER}},
100 };
101
102 auto actual = toInitialUserInfoRequest(propValue);
103
104 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
105 }
106
TEST(UserHalHelperTest,TestFailsToInitialUserInfoRequestWithInvalidRequestType)107 TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithInvalidRequestType) {
108 VehiclePropValue propValue{
109 .prop = INITIAL_USER_INFO,
110 .value = {.int32Values = {23, INT32_MAX, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
111 NONE_USER}},
112 };
113
114 auto actual = toInitialUserInfoRequest(propValue);
115
116 EXPECT_FALSE(actual.ok()) << "No error returned on invalid request type";
117 }
118
TEST(UserHalHelperTest,TestFailsToInitialUserInfoRequestWithInvalidUserFlag)119 TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithInvalidUserFlag) {
120 VehiclePropValue propValue{
121 .prop = INITIAL_USER_INFO,
122 .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
123 10, INT32_MAX}},
124 };
125
126 auto actual = toInitialUserInfoRequest(propValue);
127
128 EXPECT_FALSE(actual.ok()) << "No error returned on invalid user flags";
129 }
130
TEST(UserHalHelperTest,TestFailsToInitialUserInfoRequestWithIncompleteUsersInfo)131 TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithIncompleteUsersInfo) {
132 VehiclePropValue propValueMissingSecondUserInfo{
133 .prop = INITIAL_USER_INFO,
134 .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0,
135 SYSTEM_USER /*Missing 2nd UserInfo*/}},
136 };
137
138 auto actual = toInitialUserInfoRequest(propValueMissingSecondUserInfo);
139
140 EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
141
142 VehiclePropValue propValueMissingUsersInfo{
143 .prop = INITIAL_USER_INFO,
144 .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, /*Missing UsersInfo*/}},
145 };
146
147 actual = toInitialUserInfoRequest(propValueMissingUsersInfo);
148
149 EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
150 }
151
TEST(UserHalHelperTest,TestToSwitchUserRequest)152 TEST(UserHalHelperTest, TestToSwitchUserRequest) {
153 VehiclePropValue propValue{
154 .prop = SWITCH_USER,
155 .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
156 0, SYSTEM_USER, 10, NONE_USER}},
157 };
158 SwitchUserRequest expected{
159 .requestId = 23,
160 .messageType = SwitchUserMessageType::LEGACY_ANDROID_SWITCH,
161 .targetUser = {0, UserFlags::SYSTEM},
162 .usersInfo = {{10, UserFlags::NONE},
163 2,
164 {{0, UserFlags::SYSTEM}, {10, UserFlags::NONE}}},
165 };
166
167 auto actual = toSwitchUserRequest(propValue);
168
169 ASSERT_TRUE(actual.ok()) << actual.error().message();
170 EXPECT_THAT(actual.value(), Eq(expected));
171 }
172
TEST(UserHalHelperTest,TestFailsToSwitchUserRequestWithMismatchingPropType)173 TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithMismatchingPropType) {
174 VehiclePropValue propValue{
175 .prop = INITIAL_USER_INFO,
176 .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
177 0, SYSTEM_USER, 10, NONE_USER}},
178 };
179
180 auto actual = toSwitchUserRequest(propValue);
181
182 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
183 }
184
TEST(UserHalHelperTest,TestFailsToSwitchUserRequestWithInvalidMessageType)185 TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithInvalidMessageType) {
186 VehiclePropValue propValueIncompatibleMessageType{
187 .prop = SWITCH_USER,
188 .value = {.int32Values = {23, VEHICLE_REQUEST, 0, SYSTEM_USER, 10, NONE_USER, 2, 0,
189 SYSTEM_USER, 10, NONE_USER}},
190 };
191
192 auto actual = toSwitchUserRequest(propValueIncompatibleMessageType);
193
194 EXPECT_FALSE(actual.ok()) << "No error returned on incompatible message type";
195
196 VehiclePropValue propValueInvalidMessageType{
197 .prop = SWITCH_USER,
198 .value = {.int32Values = {23, INT32_MAX, 0, SYSTEM_USER, 10, NONE_USER, 2, 0,
199 SYSTEM_USER, 10, NONE_USER}},
200 };
201
202 actual = toSwitchUserRequest(propValueInvalidMessageType);
203
204 EXPECT_FALSE(actual.ok()) << "No error returned on invalid message type";
205 }
206
TEST(UserHalHelperTest,TestFailsToSwitchUserRequestWithIncompleteUsersInfo)207 TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithIncompleteUsersInfo) {
208 VehiclePropValue propValueMissingSecondUserInfo{
209 .prop = SWITCH_USER,
210 .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
211 0, SYSTEM_USER,
212 /*Missing 2nd UserInfo*/}},
213 };
214
215 auto actual = toSwitchUserRequest(propValueMissingSecondUserInfo);
216
217 EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
218
219 VehiclePropValue propValueMissingUsersInfo{
220 .prop = SWITCH_USER,
221 .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER,
222 /*Missing UsersInfo*/}},
223 };
224
225 actual = toSwitchUserRequest(propValueMissingUsersInfo);
226
227 EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
228
229 VehiclePropValue propValueMissingTargetUser{
230 .prop = SWITCH_USER,
231 .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, /*Missing target UserInfo*/}},
232 };
233
234 actual = toSwitchUserRequest(propValueMissingTargetUser);
235
236 EXPECT_FALSE(actual.ok()) << "No error returned on missing target user info";
237 }
238
TEST(UserHalHelperTest,TestToCreateUserRequest)239 TEST(UserHalHelperTest, TestToCreateUserRequest) {
240 VehiclePropValue propValue{
241 .prop = CREATE_USER,
242 .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
243 NONE_USER},
244 .stringValue = "Guest11"},
245 };
246 CreateUserRequest expected{
247 .requestId = 23,
248 .newUserInfo = {11, UserFlags::GUEST},
249 .newUserName = "Guest11",
250 .usersInfo = {{10, UserFlags::NONE},
251 2,
252 {{0, UserFlags::SYSTEM}, {10, UserFlags::NONE}}},
253 };
254
255 auto actual = toCreateUserRequest(propValue);
256
257 ASSERT_TRUE(actual.ok()) << actual.error().message();
258 EXPECT_THAT(actual.value(), Eq(expected));
259 }
260
TEST(UserHalHelperTest,TestFailsToCreateUserRequestWithMismatchingPropType)261 TEST(UserHalHelperTest, TestFailsToCreateUserRequestWithMismatchingPropType) {
262 VehiclePropValue propValue{
263 .prop = INITIAL_USER_INFO,
264 .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
265 NONE_USER},
266 .stringValue = "Guest11"},
267 };
268
269 auto actual = toCreateUserRequest(propValue);
270
271 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
272 }
273
TEST(UserHalHelperTest,TestFailsToCreateUserRequestWithIncompleteUsersInfo)274 TEST(UserHalHelperTest, TestFailsToCreateUserRequestWithIncompleteUsersInfo) {
275 VehiclePropValue propValueMissingSecondUserInfo{
276 .prop = CREATE_USER,
277 .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0,
278 SYSTEM_USER /*Missing 2nd UserInfo*/},
279 .stringValue = "Guest11"},
280 };
281
282 auto actual = toCreateUserRequest(propValueMissingSecondUserInfo);
283
284 EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
285
286 VehiclePropValue propValueMissingUsersInfo{
287 .prop = CREATE_USER,
288 .value = {.int32Values = {23, 11, GUEST_USER, /*Missing UsersInfo*/},
289 .stringValue = "Guest11"},
290 };
291
292 actual = toCreateUserRequest(propValueMissingUsersInfo);
293
294 EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
295
296 VehiclePropValue propValueMissingCreateUserInfo{
297 .prop = CREATE_USER,
298 .value = {.int32Values = {23, /*Missing create UserInfo*/}, .stringValue = "Guest11"},
299 };
300
301 actual = toCreateUserRequest(propValueMissingCreateUserInfo);
302
303 EXPECT_FALSE(actual.ok()) << "No error returned on missing create user info";
304 }
305
TEST(UserHalHelperTest,TestToRemoveUserRequest)306 TEST(UserHalHelperTest, TestToRemoveUserRequest) {
307 VehiclePropValue propValue{
308 .prop = REMOVE_USER,
309 .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
310 NONE_USER}},
311 };
312 RemoveUserRequest expected{
313 .requestId = 23,
314 .removedUserInfo = {10, UserFlags::NONE},
315 .usersInfo = {{10, UserFlags::NONE},
316 2,
317 {{0, UserFlags::SYSTEM}, {10, UserFlags::NONE}}},
318 };
319
320 auto actual = toRemoveUserRequest(propValue);
321
322 ASSERT_TRUE(actual.ok()) << actual.error().message();
323 EXPECT_THAT(actual.value(), Eq(expected));
324 }
325
TEST(UserHalHelperTest,TestFailsToRemoveUserRequestWithMismatchingPropType)326 TEST(UserHalHelperTest, TestFailsToRemoveUserRequestWithMismatchingPropType) {
327 VehiclePropValue propValue{
328 .prop = INITIAL_USER_INFO,
329 .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
330 NONE_USER}},
331 };
332
333 auto actual = toRemoveUserRequest(propValue);
334
335 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
336 }
337
TEST(UserHalHelperTest,TestFailsToRemoveUserRequestWithIncompleteUsersInfo)338 TEST(UserHalHelperTest, TestFailsToRemoveUserRequestWithIncompleteUsersInfo) {
339 VehiclePropValue propValueMissingSecondUserInfo{
340 .prop = REMOVE_USER,
341 .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0,
342 SYSTEM_USER /*Missing 2nd UserInfo*/}},
343 };
344
345 auto actual = toRemoveUserRequest(propValueMissingSecondUserInfo);
346
347 EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
348
349 VehiclePropValue propValueMissingUsersInfo{
350 .prop = REMOVE_USER,
351 .value = {.int32Values = {23, 10, NONE_USER, /*Missing UsersInfo*/}},
352 };
353
354 actual = toRemoveUserRequest(propValueMissingUsersInfo);
355
356 EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
357
358 VehiclePropValue propValueMissingRemoveUserInfo{
359 .prop = REMOVE_USER,
360 .value = {.int32Values = {23, /*Missing remove UserInfo*/}},
361 };
362
363 actual = toRemoveUserRequest(propValueMissingRemoveUserInfo);
364
365 EXPECT_FALSE(actual.ok()) << "No error returned on missing remove user info";
366 }
367
TEST(UserHalHelperTest,TestFailsToUserIdentificationGetRequest)368 TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequest) {
369 VehiclePropValue propValue{
370 .prop = USER_IDENTIFICATION_ASSOCIATION,
371 .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
372 USER_ID_ASSOC_CUSTOM_1}},
373 };
374 UserIdentificationGetRequest expected{
375 .requestId = 23,
376 .userInfo = {10, UserFlags::NONE},
377 .numberAssociationTypes = 2,
378 .associationTypes = {UserIdentificationAssociationType::KEY_FOB,
379 UserIdentificationAssociationType::CUSTOM_1},
380 };
381
382 auto actual = toUserIdentificationGetRequest(propValue);
383
384 ASSERT_TRUE(actual.ok()) << actual.error().message();
385 EXPECT_THAT(actual.value(), Eq(expected));
386 }
387
TEST(UserHalHelperTest,TestFailsToUserIdentificationGetRequestWithMismatchingPropType)388 TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithMismatchingPropType) {
389 VehiclePropValue propValue{
390 .prop = INITIAL_USER_INFO,
391 .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
392 USER_ID_ASSOC_CUSTOM_1}},
393 };
394
395 auto actual = toUserIdentificationGetRequest(propValue);
396
397 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
398 }
399
TEST(UserHalHelperTest,TestFailsToUserIdentificationGetRequestWithInvalidAssociationTypes)400 TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithInvalidAssociationTypes) {
401 VehiclePropValue propValue{
402 .prop = USER_IDENTIFICATION_ASSOCIATION,
403 .value = {.int32Values = {23, 10, NONE_USER, 1, INT32_MAX}},
404 };
405
406 auto actual = toUserIdentificationGetRequest(propValue);
407
408 EXPECT_FALSE(actual.ok()) << "No error returned on invalid association type";
409 }
410
TEST(UserHalHelperTest,TestFailsToUserIdentificationGetRequestWithIncompleteAssociationTypes)411 TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithIncompleteAssociationTypes) {
412 VehiclePropValue propValueMissingSecondAssociationType{
413 .prop = USER_IDENTIFICATION_ASSOCIATION,
414 .value = {.int32Values = {23, 10, NONE_USER, 2,
415 USER_ID_ASSOC_KEY_FOB /*Missing 2nd association type*/}},
416 };
417
418 auto actual = toUserIdentificationGetRequest(propValueMissingSecondAssociationType);
419
420 EXPECT_FALSE(actual.ok()) << "No error returned on missing second association type";
421
422 VehiclePropValue propValueMissingNumberAssociationTypes{
423 .prop = USER_IDENTIFICATION_ASSOCIATION,
424 .value = {.int32Values = {23, 10, NONE_USER, /*Missing number association types*/}},
425 };
426
427 actual = toUserIdentificationGetRequest(propValueMissingNumberAssociationTypes);
428
429 EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
430 }
431
TEST(UserHalHelperTest,TestFailsToUserIdentificationGetRequestWithMissingUserInfo)432 TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithMissingUserInfo) {
433 VehiclePropValue propValue{
434 .prop = USER_IDENTIFICATION_ASSOCIATION,
435 .value = {.int32Values = {23, /*Missing user info*/}},
436 };
437
438 auto actual = toUserIdentificationGetRequest(propValue);
439
440 EXPECT_FALSE(actual.ok()) << "No error returned on missing UserInfo";
441 }
442
TEST(UserHalHelperTest,TestToUserIdentificationSetRequest)443 TEST(UserHalHelperTest, TestToUserIdentificationSetRequest) {
444 VehiclePropValue propValue{
445 .prop = USER_IDENTIFICATION_ASSOCIATION,
446 .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
447 USER_ID_ASSOC_SET_CURRENT_USER, USER_ID_ASSOC_CUSTOM_1,
448 USER_ID_ASSOC_UNSET_CURRENT_USER}},
449 };
450 UserIdentificationSetRequest expected{
451 .requestId = 23,
452 .userInfo = {10, UserFlags::NONE},
453 .numberAssociations = 2,
454 .associations = {{UserIdentificationAssociationType::KEY_FOB,
455 UserIdentificationAssociationSetValue::ASSOCIATE_CURRENT_USER},
456 {UserIdentificationAssociationType::CUSTOM_1,
457 UserIdentificationAssociationSetValue::DISASSOCIATE_CURRENT_USER}},
458 };
459
460 auto actual = toUserIdentificationSetRequest(propValue);
461
462 ASSERT_TRUE(actual.ok()) << actual.error().message();
463 EXPECT_THAT(actual.value(), Eq(expected));
464 }
465
TEST(UserHalHelperTest,TestFailsToUserIdentificationSetRequestWithMismatchingPropType)466 TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithMismatchingPropType) {
467 VehiclePropValue propValue{
468 .prop = INITIAL_USER_INFO,
469 .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
470 USER_ID_ASSOC_SET_CURRENT_USER, USER_ID_ASSOC_CUSTOM_1,
471 USER_ID_ASSOC_UNSET_CURRENT_USER}},
472 };
473
474 auto actual = toUserIdentificationSetRequest(propValue);
475
476 EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
477 }
478
TEST(UserHalHelperTest,TestFailsToUserIdentificationSetRequestWithInvalidAssociations)479 TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithInvalidAssociations) {
480 VehiclePropValue propValueInvalidAssociationType{
481 .prop = USER_IDENTIFICATION_ASSOCIATION,
482 .value = {.int32Values = {23, 10, NONE_USER, 1, INT32_MAX,
483 USER_ID_ASSOC_SET_CURRENT_USER}},
484 };
485
486 auto actual = toUserIdentificationSetRequest(propValueInvalidAssociationType);
487
488 EXPECT_FALSE(actual.ok()) << "No error returned on invalid association type";
489
490 VehiclePropValue propValueInvalidAssociationValue{
491 .prop = USER_IDENTIFICATION_ASSOCIATION,
492 .value = {.int32Values = {23, 10, NONE_USER, USER_ID_ASSOC_KEY_FOB, INT32_MAX}},
493 };
494
495 actual = toUserIdentificationSetRequest(propValueInvalidAssociationValue);
496
497 EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
498 }
499
TEST(UserHalHelperTest,TestFailsToUserIdentificationSetRequestWithIncompleteAssociations)500 TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithIncompleteAssociations) {
501 VehiclePropValue propValueMissingSecondAssociationType{
502 .prop = USER_IDENTIFICATION_ASSOCIATION,
503 .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
504 USER_ID_ASSOC_SET_CURRENT_USER,
505 /*Missing 2nd association*/}},
506 };
507
508 auto actual = toUserIdentificationSetRequest(propValueMissingSecondAssociationType);
509
510 EXPECT_FALSE(actual.ok()) << "No error returned on missing second association type";
511
512 VehiclePropValue propValueMissingNumberAssociationTypes{
513 .prop = USER_IDENTIFICATION_ASSOCIATION,
514 .value = {.int32Values = {23, 10, NONE_USER, /*Missing number associations*/}},
515 };
516
517 actual = toUserIdentificationSetRequest(propValueMissingNumberAssociationTypes);
518
519 EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
520 }
521
TEST(UserHalHelperTest,TestFailsToUserIdentificationSetRequestWithMissingUserInfo)522 TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithMissingUserInfo) {
523 VehiclePropValue propValue{
524 .prop = USER_IDENTIFICATION_ASSOCIATION,
525 .value = {.int32Values = {23, /*Missing user info*/}},
526 };
527
528 auto actual = toUserIdentificationSetRequest(propValue);
529
530 EXPECT_FALSE(actual.ok()) << "No error returned on missing UserInfo";
531 }
532
TEST(UserHalHelperTest,TestSwitchUserRequestToVehiclePropValue)533 TEST(UserHalHelperTest, TestSwitchUserRequestToVehiclePropValue) {
534 SwitchUserRequest request{
535 .requestId = 23,
536 .messageType = SwitchUserMessageType::VEHICLE_REQUEST,
537 .targetUser = {11, UserFlags::GUEST},
538 };
539 VehiclePropValue expected{
540 .prop = SWITCH_USER,
541 .value = {.int32Values = {23,
542 static_cast<int32_t>(SwitchUserMessageType::VEHICLE_REQUEST),
543 11}},
544 };
545
546 auto actual = toVehiclePropValue(request);
547
548 ASSERT_THAT(actual, NotNull());
549 EXPECT_THAT(actual->timestamp, Gt(0));
550 // Don't rely on real timestamp in tests as the expected and actual objects won't have the same
551 // timestamps. Thus remove the timestamps before comparing them.
552 actual->timestamp = 0;
553 EXPECT_THAT(actual, Pointee(Eq(expected)));
554 }
555
TEST(UserHalHelperTest,TestFailsSwitchUserRequestToVehiclePropValueWithIncompatibleMessageType)556 TEST(UserHalHelperTest, TestFailsSwitchUserRequestToVehiclePropValueWithIncompatibleMessageType) {
557 SwitchUserRequest request{
558 .requestId = 23,
559 .messageType = SwitchUserMessageType::VEHICLE_RESPONSE,
560 .targetUser = {11, UserFlags::GUEST},
561 };
562
563 auto actual = toVehiclePropValue(request);
564
565 EXPECT_THAT(actual, IsNull());
566 }
567
TEST(UserHalHelperTest,TestInitialUserInfoResponseToVehiclePropValue)568 TEST(UserHalHelperTest, TestInitialUserInfoResponseToVehiclePropValue) {
569 InitialUserInfoResponse response{
570 .requestId = 23,
571 .action = InitialUserInfoResponseAction::CREATE,
572 .userToSwitchOrCreate = {11, UserFlags::GUEST},
573 .userLocales = "en-US,pt-BR",
574 .userNameToCreate = "Owner",
575 };
576 VehiclePropValue expected{
577 .prop = INITIAL_USER_INFO,
578 .value = {.int32Values = {23,
579 static_cast<int32_t>(InitialUserInfoResponseAction::CREATE),
580 11, GUEST_USER},
581 .stringValue = "en-US,pt-BR||Owner"},
582 };
583
584 auto actual = toVehiclePropValue(response);
585
586 ASSERT_THAT(actual, NotNull());
587 EXPECT_THAT(actual->timestamp, Gt(0));
588 actual->timestamp = 0;
589 EXPECT_THAT(actual, Pointee(Eq(expected)));
590 }
591
TEST(UserHalHelperTest,TestSwitchUserResponseToVehiclePropValue)592 TEST(UserHalHelperTest, TestSwitchUserResponseToVehiclePropValue) {
593 SwitchUserResponse response{
594 .requestId = 23,
595 .messageType = SwitchUserMessageType::VEHICLE_RESPONSE,
596 .status = SwitchUserStatus::FAILURE,
597 .errorMessage = "random error",
598 };
599 VehiclePropValue expected{
600 .prop = SWITCH_USER,
601 .value = {.int32Values = {23,
602 static_cast<int32_t>(SwitchUserMessageType::VEHICLE_RESPONSE),
603 static_cast<int32_t>(SwitchUserStatus::FAILURE)},
604 .stringValue = "random error"},
605 };
606
607 auto actual = toVehiclePropValue(response);
608
609 ASSERT_THAT(actual, NotNull());
610 EXPECT_THAT(actual->timestamp, Gt(0));
611 actual->timestamp = 0;
612 EXPECT_THAT(actual, Pointee(Eq(expected)));
613 }
614
TEST(UserHalHelperTest,TestCreateUserResponseToVehiclePropValue)615 TEST(UserHalHelperTest, TestCreateUserResponseToVehiclePropValue) {
616 CreateUserResponse response{
617 .requestId = 23,
618 .status = CreateUserStatus::FAILURE,
619 .errorMessage = "random error",
620 };
621 VehiclePropValue expected{
622 .prop = CREATE_USER,
623 .value = {.int32Values = {23, static_cast<int32_t>(CreateUserStatus::FAILURE)},
624 .stringValue = "random error"},
625 };
626
627 auto actual = toVehiclePropValue(response);
628
629 ASSERT_THAT(actual, NotNull());
630 EXPECT_THAT(actual->timestamp, Gt(0));
631 actual->timestamp = 0;
632 EXPECT_THAT(actual, Pointee(Eq(expected)));
633 }
634
TEST(UserHalHelperTest,TestUserIdentificationResponseToVehiclePropValue)635 TEST(UserHalHelperTest, TestUserIdentificationResponseToVehiclePropValue) {
636 UserIdentificationResponse response{
637 .requestId = 23,
638 .numberAssociation = 2,
639 .associations = {{UserIdentificationAssociationType::KEY_FOB,
640 UserIdentificationAssociationValue::ASSOCIATED_CURRENT_USER},
641 {UserIdentificationAssociationType::CUSTOM_1,
642 UserIdentificationAssociationValue::NOT_ASSOCIATED_ANY_USER}},
643 .errorMessage = "random error",
644 };
645 VehiclePropValue expected{
646 .prop = USER_IDENTIFICATION_ASSOCIATION,
647 .value = {.int32Values = {23, 2, USER_ID_ASSOC_KEY_FOB, USER_ID_ASSOC_CURRENT_USER,
648 USER_ID_ASSOC_CUSTOM_1, USER_ID_ASSOC_NO_USER},
649 .stringValue = "random error"},
650 };
651
652 auto actual = toVehiclePropValue(response);
653
654 ASSERT_THAT(actual, NotNull());
655 EXPECT_THAT(actual->timestamp, Gt(0));
656 actual->timestamp = 0;
657 EXPECT_THAT(actual, Pointee(Eq(expected)));
658 }
659
660 } // namespace user_hal_helper
661
662 } // namespace V2_0
663 } // namespace vehicle
664 } // namespace automotive
665 } // namespace hardware
666 } // namespace android
667