1 /*
2  * Copyright (C) 2021 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 <ConcurrentQueue.h>
18 #include <PropertyUtils.h>
19 #include <VehicleUtils.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include <atomic>
24 #include <thread>
25 #include <vector>
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace vehicle {
31 
32 namespace {
33 
34 using ::aidl::android::hardware::automotive::vehicle::StatusCode;
35 using ::aidl::android::hardware::automotive::vehicle::VehicleArea;
36 using ::aidl::android::hardware::automotive::vehicle::VehicleAreaConfig;
37 using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
38 using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
39 using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
40 using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
41 using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
42 using ::android::base::Error;
43 using ::android::base::Result;
44 
45 struct InvalidPropValueTestCase {
46     std::string name;
47     VehiclePropValue value;
48     bool valid = false;
49     VehiclePropConfig config;
50 };
51 
52 constexpr int32_t int32Prop = toInt(VehicleProperty::INFO_MODEL_YEAR);
53 constexpr int32_t int32VecProp = toInt(VehicleProperty::INFO_FUEL_TYPE);
54 constexpr int32_t int64Prop = toInt(VehicleProperty::ANDROID_EPOCH_TIME);
55 constexpr int32_t int64VecProp = toInt(VehicleProperty::WHEEL_TICK);
56 constexpr int32_t floatProp = toInt(VehicleProperty::ENV_OUTSIDE_TEMPERATURE);
57 constexpr int32_t floatVecProp = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION);
58 constexpr int32_t kMixedTypePropertyForTest = 0x1111 | toInt(VehiclePropertyGroup::VENDOR) |
59                                               toInt(VehicleArea::GLOBAL) |
60                                               toInt(VehiclePropertyType::MIXED);
61 
getInvalidPropValuesTestCases()62 std::vector<InvalidPropValueTestCase> getInvalidPropValuesTestCases() {
63     return std::vector<InvalidPropValueTestCase>(
64             {
65                     InvalidPropValueTestCase{
66                             .name = "int32_normal",
67                             .value =
68                                     {
69                                             .prop = int32Prop,
70                                             .value.int32Values = {0},
71                                     },
72                             .valid = true,
73                     },
74                     InvalidPropValueTestCase{
75                             .name = "int32_no_value",
76                             .value =
77                                     {
78                                             .prop = int32Prop,
79                                     },
80                     },
81                     InvalidPropValueTestCase{
82                             .name = "int32_more_than_one_value",
83                             .value =
84                                     {
85                                             .prop = int32Prop,
86                                             .value.int32Values = {0, 1},
87                                     },
88                     },
89                     InvalidPropValueTestCase{
90                             .name = "int32_vec_normal",
91                             .value =
92                                     {
93                                             .prop = int32VecProp,
94                                             .value.int32Values = {0, 1},
95                                     },
96                             .valid = true,
97                     },
98                     InvalidPropValueTestCase{
99                             .name = "int32_vec_no_value",
100                             .value =
101                                     {
102                                             .prop = int32VecProp,
103                                     },
104                     },
105                     InvalidPropValueTestCase{
106                             .name = "int64_normal",
107                             .value =
108                                     {
109                                             .prop = int64Prop,
110                                             .value.int64Values = {0},
111                                     },
112                             .valid = true,
113                     },
114                     InvalidPropValueTestCase{
115                             .name = "int64_no_value",
116                             .value =
117                                     {
118                                             .prop = int64Prop,
119                                     },
120                     },
121                     InvalidPropValueTestCase{
122                             .name = "int64_more_than_one_value",
123                             .value =
124                                     {
125                                             .prop = int64Prop,
126                                             .value.int64Values = {0, 1},
127                                     },
128                     },
129                     InvalidPropValueTestCase{
130                             .name = "int64_vec_normal",
131                             .value =
132                                     {
133                                             .prop = int64VecProp,
134                                             .value.int64Values = {0, 1},
135                                     },
136                             .valid = true,
137                     },
138                     InvalidPropValueTestCase{
139                             .name = "int64_vec_no_value",
140                             .value =
141                                     {
142                                             .prop = int64VecProp,
143                                     },
144                     },
145                     InvalidPropValueTestCase{
146                             .name = "float_normal",
147                             .value =
148                                     {
149                                             .prop = floatProp,
150                                             .value.floatValues = {0.0},
151                                     },
152                             .valid = true,
153                     },
154                     InvalidPropValueTestCase{
155                             .name = "float_no_value",
156                             .value =
157                                     {
158                                             .prop = floatProp,
159                                     },
160                     },
161                     InvalidPropValueTestCase{
162                             .name = "float_more_than_one_value",
163                             .value =
164                                     {
165                                             .prop = floatProp,
166                                             .value.floatValues = {0.0, 1.0},
167                                     },
168                     },
169                     InvalidPropValueTestCase{
170                             .name = "float_vec_normal",
171                             .value =
172                                     {
173                                             .prop = floatVecProp,
174                                             .value.floatValues = {0.0, 1.0},
175                                     },
176                             .valid = true,
177                     },
178                     InvalidPropValueTestCase{
179                             .name = "float_vec_no_value",
180                             .value =
181                                     {
182                                             .prop = floatVecProp,
183                                     },
184                     },
185                     InvalidPropValueTestCase{
186                             .name = "mixed_normal",
187                             .value =
188                                     {
189                                             .prop = kMixedTypePropertyForTest,
190                                             // Expect 3 values.
191                                             .value.int32Values = {0, 1, 2},
192                                             // Expect 2 values.
193                                             .value.int64Values = {0, 1},
194                                             // Expect 2 values.
195                                             .value.floatValues = {0.0, 1.0},
196                                             // Expect 1 value.
197                                             .value.byteValues = {static_cast<uint8_t>(0)},
198                                     },
199                             .config =
200                                     {
201                                             .prop = kMixedTypePropertyForTest,
202                                             .configArray = {0, 1, 1, 1, 1, 1, 1, 1, 1},
203                                     },
204                             .valid = true,
205                     },
206                     InvalidPropValueTestCase{
207                             .name = "mixed_mismatch_int32_values_count",
208                             .value =
209                                     {
210                                             .prop = kMixedTypePropertyForTest,
211                                             // Expect 3 values.
212                                             .value.int32Values = {0, 1},
213                                             // Expect 2 values.
214                                             .value.int64Values = {0, 1},
215                                             // Expect 2 values.
216                                             .value.floatValues = {0.0, 1.0},
217                                             // Expect 1 value.
218                                             .value.byteValues = {static_cast<uint8_t>(0)},
219                                     },
220                             .config =
221                                     {
222                                             .prop = kMixedTypePropertyForTest,
223                                             .configArray = {0, 1, 1, 1, 1, 1, 1, 1, 1},
224                                     },
225                     },
226                     InvalidPropValueTestCase{
227                             .name = "mixed_mismatch_int64_values_count",
228                             .value =
229                                     {
230                                             .prop = kMixedTypePropertyForTest,
231                                             // Expect 3 values.
232                                             .value.int32Values = {0, 1, 2},
233                                             // Expect 2 values.
234                                             .value.int64Values = {0},
235                                             // Expect 2 values.
236                                             .value.floatValues = {0.0, 1.0},
237                                             // Expect 1 value.
238                                             .value.byteValues = {static_cast<uint8_t>(0)},
239                                     },
240                             .config =
241                                     {
242                                             .prop = kMixedTypePropertyForTest,
243                                             .configArray = {0, 1, 1, 1, 1, 1, 1, 1, 1},
244                                     },
245                     },
246                     InvalidPropValueTestCase{
247                             .name = "mixed_mismatch_float_values_count",
248                             .value =
249                                     {
250                                             .prop = kMixedTypePropertyForTest,
251                                             // Expect 3 values.
252                                             .value.int32Values = {0, 1, 2},
253                                             // Expect 2 values.
254                                             .value.int64Values = {0, 1},
255                                             // Expect 2 values.
256                                             .value.floatValues = {0.0},
257                                             // Expect 1 value.
258                                             .value.byteValues = {static_cast<uint8_t>(0)},
259                                     },
260                             .config =
261                                     {
262                                             .prop = kMixedTypePropertyForTest,
263                                             .configArray = {0, 1, 1, 1, 1, 1, 1, 1, 1},
264                                     },
265                     },
266                     InvalidPropValueTestCase{
267                             .name = "mixed_mismatch_byte_values_count",
268                             .value =
269                                     {
270                                             .prop = kMixedTypePropertyForTest,
271                                             // Expect 3 values.
272                                             .value.int32Values = {0, 1, 2},
273                                             // Expect 2 values.
274                                             .value.int64Values = {0, 1},
275                                             // Expect 2 values.
276                                             .value.floatValues = {0.0, 1.0},
277                                             // Expect 1 value.
278                                             .value.byteValues = {static_cast<uint8_t>(0),
279                                                                  static_cast<uint8_t>(1)},
280                                     },
281                             .config =
282                                     {
283                                             .prop = kMixedTypePropertyForTest,
284                                             .configArray = {0, 1, 1, 1, 1, 1, 1, 1, 1},
285                                     },
286                     },
287             });
288 }
289 
290 struct InvalidValueRangeTestCase {
291     std::string name;
292     VehiclePropValue value;
293     bool valid = false;
294     VehicleAreaConfig config;
295 };
296 
getInvalidValueRangeTestCases()297 std::vector<InvalidValueRangeTestCase> getInvalidValueRangeTestCases() {
298     return std::vector<InvalidValueRangeTestCase>({{
299             InvalidValueRangeTestCase{
300                     .name = "int32_normal",
301                     .value =
302                             {
303                                     .prop = int32Prop,
304                                     .value.int32Values = {0},
305                             },
306                     .valid = true,
307                     .config =
308                             {
309                                     .minInt32Value = 0,
310                                     .maxInt32Value = 10,
311                             },
312             },
313             InvalidValueRangeTestCase{
314                     .name = "int32_vec_normal",
315                     .value =
316                             {
317                                     .prop = int32VecProp,
318                                     .value.int32Values = {0, 1},
319                             },
320                     .valid = true,
321                     .config =
322                             {
323                                     .minInt32Value = 0,
324                                     .maxInt32Value = 10,
325                             },
326             },
327             InvalidValueRangeTestCase{
328                     .name = "int32_vec_underflow",
329                     .value =
330                             {
331                                     .prop = int32VecProp,
332                                     .value.int32Values = {-1, 1},
333                             },
334 
335                     .config =
336                             {
337                                     .minInt32Value = 0,
338                                     .maxInt32Value = 10,
339                             },
340             },
341             InvalidValueRangeTestCase{
342                     .name = "int32_vec_overflow",
343                     .value =
344                             {
345                                     .prop = int32VecProp,
346                                     .value.int32Values = {0, 100},
347                             },
348                     .config =
349                             {
350                                     .minInt32Value = 0,
351                                     .maxInt32Value = 10,
352                             },
353             },
354             InvalidValueRangeTestCase{
355                     .name = "int64_normal",
356                     .value =
357                             {
358                                     .prop = int64Prop,
359                                     .value.int64Values = {0},
360                             },
361                     .valid = true,
362                     .config =
363                             {
364                                     .minInt64Value = 0,
365                                     .maxInt64Value = 10,
366                             },
367             },
368             InvalidValueRangeTestCase{
369                     .name = "int64_vec_normal",
370                     .value =
371                             {
372                                     .prop = int64VecProp,
373                                     .value.int64Values = {0, 1},
374                             },
375                     .valid = true,
376                     .config =
377                             {
378                                     .minInt64Value = 0,
379                                     .maxInt64Value = 10,
380                             },
381             },
382             InvalidValueRangeTestCase{
383                     .name = "int64_vec_underflow",
384                     .value =
385                             {
386                                     .prop = int64VecProp,
387                                     .value.int64Values = {-1, 1},
388                             },
389 
390                     .config =
391                             {
392                                     .minInt64Value = 0,
393                                     .maxInt64Value = 10,
394                             },
395             },
396             InvalidValueRangeTestCase{
397                     .name = "int64_vec_overflow",
398                     .value =
399                             {
400                                     .prop = int64VecProp,
401                                     .value.int64Values = {0, 100},
402                             },
403                     .config =
404                             {
405                                     .minInt64Value = 0,
406                                     .maxInt64Value = 10,
407                             },
408             },
409             InvalidValueRangeTestCase{
410                     .name = "float_normal",
411                     .value =
412                             {
413                                     .prop = floatProp,
414                                     .value.floatValues = {0.0},
415                             },
416                     .valid = true,
417                     .config =
418                             {
419                                     .minFloatValue = 0.0,
420                                     .maxFloatValue = 10.0,
421                             },
422             },
423             InvalidValueRangeTestCase{
424                     .name = "float_vec_normal",
425                     .value =
426                             {
427                                     .prop = floatVecProp,
428                                     .value.floatValues = {0.0, 10.0},
429                             },
430                     .valid = true,
431                     .config =
432                             {
433                                     .minFloatValue = 0.0,
434                                     .maxFloatValue = 10.0,
435                             },
436             },
437             InvalidValueRangeTestCase{
438                     .name = "float_vec_underflow",
439                     .value =
440                             {
441                                     .prop = floatVecProp,
442                                     .value.floatValues = {-0.1, 1.1},
443                             },
444 
445                     .config =
446                             {
447                                     .minFloatValue = 0.0,
448                                     .maxFloatValue = 10.0,
449                             },
450             },
451             InvalidValueRangeTestCase{
452                     .name = "float_vec_overflow",
453                     .value =
454                             {
455                                     .prop = floatVecProp,
456                                     .value.floatValues = {0.0, 10.1},
457                             },
458                     .config =
459                             {
460                                     .minFloatValue = 0.0,
461                                     .maxFloatValue = 10.0,
462                             },
463             },
464     }});
465 }
466 
467 }  // namespace
468 
TEST(VehicleUtilsTest,testToInt)469 TEST(VehicleUtilsTest, testToInt) {
470     int areaGlobal = toInt(VehicleArea::GLOBAL);
471 
472     ASSERT_EQ(areaGlobal, 0x01000000);
473 }
474 
TEST(VehicleUtilsTest,testGetPropType)475 TEST(VehicleUtilsTest, testGetPropType) {
476     VehiclePropertyType type = getPropType(toInt(VehicleProperty::INFO_VIN));
477 
478     ASSERT_EQ(type, VehiclePropertyType::STRING);
479 }
480 
TEST(VehicleUtilsTest,testGetPropGroup)481 TEST(VehicleUtilsTest, testGetPropGroup) {
482     VehiclePropertyGroup group = getPropGroup(toInt(VehicleProperty::INFO_VIN));
483 
484     ASSERT_EQ(group, VehiclePropertyGroup::SYSTEM);
485 }
486 
TEST(VehicleUtilsTest,testGetPropArea)487 TEST(VehicleUtilsTest, testGetPropArea) {
488     VehicleArea area = getPropArea(toInt(VehicleProperty::INFO_VIN));
489 
490     ASSERT_EQ(area, VehicleArea::GLOBAL);
491 }
492 
TEST(VehicleUtilsTest,testIsGlobalPropTrue)493 TEST(VehicleUtilsTest, testIsGlobalPropTrue) {
494     ASSERT_TRUE(isGlobalProp(toInt(VehicleProperty::INFO_VIN)));
495 }
496 
TEST(VehicleUtilsTest,testIsGlobalPropFalse)497 TEST(VehicleUtilsTest, testIsGlobalPropFalse) {
498     ASSERT_FALSE(isGlobalProp(toInt(VehicleProperty::TIRE_PRESSURE)));
499 }
500 
TEST(VehicleUtilsTest,testIsSystemPropTrue)501 TEST(VehicleUtilsTest, testIsSystemPropTrue) {
502     ASSERT_TRUE(isSystemProp(toInt(VehicleProperty::INFO_VIN)));
503 }
504 
TEST(VehicleUtilsTest,testIsSystemPropFalse)505 TEST(VehicleUtilsTest, testIsSystemPropFalse) {
506     // VehiclePropertyGroup:VENDOR,VehicleArea:GLOBAL,VehiclePropertyType:STRING
507     int vendorProp = 0x0100 + 0x20000000 + 0x01000000 + 0x00100000;
508 
509     ASSERT_FALSE(isSystemProp(vendorProp));
510 }
511 
TEST(VehicleUtilsTest,testGetAreaConfigGlobal)512 TEST(VehicleUtilsTest, testGetAreaConfigGlobal) {
513     VehiclePropValue testPropValue{.prop = toInt(VehicleProperty::INFO_VIN)};
514     VehicleAreaConfig testAreaConfig{.areaId = 0, .minInt32Value = 1};
515     VehiclePropConfig testConfig{.areaConfigs = {testAreaConfig}};
516 
517     const VehicleAreaConfig* gotConfig = getAreaConfig(testPropValue, testConfig);
518 
519     ASSERT_EQ(*gotConfig, testAreaConfig);
520 }
521 
TEST(VehicleUtilsTest,testGetAreaConfigGlobalNoAreaConfig)522 TEST(VehicleUtilsTest, testGetAreaConfigGlobalNoAreaConfig) {
523     VehiclePropValue testPropValue{.prop = toInt(VehicleProperty::INFO_VIN)};
524     VehiclePropConfig testConfig{};
525 
526     const VehicleAreaConfig* gotConfig = getAreaConfig(testPropValue, testConfig);
527 
528     ASSERT_EQ(gotConfig, nullptr);
529 }
530 
TEST(VehicleUtilsTest,testGetAreaConfigNonGlobal)531 TEST(VehicleUtilsTest, testGetAreaConfigNonGlobal) {
532     VehiclePropValue testPropValue = {
533             .prop = toInt(VehicleProperty::TIRE_PRESSURE),
534     };
535     VehicleAreaConfig leftConfig{.areaId = WHEEL_FRONT_LEFT, .minInt32Value = 1};
536     VehicleAreaConfig rightConfig{.areaId = WHEEL_FRONT_RIGHT, .minInt32Value = 2};
537     VehiclePropConfig testConfig{.areaConfigs = {leftConfig, rightConfig}};
538 
539     testPropValue.areaId = WHEEL_FRONT_LEFT;
540     const VehicleAreaConfig* gotConfig = getAreaConfig(testPropValue, testConfig);
541 
542     ASSERT_EQ(*gotConfig, leftConfig);
543 }
544 
TEST(VehicleUtilsTest,testGetAreaConfigNonGlobalNull)545 TEST(VehicleUtilsTest, testGetAreaConfigNonGlobalNull) {
546     VehiclePropValue testPropValue = {
547             .prop = toInt(VehicleProperty::TIRE_PRESSURE),
548     };
549     VehicleAreaConfig leftConfig{.areaId = WHEEL_FRONT_LEFT, .minInt32Value = 1};
550     VehicleAreaConfig rightConfig{.areaId = WHEEL_FRONT_RIGHT, .minInt32Value = 2};
551     VehiclePropConfig testConfig{.areaConfigs = {leftConfig, rightConfig}};
552 
553     // No config for this area.
554     testPropValue.areaId = 0;
555     const VehicleAreaConfig* gotConfig = getAreaConfig(testPropValue, testConfig);
556 
557     ASSERT_EQ(gotConfig, nullptr);
558 }
559 
TEST(VehicleUtilsTest,testCreateVehiclePropValueInt32)560 TEST(VehicleUtilsTest, testCreateVehiclePropValueInt32) {
561     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::INT32);
562 
563     ASSERT_NE(value, nullptr);
564     ASSERT_EQ(1u, value->value.int32Values.size());
565 }
566 
TEST(VehicleUtilsTest,testCreateVehiclePropValueInt32Vec)567 TEST(VehicleUtilsTest, testCreateVehiclePropValueInt32Vec) {
568     std::unique_ptr<VehiclePropValue> value =
569             createVehiclePropValue(VehiclePropertyType::INT32_VEC);
570 
571     ASSERT_NE(value, nullptr);
572     ASSERT_EQ(1u, value->value.int32Values.size());
573 }
574 
TEST(VehicleUtilsTest,testCreateVehiclePropValueInt64)575 TEST(VehicleUtilsTest, testCreateVehiclePropValueInt64) {
576     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::INT64);
577 
578     ASSERT_NE(value, nullptr);
579     ASSERT_EQ(1u, value->value.int64Values.size());
580 }
581 
TEST(VehicleUtilsTest,testCreateVehiclePropValueInt64Vec)582 TEST(VehicleUtilsTest, testCreateVehiclePropValueInt64Vec) {
583     std::unique_ptr<VehiclePropValue> value =
584             createVehiclePropValue(VehiclePropertyType::INT64_VEC);
585 
586     ASSERT_NE(value, nullptr);
587     ASSERT_EQ(1u, value->value.int64Values.size());
588 }
589 
TEST(VehicleUtilsTest,testCreateVehiclePropValueFloat)590 TEST(VehicleUtilsTest, testCreateVehiclePropValueFloat) {
591     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::FLOAT);
592 
593     ASSERT_NE(value, nullptr);
594     ASSERT_EQ(1u, value->value.floatValues.size());
595 }
596 
TEST(VehicleUtilsTest,testCreateVehiclePropValueFloatVec)597 TEST(VehicleUtilsTest, testCreateVehiclePropValueFloatVec) {
598     std::unique_ptr<VehiclePropValue> value =
599             createVehiclePropValue(VehiclePropertyType::FLOAT_VEC);
600 
601     ASSERT_NE(value, nullptr);
602     ASSERT_EQ(1u, value->value.floatValues.size());
603 }
604 
TEST(VehicleUtilsTest,testCreateVehiclePropValueBytes)605 TEST(VehicleUtilsTest, testCreateVehiclePropValueBytes) {
606     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::BYTES);
607 
608     ASSERT_NE(value, nullptr);
609     ASSERT_EQ(1u, value->value.byteValues.size());
610 }
611 
TEST(VehicleUtilsTest,testCreateVehiclePropValueString)612 TEST(VehicleUtilsTest, testCreateVehiclePropValueString) {
613     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::STRING);
614 
615     ASSERT_NE(value, nullptr);
616 }
617 
TEST(VehicleUtilsTest,testCreateVehiclePropValueMixed)618 TEST(VehicleUtilsTest, testCreateVehiclePropValueMixed) {
619     std::unique_ptr<VehiclePropValue> value = createVehiclePropValue(VehiclePropertyType::MIXED);
620 
621     ASSERT_NE(value, nullptr);
622 }
623 
TEST(VehicleUtilsTest,testCreateVehiclePropValueVecInt32)624 TEST(VehicleUtilsTest, testCreateVehiclePropValueVecInt32) {
625     std::unique_ptr<VehiclePropValue> value =
626             createVehiclePropValueVec(VehiclePropertyType::INT32, /*vecSize=*/2);
627 
628     ASSERT_NE(value, nullptr);
629     ASSERT_EQ(1u, value->value.int32Values.size())
630             << "vector size should always be 1 for single value type";
631 }
632 
TEST(VehicleUtilsTest,testCreateVehiclePropValueIntVec32Vec)633 TEST(VehicleUtilsTest, testCreateVehiclePropValueIntVec32Vec) {
634     std::unique_ptr<VehiclePropValue> value =
635             createVehiclePropValueVec(VehiclePropertyType::INT32_VEC, /*vecSize=*/2);
636 
637     ASSERT_NE(value, nullptr);
638     ASSERT_EQ(2u, value->value.int32Values.size());
639 }
640 
TEST(VehicleUtilsTest,testCreateVehiclePropValueVecInt64)641 TEST(VehicleUtilsTest, testCreateVehiclePropValueVecInt64) {
642     std::unique_ptr<VehiclePropValue> value =
643             createVehiclePropValueVec(VehiclePropertyType::INT64, /*vecSize=*/2);
644 
645     ASSERT_NE(value, nullptr);
646     ASSERT_EQ(1u, value->value.int64Values.size())
647             << "vector size should always be 1 for single value type";
648 }
649 
TEST(VehicleUtilsTest,testCreateVehiclePropValueIntVec64Vec)650 TEST(VehicleUtilsTest, testCreateVehiclePropValueIntVec64Vec) {
651     std::unique_ptr<VehiclePropValue> value =
652             createVehiclePropValueVec(VehiclePropertyType::INT64_VEC, /*vecSize=*/2);
653 
654     ASSERT_NE(value, nullptr);
655     ASSERT_EQ(2u, value->value.int64Values.size());
656 }
657 
TEST(VehicleUtilsTest,testCreateVehiclePropValueVecFloat)658 TEST(VehicleUtilsTest, testCreateVehiclePropValueVecFloat) {
659     std::unique_ptr<VehiclePropValue> value =
660             createVehiclePropValueVec(VehiclePropertyType::FLOAT, /*vecSize=*/2);
661 
662     ASSERT_NE(value, nullptr);
663     ASSERT_EQ(1u, value->value.floatValues.size())
664             << "vector size should always be 1 for single value type";
665 }
666 
TEST(VehicleUtilsTest,testCreateVehiclePropValueFloatVecMultiValues)667 TEST(VehicleUtilsTest, testCreateVehiclePropValueFloatVecMultiValues) {
668     std::unique_ptr<VehiclePropValue> value =
669             createVehiclePropValueVec(VehiclePropertyType::FLOAT_VEC, /*vecSize=*/2);
670 
671     ASSERT_NE(value, nullptr);
672     ASSERT_EQ(2u, value->value.floatValues.size());
673 }
674 
TEST(VehicleUtilsTest,testCreateVehiclePropValueVecBytes)675 TEST(VehicleUtilsTest, testCreateVehiclePropValueVecBytes) {
676     std::unique_ptr<VehiclePropValue> value =
677             createVehiclePropValueVec(VehiclePropertyType::BYTES, /*vecSize=*/2);
678 
679     ASSERT_NE(value, nullptr);
680     ASSERT_EQ(2u, value->value.byteValues.size());
681 }
682 
TEST(VehicleUtilsTest,testConcurrentQueueOneThread)683 TEST(VehicleUtilsTest, testConcurrentQueueOneThread) {
684     ConcurrentQueue<int> queue;
685 
686     queue.push(1);
687     queue.push(2);
688     auto result = queue.flush();
689 
690     ASSERT_EQ(result, std::vector<int>({1, 2}));
691 }
692 
TEST(VehicleUtilsTest,testConcurrentQueueMultipleThreads)693 TEST(VehicleUtilsTest, testConcurrentQueueMultipleThreads) {
694     ConcurrentQueue<int> queue;
695     std::vector<int> results;
696     std::atomic<bool> stop = false;
697 
698     std::thread t1([&queue]() {
699         for (int i = 0; i < 100; i++) {
700             queue.push(0);
701         }
702     });
703     std::thread t2([&queue]() {
704         for (int i = 0; i < 100; i++) {
705             queue.push(1);
706         }
707     });
708     std::thread t3([&queue, &results, &stop]() {
709         while (!stop) {
710             queue.waitForItems();
711             for (int i : queue.flush()) {
712                 results.push_back(i);
713             }
714         }
715 
716         // After we stop, get all the remaining values in the queue.
717         for (int i : queue.flush()) {
718             results.push_back(i);
719         }
720     });
721 
722     t1.join();
723     t2.join();
724 
725     stop = true;
726     queue.deactivate();
727     t3.join();
728 
729     size_t zeroCount = 0;
730     size_t oneCount = 0;
731     for (int i : results) {
732         if (i == 0) {
733             zeroCount++;
734         }
735         if (i == 1) {
736             oneCount++;
737         }
738     }
739 
740     EXPECT_EQ(results.size(), static_cast<size_t>(200));
741     EXPECT_EQ(zeroCount, static_cast<size_t>(100));
742     EXPECT_EQ(oneCount, static_cast<size_t>(100));
743 }
744 
TEST(VehicleUtilsTest,testConcurrentQueuePushAfterDeactivate)745 TEST(VehicleUtilsTest, testConcurrentQueuePushAfterDeactivate) {
746     ConcurrentQueue<int> queue;
747 
748     queue.deactivate();
749     queue.push(1);
750 
751     ASSERT_TRUE(queue.flush().empty());
752 }
753 
TEST(VehicleUtilsTest,testConcurrentQueueDeactivateNotifyWaitingThread)754 TEST(VehicleUtilsTest, testConcurrentQueueDeactivateNotifyWaitingThread) {
755     ConcurrentQueue<int> queue;
756 
757     std::thread t([&queue]() {
758         // This would block until queue is deactivated.
759         queue.waitForItems();
760     });
761 
762     queue.deactivate();
763 
764     t.join();
765 }
766 
TEST(VehicleUtilsTest,testVhalError)767 TEST(VehicleUtilsTest, testVhalError) {
768     VhalResult<void> result = Error<VhalError>(StatusCode::INVALID_ARG) << "error message";
769 
770     ASSERT_EQ(result.error().message(), "error message: INVALID_ARG");
771 }
772 
TEST(VehicleUtilsTest,testPropIdToString)773 TEST(VehicleUtilsTest, testPropIdToString) {
774     ASSERT_EQ(propIdToString(toInt(VehicleProperty::PERF_VEHICLE_SPEED)), "PERF_VEHICLE_SPEED");
775 }
776 
TEST(VehicleUtilsTest,testStringToPropId)777 TEST(VehicleUtilsTest, testStringToPropId) {
778     auto result = stringToPropId("PERF_VEHICLE_SPEED");
779 
780     ASSERT_TRUE(result.ok());
781     ASSERT_EQ(result.value(), toInt(VehicleProperty::PERF_VEHICLE_SPEED));
782 }
783 
TEST(VehicleUtilsTest,testStringToPropId_InvalidName)784 TEST(VehicleUtilsTest, testStringToPropId_InvalidName) {
785     auto result = stringToPropId("PERF_VEHICLE_SPEED12345");
786 
787     ASSERT_FALSE(result.ok());
788 }
789 
790 class InvalidPropValueTest : public testing::TestWithParam<InvalidPropValueTestCase> {};
791 
792 INSTANTIATE_TEST_SUITE_P(InvalidPropValueTests, InvalidPropValueTest,
793                          testing::ValuesIn(getInvalidPropValuesTestCases()),
__anon7eca4c4d0602(const testing::TestParamInfo<InvalidPropValueTest::ParamType>& info) 794                          [](const testing::TestParamInfo<InvalidPropValueTest::ParamType>& info) {
795                              return info.param.name;
796                          });
797 
TEST_P(InvalidPropValueTest,testCheckPropValue)798 TEST_P(InvalidPropValueTest, testCheckPropValue) {
799     InvalidPropValueTestCase tc = GetParam();
800 
801     // Config is not used for non-mixed types.
802     auto result = checkPropValue(tc.value, &tc.config);
803 
804     ASSERT_EQ(tc.valid, result.ok());
805 }
806 
807 class InvalidValueRangeTest : public testing::TestWithParam<InvalidValueRangeTestCase> {};
808 
809 INSTANTIATE_TEST_SUITE_P(InvalidValueRangeTests, InvalidValueRangeTest,
810                          testing::ValuesIn(getInvalidValueRangeTestCases()),
__anon7eca4c4d0702(const testing::TestParamInfo<InvalidValueRangeTest::ParamType>& info) 811                          [](const testing::TestParamInfo<InvalidValueRangeTest::ParamType>& info) {
812                              return info.param.name;
813                          });
814 
TEST_P(InvalidValueRangeTest,testCheckValueRange)815 TEST_P(InvalidValueRangeTest, testCheckValueRange) {
816     InvalidValueRangeTestCase tc = GetParam();
817 
818     // Config is not used for non-mixed types.
819     auto result = checkValueRange(tc.value, &tc.config);
820 
821     ASSERT_EQ(tc.valid, result.ok());
822 }
823 
824 }  // namespace vehicle
825 }  // namespace automotive
826 }  // namespace hardware
827 }  // namespace android
828