summaryrefslogtreecommitdiff
path: root/test/components/protocol_handler/include/protocol_handler/protocol_handler_tm_test.h
blob: 08f661b80650c167790114811627e30a9e4f523e (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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * Copyright (c) 2014, 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.
 */
#ifndef TEST_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_TM_TEST_H_
#define TEST_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_TM_TEST_H_
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <string>

#include "utils/shared_ptr.h"

#include "protocol_handler/protocol_handler_impl.h"
#include "protocol/common.h"

#include "protocol_handler/protocol_handler_mock.h"
#include "protocol_handler/protocol_observer_mock.h"
#include "protocol_handler/session_observer_mock.h"
#include "protocol_handler/control_message_matcher.h"
#include "security_manager/security_manager_mock.h"
#include "security_manager/ssl_context_mock.h"
#include "transport_manager/transport_manager_mock.h"
#include "protocol_handler/control_message_matcher.h"

namespace test {
namespace components {
namespace protocol_handler_test {

// id passed as NULL for new session establishing
#define NEW_SESSION_ID        0u
#define SESSION_START_REJECT  0u

using namespace ::protocol_handler;
using namespace ::transport_manager;  // For TM states
//using namespace ::security_manager;
using ::transport_manager::TransportManagerListener;
using protocol_handler_test::ControlMessage;
using ::testing::Return;
using ::testing::ReturnNull;
using ::testing::AnyOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::_;
using ::testing::Invoke;

class ProtocolHandlerImplTest : public ::testing::Test {
 protected:
  void IntitProtocolHandlerImpl(const size_t period_msec, const size_t max_messages) {
    protocol_handler_impl.reset(new ProtocolHandlerImpl(&transport_manager_mock,
                                                        period_msec, max_messages));
    protocol_handler_impl->set_session_observer(&session_observer_mock);
    tm_listener = protocol_handler_impl.get();
  }
  void SetUp() OVERRIDE {
    IntitProtocolHandlerImpl(0u, 0u);
    connection_id = 0xAu;
    session_id = 0xFFu;
    connection_key = 0xFF00AAu;
    message_id = 0xABCDEFu;
    some_date.resize(256, 0xAB);

    // expect ConnectionHandler support methods call (conversion, check heartbeat)
    EXPECT_CALL(session_observer_mock,
                KeyFromPair(connection_id, _)).
        //return some connection_key
        WillRepeatedly(Return(connection_key));
    EXPECT_CALL(session_observer_mock,
                IsHeartBeatSupported(connection_id, _)).
        //return false to avoid call KeepConnectionAlive
        WillRepeatedly(Return(false));
  }

  void TearDown() OVERRIDE {
    // Wait call methods in thread
    usleep(100000);
  }

  // Emulate connection establish
  void AddConnection() {
    tm_listener->OnConnectionEstablished(
          DeviceInfo(DeviceHandle(1u),
                     std::string("mac"),
                     std::string("name"),
                     std::string("BTMAC")),
          connection_id);
  }
  void AddSession() {
    AddConnection();
    const ServiceType start_service = kRpc;
  #ifdef ENABLE_SECURITY
    // For enabled protection callback shall use protection ON
    const bool callback_protection_flag = PROTECTION_ON;
  #else
    // For disabled protection callback shall ignore protection income flad and use protection OFF
    const bool callback_protection_flag  = PROTECTION_OFF;
  #endif  // ENABLE_SECURITY
    // expect ConnectionHandler check
    EXPECT_CALL(session_observer_mock,
                OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service,
                                         callback_protection_flag, _)).
        //return sessions start success
        WillOnce(Return(session_id));

    // expect send Ack with PROTECTION_OFF (on no Security Manager)
    EXPECT_CALL(transport_manager_mock,
                SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
        WillOnce(Return(E_SUCCESS));

    SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  }

#ifdef ENABLE_SECURITY
  // Emulate security manager initilization establish
  void AddSecurityManager() {
    protocol_handler_impl->set_security_manager(&security_manager_mock);
  }
#endif  // ENABLE_SECURITY
  void SendTMMessage(uint8_t connection_id,
                     uint8_t version, bool protection, uint8_t frameType,
                     uint8_t serviceType, uint8_t frameData,
                     uint8_t sessionId, uint32_t dataSize,
                     uint32_t messageID, const uint8_t *data = 0) {
    // Create packet
    const ProtocolPacket packet(
          connection_id, version, protection, frameType,
          serviceType, frameData, sessionId, dataSize,
          messageID, data);
    // Emulate resive packet from transoprt manager
    tm_listener->OnTMMessageReceived(packet.serializePacket());
  }
  void SendControlMessage(bool protection, uint8_t service_type,
                          uint8_t sessionId, uint32_t frame_data,
                          uint32_t dataSize = 0u, const uint8_t *data = NULL) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, protection, FRAME_TYPE_CONTROL,
                  service_type, frame_data, sessionId, dataSize, message_id, data);
  }

  ::utils::SharedPtr<ProtocolHandlerImpl> protocol_handler_impl;
  TransportManagerListener* tm_listener;
  // Uniq connection
  ::transport_manager::ConnectionUID connection_id;
  // id of established session
  uint8_t session_id;
  // uniq id as connection_id and session_id in one
  uint32_t connection_key;
  uint32_t message_id;
  std::vector<uint8_t> some_date;
  // Strict mocks (same as all methods EXPECT_CALL().Times(0))
  testing::StrictMock<transport_manager_test::TransportManagerMock> transport_manager_mock;
  testing::StrictMock<protocol_handler_test::SessionObserverMock>   session_observer_mock;
#ifdef ENABLE_SECURITY
  testing::NiceMock<security_manager_test::SecurityManagerMock>     security_manager_mock;
  testing::NiceMock<security_manager_test::SSLContextMock>          ssl_context_mock;
#endif  // ENABLE_SECURITY
};

#ifdef ENABLE_SECURITY
class OnHandshakeDoneFunctor {
public:
  OnHandshakeDoneFunctor(const uint32_t connection_key, const bool result)
    : connection_key(connection_key), result(result) {}
  void operator()(security_manager::SecurityManagerListener * listener) const {
    listener->OnHandshakeDone(connection_key, result);
  }
private:
  const uint32_t connection_key;
  const bool result;
};
#endif  // ENABLE_SECURITY

/*
 * ProtocolHandler shall skip empty message
 */
TEST_F(ProtocolHandlerImplTest, RecieveEmptyRawMessage) {
  tm_listener->OnTMMessageReceived(RawMessagePtr());
}
/*
 * ProtocolHandler shall disconnect on no connection
 */
TEST_F(ProtocolHandlerImplTest, RecieveOnUnknownConenction) {
  // expect force dicsonnect on no connection for received data
  EXPECT_CALL(transport_manager_mock,
              DisconnectForce(connection_id)).
      WillOnce(Return(E_SUCCESS));

  SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_CONTROL,
                kRpc, FRAME_DATA_START_SERVICE, NEW_SESSION_ID, 0, message_id);
}
/*
 * ProtocolHandler shall send NAck on session_observer rejection
 * Check protection flag OFF for all services from kControl to kBulk
 */
TEST_F(ProtocolHandlerImplTest, StartSession_Unprotected_SessionObserverReject) {
  const int call_times = 5;
  AddConnection();
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(
                connection_id, NEW_SESSION_ID, AnyOf(kControl, kRpc, kAudio,
                                                     kMobileNav, kBulk), PROTECTION_OFF, _)).
      Times(call_times).
      //return sessions start rejection
      WillRepeatedly(Return(SESSION_START_REJECT));

  // expect send NAck
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_NACK, PROTECTION_OFF))).
      Times(call_times).
      WillRepeatedly(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_OFF, kControl,  NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_OFF, kRpc,      NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_OFF, kAudio,    NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_OFF, kMobileNav,NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_OFF, kBulk,     NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send NAck on session_observer rejection
 * Emulate getting PROTECTION_ON and check protection flag OFF in NAck
 * For ENABLE_SECURITY=OFF session_observer shall be called with protection flag OFF
 */
TEST_F(ProtocolHandlerImplTest, StartSession_Protected_SessionObserverReject) {
  const int call_times = 5;
  AddConnection();
#ifdef ENABLE_SECURITY
  // For enabled protection callback shall use protection ON
  const bool callback_protection_flag  = PROTECTION_ON;
#else
  // For disabled protection callback shall ignore protection income flad and use protection OFF
  const bool callback_protection_flag  = PROTECTION_OFF;
#endif  // ENABLE_SECURITY
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(
                connection_id, NEW_SESSION_ID, AnyOf(kControl, kRpc, kAudio,
                                                     kMobileNav, kBulk),  callback_protection_flag, _)).
      Times(call_times).
      //return sessions start rejection
      WillRepeatedly(Return(SESSION_START_REJECT));

  // expect send NAck with encryption OFF
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_NACK, PROTECTION_OFF))).
      Times(call_times).
      WillRepeatedly(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, kControl,  NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_ON, kRpc,      NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_ON, kAudio,    NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_ON, kMobileNav,NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
  SendControlMessage(PROTECTION_ON, kBulk,     NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack on session_observer accept
 * Check protection flag OFF
 */
TEST_F(ProtocolHandlerImplTest, StartSession_Unprotected_SessionObserverAccept) {
  AddConnection();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_OFF, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // expect send Ack
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_OFF, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack on session_observer accept
 * Emulate getting PROTECTION_ON and check protection flag OFF in Ack
 * For ENABLE_SECURITY=OFF session_observer shall be called with protection flag OFF
 */
TEST_F(ProtocolHandlerImplTest, StartSession_Protected_SessionObserverAccept) {
  AddSession();
}
// TODO(EZamakhov): add test for get_hash_id/set_hash_id from protocol_handler_impl.cc
/*
 * ProtocolHandler shall send NAck on session_observer rejection
 */
TEST_F(ProtocolHandlerImplTest, EndSession_SessionObserverReject) {
  AddSession();
  const ServiceType service = kRpc;

  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionEndedCallback(connection_id, session_id, _, service)).
      // reject session start
      WillOnce(Return(SESSION_START_REJECT));

  // expect send NAck
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_END_SERVICE_NACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_OFF, service, session_id, FRAME_DATA_END_SERVICE);
}
/*
 * ProtocolHandler shall send NAck on wrong hash code
 */
TEST_F(ProtocolHandlerImplTest, EndSession_Success) {
  AddSession();
  const ServiceType service = kRpc;

  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionEndedCallback(connection_id, session_id, _, service)).
      // return sessions start success
      WillOnce(Return(connection_key));

  // expect send Ack
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_END_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_OFF, service, session_id, FRAME_DATA_END_SERVICE);
}

