1 /*
2  * Copyright (C) 2018 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 <android/hardware/automotive/vehicle/2.0/IVehicle.h>
18 #include <gtest/gtest.h>
19 
20 #include "VehicleHalTestUtils.h"
21 #include "vhal_v2_0/VmsUtils.h"
22 
23 namespace android {
24 namespace hardware {
25 namespace automotive {
26 namespace vehicle {
27 namespace V2_0 {
28 namespace vms {
29 
30 namespace {
31 
TEST(VmsUtilsTest,subscribeMessage)32 TEST(VmsUtilsTest, subscribeMessage) {
33     VmsLayer layer(1, 0, 2);
34     auto message = createSubscribeMessage(layer);
35     ASSERT_NE(message, nullptr);
36     EXPECT_TRUE(isValidVmsMessage(*message));
37     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
38     EXPECT_EQ(message->value.int32Values.size(), 0x4ul);
39     EXPECT_EQ(parseMessageType(*message), VmsMessageType::SUBSCRIBE);
40 
41     // Layer
42     EXPECT_EQ(message->value.int32Values[1], 1);
43     EXPECT_EQ(message->value.int32Values[2], 0);
44     EXPECT_EQ(message->value.int32Values[3], 2);
45 }
46 
TEST(VmsUtilsTest,unsubscribeMessage)47 TEST(VmsUtilsTest, unsubscribeMessage) {
48     VmsLayer layer(1, 0, 2);
49     auto message = createUnsubscribeMessage(layer);
50     ASSERT_NE(message, nullptr);
51     EXPECT_TRUE(isValidVmsMessage(*message));
52     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
53     EXPECT_EQ(message->value.int32Values.size(), 0x4ul);
54     EXPECT_EQ(parseMessageType(*message), VmsMessageType::UNSUBSCRIBE);
55 
56     // Layer
57     EXPECT_EQ(message->value.int32Values[1], 1);
58     EXPECT_EQ(message->value.int32Values[2], 0);
59     EXPECT_EQ(message->value.int32Values[3], 2);
60 }
61 
TEST(VmsUtilsTest,singleOfferingMessage)62 TEST(VmsUtilsTest, singleOfferingMessage) {
63     VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 2))}};
64     auto message = createOfferingMessage(offers);
65     ASSERT_NE(message, nullptr);
66     EXPECT_TRUE(isValidVmsMessage(*message));
67     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
68     EXPECT_EQ(message->value.int32Values.size(), 0x7ul);
69     EXPECT_EQ(parseMessageType(*message), VmsMessageType::OFFERING);
70 
71     // Publisher ID
72     EXPECT_EQ(message->value.int32Values[1], 123);
73 
74     // Number of layer offerings
75     EXPECT_EQ(message->value.int32Values[2], 1);
76 
77     // Layer
78     EXPECT_EQ(message->value.int32Values[3], 1);
79     EXPECT_EQ(message->value.int32Values[4], 0);
80     EXPECT_EQ(message->value.int32Values[5], 2);
81 
82     // Number of dependencies
83     EXPECT_EQ(message->value.int32Values[6], 0);
84 }
85 
TEST(VmsUtilsTest,offeringWithDependencies)86 TEST(VmsUtilsTest, offeringWithDependencies) {
87     VmsLayer layer(1, 0, 2);
88     std::vector<VmsLayer> dependencies = {VmsLayer(2, 0, 2), VmsLayer(3, 0, 3)};
89     std::vector<VmsLayerOffering> offering = {VmsLayerOffering(layer, dependencies)};
90     VmsOffers offers = {123, offering};
91     auto message = createOfferingMessage(offers);
92     ASSERT_NE(message, nullptr);
93     EXPECT_TRUE(isValidVmsMessage(*message));
94     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
95     EXPECT_EQ(message->value.int32Values.size(), 0xdul);
96     EXPECT_EQ(parseMessageType(*message), VmsMessageType::OFFERING);
97 
98     // Publisher ID
99     EXPECT_EQ(message->value.int32Values[1], 123);
100 
101     // Number of layer offerings
102     EXPECT_EQ(message->value.int32Values[2], 1);
103 
104     // Layer
105     EXPECT_EQ(message->value.int32Values[3], 1);
106     EXPECT_EQ(message->value.int32Values[4], 0);
107     EXPECT_EQ(message->value.int32Values[5], 2);
108 
109     // Number of dependencies
110     EXPECT_EQ(message->value.int32Values[6], 2);
111 
112     // Dependency 1
113     EXPECT_EQ(message->value.int32Values[7], 2);
114     EXPECT_EQ(message->value.int32Values[8], 0);
115     EXPECT_EQ(message->value.int32Values[9], 2);
116 
117     // Dependency 2
118     EXPECT_EQ(message->value.int32Values[10], 3);
119     EXPECT_EQ(message->value.int32Values[11], 0);
120     EXPECT_EQ(message->value.int32Values[12], 3);
121 }
122 
TEST(VmsUtilsTest,availabilityMessage)123 TEST(VmsUtilsTest, availabilityMessage) {
124     auto message = createAvailabilityRequest();
125     ASSERT_NE(message, nullptr);
126     EXPECT_TRUE(isValidVmsMessage(*message));
127     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
128     EXPECT_EQ(message->value.int32Values.size(), 0x1ul);
129     EXPECT_EQ(parseMessageType(*message), VmsMessageType::AVAILABILITY_REQUEST);
130 }
131 
TEST(VmsUtilsTest,subscriptionsMessage)132 TEST(VmsUtilsTest, subscriptionsMessage) {
133     auto message = createSubscriptionsRequest();
134     ASSERT_NE(message, nullptr);
135     EXPECT_TRUE(isValidVmsMessage(*message));
136     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
137     EXPECT_EQ(message->value.int32Values.size(), 0x1ul);
138     EXPECT_EQ(parseMessageType(*message), VmsMessageType::SUBSCRIPTIONS_REQUEST);
139 }
140 
TEST(VmsUtilsTest,dataMessage)141 TEST(VmsUtilsTest, dataMessage) {
142     const std::string bytes = "aaa";
143     const VmsLayerAndPublisher layer_and_publisher(VmsLayer(2, 0, 1), 123);
144     auto message = createDataMessageWithLayerPublisherInfo(layer_and_publisher, bytes);
145     ASSERT_NE(message, nullptr);
146     EXPECT_TRUE(isValidVmsMessage(*message));
147     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
148     EXPECT_EQ(message->value.int32Values.size(), 0x5ul);
149     EXPECT_EQ(message->value.int32Values[0], toInt(VmsMessageType::DATA));
150 
151     // Layer
152     EXPECT_EQ(message->value.int32Values[1], 2);
153     EXPECT_EQ(message->value.int32Values[2], 0);
154     EXPECT_EQ(message->value.int32Values[3], 1);
155 
156     // Publisher ID
157     EXPECT_EQ(message->value.int32Values[4], 123);
158 
159     EXPECT_EQ(parseMessageType(*message), VmsMessageType::DATA);
160     EXPECT_EQ(message->value.bytes.size(), bytes.size());
161     EXPECT_EQ(memcmp(message->value.bytes.data(), bytes.data(), bytes.size()), 0);
162 }
163 
TEST(VmsUtilsTest,emptyMessageInvalid)164 TEST(VmsUtilsTest, emptyMessageInvalid) {
165     VehiclePropValue empty_prop;
166     EXPECT_FALSE(isValidVmsMessage(empty_prop));
167 }
168 
TEST(VmsUtilsTest,invalidMessageType)169 TEST(VmsUtilsTest, invalidMessageType) {
170     VmsLayer layer(1, 0, 2);
171     auto message = createSubscribeMessage(layer);
172     message->value.int32Values[0] = -1;
173 
174     EXPECT_FALSE(isValidVmsMessage(*message));
175 }
176 
TEST(VmsUtilsTest,parseDataMessage)177 TEST(VmsUtilsTest, parseDataMessage) {
178     const std::string bytes = "aaa";
179     const VmsLayerAndPublisher layer_and_publisher(VmsLayer(1, 0, 1), 123);
180     auto message = createDataMessageWithLayerPublisherInfo(layer_and_publisher, bytes);
181     auto data_str = parseData(*message);
182     ASSERT_FALSE(data_str.empty());
183     EXPECT_EQ(data_str, bytes);
184 }
185 
TEST(VmsUtilsTest,parseInvalidDataMessage)186 TEST(VmsUtilsTest, parseInvalidDataMessage) {
187     VmsLayer layer(1, 0, 2);
188     auto message = createSubscribeMessage(layer);
189     auto data_str = parseData(*message);
190     EXPECT_TRUE(data_str.empty());
191 }
192 
TEST(VmsUtilsTest,publisherIdRequest)193 TEST(VmsUtilsTest, publisherIdRequest) {
194     std::string bytes = "pub_id";
195     auto message = createPublisherIdRequest(bytes);
196     ASSERT_NE(message, nullptr);
197     EXPECT_TRUE(isValidVmsMessage(*message));
198     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
199     EXPECT_EQ(message->value.int32Values.size(), 0x1ul);
200     EXPECT_EQ(parseMessageType(*message), VmsMessageType::PUBLISHER_ID_REQUEST);
201     EXPECT_EQ(message->value.bytes.size(), bytes.size());
202     EXPECT_EQ(memcmp(message->value.bytes.data(), bytes.data(), bytes.size()), 0);
203 }
204 
TEST(VmsUtilsTest,validPublisherIdResponse)205 TEST(VmsUtilsTest, validPublisherIdResponse) {
206     auto message = createBaseVmsMessage(2);
207     message->value.int32Values =
208             hidl_vec<int32_t>{toInt(VmsMessageType::PUBLISHER_ID_RESPONSE), 1234};
209     EXPECT_EQ(parsePublisherIdResponse(*message), 1234);
210 }
211 
TEST(VmsUtilsTest,invalidPublisherIdResponse)212 TEST(VmsUtilsTest, invalidPublisherIdResponse) {
213     auto message = createBaseVmsMessage(1);
214     EXPECT_EQ(parsePublisherIdResponse(*message), -1);
215 }
216 
TEST(VmsUtilsTest,validSequenceNumberForSubscriptionsChange)217 TEST(VmsUtilsTest, validSequenceNumberForSubscriptionsChange) {
218     auto message = createBaseVmsMessage(2);
219     message->value.int32Values =
220             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234};
221     EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), 1234);
222 }
223 
TEST(VmsUtilsTest,validSequenceNumberForSubscriptionsResponse)224 TEST(VmsUtilsTest, validSequenceNumberForSubscriptionsResponse) {
225     auto message = createBaseVmsMessage(2);
226     message->value.int32Values =
227             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234};
228     EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), 1234);
229 }
230 
TEST(VmsUtilsTest,invalidSubscriptionsState)231 TEST(VmsUtilsTest, invalidSubscriptionsState) {
232     auto message = createBaseVmsMessage(1);
233     EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), -1);
234 }
235 
TEST(VmsUtilsTest,newSequenceNumberForExistingSmallerNumberForChange)236 TEST(VmsUtilsTest, newSequenceNumberForExistingSmallerNumberForChange) {
237     auto message = createBaseVmsMessage(2);
238     message->value.int32Values =
239             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234};
240     EXPECT_TRUE(isSequenceNumberNewer(*message, 1233));
241 }
242 
TEST(VmsUtilsTest,newSequenceNumberForExistingSmallerNumberForResponse)243 TEST(VmsUtilsTest, newSequenceNumberForExistingSmallerNumberForResponse) {
244     auto message = createBaseVmsMessage(2);
245     message->value.int32Values =
246             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234};
247     EXPECT_TRUE(isSequenceNumberNewer(*message, 1233));
248 }
249 
TEST(VmsUtilsTest,newSequenceNumberForExistingGreaterNumberForChange)250 TEST(VmsUtilsTest, newSequenceNumberForExistingGreaterNumberForChange) {
251     auto message = createBaseVmsMessage(2);
252     message->value.int32Values =
253             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234};
254     EXPECT_FALSE(isSequenceNumberNewer(*message, 1235));
255 }
256 
TEST(VmsUtilsTest,newSequenceNumberForExistingGreaterNumberForResponse)257 TEST(VmsUtilsTest, newSequenceNumberForExistingGreaterNumberForResponse) {
258     auto message = createBaseVmsMessage(2);
259     message->value.int32Values =
260             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234};
261     EXPECT_FALSE(isSequenceNumberNewer(*message, 1235));
262 }
263 
TEST(VmsUtilsTest,newSequenceNumberForSameNumberForChange)264 TEST(VmsUtilsTest, newSequenceNumberForSameNumberForChange) {
265     auto message = createBaseVmsMessage(2);
266     message->value.int32Values =
267             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234};
268     EXPECT_FALSE(isSequenceNumberNewer(*message, 1234));
269 }
270 
TEST(VmsUtilsTest,newSequenceNumberForSameNumberForResponse)271 TEST(VmsUtilsTest, newSequenceNumberForSameNumberForResponse) {
272     auto message = createBaseVmsMessage(2);
273     message->value.int32Values =
274             hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234};
275     EXPECT_FALSE(isSequenceNumberNewer(*message, 1234));
276 }
277 
testSubscribedLayers(VmsMessageType type)278 void testSubscribedLayers(VmsMessageType type) {
279     VmsOffers offers = {123,
280                         {VmsLayerOffering(VmsLayer(1, 0, 1), {VmsLayer(4, 1, 1)}),
281                          VmsLayerOffering(VmsLayer(2, 0, 1))}};
282     auto message = createBaseVmsMessage(16);
283     message->value.int32Values = hidl_vec<int32_t>{toInt(type),
284                                                    1234,  // sequence number
285                                                    2,     // number of layers
286                                                    1,     // number of associated layers
287                                                    1,     // layer 1
288                                                    0,           1,
289                                                    4,  // layer 2
290                                                    1,           1,
291                                                    2,  // associated layer
292                                                    0,           1,
293                                                    2,    // number of publisher IDs
294                                                    111,  // publisher IDs
295                                                    123};
296     EXPECT_TRUE(isValidVmsMessage(*message));
297     auto result = getSubscribedLayers(*message, offers);
298     EXPECT_EQ(static_cast<int>(result.size()), 2);
299     EXPECT_EQ(result.at(0), VmsLayer(1, 0, 1));
300     EXPECT_EQ(result.at(1), VmsLayer(2, 0, 1));
301 }
302 
TEST(VmsUtilsTest,subscribedLayersForChange)303 TEST(VmsUtilsTest, subscribedLayersForChange) {
304     testSubscribedLayers(VmsMessageType::SUBSCRIPTIONS_CHANGE);
305 }
306 
TEST(VmsUtilsTest,subscribedLayersForResponse)307 TEST(VmsUtilsTest, subscribedLayersForResponse) {
308     testSubscribedLayers(VmsMessageType::SUBSCRIPTIONS_RESPONSE);
309 }
310 
testGetSubscribedLayersMalformedData(VmsMessageType type)311 void testGetSubscribedLayersMalformedData(VmsMessageType type) {
312     VmsOffers offers = {123,
313                         {VmsLayerOffering(VmsLayer(1, 0, 1), {VmsLayer(4, 1, 1)}),
314                          VmsLayerOffering(VmsLayer(2, 0, 1))}};
315     auto message = createBaseVmsMessage(2);
316     message->value.int32Values = hidl_vec<int32_t>{toInt(type), 1234};  // sequence number
317     EXPECT_TRUE(isValidVmsMessage(*message));
318     auto result = getSubscribedLayers(*message, offers);
319     EXPECT_EQ(static_cast<int>(result.size()), 0);
320 }
321 
TEST(VmsUtilsTest,subscribedLayersForMalformedChange)322 TEST(VmsUtilsTest, subscribedLayersForMalformedChange) {
323     testGetSubscribedLayersMalformedData(VmsMessageType::SUBSCRIPTIONS_CHANGE);
324 }
325 
TEST(VmsUtilsTest,subscribedLayersForMalformedResponse)326 TEST(VmsUtilsTest, subscribedLayersForMalformedResponse) {
327     testGetSubscribedLayersMalformedData(VmsMessageType::SUBSCRIPTIONS_RESPONSE);
328 }
329 
testSubscribedLayersWithDifferentSubtype(VmsMessageType type)330 void testSubscribedLayersWithDifferentSubtype(VmsMessageType type) {
331     VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}};
332     auto message = createBaseVmsMessage(7);
333     message->value.int32Values = hidl_vec<int32_t>{toInt(type),
334                                                    1234,  // sequence number
335                                                    1,     // number of layers
336                                                    0,     // number of associated layers
337                                                    1,     // layer 1
338                                                    1,     // different subtype
339                                                    1};
340     EXPECT_TRUE(isValidVmsMessage(*message));
341     EXPECT_TRUE(getSubscribedLayers(*message, offers).empty());
342 }
343 
TEST(VmsUtilsTest,subscribedLayersWithDifferentSubtypeForChange)344 TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtypeForChange) {
345     testSubscribedLayersWithDifferentSubtype(VmsMessageType::SUBSCRIPTIONS_CHANGE);
346 }
347 
TEST(VmsUtilsTest,subscribedLayersWithDifferentSubtypeForResponse)348 TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtypeForResponse) {
349     testSubscribedLayersWithDifferentSubtype(VmsMessageType::SUBSCRIPTIONS_RESPONSE);
350 }
351 
subscribedLayersWithDifferentVersion(VmsMessageType type)352 void subscribedLayersWithDifferentVersion(VmsMessageType type) {
353     VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}};
354     auto message = createBaseVmsMessage(7);
355     message->value.int32Values = hidl_vec<int32_t>{toInt(type),
356                                                    1234,             // sequence number
357                                                    1,                // number of layers
358                                                    0,                // number of associated layers
359                                                    1,                // layer 1
360                                                    0,           2};  // different version
361     EXPECT_TRUE(isValidVmsMessage(*message));
362     EXPECT_TRUE(getSubscribedLayers(*message, offers).empty());
363 }
364 
TEST(VmsUtilsTest,subscribedLayersWithDifferentVersionForChange)365 TEST(VmsUtilsTest, subscribedLayersWithDifferentVersionForChange) {
366     subscribedLayersWithDifferentVersion(VmsMessageType::SUBSCRIPTIONS_CHANGE);
367 }
368 
TEST(VmsUtilsTest,subscribedLayersWithDifferentVersionForResponse)369 TEST(VmsUtilsTest, subscribedLayersWithDifferentVersionForResponse) {
370     subscribedLayersWithDifferentVersion(VmsMessageType::SUBSCRIPTIONS_RESPONSE);
371 }
372 
subscribedLayersWithDifferentPublisherId(VmsMessageType type)373 void subscribedLayersWithDifferentPublisherId(VmsMessageType type) {
374     VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}};
375     auto message = createBaseVmsMessage(9);
376     message->value.int32Values = hidl_vec<int32_t>{toInt(type),
377                                                    1234,  // sequence number
378                                                    0,     // number of layers
379                                                    1,     // number of associated layers
380                                                    1,     // associated layer 1
381                                                    0,           1,
382                                                    1,     // number of publisher IDs
383                                                    234};  // publisher ID 1
384     EXPECT_TRUE(isValidVmsMessage(*message));
385     EXPECT_TRUE(getSubscribedLayers(*message, offers).empty());
386 }
387 
TEST(VmsUtilsTest,subscribedLayersWithDifferentPublisherIdForChange)388 TEST(VmsUtilsTest, subscribedLayersWithDifferentPublisherIdForChange) {
389     subscribedLayersWithDifferentPublisherId(VmsMessageType::SUBSCRIPTIONS_CHANGE);
390 }
391 
TEST(VmsUtilsTest,subscribedLayersWithDifferentPublisherIdForResponse)392 TEST(VmsUtilsTest, subscribedLayersWithDifferentPublisherIdForResponse) {
393     subscribedLayersWithDifferentPublisherId(VmsMessageType::SUBSCRIPTIONS_RESPONSE);
394 }
395 
TEST(VmsUtilsTest,serviceNewlyStarted)396 TEST(VmsUtilsTest, serviceNewlyStarted) {
397     auto message = createBaseVmsMessage(2);
398     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 0};
399     EXPECT_TRUE(hasServiceNewlyStarted(*message));
400 }
401 
TEST(VmsUtilsTest,serviceNotNewlyStarted)402 TEST(VmsUtilsTest, serviceNotNewlyStarted) {
403     auto message = createBaseVmsMessage(2);
404     message->value.int32Values =
405             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 1234};
406     EXPECT_FALSE(hasServiceNewlyStarted(*message));
407 }
408 
TEST(VmsUtilsTest,invalidAvailabilityChange)409 TEST(VmsUtilsTest, invalidAvailabilityChange) {
410     auto message = createBaseVmsMessage(1);
411     EXPECT_FALSE(hasServiceNewlyStarted(*message));
412 }
413 
TEST(VmsUtilsTest,startSessionRequest)414 TEST(VmsUtilsTest, startSessionRequest) {
415     auto message = createStartSessionMessage(123, 456);
416     ASSERT_NE(message, nullptr);
417     EXPECT_TRUE(isValidVmsMessage(*message));
418     EXPECT_EQ(message->prop, toInt(VehicleProperty::VEHICLE_MAP_SERVICE));
419     EXPECT_EQ(message->value.int32Values.size(), 0x3ul);
420     EXPECT_EQ(parseMessageType(*message), VmsMessageType::START_SESSION);
421     EXPECT_EQ(message->value.int32Values[1], 123);
422     EXPECT_EQ(message->value.int32Values[2], 456);
423 }
424 
TEST(VmsUtilsTest,startSessionServiceNewlyStarted)425 TEST(VmsUtilsTest, startSessionServiceNewlyStarted) {
426     auto message = createBaseVmsMessage(3);
427     int new_service_id;
428     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 123, -1};
429     EXPECT_EQ(parseStartSessionMessage(*message, 122, 456, &new_service_id),
430               VmsSessionStatus::kNewServerSession);
431     EXPECT_EQ(new_service_id, 123);
432 }
433 
TEST(VmsUtilsTest,startSessionServiceNewlyStartedEdgeCase)434 TEST(VmsUtilsTest, startSessionServiceNewlyStartedEdgeCase) {
435     auto message = createBaseVmsMessage(3);
436     int new_service_id;
437     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 0, -1};
438     EXPECT_EQ(parseStartSessionMessage(*message, -1, 0, &new_service_id),
439               VmsSessionStatus::kNewServerSession);
440     EXPECT_EQ(new_service_id, 0);
441 }
442 
TEST(VmsUtilsTest,startSessionClientNewlyStarted)443 TEST(VmsUtilsTest, startSessionClientNewlyStarted) {
444     auto message = createBaseVmsMessage(3);
445     int new_service_id;
446     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 123, 456};
447     EXPECT_EQ(parseStartSessionMessage(*message, -1, 456, &new_service_id),
448               VmsSessionStatus::kAckToCurrentSession);
449     EXPECT_EQ(new_service_id, 123);
450 }
451 
TEST(VmsUtilsTest,startSessionClientNewlyStartedWithSameServerAndClientId)452 TEST(VmsUtilsTest, startSessionClientNewlyStartedWithSameServerAndClientId) {
453     auto message = createBaseVmsMessage(3);
454     int new_service_id;
455     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 123, 456};
456     EXPECT_EQ(parseStartSessionMessage(*message, 123, 456, &new_service_id),
457               VmsSessionStatus::kAckToCurrentSession);
458     EXPECT_EQ(new_service_id, 123);
459 }
460 
TEST(VmsUtilsTest,startSessionWithZeroAsIds)461 TEST(VmsUtilsTest, startSessionWithZeroAsIds) {
462     auto message = createBaseVmsMessage(3);
463     int new_service_id;
464     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 0, 0};
465     EXPECT_EQ(parseStartSessionMessage(*message, 0, 0, &new_service_id),
466               VmsSessionStatus::kAckToCurrentSession);
467     EXPECT_EQ(new_service_id, 0);
468 }
469 
TEST(VmsUtilsTest,startSessionOldServiceId)470 TEST(VmsUtilsTest, startSessionOldServiceId) {
471     auto message = createBaseVmsMessage(3);
472     int new_service_id;
473     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 120, 456};
474     EXPECT_EQ(parseStartSessionMessage(*message, 123, 456, &new_service_id),
475               VmsSessionStatus::kAckToCurrentSession);
476     EXPECT_EQ(new_service_id, 120);
477 }
478 
TEST(VmsUtilsTest,startSessionNegativeServerId)479 TEST(VmsUtilsTest, startSessionNegativeServerId) {
480     auto message = createBaseVmsMessage(3);
481     int new_service_id;
482     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), -1, 456};
483     EXPECT_EQ(parseStartSessionMessage(*message, -1, 456, &new_service_id),
484               VmsSessionStatus::kAckToCurrentSession);
485     EXPECT_EQ(new_service_id, -1);
486 }
487 
TEST(VmsUtilsTest,startSessionInvalidMessageFormat)488 TEST(VmsUtilsTest, startSessionInvalidMessageFormat) {
489     auto message = createBaseVmsMessage(2);
490     int new_service_id;
491     message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::START_SESSION), 123};
492     EXPECT_EQ(parseStartSessionMessage(*message, 123, 456, &new_service_id),
493               VmsSessionStatus::kInvalidMessage);
494     EXPECT_EQ(new_service_id, 123);
495 }
496 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForExistingSmallerNumberForChange)497 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForExistingSmallerNumberForChange) {
498     auto message = createBaseVmsMessage(2);
499     message->value.int32Values =
500             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 1234};
501     EXPECT_TRUE(isAvailabilitySequenceNumberNewer(*message, 1233));
502 }
503 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForExistingSmallerNumberForResponse)504 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForExistingSmallerNumberForResponse) {
505     auto message = createBaseVmsMessage(2);
506     message->value.int32Values =
507             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_RESPONSE), 1234};
508     EXPECT_TRUE(isAvailabilitySequenceNumberNewer(*message, 1233));
509 }
510 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForExistingGreaterNumberForChange)511 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForExistingGreaterNumberForChange) {
512     auto message = createBaseVmsMessage(2);
513     message->value.int32Values =
514             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 1234};
515     EXPECT_FALSE(isAvailabilitySequenceNumberNewer(*message, 1235));
516 }
517 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForExistingGreaterNumberForResponse)518 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForExistingGreaterNumberForResponse) {
519     auto message = createBaseVmsMessage(2);
520     message->value.int32Values =
521             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_RESPONSE), 1234};
522     EXPECT_FALSE(isAvailabilitySequenceNumberNewer(*message, 1235));
523 }
524 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForSameNumberForChange)525 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForSameNumberForChange) {
526     auto message = createBaseVmsMessage(2);
527     message->value.int32Values =
528             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 1234};
529     EXPECT_FALSE(isAvailabilitySequenceNumberNewer(*message, 1234));
530 }
531 
TEST(VmsUtilsTest,newAvailabilitySequenceNumberForSameNumberForResponse)532 TEST(VmsUtilsTest, newAvailabilitySequenceNumberForSameNumberForResponse) {
533     auto message = createBaseVmsMessage(2);
534     message->value.int32Values =
535             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_RESPONSE), 1234};
536     EXPECT_FALSE(isAvailabilitySequenceNumberNewer(*message, 1234));
537 }
538 
TEST(VmsUtilsTest,validSequenceNumberForAvailabilityChange)539 TEST(VmsUtilsTest, validSequenceNumberForAvailabilityChange) {
540     auto message = createBaseVmsMessage(2);
541     message->value.int32Values =
542             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 1234};
543     EXPECT_EQ(getSequenceNumberForAvailabilityState(*message), 1234);
544 }
545 
TEST(VmsUtilsTest,validSequenceNumberForAvailabilityResponse)546 TEST(VmsUtilsTest, validSequenceNumberForAvailabilityResponse) {
547     auto message = createBaseVmsMessage(2);
548     message->value.int32Values =
549             hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_RESPONSE), 1234};
550     EXPECT_EQ(getSequenceNumberForAvailabilityState(*message), 1234);
551 }
552 
TEST(VmsUtilsTest,invalidAvailabilityState)553 TEST(VmsUtilsTest, invalidAvailabilityState) {
554     auto message = createBaseVmsMessage(1);
555     EXPECT_EQ(getSequenceNumberForAvailabilityState(*message), -1);
556 }
557 
testGetAvailableLayers(VmsMessageType type)558 void testGetAvailableLayers(VmsMessageType type) {
559     auto message = createBaseVmsMessage(13);
560     message->value.int32Values = hidl_vec<int32_t>{toInt(type),
561                                                    1234,  // sequence number
562                                                    2,     // number of associated layers
563                                                    1,     // associated layer 1
564                                                    0,           1,
565                                                    2,    // number of publisher IDs
566                                                    111,  // publisher IDs
567                                                    123,
568                                                    2,                   // associated layer 2
569                                                    0,           1, 0};  // number of publisher IDs
570     EXPECT_TRUE(isValidVmsMessage(*message));
571     auto result = getAvailableLayers(*message);
572     EXPECT_EQ(static_cast<int>(result.size()), 2);
573     EXPECT_EQ(result.at(0).layer, VmsLayer(1, 0, 1));
574     EXPECT_EQ(result.at(0).publisher_ids.at(0), 111);
575     EXPECT_EQ(result.at(0).publisher_ids.at(1), 123);
576     EXPECT_EQ(result.at(1).layer, VmsLayer(2, 0, 1));
577     EXPECT_EQ(static_cast<int>(result.at(1).publisher_ids.size()), 0);
578 }
579 
TEST(VmsUtilsTest,availableLayersForChange)580 TEST(VmsUtilsTest, availableLayersForChange) {
581     testGetAvailableLayers(VmsMessageType::AVAILABILITY_CHANGE);
582 }
583 
TEST(VmsUtilsTest,availableLayersForResponse)584 TEST(VmsUtilsTest, availableLayersForResponse) {
585     testGetAvailableLayers(VmsMessageType::AVAILABILITY_RESPONSE);
586 }
587 
testGetAvailableLayersMalformedData(VmsMessageType type)588 void testGetAvailableLayersMalformedData(VmsMessageType type) {
589     auto message = createBaseVmsMessage(2);
590     message->value.int32Values = hidl_vec<int32_t>{toInt(type), 1234};  // sequence number
591     EXPECT_TRUE(isValidVmsMessage(*message));
592     auto result = getAvailableLayers(*message);
593     EXPECT_EQ(static_cast<int>(result.size()), 0);
594 }
595 
TEST(VmsUtilsTest,availableLayersForMalformedChange)596 TEST(VmsUtilsTest, availableLayersForMalformedChange) {
597     testGetAvailableLayersMalformedData(VmsMessageType::AVAILABILITY_CHANGE);
598 }
599 
TEST(VmsUtilsTest,availableLayersForMalformedResponse)600 TEST(VmsUtilsTest, availableLayersForMalformedResponse) {
601     testGetAvailableLayersMalformedData(VmsMessageType::AVAILABILITY_RESPONSE);
602 }
603 
604 }  // namespace
605 
606 }  // namespace vms
607 }  // namespace V2_0
608 }  // namespace vehicle
609 }  // namespace automotive
610 }  // namespace hardware
611 }  // namespace android
612