summaryrefslogtreecommitdiffstats
blob: 44df96154457cf2c07d5b2d4b37e3e8f3067d686 (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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <deque>
#include <memory>

#include <boost/bind.hpp>
#include <boost/optional.hpp>

#include <Swiften/Base/Debug.h>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Client/ClientSession.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Elements/AuthChallenge.h>
#include <Swiften/Elements/AuthFailure.h>
#include <Swiften/Elements/AuthRequest.h>
#include <Swiften/Elements/AuthSuccess.h>
#include <Swiften/Elements/EnableStreamManagement.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/ResourceBind.h>
#include <Swiften/Elements/StanzaAck.h>
#include <Swiften/Elements/StartTLSFailure.h>
#include <Swiften/Elements/StartTLSRequest.h>
#include <Swiften/Elements/StreamError.h>
#include <Swiften/Elements/StreamFeatures.h>
#include <Swiften/Elements/StreamManagementEnabled.h>
#include <Swiften/Elements/StreamManagementFailed.h>
#include <Swiften/Elements/TLSProceed.h>
#include <Swiften/IDN/IDNConverter.h>
#include <Swiften/IDN/PlatformIDNConverter.h>
#include <Swiften/Network/DummyTimerFactory.h>
#include <Swiften/Session/SessionStream.h>
#include <Swiften/TLS/BlindCertificateTrustChecker.h>
#include <Swiften/TLS/SimpleCertificate.h>

using namespace Swift;

class ClientSessionTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(ClientSessionTest);
        CPPUNIT_TEST(testStart_Error);
        CPPUNIT_TEST(testStart_StreamError);
        CPPUNIT_TEST(testStartTLS);
        CPPUNIT_TEST(testStartTLS_ServerError);
        CPPUNIT_TEST(testStartTLS_ConnectError);
        CPPUNIT_TEST(testStartTLS_InvalidIdentity);
        CPPUNIT_TEST(testStart_StreamFeaturesWithoutResourceBindingFails);
        CPPUNIT_TEST(testAuthenticate);
        CPPUNIT_TEST(testAuthenticate_Unauthorized);
        CPPUNIT_TEST(testAuthenticate_NoValidAuthMechanisms);
        CPPUNIT_TEST(testAuthenticate_PLAINOverNonTLS);
        CPPUNIT_TEST(testAuthenticate_RequireTLS);
        CPPUNIT_TEST(testAuthenticate_EXTERNAL);
        CPPUNIT_TEST(testStreamManagement);
        CPPUNIT_TEST(testStreamManagement_Failed);
        CPPUNIT_TEST(testUnexpectedChallenge);
        CPPUNIT_TEST(testFinishAcksStanzas);

        CPPUNIT_TEST(testServerInitiatedSessionClose);
        CPPUNIT_TEST(testClientInitiatedSessionClose);
        CPPUNIT_TEST(testTimeoutOnShutdown);

        /*
        CPPUNIT_TEST(testResourceBind);
        CPPUNIT_TEST(testResourceBind_ChangeResource);
        CPPUNIT_TEST(testResourceBind_EmptyResource);
        CPPUNIT_TEST(testResourceBind_Error);
        CPPUNIT_TEST(testSessionStart);
        CPPUNIT_TEST(testSessionStart_Error);
        CPPUNIT_TEST(testSessionStart_AfterResourceBind);
        CPPUNIT_TEST(testWhitespacePing);
        CPPUNIT_TEST(testReceiveElementAfterSessionStarted);
        CPPUNIT_TEST(testSendElement);
        */
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            crypto = std::shared_ptr<CryptoProvider>(PlatformCryptoProvider::create());
            idnConverter = std::shared_ptr<IDNConverter>(PlatformIDNConverter::create());
            timerFactory = std::make_shared<DummyTimerFactory>();
            server = std::make_shared<MockSessionStream>();
            sessionFinishedReceived = false;
            needCredentials = false;
            blindCertificateTrustChecker = new BlindCertificateTrustChecker();
        }

        void tearDown() {
            delete blindCertificateTrustChecker;
        }

        void testStart_Error() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->breakConnection();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testStart_StreamError() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->sendStreamStart();
            server->sendStreamError();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testStartTLS() {
            std::shared_ptr<ClientSession> session(createSession());
            session->setCertificateTrustChecker(blindCertificateTrustChecker);
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithStartTLS();
            server->receiveStartTLS();
            CPPUNIT_ASSERT(!server->tlsEncrypted);
            server->sendTLSProceed();
            CPPUNIT_ASSERT(server->tlsEncrypted);
            server->onTLSEncrypted();
            server->receiveStreamStart();
            server->sendStreamStart();

            session->finish();
        }

        void testStartTLS_ServerError() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithStartTLS();
            server->receiveStartTLS();
            server->sendTLSFailure();

            CPPUNIT_ASSERT(!server->tlsEncrypted);
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testStartTLS_ConnectError() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithStartTLS();
            server->receiveStartTLS();
            server->sendTLSProceed();
            server->breakTLS();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testStartTLS_InvalidIdentity() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithStartTLS();
            server->receiveStartTLS();
            CPPUNIT_ASSERT(!server->tlsEncrypted);
            server->sendTLSProceed();
            CPPUNIT_ASSERT(server->tlsEncrypted);
            server->onTLSEncrypted();
            server->close();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());

            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
            CPPUNIT_ASSERT(std::dynamic_pointer_cast<CertificateVerificationError>(sessionFinishedError));
            CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidServerIdentity, std::dynamic_pointer_cast<CertificateVerificationError>(sessionFinishedError)->getType());
        }

        void testStart_StreamFeaturesWithoutResourceBindingFails() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendEmptyStreamFeatures();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testAuthenticate() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();
            CPPUNIT_ASSERT(needCredentials);
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::WaitingForCredentials, session->getState());
            session->sendCredentials(createSafeByteArray("mypass"));
            server->receiveAuthRequest("PLAIN");
            server->sendAuthSuccess();
            server->receiveStreamStart();

            session->finish();
        }

        void testAuthenticate_Unauthorized() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();
            CPPUNIT_ASSERT(needCredentials);
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::WaitingForCredentials, session->getState());
            session->sendCredentials(createSafeByteArray("mypass"));
            server->receiveAuthRequest("PLAIN");
            server->sendAuthFailure();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testAuthenticate_PLAINOverNonTLS() {
            std::shared_ptr<ClientSession> session(createSession());
            session->setAllowPLAINOverNonTLS(false);
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testAuthenticate_RequireTLS() {
            std::shared_ptr<ClientSession> session(createSession());
            session->setUseTLS(ClientSession::RequireTLS);
            session->setAllowPLAINOverNonTLS(true);
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithMultipleAuthentication();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testAuthenticate_NoValidAuthMechanisms() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithUnknownAuthentication();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            server->close();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testAuthenticate_EXTERNAL() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithEXTERNALAuthentication();
            server->receiveAuthRequest("EXTERNAL");
            server->sendAuthSuccess();
            server->receiveStreamStart();

            session->finish();
        }

        void testUnexpectedChallenge() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithEXTERNALAuthentication();
            server->receiveAuthRequest("EXTERNAL");
            server->sendChallenge();
            server->sendChallenge();

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
            CPPUNIT_ASSERT(sessionFinishedError);
        }

        void testStreamManagement() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();
            session->sendCredentials(createSafeByteArray("mypass"));
            server->receiveAuthRequest("PLAIN");
            server->sendAuthSuccess();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithBindAndStreamManagement();
            server->receiveBind();
            server->sendBindResult();
            server->receiveStreamManagementEnable();
            server->sendStreamManagementEnabled();

            CPPUNIT_ASSERT(session->getStreamManagementEnabled());
            // TODO: Test if the requesters & responders do their work
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Initialized, session->getState());

            session->finish();
        }

        void testStreamManagement_Failed() {
            std::shared_ptr<ClientSession> session(createSession());
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();
            session->sendCredentials(createSafeByteArray("mypass"));
            server->receiveAuthRequest("PLAIN");
            server->sendAuthSuccess();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithBindAndStreamManagement();
            server->receiveBind();
            server->sendBindResult();
            server->receiveStreamManagementEnable();
            server->sendStreamManagementFailed();

            CPPUNIT_ASSERT(!session->getStreamManagementEnabled());
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Initialized, session->getState());

            session->finish();
        }

        void testFinishAcksStanzas() {
            std::shared_ptr<ClientSession> session(createSession());
            initializeSession(session);
            server->sendMessage();
            server->sendMessage();
            server->sendMessage();

            session->finish();

            server->receiveAck(3);
        }

        void testServerInitiatedSessionClose() {
            std::shared_ptr<ClientSession> session(createSession());
            initializeSession(session);

            server->onStreamEndReceived();
            server->close();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);
        }

        void testClientInitiatedSessionClose() {
            std::shared_ptr<ClientSession> session(createSession());
            initializeSession(session);

            session->finish();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);

            server->onStreamEndReceived();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
        }

        void testTimeoutOnShutdown() {
            std::shared_ptr<ClientSession> session(createSession());
            initializeSession(session);

            session->finish();
            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finishing, session->getState());
            CPPUNIT_ASSERT_EQUAL(true, server->receivedEvents.back().footer);
            timerFactory->setTime(60000);

            CPPUNIT_ASSERT_EQUAL(ClientSession::State::Finished, session->getState());
            CPPUNIT_ASSERT(sessionFinishedReceived);
        }

    private:
        std::shared_ptr<ClientSession> createSession() {
            std::shared_ptr<ClientSession> session = ClientSession::create(JID("me@foo.com"), server, idnConverter.get(), crypto.get(), timerFactory.get());
            session->onFinished.connect(boost::bind(&ClientSessionTest::handleSessionFinished, this, _1));
            session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::handleSessionNeedCredentials, this));
            session->setAllowPLAINOverNonTLS(true);
            return session;
        }

        void initializeSession(std::shared_ptr<ClientSession> session) {
            session->start();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithPLAINAuthentication();
            session->sendCredentials(createSafeByteArray("mypass"));
            server->receiveAuthRequest("PLAIN");
            server->sendAuthSuccess();
            server->receiveStreamStart();
            server->sendStreamStart();
            server->sendStreamFeaturesWithBindAndStreamManagement();
            server->receiveBind();
            server->sendBindResult();
            server->receiveStreamManagementEnable();
            server->sendStreamManagementEnabled();
        }

        void handleSessionFinished(std::shared_ptr<Error> error) {
            sessionFinishedReceived = true;
            sessionFinishedError = error;
        }

        void handleSessionNeedCredentials() {
            needCredentials = true;
        }

        class MockSessionStream : public SessionStream {
            public:
                struct Event {
                    Event(std::shared_ptr<ToplevelElement> element) : element(element), footer(false) {}
                    Event(const ProtocolHeader& header) : header(header), footer(false) {}
                    Event() : footer(true) {}

                    std::shared_ptr<ToplevelElement> element;
                    boost::optional<ProtocolHeader> header;
                    bool footer;
                };

                MockSessionStream() : available(true), canTLSEncrypt(true), tlsEncrypted(false), compressed(false), whitespacePingEnabled(false), resetCount(0) {
                }

                virtual void close() {
                    onClosed(std::shared_ptr<Error>());
                }

                virtual bool isOpen() {
                    return available;
                }

                virtual void writeHeader(const ProtocolHeader& header) {
                    receivedEvents.push_back(Event(header));
                }

                virtual void writeFooter() {
                    receivedEvents.push_back(Event());
                }

                virtual void writeElement(std::shared_ptr<ToplevelElement> element) {
                    receivedEvents.push_back(Event(element));
                }

                virtual void writeData(const std::string&) {
                }

                virtual bool supportsTLSEncryption() {
                    return canTLSEncrypt;
                }

                virtual void addTLSEncryption() {
                    tlsEncrypted = true;
                }

                virtual bool isTLSEncrypted() {
                    return tlsEncrypted;
                }

                virtual ByteArray getTLSFinishMessage() const {
                    return ByteArray();
                }

                virtual Certificate::ref getPeerCertificate() const {
                    return Certificate::ref(new SimpleCertificate());
                }

                virtual std::vector<Certificate::ref> getPeerCertificateChain() const {
                     return std::vector<Certificate::ref>();
                }

                virtual std::shared_ptr<CertificateVerificationError> getPeerCertificateVerificationError() const {
                    return std::shared_ptr<CertificateVerificationError>();
                }

                virtual bool supportsZLibCompression() {
                    return true;
                }

                virtual void addZLibCompression() {
                    compressed = true;
                }

                virtual void setWhitespacePingEnabled(bool enabled) {
                    whitespacePingEnabled = enabled;
                }

                virtual void resetXMPPParser() {
                    resetCount++;
                }

                void breakConnection() {
                    onClosed(std::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::ConnectionReadError));
                }

                void breakTLS() {
                    onClosed(std::make_shared<SessionStream::SessionStreamError>(SessionStream::SessionStreamError::TLSError));
                }


                void sendStreamStart() {
                    ProtocolHeader header;
                    header.setTo("foo.com");
                    return onStreamStartReceived(header);
                }

                void sendStreamFeaturesWithStartTLS() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->setHasStartTLS();
                    onElementReceived(streamFeatures);
                }

                void sendChallenge() {
                    onElementReceived(std::make_shared<AuthChallenge>());
                }

                void sendStreamError() {
                    onElementReceived(std::make_shared<StreamError>());
                }

                void sendTLSProceed() {
                    onElementReceived(std::make_shared<TLSProceed>());
                }

                void sendTLSFailure() {
                    onElementReceived(std::make_shared<StartTLSFailure>());
                }

                void sendStreamFeaturesWithMultipleAuthentication() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->addAuthenticationMechanism("PLAIN");
                    streamFeatures->addAuthenticationMechanism("DIGEST-MD5");
                    streamFeatures->addAuthenticationMechanism("SCRAM-SHA1");
                    onElementReceived(streamFeatures);
                }

                void sendStreamFeaturesWithPLAINAuthentication() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->addAuthenticationMechanism("PLAIN");
                    onElementReceived(streamFeatures);
                }

                void sendStreamFeaturesWithEXTERNALAuthentication() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->addAuthenticationMechanism("EXTERNAL");
                    onElementReceived(streamFeatures);
                }

                void sendStreamFeaturesWithUnknownAuthentication() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->addAuthenticationMechanism("UNKNOWN");
                    onElementReceived(streamFeatures);
                }

                void sendStreamFeaturesWithBindAndStreamManagement() {
                    std::shared_ptr<StreamFeatures> streamFeatures(new StreamFeatures());
                    streamFeatures->setHasResourceBind();
                    streamFeatures->setHasStreamManagement();
                    onElementReceived(streamFeatures);
                }

                void sendEmptyStreamFeatures() {
                    onElementReceived(std::make_shared<StreamFeatures>());
                }

                void sendAuthSuccess() {
                    onElementReceived(std::make_shared<AuthSuccess>());
                }

                void sendAuthFailure() {
                    onElementReceived(std::make_shared<AuthFailure>());
                }

                void sendStreamManagementEnabled() {
                    onElementReceived(std::make_shared<StreamManagementEnabled>());
                }

                void sendStreamManagementFailed() {
                    onElementReceived(std::make_shared<StreamManagementFailed>());
                }

                void sendBindResult() {
                    std::shared_ptr<ResourceBind> resourceBind(new ResourceBind());
                    resourceBind->setJID(JID("foo@bar.com/bla"));
                    std::shared_ptr<IQ> iq = IQ::createResult(JID("foo@bar.com"), bindID, resourceBind);
                    onElementReceived(iq);
                }

                void sendMessage() {
                    std::shared_ptr<Message> message = std::make_shared<Message>();
                    message->setTo(JID("foo@bar.com/bla"));
                    onElementReceived(message);
                }

                void receiveStreamStart() {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.header);
                }

                void receiveStartTLS() {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.element);
                    CPPUNIT_ASSERT(std::dynamic_pointer_cast<StartTLSRequest>(event.element));
                }

                void receiveAuthRequest(const std::string& mech) {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.element);
                    std::shared_ptr<AuthRequest> request(std::dynamic_pointer_cast<AuthRequest>(event.element));
                    CPPUNIT_ASSERT(request);
                    CPPUNIT_ASSERT_EQUAL(mech, request->getMechanism());
                }

                void receiveStreamManagementEnable() {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.element);
                    CPPUNIT_ASSERT(std::dynamic_pointer_cast<EnableStreamManagement>(event.element));
                }

                void receiveBind() {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.element);
                    std::shared_ptr<IQ> iq = std::dynamic_pointer_cast<IQ>(event.element);
                    CPPUNIT_ASSERT(iq);
                    CPPUNIT_ASSERT(iq->getPayload<ResourceBind>());
                    bindID = iq->getID();
                }

                void receiveAck(unsigned int n) {
                    Event event = popEvent();
                    CPPUNIT_ASSERT(event.element);
                    std::shared_ptr<StanzaAck> ack = std::dynamic_pointer_cast<StanzaAck>(event.element);
                    CPPUNIT_ASSERT(ack);
                    CPPUNIT_ASSERT_EQUAL(n, ack->getHandledStanzasCount());
                }

                Event popEvent() {
                    CPPUNIT_ASSERT(!receivedEvents.empty());
                    Event event = receivedEvents.front();
                    receivedEvents.pop_front();
                    return event;
                }

                bool available;
                bool canTLSEncrypt;
                bool tlsEncrypted;
                bool compressed;
                bool whitespacePingEnabled;
                std::string bindID;
                int resetCount;
                std::deque<Event> receivedEvents;
        };

        std::shared_ptr<IDNConverter> idnConverter;
        std::shared_ptr<MockSessionStream> server;
        bool sessionFinishedReceived;
        bool needCredentials;
        std::shared_ptr<Error> sessionFinishedError;
        BlindCertificateTrustChecker* blindCertificateTrustChecker;
        std::shared_ptr<CryptoProvider> crypto;
        std::shared_ptr<DummyTimerFactory> timerFactory;
};