#ifdef ENABLE_SECURITY
/*
 * ProtocolHandler shall not call Security logics with Protocol version 1
 * Check session_observer with PROTECTION_OFF and Ack with PROTECTION_OFF
 */
TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtocoloV1) {
  AddConnection();
  // Add security manager
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_OFF, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // expect send Ack with PROTECTION_OFF (on no Security Manager)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendTMMessage(connection_id, PROTOCOL_VERSION_1, PROTECTION_ON, FRAME_TYPE_CONTROL,
                start_service, FRAME_DATA_START_SERVICE, NEW_SESSION_ID, 0, message_id);
}
/*
 * ProtocolHandler shall not call Security logics on start session with PROTECTION_OFF
 */
TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionUnprotected) {
  AddConnection();
  // Add security manager
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_OFF, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // expect send Ack with PROTECTION_OFF (on no Security Manager)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_OFF, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_OFF on fail SLL creation
 */
TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtected_Fail) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // expect start protection for unprotected session
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return fail protection
      WillOnce(ReturnNull());

  // expect send Ack with PROTECTION_OFF (on fail SLL creation)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_ON on already established and initialized SLLContext
 */
TEST_F(ProtocolHandlerImplTest,SecurityEnable_StartSessionProtected_SSLInitialized) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // call new SSLContext creation
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return new SSLContext
      WillOnce(Return(&ssl_context_mock));

  // initilization check
  EXPECT_CALL(ssl_context_mock,
              IsInitCompleted()).
      //emulate SSL is initilized
      WillOnce(Return(true));

  // Expect service protection enable
  EXPECT_CALL(session_observer_mock,
              SetProtectionFlag(connection_key, start_service));

  // expect send Ack with PROTECTION_ON (on SSL is initilized)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_ON))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_OFF on session handshhake fail
 */
TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtected_HandshakeFail) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // call new SSLContext creation
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return new SSLContext
      WillOnce(Return(&ssl_context_mock));

  // initilization check
  EXPECT_CALL(ssl_context_mock,
              IsInitCompleted()).
      //emulate SSL is not initilized
      WillOnce(Return(false));

  // Pending handshake check
  EXPECT_CALL(ssl_context_mock,
              IsHandshakePending()).
      //emulate is pending
      WillOnce(Return(true));

  // expect add listener for handshake result
  EXPECT_CALL(security_manager_mock,
              AddListener(_))
      // emulate handshake fail
      .WillOnce(Invoke(OnHandshakeDoneFunctor(connection_key, PROTECTION_OFF)));

  // Listener check SSLContext
  EXPECT_CALL(session_observer_mock,
              GetSSLContext(connection_key, start_service)).
      // emulate protection for service is not enabled
      WillOnce(ReturnNull());

  // expect send Ack with PROTECTION_OFF (on fail handshake)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_OFF))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_ON on session handshhake success
 */
TEST_F(ProtocolHandlerImplTest, SecurityEnable_StartSessionProtected_HandshakeSuccess) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // call new SSLContext creation
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return new SSLContext
      WillOnce(Return(&ssl_context_mock));

  // initilization check
  EXPECT_CALL(ssl_context_mock,
              IsInitCompleted()).
      //emulate SSL is not initilized
      WillOnce(Return(false));

  // Pending handshake check
  EXPECT_CALL(ssl_context_mock,
              IsHandshakePending()).
      //emulate is pending
      WillOnce(Return(true));

  // expect add listener for handshake result
  EXPECT_CALL(security_manager_mock,
              AddListener(_))
      // emulate handshake fail
      .WillOnce(Invoke(OnHandshakeDoneFunctor(connection_key, PROTECTION_ON)));

  // Listener check SSLContext
  EXPECT_CALL(session_observer_mock,
              GetSSLContext(connection_key, start_service)).
      // emulate protection for service is not enabled
      WillOnce(ReturnNull());

  // Expect service protection enable
  EXPECT_CALL(session_observer_mock,
              SetProtectionFlag(connection_key, start_service));

  // expect send Ack with PROTECTION_OFF (on fail handshake)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_ON))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_ON on session handshhake success
 */
