summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-12-17 15:25:20 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-12-17 16:11:19 (GMT)
commitbdb8ace202917809c5492aa37921e0ffd0354730 (patch)
tree52e93ebf3a555c690086b1bce0b2e01c1bf943b6 /Swiften
parente32059da8bffc67806862bf78f762d67fb3e4848 (diff)
downloadswift-bdb8ace202917809c5492aa37921e0ffd0354730.zip
swift-bdb8ace202917809c5492aa37921e0ffd0354730.tar.bz2
Added eclipse project file.
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/JID/JID.h134
-rw-r--r--Swiften/Network/UnitTest/ConnectorTest.cpp57
2 files changed, 125 insertions, 66 deletions
diff --git a/Swiften/JID/JID.h b/Swiften/JID/JID.h
index ad89c85..0bfb858 100644
--- a/Swiften/JID/JID.h
+++ b/Swiften/JID/JID.h
@@ -1,68 +1,80 @@
-#ifndef SWIFTEN_JID_H
-#define SWIFTEN_JID_H
+#pragma once
#include "Swiften/Base/String.h"
namespace Swift {
- class JID
- {
- public:
- enum CompareType { WithResource, WithoutResource };
-
- explicit JID(const String& = String());
- explicit JID(const char*);
- JID(const String& node, const String& domain);
- JID(const String& node, const String& domain, const String& resource);
-
- bool isValid() const { return !domain_.isEmpty(); /* FIXME */ }
-
- const String& getNode() const { return node_; }
- const String& getDomain() const { return domain_; }
- const String& getResource() const { return resource_; }
- bool isBare() const { return !hasResource_; }
-
- JID toBare() const { return JID(getNode(), getDomain()); /* FIXME: Duplicate unnecessary nameprepping. Probably ok. */ }
-
- String toString() const;
-
- bool equals(const JID& o, CompareType compareType) const {
- return compare(o, compareType) == 0;
- }
-
- int compare(const JID& o, CompareType compareType) const;
-
- operator String () const { return toString(); }
-
- bool operator<(const Swift::JID& b) const {
- return compare(b, Swift::JID::WithResource) < 0;
- }
-
- friend std::ostream& operator<<(std::ostream& os, const Swift::JID& j) {
- os << j.toString();
- return os;
- }
-
- friend bool operator==(const Swift::JID& a, const Swift::JID& b) {
- return a.compare(b, Swift::JID::WithResource) == 0;
- }
-
- friend bool operator!=(const Swift::JID& a, const Swift::JID& b) {
- return a.compare(b, Swift::JID::WithResource) != 0;
- }
-
- protected:
- void nameprepAndSetComponents(const String& node, const String& domain, const String& resource);
+ class JID {
+ public:
+ enum CompareType {
+ WithResource, WithoutResource
+ };
+
+ explicit JID(const String& = String());
+ explicit JID(const char*);
+ JID(const String& node, const String& domain);
+ JID(const String& node, const String& domain, const String& resource);
+
+ bool isValid() const {
+ return !domain_.isEmpty(); /* FIXME */
+ }
+
+ const String& getNode() const {
+ return node_;
+ }
+ const String& getDomain() const {
+ return domain_;
+ }
+ const String& getResource() const {
+ return resource_;
+ }
+ bool isBare() const {
+ return !hasResource_;
+ }
+
+ JID toBare() const {
+ return JID(getNode(), getDomain()); /* FIXME: Duplicate unnecessary nameprepping. Probably ok. */
+ }
+
+ String toString() const;
+
+ bool equals(const JID& o, CompareType compareType) const {
+ return compare(o, compareType) == 0;
+ }
+
+ int compare(const JID& o, CompareType compareType) const;
+
+ operator String() const {
+ return toString();
+ }
+
+ bool operator<(const Swift::JID& b) const {
+ return compare(b, Swift::JID::WithResource) < 0;
+ }
+
+ friend std::ostream& operator<<(std::ostream& os, const Swift::JID& j) {
+ os << j.toString();
+ return os;
+ }
+
+ friend bool operator==(const Swift::JID& a, const Swift::JID& b) {
+ return a.compare(b, Swift::JID::WithResource) == 0;
+ }
+
+ friend bool operator!=(const Swift::JID& a, const Swift::JID& b) {
+ return a.compare(b, Swift::JID::WithResource) != 0;
+ }
+
+ protected:
+ void nameprepAndSetComponents(const String& node, const String& domain,
+ const String& resource);
+
+ private:
+ void initializeFromString(const String&);
- private:
- void initializeFromString(const String&);
-
- private:
- String node_;
- String domain_;
- bool hasResource_;
- String resource_;
+ private:
+ String node_;
+ String domain_;
+ bool hasResource_;
+ String resource_;
};
}
-
-#endif
-
diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp
index 05c6e28..af1ad4e 100644
--- a/Swiften/Network/UnitTest/ConnectorTest.cpp
+++ b/Swiften/Network/UnitTest/ConnectorTest.cpp
@@ -1,140 +1,187 @@
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/optional.hpp>
#include <boost/bind.hpp>
#include "Swiften/Network/Connector.h"
#include "Swiften/Network/Connection.h"
#include "Swiften/Network/ConnectionFactory.h"
#include "Swiften/Network/HostAddressPort.h"
#include "Swiften/Network/StaticDomainNameResolver.h"
#include "Swiften/EventLoop/MainEventLoop.h"
#include "Swiften/EventLoop/DummyEventLoop.h"
using namespace Swift;
class ConnectorTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ConnectorTest);
CPPUNIT_TEST(testConnect);
+ CPPUNIT_TEST(testConnect_NoSRVHost);
CPPUNIT_TEST(testConnect_NoHosts);
- CPPUNIT_TEST(testConnect_FirstHostFails);
- CPPUNIT_TEST(testConnect_AllHostsFail);
+ CPPUNIT_TEST(testConnect_FirstSRVHostFails);
+ CPPUNIT_TEST(testConnect_AllSRVHostsFailWithoutFallbackHost);
+ CPPUNIT_TEST(testConnect_AllSRVHostsFailWithFallbackHost);
+ CPPUNIT_TEST(testConnect_SRVAndFallbackHostsFail);
CPPUNIT_TEST_SUITE_END();
public:
- ConnectorTest() : host1(HostAddress("1.1.1.1"), 1234), host2(HostAddress("2.2.2.2"), 2345) {
+ ConnectorTest() : host1(HostAddress("1.1.1.1"), 1234), host2(HostAddress("2.2.2.2"), 2345), host3(HostAddress("3.3.3.3"), 5222) {
}
void setUp() {
eventLoop = new DummyEventLoop();
resolver = new StaticDomainNameResolver();
connectionFactory = new MockConnectionFactory();
}
void tearDown() {
delete connectionFactory;
delete resolver;
delete eventLoop;
}
void testConnect() {
std::auto_ptr<Connector> testling(createConnector());
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
+ resolver->addAddress("foo.com", host3.getAddress());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(connections[0]);
CPPUNIT_ASSERT(host1 == *(connections[0]->hostAddressPort));
}
+ void testConnect_NoSRVHost() {
+ std::auto_ptr<Connector> testling(createConnector());
+ resolver->addAddress("foo.com", host3.getAddress());
+
+ testling->start();
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
+ CPPUNIT_ASSERT(connections[0]);
+ CPPUNIT_ASSERT(host3 == *(connections[0]->hostAddressPort));
+ }
+
void testConnect_NoHosts() {
std::auto_ptr<Connector> testling(createConnector());
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
}
- void testConnect_FirstHostFails() {
+ void testConnect_FirstSRVHostFails() {
std::auto_ptr<Connector> testling(createConnector());
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
connectionFactory->failingPorts.push_back(host1);
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(host2 == *(connections[0]->hostAddressPort));
}
- void testConnect_AllHostsFail() {
+ void testConnect_AllSRVHostsFailWithoutFallbackHost() {
std::auto_ptr<Connector> testling(createConnector());
resolver->addXMPPClientService("foo.com", host1);
resolver->addXMPPClientService("foo.com", host2);
connectionFactory->failingPorts.push_back(host1);
connectionFactory->failingPorts.push_back(host2);
testling->start();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
CPPUNIT_ASSERT(!connections[0]);
}
+ void testConnect_AllSRVHostsFailWithFallbackHost() {
+ std::auto_ptr<Connector> testling(createConnector());
+ resolver->addXMPPClientService("foo.com", host1);
+ resolver->addXMPPClientService("foo.com", host2);
+ resolver->addAddress("foo.com", host3.getAddress());
+ connectionFactory->failingPorts.push_back(host1);
+ connectionFactory->failingPorts.push_back(host2);
+
+ testling->start();
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
+ CPPUNIT_ASSERT(connections[0]);
+ CPPUNIT_ASSERT(host3 == *(connections[0]->hostAddressPort));
+ }
+
+ void testConnect_SRVAndFallbackHostsFail() {
+ std::auto_ptr<Connector> testling(createConnector());
+ resolver->addXMPPClientService("foo.com", host1);
+ resolver->addAddress("foo.com", host3.getAddress());
+ connectionFactory->failingPorts.push_back(host1);
+ connectionFactory->failingPorts.push_back(host3);
+
+ testling->start();
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
+ CPPUNIT_ASSERT(!connections[0]);
+ }
+
private:
Connector* createConnector() {
Connector* connector = new Connector("foo.com", resolver, connectionFactory);
connector->onConnectFinished.connect(boost::bind(&ConnectorTest::handleConnectorFinished, this, _1));
return connector;
}
void handleConnectorFinished(boost::shared_ptr<Connection> connection) {
boost::shared_ptr<MockConnection> c(boost::dynamic_pointer_cast<MockConnection>(connection));
if (connection) {
assert(c);
}
connections.push_back(c);
}
struct MockConnection : public Connection {
public:
MockConnection(const std::vector<HostAddressPort>& failingPorts) : failingPorts(failingPorts) {}
void listen() { assert(false); }
void connect(const HostAddressPort& address) {
hostAddressPort = address;
MainEventLoop::postEvent(boost::bind(boost::ref(onConnectFinished), std::find(failingPorts.begin(), failingPorts.end(), address) != failingPorts.end()));
}
void disconnect() { assert(false); }
void write(const ByteArray&) { assert(false); }
boost::optional<HostAddressPort> hostAddressPort;
std::vector<HostAddressPort> failingPorts;
};
struct MockConnectionFactory : public ConnectionFactory {
boost::shared_ptr<Connection> createConnection() {
return boost::shared_ptr<Connection>(new MockConnection(failingPorts));
}
std::vector<HostAddressPort> failingPorts;
};
private:
HostAddressPort host1;
HostAddressPort host2;
+ HostAddressPort host3;
DummyEventLoop* eventLoop;
StaticDomainNameResolver* resolver;
MockConnectionFactory* connectionFactory;
std::vector< boost::shared_ptr<MockConnection> > connections;
};
CPPUNIT_TEST_SUITE_REGISTRATION(ConnectorTest);