CPPUNIT_TEST_SUITE_REGISTRATION(ClientSessionTest);

#if 0
        void testAuthenticate() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            session->onNeedCredentials.connect(boost::bind(&ClientSessionTest::setNeedCredentials, this));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithAuthentication();
            session->startSession();
            processEvents();
            CPPUNIT_ASSERT_EQUAL(ClientSession::WaitingForCredentials, session->getState());
            CPPUNIT_ASSERT(needCredentials_);

            getMockServer()->expectAuth("me", "mypass");
            getMockServer()->sendAuthSuccess();
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            session->sendCredentials("mypass");
            CPPUNIT_ASSERT_EQUAL(ClientSession::Authenticating, session->getState());
            processEvents();
            CPPUNIT_ASSERT_EQUAL(ClientSession::Negotiating, session->getState());
        }

        void testAuthenticate_Unauthorized() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithAuthentication();
            session->startSession();
            processEvents();

            getMockServer()->expectAuth("me", "mypass");
            getMockServer()->sendAuthFailure();
            session->sendCredentials("mypass");
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState());
            CPPUNIT_ASSERT_EQUAL(ClientSession::AuthenticationFailedError, *session->getError());
        }

        void testAuthenticate_NoValidAuthMechanisms() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithUnsupportedAuthentication();
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState());
            CPPUNIT_ASSERT_EQUAL(ClientSession::NoSupportedAuthMechanismsError, *session->getError());
        }

        void testResourceBind() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithResourceBind();
            getMockServer()->expectResourceBind("Bar", "session-bind");
            // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::BindingResource, session->getState());
            getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind");
            session->startSession();

            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
            CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar"), session->getLocalJID());
        }

        void testResourceBind_ChangeResource() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithResourceBind();
            getMockServer()->expectResourceBind("Bar", "session-bind");
            getMockServer()->sendResourceBindResponse("me@foo.com/Bar123", "session-bind");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
            CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/Bar123"), session->getLocalJID());
        }

        void testResourceBind_EmptyResource() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithResourceBind();
            getMockServer()->expectResourceBind("", "session-bind");
            getMockServer()->sendResourceBindResponse("me@foo.com/NewResource", "session-bind");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
            CPPUNIT_ASSERT_EQUAL(JID("me@foo.com/NewResource"), session->getLocalJID());
        }

        void testResourceBind_Error() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithResourceBind();
            getMockServer()->expectResourceBind("", "session-bind");
            getMockServer()->sendError("session-bind");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState());
            CPPUNIT_ASSERT_EQUAL(ClientSession::ResourceBindError, *session->getError());
        }

        void testSessionStart() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithSession();
            getMockServer()->expectSessionStart("session-start");
            // FIXME: Check CPPUNIT_ASSERT_EQUAL(ClientSession::StartingSession, session->getState());
            getMockServer()->sendSessionStartResponse("session-start");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
            CPPUNIT_ASSERT(sessionStarted_);
        }

        void testSessionStart_Error() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithSession();
            getMockServer()->expectSessionStart("session-start");
            getMockServer()->sendError("session-start");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::Error, session->getState());
            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStartError, *session->getError());
        }

        void testSessionStart_AfterResourceBind() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            session->onSessionStarted.connect(boost::bind(&ClientSessionTest::setSessionStarted, this));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeaturesWithResourceBindAndSession();
            getMockServer()->expectResourceBind("Bar", "session-bind");
            getMockServer()->sendResourceBindResponse("me@foo.com/Bar", "session-bind");
            getMockServer()->expectSessionStart("session-start");
            getMockServer()->sendSessionStartResponse("session-start");
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(ClientSession::SessionStarted, session->getState());
            CPPUNIT_ASSERT(sessionStarted_);
        }

        void testWhitespacePing() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeatures();
            session->startSession();
            processEvents();
            CPPUNIT_ASSERT(session->getWhitespacePingLayer());
        }

        void testReceiveElementAfterSessionStarted() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeatures();
            session->startSession();
            processEvents();

            getMockServer()->expectMessage();
            session->sendElement(std::make_shared<Message>()));
        }

        void testSendElement() {
            std::shared_ptr<MockSession> session(createSession("me@foo.com/Bar"));
            session->onElementReceived.connect(boost::bind(&ClientSessionTest::addReceivedElement, this, _1));
            getMockServer()->expectStreamStart();
            getMockServer()->sendStreamStart();
            getMockServer()->sendStreamFeatures();
            getMockServer()->sendMessage();
            session->startSession();
            processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedElements_.size()));
            CPPUNIT_ASSERT(std::dynamic_pointer_cast<Message>(receivedElements_[0]));
        }
#endif