TEST_F(ProtocolHandlerImplTest,
       SecurityEnable_StartSessionProtected_HandshakeSuccess_ServiceProtectedBefore) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // call new SSLContext creation
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return new SSLContext
      WillOnce(Return(&ssl_context_mock));

  // initilization check
  EXPECT_CALL(ssl_context_mock,
              IsInitCompleted()).
      //emulate SSL is not initilized
      WillOnce(Return(false));

  // Pending handshake check
  EXPECT_CALL(ssl_context_mock,
              IsHandshakePending()).
      //emulate is pending
      WillOnce(Return(true));

  // expect add listener for handshake result
  EXPECT_CALL(security_manager_mock,
              AddListener(_))
      // emulate handshake fail
      .WillOnce(Invoke(OnHandshakeDoneFunctor(connection_key, PROTECTION_ON)));

  // Listener check SSLContext
  EXPECT_CALL(session_observer_mock,
              GetSSLContext(connection_key, start_service)).
      // emulate protection for service is not enabled
      WillOnce(ReturnNull());

  // Expect service protection enable
  EXPECT_CALL(session_observer_mock,
              SetProtectionFlag(connection_key, start_service));

  // expect send Ack with PROTECTION_OFF (on fail handshake)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_ON))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
/*
 * ProtocolHandler shall send Ack with PROTECTION_ON on session handshhake success
 */
