blob: 86776e8652816cc1ec3f9cbaa10ad9a86b1765a5 (
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
|
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <deque>
#include <boost/bind.hpp>
#include <boost/optional.hpp>
#include "Swiften/Session/SessionStream.h"
#include "Swiften/Component/ComponentSession.h"
#include "Swiften/Elements/ComponentHandshake.h"
#include "Swiften/Elements/AuthFailure.h"
using namespace Swift;
class ComponentSessionTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ComponentSessionTest);
CPPUNIT_TEST(testStart);
CPPUNIT_TEST(testStart_Error);
CPPUNIT_TEST(testStart_Unauthorized);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {
server = boost::shared_ptr<MockSessionStream>(new MockSessionStream());
sessionFinishedReceived = false;
}
void testStart() {
boost::shared_ptr<ComponentSession> session(createSession());
session->start();
server->receiveStreamStart();
server->sendStreamStart();
server->receiveHandshake();
server->sendHandshakeResponse();
CPPUNIT_ASSERT(server->whitespacePingEnabled);
session->finish();
CPPUNIT_ASSERT(!server->whitespacePingEnabled);
}
void testStart_Error() {
boost::shared_ptr<ComponentSession> session(createSession());
session->start();
server->breakConnection();
CPPUNIT_ASSERT_EQUAL(ComponentSession::Finished, session->getState());
CPPUNIT_ASSERT(sessionFinishedReceived);
CPPUNIT_ASSERT(sessionFinishedError);
}
void testStart_Unauthorized() {
boost::shared_ptr<ComponentSession> session(createSession());
session->start();
server->receiveStreamStart();
server->sendStreamStart();
server->receiveHandshake();
server->sendHandshakeError();
CPPUNIT_ASSERT_EQUAL(ComponentSession::Finished, session->getState());
CPPUNIT_ASSERT(sessionFinishedReceived);
CPPUNIT_ASSERT(sessionFinishedError);
}
private:
boost::shared_ptr<ComponentSession> createSession() {
boost::shared_ptr<ComponentSession> session = ComponentSession::create(JID("service.foo.com"), "servicesecret", server);
session->onFinished.connect(boost::bind(&ComponentSessionTest::handleSessionFinished, this, _1));
return session;
}
void handleSessionFinished(boost::shared_ptr<Error> error) {
sessionFinishedReceived = true;
sessionFinishedError = error;
}
class MockSessionStream : public SessionStream {
public:
struct Event {
Event(boost::shared_ptr<Element> element) : element(element), footer(false) {}
Event(const ProtocolHeader& header) : header(header), footer(false) {}
Event() : footer(true) {}
boost::shared_ptr<Element> element;
boost::optional<ProtocolHeader> header;
bool footer;
};
MockSessionStream() : available(true), whitespacePingEnabled(false), resetCount(0) {
}
virtual void close() {
onClosed(boost::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(boost::shared_ptr<Element> element) {
receivedEvents.push_back(Event(element));
}
virtual bool supportsTLSEncryption() {
return false;
}
virtual void addTLSEncryption() {
assert(false);
}
virtual bool isTLSEncrypted() {
return false;
}
virtual ByteArray getTLSFinishMessage() const {
return ByteArray();
}
virtual Certificate::ref getPeerCertificate() const {
return Certificate::ref();
}
virtual boost::shared_ptr<CertificateVerificationError> getPeerCertificateVerificationError() const {
return boost::shared_ptr<CertificateVerificationError>();
}
virtual void addZLibCompression() {
assert(false);
}
virtual void setWhitespacePingEnabled(bool enabled) {
whitespacePingEnabled = enabled;
}
virtual void resetXMPPParser() {
resetCount++;
}
void breakConnection() {
onClosed(boost::shared_ptr<SessionStream::Error>(new SessionStream::Error(SessionStream::Error::ConnectionReadError)));
}
void sendStreamStart() {
ProtocolHeader header;
header.setFrom("service.foo.com");
return onStreamStartReceived(header);
}
void sendHandshakeResponse() {
onElementReceived(ComponentHandshake::ref(new ComponentHandshake()));
}
void sendHandshakeError() {
// FIXME: This isn't the correct element
onElementReceived(AuthFailure::ref(new AuthFailure()));
}
void receiveStreamStart() {
Event event = popEvent();
CPPUNIT_ASSERT(event.header);
}
void receiveHandshake() {
Event event = popEvent();
CPPUNIT_ASSERT(event.element);
ComponentHandshake::ref handshake(boost::dynamic_pointer_cast<ComponentHandshake>(event.element));
CPPUNIT_ASSERT(handshake);
CPPUNIT_ASSERT_EQUAL(String("4c4f8a41141722c8bbfbdd92d827f7b2fc0a542b"), handshake->getData());
}
Event popEvent() {
CPPUNIT_ASSERT(receivedEvents.size() > 0);
Event event = receivedEvents.front();
receivedEvents.pop_front();
return event;
}
bool available;
bool whitespacePingEnabled;
String bindID;
int resetCount;
std::deque<Event> receivedEvents;
};
boost::shared_ptr<MockSessionStream> server;
bool sessionFinishedReceived;
bool needCredentials;
boost::shared_ptr<Error> sessionFinishedError;
};
CPPUNIT_TEST_SUITE_REGISTRATION(ComponentSessionTest);
|