summaryrefslogtreecommitdiff
path: root/src/components/connection_handler/test/connection_test.cc
blob: 98b83f5fc4c604a35f369dafb33f161d06df7512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 * Copyright (c) 2015, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <algorithm>

#include "gtest/gtest.h"

#include "protocol/common.h"
#include "connection_handler/connection.h"
#include "connection_handler/mock_connection_handler.h"
#include "connection_handler/connection_handler_impl.h"
#include "protocol/service_type.h"
#include "connection_handler/mock_connection_handler_settings.h"
#include "transport_manager/mock_transport_manager.h"
#ifdef ENABLE_SECURITY
#include "security_manager/mock_security_manager.h"
#include "security_manager/mock_ssl_context.h"
#endif  // ENABLE_SECURITY

#define EXPECT_RETURN_TRUE true
#define EXPECT_RETURN_FALSE false
#define EXPECT_SERVICE_EXISTS true
#define EXPECT_SERVICE_NOT_EXISTS false

namespace test {
namespace components {
namespace connection_handler_test {
using namespace ::connection_handler;
using namespace ::protocol_handler;

using ::testing::Return;

class ConnectionTest : public ::testing::Test {
 protected:
  void SetUp() OVERRIDE {
    connection_handler_ = new ConnectionHandlerImpl(
        mock_connection_handler_settings, transport_manager_mock);
    const ConnectionHandle connectionHandle = 0;
    const DeviceHandle device_handle = 0u;
    const uint32_t heart_beat = 10000u;
    connection_ = new Connection(
        connectionHandle, device_handle, connection_handler_, heart_beat);
  }

  void TearDown() OVERRIDE {
    delete connection_;
    delete connection_handler_;
  }
  void StartSession() {
    StartDefaultSession();
    connection_->UpdateProtocolVersionSession(
        session_id, protocol_handler::PROTOCOL_VERSION_3);
  }
  void StartDefaultSession() {
    session_id = connection_->AddNewSession(kDefaultConnectionHandle);
    EXPECT_NE(session_id, 0u);
    const SessionMap sessionMap = connection_->session_map();
    EXPECT_FALSE(sessionMap.empty());
    const ServiceList serviceList = sessionMap.begin()->second.service_list;
    EXPECT_FALSE(serviceList.empty());
    const ServiceList::const_iterator it =
        std::find(serviceList.begin(), serviceList.end(), kRpc);
    const bool found_result = (it != serviceList.end());
    EXPECT_TRUE(found_result);
    EXPECT_EQ(connection_->primary_connection_handle(), 0);
  }
  void AddNewService(const ServiceType service_type,
                     const bool protection,
                     const bool expect_add_new_service_call_result,
                     const bool expect_exist_service) {
    const bool result = connection_->AddNewService(
        session_id, service_type, protection, kDefaultConnectionHandle);
    EXPECT_EQ(result, expect_add_new_service_call_result);

#ifdef ENABLE_SECURITY
    if (protection) {
      connection_->SetProtectionFlag(session_id, service_type);
    }
#endif  // ENABLE_SECURITY
    const SessionMap session_map = connection_->session_map();
    EXPECT_FALSE(session_map.empty());
    const ServiceList newServiceList = session_map.begin()->second.service_list;
    EXPECT_FALSE(newServiceList.empty());
    const ServiceList::const_iterator it =
        std::find(newServiceList.begin(), newServiceList.end(), service_type);
    const bool found_result = it != newServiceList.end();
    EXPECT_EQ(expect_exist_service, found_result);
    if (found_result) {
      const Service& service = *it;
      transport_manager::ConnectionUID expected_connection_handle =
          kDefaultConnectionHandle;
      EXPECT_EQ(service.connection_id, expected_connection_handle);
#ifdef ENABLE_SECURITY
      EXPECT_EQ(service.is_protected_, protection);
#endif  // ENABLE_SECURITY
    }
  }
  void AddNewSecondaryService(const ServiceType service_type) {
    const bool result = connection_->AddNewService(
        session_id, service_type, false, kSecondaryConnectionHandle);
    EXPECT_EQ(result, true);

    const SessionMap session_map = connection_->session_map();
    EXPECT_FALSE(session_map.empty());
    const ServiceList newServiceList = session_map.begin()->second.service_list;
    EXPECT_FALSE(newServiceList.empty());
    const ServiceList::const_iterator it =
        std::find(newServiceList.begin(), newServiceList.end(), service_type);
    const bool found_result = it != newServiceList.end();
    EXPECT_TRUE(found_result);
    if (found_result) {
      const Service& service = *it;
      transport_manager::ConnectionUID expected_secondary_connection_handle =
          kSecondaryConnectionHandle;
      EXPECT_EQ(service.connection_id, expected_secondary_connection_handle);
    }
  }