TEST_F(ProtocolHandlerImplTest,
       SecurityEnable_StartSessionProtected_HandshakeSuccess_SSLIsNotPending) {
  AddConnection();
  AddSecurityManager();
  const ServiceType start_service = kRpc;
  // expect ConnectionHandler check
  EXPECT_CALL(session_observer_mock,
              OnSessionStartedCallback(connection_id, NEW_SESSION_ID, start_service, PROTECTION_ON, _)).
      //return sessions start success
      WillOnce(Return(session_id));

  // call new SSLContext creation
  EXPECT_CALL(security_manager_mock,
              CreateSSLContext(connection_key)).
      //return new SSLContext
      WillOnce(Return(&ssl_context_mock));

  // initilization check
  EXPECT_CALL(ssl_context_mock,
              IsInitCompleted()).
      //emulate SSL is not initilized
      WillOnce(Return(false));

  // Pending handshake check
  EXPECT_CALL(ssl_context_mock,
              IsHandshakePending()).
      //emulate is pending
      WillOnce(Return(false));

  // Wait restart handshake operation
  EXPECT_CALL(security_manager_mock,
              StartHandshake(connection_key));

  // expect add listener for handshake result
  EXPECT_CALL(security_manager_mock,
              AddListener(_))
      // emulate handshake fail
      .WillOnce(Invoke(OnHandshakeDoneFunctor(connection_key, PROTECTION_ON)));

  // Listener check SSLContext
  EXPECT_CALL(session_observer_mock,
              GetSSLContext(connection_key, start_service)).
      // emulate protection for service is not enabled
      WillOnce(ReturnNull());

  // Expect service protection enable
  EXPECT_CALL(session_observer_mock,
              SetProtectionFlag(connection_key, start_service));

  // expect send Ack with PROTECTION_OFF (on fail handshake)
  EXPECT_CALL(transport_manager_mock,
              SendMessageToDevice(ControlMessage(FRAME_DATA_START_SERVICE_ACK, PROTECTION_ON))).
      WillOnce(Return(E_SUCCESS));

  SendControlMessage(PROTECTION_ON, start_service, NEW_SESSION_ID, FRAME_DATA_START_SERVICE);
}
TEST_F(ProtocolHandlerImplTest,
       FloodVerification) {
  const size_t period_msec = 1000;
  const size_t max_messages = 1000;
  IntitProtocolHandlerImpl(period_msec, max_messages);
  AddConnection();
  AddSession();

  // expect flood notification to CH
  EXPECT_CALL(session_observer_mock,
              OnApplicationFloodCallBack(connection_key)).
      Times(1);

  for (size_t i = 0; i < max_messages + 1; ++i) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_SINGLE,
                  kControl, FRAME_DATA_SINGLE, session_id,
                  some_date.size(), message_id, &some_date[0]);
  }
}
TEST_F(ProtocolHandlerImplTest,
       FloodVerification_ThresholdValue) {
  const size_t period_msec = 1000;
  const size_t max_messages = 1000;
  IntitProtocolHandlerImpl(period_msec, max_messages);
  AddConnection();
  AddSession();

  // expect NO flood notification to CH
  for (size_t i = 0; i < max_messages - 1; ++i) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_SINGLE,
                  kControl, FRAME_DATA_SINGLE, session_id,
                  some_date.size(), message_id, &some_date[0]);
  }
}
TEST_F(ProtocolHandlerImplTest,
       FloodVerification_VideoFrameSkip) {
  const size_t period_msec = 1000;
  const size_t max_messages = 1000;
  IntitProtocolHandlerImpl(period_msec, max_messages);
  AddConnection();
  AddSession();

  // expect NO flood notification to CH on video data streaming
  for (size_t i = 0; i < max_messages + 1; ++i) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_SINGLE,
                  kMobileNav, FRAME_DATA_SINGLE, session_id,
                  some_date.size(), message_id, &some_date[0]);
  }
}
TEST_F(ProtocolHandlerImplTest,
       FloodVerification_AudioFrameSkip) {
  const size_t period_msec = 1000;
  const size_t max_messages = 1000;
  IntitProtocolHandlerImpl(period_msec, max_messages);
  AddConnection();
  AddSession();

  // expect NO flood notification to CH on video data streaming
  for (size_t i = 0; i < max_messages + 1; ++i) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_SINGLE,
                  kAudio, FRAME_DATA_SINGLE, session_id,
                  some_date.size(), message_id, &some_date[0]);
  }
}
TEST_F(ProtocolHandlerImplTest,
       FloodVerificationDisable) {
  const size_t period_msec = 0;
  const size_t max_messages = 0;
  IntitProtocolHandlerImpl(period_msec, max_messages);
  AddConnection();
  AddSession();

  // expect NO flood notification to session observer
  for (size_t i = 0; i < max_messages + 1; ++i) {
    SendTMMessage(connection_id, PROTOCOL_VERSION_3, PROTECTION_OFF, FRAME_TYPE_SINGLE,
                  kControl, FRAME_DATA_SINGLE, session_id,
                  some_date.size(), message_id, &some_date[0]);
  }
}
#endif  // ENABLE_SECURITY
}  // namespace test
}  // namespace components
}  // namespace protocol_handler_test
#endif  //TEST_COMPONENTS_PROTOCOL_HANDLER_INCLUDE_PROTOCOL_HANDLER_PROTOCOL_HANDLER_TM_TEST_H_