  void RemoveService(const ServiceType service_type,
                     const bool expect_remove_service_result,
                     const bool expect_exist_service) {
    const bool result = connection_->RemoveService(session_id, service_type);
    EXPECT_EQ(result, expect_remove_service_result);

    const SessionMap newSessionMap = connection_->session_map();
    EXPECT_FALSE(newSessionMap.empty());
    const ServiceList newServiceList =
        newSessionMap.begin()->second.service_list;
    EXPECT_FALSE(newServiceList.empty());
    const ServiceList::const_iterator it =
        std::find(newServiceList.begin(), newServiceList.end(), service_type);
    const bool found_result = (it != newServiceList.end());
    EXPECT_EQ(expect_exist_service, found_result);
  }

  Connection* connection_;
  MockConnectionHandlerSettings mock_connection_handler_settings;
  testing::StrictMock<transport_manager_test::MockTransportManager>
      transport_manager_mock;
  ConnectionHandlerImpl* connection_handler_;
  uint32_t session_id;
  static const transport_manager::ConnectionUID kDefaultConnectionHandle = 1;
  static const transport_manager::ConnectionUID kSecondaryConnectionHandle = 2;
};

TEST_F(ConnectionTest, Session_TryGetProtocolVersionWithoutSession) {
  uint8_t protocol_version;
  EXPECT_FALSE(connection_->ProtocolVersion(session_id, protocol_version));
}

TEST_F(ConnectionTest, Session_GetDefaultProtocolVersion) {
  StartDefaultSession();
  uint8_t protocol_version;
  EXPECT_TRUE(connection_->ProtocolVersion(session_id, protocol_version));
  EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_2), protocol_version);
}
TEST_F(ConnectionTest, Session_UpdateProtocolVersion) {
  StartDefaultSession();
  uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_3);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_TRUE(connection_->ProtocolVersion(session_id, protocol_version));
  EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_3), protocol_version);
}

TEST_F(ConnectionTest, HeartBeat_NotSupported) {
  // Arrange
  StartDefaultSession();
  uint8_t protocol_version;
  EXPECT_TRUE(connection_->ProtocolVersion(session_id, protocol_version));
  EXPECT_EQ(static_cast<uint8_t>(PROTOCOL_VERSION_2), protocol_version);

  // Assert
  EXPECT_FALSE(connection_->SupportHeartBeat(session_id));
}

TEST_F(ConnectionTest, HeartBeat_Protocol3_Supported) {
  // Arrange
  StartSession();
  // Check execution if protocol version is 3
  const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_3);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_TRUE(connection_->SupportHeartBeat(session_id));
}

TEST_F(ConnectionTest, HeartBeat_Protocol4_PositiveHeartBeat_Supported) {
  // Arrange
  StartSession();
  // Check execution if protocol version is 4
  const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_4);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_TRUE(connection_->SupportHeartBeat(session_id));
}

TEST_F(ConnectionTest, HeartBeat_Protocol4_ZeroHeartBeat_NotSupported) {
  // Correctc of connection (need connection with heartbeat=0)
  delete connection_;
  connection_ = 0;

  const ConnectionHandle connectionHandle = 0;
  const DeviceHandle device_handle = 0u;
  const uint32_t heart_beat = 0u;
  connection_ = new Connection(
      connectionHandle, device_handle, connection_handler_, heart_beat);
  StartSession();
  // Check execution if protocol version is 4
  const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_4);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_FALSE(connection_->SupportHeartBeat(session_id));
}

TEST_F(ConnectionTest, HeartBeat_Protocol5_PositiveHeartBeat_Supported) {
  // Arrange
  StartSession();
  // Check execution if protocol version is 5
  const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_5);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_TRUE(connection_->SupportHeartBeat(session_id));
}

TEST_F(ConnectionTest, HeartBeat_Protocol5_ZeroHeartBeat_NotSupported) {
  // Correctc of connection (need connection with heartbeat=0)
  delete connection_;
  connection_ = 0;

  const ConnectionHandle connectionHandle = 0;
  const DeviceHandle device_handle = 0u;
  const uint32_t heart_beat = 0u;
  connection_ = new Connection(
      connectionHandle, device_handle, connection_handler_, heart_beat);
  StartSession();
  // Check execution if protocol version is 5
  const uint8_t protocol_version = static_cast<uint8_t>(PROTOCOL_VERSION_5);
  connection_->UpdateProtocolVersionSession(session_id, protocol_version);
  EXPECT_FALSE(connection_->SupportHeartBeat(session_id));
}

// Try to add service without session
TEST_F(ConnectionTest, Session_AddNewServiceWithoutSession) {
  EXPECT_EQ(connection_->AddNewService(
                session_id, kAudio, true, kDefaultConnectionHandle),
            EXPECT_RETURN_FALSE);
  EXPECT_EQ(connection_->AddNewService(
                session_id, kAudio, false, kDefaultConnectionHandle),
            EXPECT_RETURN_FALSE);
  EXPECT_EQ(connection_->AddNewService(
                session_id, kMobileNav, true, kDefaultConnectionHandle),
            EXPECT_RETURN_FALSE);
  EXPECT_EQ(connection_->AddNewService(
                session_id, kMobileNav, false, kDefaultConnectionHandle),
            EXPECT_RETURN_FALSE);
}

// Try to remove service without session
TEST_F(ConnectionTest, Session_RemoveServiceWithoutSession) {
  EXPECT_EQ(connection_->RemoveService(session_id, kAudio),
            EXPECT_RETURN_FALSE);
  EXPECT_EQ(connection_->RemoveService(session_id, kMobileNav),
            EXPECT_RETURN_FALSE);
  EXPECT_EQ(
      connection_->RemoveService(session_id, protocol_handler::kMobileNav),
      EXPECT_RETURN_FALSE);
}
// Try to remove RPC
TEST_F(ConnectionTest, Session_RemoveRPCBulk) {
  StartSession();
  EXPECT_EQ(connection_->RemoveService(session_id, kRpc), EXPECT_RETURN_FALSE);
  EXPECT_EQ(connection_->RemoveService(session_id, kBulk), EXPECT_RETURN_FALSE);
}
// Control Service could not be started anyway
TEST_F(ConnectionTest, Session_AddControlService) {
  StartSession();
  AddNewService(
      kControl, PROTECTION_OFF, EXPECT_RETURN_FALSE, EXPECT_SERVICE_NOT_EXISTS);
  AddNewService(
      kControl, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_NOT_EXISTS);
}

// Invalid Services couldnot be started anyway
TEST_F(ConnectionTest, Session_AddInvalidService) {
  StartSession();

  AddNewService(kInvalidServiceType,
                PROTECTION_OFF,
                EXPECT_RETURN_FALSE,
                EXPECT_SERVICE_NOT_EXISTS);
  AddNewService(kInvalidServiceType,
                PROTECTION_ON,
                EXPECT_RETURN_FALSE,
                EXPECT_SERVICE_NOT_EXISTS);
}

// RPC and Bulk Services could be only delay protected
TEST_F(ConnectionTest, Session_AddRPCBulkServices) {
  StartSession();
  AddNewService(
      kRpc, PROTECTION_OFF, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);

  //  Bulk shall not be added and shall be PROTECTION_OFF
  AddNewService(
      kBulk, PROTECTION_OFF, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
#ifdef ENABLE_SECURITY
  AddNewService(kRpc, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#else
  AddNewService(
      kRpc, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
#endif  // ENABLE_SECURITY

  //  Bulk shall not be added and shall be PROTECTION_ON
  AddNewService(
      kBulk, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
}

TEST_F(ConnectionTest, Session_AddAllOtherService_Unprotected) {
  StartSession();
  AddNewService(
      kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
  AddNewService(
      kMobileNav, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
}

TEST_F(ConnectionTest, Session_AddAllOtherService_Protected) {
  StartSession();
  AddNewService(
      kAudio, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
}

TEST_F(ConnectionTest, FindAddedService) {
  StartSession();

  // Arrange
  SessionMap currentSessionMap = connection_->session_map();
  Service* sessionWithService =
      currentSessionMap.find(session_id)->second.FindService(kAudio);
  EXPECT_EQ(NULL, sessionWithService);

  // Act
  AddNewService(
      kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

  currentSessionMap = connection_->session_map();

  // Expect that service is existing
  sessionWithService =
      currentSessionMap.find(session_id)->second.FindService(kAudio);
  EXPECT_TRUE(sessionWithService != NULL);
}

TEST_F(ConnectionTest, Session_RemoveAddedService) {
  StartSession();
  AddNewService(
      kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

  EXPECT_EQ(connection_->RemoveService(session_id, kAudio), EXPECT_RETURN_TRUE);

  // Try delete nonexisting service
  EXPECT_EQ(connection_->RemoveService(session_id, kAudio),
            EXPECT_RETURN_FALSE);
}

TEST_F(ConnectionTest, Session_AddAllOtherService_DelayProtected1) {
  StartSession();

  AddNewService(
      kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

  AddNewService(
      kMobileNav, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

#ifdef ENABLE_SECURITY
  AddNewService(
      kAudio, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#else
  AddNewService(
      kAudio, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);

  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
#endif  // ENABLE_SECURITY
}

//  Use other order
TEST_F(ConnectionTest, Session_AddAllOtherService_DelayProtected2) {
  StartSession();
  AddNewService(
      kAudio, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#ifdef ENABLE_SECURITY
  AddNewService(
      kAudio, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#else
  AddNewService(
      kAudio, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
#endif  // ENABLE_SECURITY

  AddNewService(
      kMobileNav, PROTECTION_OFF, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#ifdef ENABLE_SECURITY
  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);
#else
  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);
#endif  // ENABLE_SECURITY
}

TEST_F(ConnectionTest, RemoveSession) {
  StartSession();
  EXPECT_EQ(session_id, connection_->RemoveSession(session_id));
  EXPECT_EQ(0u, connection_->RemoveSession(session_id));
}

TEST_F(ConnectionTest, AddNewSession_VerifyAddSessionCalled) {
  MockConnectionHandler mock_connection_handler;

  ConnectionHandle connection_handle = 123;
  DeviceHandle device_handle = 0u;
  uint32_t heart_beat = 10000u;
  Connection* connection = new Connection(
      connection_handle, device_handle, &mock_connection_handler, heart_beat);

  transport_manager::ConnectionUID connection_handle_uid = 1;
  uint32_t mock_session_id = 2;
  EXPECT_CALL(mock_connection_handler, AddSession(connection_handle_uid))
      .WillOnce(Return(mock_session_id));

  uint32_t sid = connection->AddNewSession(connection_handle_uid);
  EXPECT_EQ(mock_session_id, sid);

  EXPECT_CALL(mock_connection_handler, RemoveSession(mock_session_id))
      .WillOnce(Return(true));  // invoked by destructor of connection
  delete connection;
}

TEST_F(ConnectionTest, RemoveSession_VerifyRemoveSessionCalled) {
  MockConnectionHandler mock_connection_handler;

  ConnectionHandle connection_handle = 123;
  DeviceHandle device_handle = 0u;
  uint32_t heart_beat = 10000u;
  Connection* connection = new Connection(
      connection_handle, device_handle, &mock_connection_handler, heart_beat);

  transport_manager::ConnectionUID connection_handle_uid = 1;
  uint32_t mock_session_id = 10;
  EXPECT_CALL(mock_connection_handler, AddSession(connection_handle_uid))
      .WillOnce(Return(mock_session_id));
  EXPECT_CALL(mock_connection_handler,
              RemoveSession(static_cast<uint8_t>(mock_session_id)))
      .WillOnce(Return(true));

  uint32_t sid = connection->AddNewSession(connection_handle_uid);

  uint32_t ret = connection->RemoveSession(sid);
  EXPECT_EQ(sid, ret);

  delete connection;
}

TEST_F(ConnectionTest, SecondarySessionTest) {
  StartSession();
  AddNewService(
      kRpc, PROTECTION_OFF, EXPECT_RETURN_FALSE, EXPECT_SERVICE_EXISTS);

  const ConnectionHandle connectionHandle = 0;
  const DeviceHandle device_handle = 0u;
  const uint32_t heart_beat = 0u;
  Connection* secondary_connection = new Connection(
      connectionHandle, device_handle, connection_handler_, heart_beat);

  secondary_connection->SetPrimaryConnectionHandle(kDefaultConnectionHandle);
  connection_handler::ConnectionHandle expected_primary_connection_handle =
      kDefaultConnectionHandle;
  EXPECT_EQ(secondary_connection->primary_connection_handle(),
            expected_primary_connection_handle);

  AddNewSecondaryService(kAudio);
  AddNewSecondaryService(kMobileNav);

  delete secondary_connection;
}

TEST_F(ConnectionTest, RemoveSecondaryServices_SUCCESS) {
  StartSession();

  ServiceType services[2] = {kMobileNav, kAudio};
  AddNewSecondaryService(services[0]);
  AddNewSecondaryService(services[1]);
  size_t services_count = sizeof(services) / sizeof(services[0]);

  std::list<ServiceType> removed_services;
  uint8_t ret_session_id = connection_->RemoveSecondaryServices(
      kSecondaryConnectionHandle, removed_services);

  // check return value
  EXPECT_EQ(session_id, ret_session_id);
  // check returned list
  EXPECT_EQ(services_count, removed_services.size());
  std::list<protocol_handler::ServiceType>::iterator it;
  it = std::find(removed_services.begin(), removed_services.end(), services[0]);
  EXPECT_TRUE(it != removed_services.end());
  it = std::find(removed_services.begin(), removed_services.end(), services[1]);
  EXPECT_TRUE(it != removed_services.end());
}

TEST_F(ConnectionTest, RemoveSecondaryServices_NoService) {
  StartSession();
  /* do not call AddNewSecondaryService() */

  std::list<ServiceType> removed_services;
  uint8_t ret_session_id = connection_->RemoveSecondaryServices(
      kSecondaryConnectionHandle, removed_services);

  // check return value
  EXPECT_EQ(0, ret_session_id);
  // check returned list
  EXPECT_EQ(0u, removed_services.size());
}

TEST_F(ConnectionTest, RemoveSecondaryServices_InvalidConnectionHandle) {
  StartSession();

  ServiceType services[2] = {kMobileNav, kAudio};
  AddNewSecondaryService(services[0]);
  AddNewSecondaryService(services[1]);

  transport_manager::ConnectionUID invalid_connection_handle = 123;
  ASSERT_TRUE(kSecondaryConnectionHandle != invalid_connection_handle);

  std::list<ServiceType> removed_services;
  uint8_t ret_session_id = connection_->RemoveSecondaryServices(
      invalid_connection_handle, removed_services);

  // check return value
  EXPECT_EQ(0, ret_session_id);
  // check returned list
  EXPECT_EQ(0u, removed_services.size());
}

#ifdef ENABLE_SECURITY

TEST_F(ConnectionTest, SetSSLContextWithoutSession) {
  // random value. Session was not started
  uint8_t session_id = 10;
  security_manager_test::MockSSLContext mock_ssl_context;
  int setResult = connection_->SetSSLContext(session_id, &mock_ssl_context);
  EXPECT_EQ(security_manager::SecurityManager::ERROR_INTERNAL, setResult);
}

TEST_F(ConnectionTest, GetSSLContextWithoutSession) {
  // random value. Session was not started
  uint8_t session_id = 10;

  EXPECT_EQ(NULL, connection_->GetSSLContext(session_id, kMobileNav));
}

TEST_F(ConnectionTest, SetGetSSLContext) {
  StartSession();

  EXPECT_EQ(NULL, connection_->GetSSLContext(session_id, kMobileNav));
  AddNewService(
      kMobileNav, PROTECTION_ON, EXPECT_RETURN_TRUE, EXPECT_SERVICE_EXISTS);

  EXPECT_EQ(NULL, connection_->GetSSLContext(session_id, kMobileNav));

  security_manager_test::MockSSLContext mock_ssl_context;
  // Set SSLContext
  int setResult = connection_->SetSSLContext(session_id, &mock_ssl_context);
  EXPECT_EQ(security_manager::SecurityManager::ERROR_SUCCESS, setResult);

  security_manager::SSLContext* result =
      connection_->GetSSLContext(session_id, kMobileNav);
  EXPECT_EQ(result, &mock_ssl_context);
}

TEST_F(ConnectionTest, SetProtectionFlagForRPC) {
  StartSession();
  // Arrange
  SessionMap currentSessionMap = connection_->session_map();
  Service* service_rpc =
      currentSessionMap.find(session_id)->second.FindService(kRpc);

  EXPECT_FALSE(service_rpc->is_protected_);

  Service* service_bulk =
      currentSessionMap.find(session_id)->second.FindService(kBulk);
  EXPECT_FALSE(service_bulk->is_protected_);

  // Expect that service protection is enabled
  connection_->SetProtectionFlag(session_id, kRpc);

  currentSessionMap = connection_->session_map();
  service_bulk = currentSessionMap.find(session_id)->second.FindService(kBulk);
  EXPECT_TRUE(service_bulk->is_protected_);

  service_rpc = currentSessionMap.find(session_id)->second.FindService(kRpc);
  EXPECT_TRUE(service_rpc->is_protected_);
}

TEST_F(ConnectionTest, SetProtectionFlagForBulk) {
  StartSession();
  // Arrange
  SessionMap currentSessionMap = connection_->session_map();
  Service* service_rpc =
      currentSessionMap.find(session_id)->second.FindService(kRpc);

  EXPECT_FALSE(service_rpc->is_protected_);

  Service* service_bulk =
      currentSessionMap.find(session_id)->second.FindService(kBulk);
  EXPECT_FALSE(service_bulk->is_protected_);

  // Expect that service protection is enabled
  connection_->SetProtectionFlag(session_id, kBulk);

  currentSessionMap = connection_->session_map();
  service_bulk = currentSessionMap.find(session_id)->second.FindService(kBulk);
  EXPECT_TRUE(service_bulk->is_protected_);

  service_rpc = currentSessionMap.find(session_id)->second.FindService(kRpc);
  EXPECT_TRUE(service_rpc->is_protected_);
}

#endif  // ENABLE_SECURITY

}  // namespace connection_handler_test
}  // namespace components
}  // namespace test