summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/JID')
-rw-r--r--Swiften/JID/JID.cpp116
-rw-r--r--Swiften/JID/JID.h68
-rw-r--r--Swiften/JID/Makefile.inc4
-rw-r--r--Swiften/JID/UnitTest/JIDTest.cpp310
-rw-r--r--Swiften/JID/UnitTest/Makefile.inc2
5 files changed, 500 insertions, 0 deletions
diff --git a/Swiften/JID/JID.cpp b/Swiften/JID/JID.cpp
new file mode 100644
index 0000000..dcd6dd1
--- /dev/null
+++ b/Swiften/JID/JID.cpp
@@ -0,0 +1,116 @@
+#include <stringprep.h>
+#include <vector>
+#include <iostream>
+
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+
+
+class StringPrepper {
+ private:
+ static const int MAX_STRINGPREP_SIZE = 1024;
+
+ public:
+ static String getNamePrepped(const String& name) {
+ return getStringPrepped(name, stringprep_nameprep);
+ }
+
+ static String getNodePrepped(const String& node) {
+ return getStringPrepped(node, stringprep_xmpp_nodeprep);
+ }
+
+ static String getResourcePrepped(const String& resource) {
+ return getStringPrepped(resource, stringprep_xmpp_resourceprep);
+ }
+
+ static String getStringPrepped(const String& s, const Stringprep_profile profile[]) {
+ std::vector<char> input(s.getUTF8String().begin(), s.getUTF8String().end());
+ input.resize(MAX_STRINGPREP_SIZE);
+ if (stringprep(&input[0], MAX_STRINGPREP_SIZE, static_cast<Stringprep_profile_flags>(0), profile) == 0) {
+ return String(&input[0]);
+ }
+ else {
+ return "";
+ }
+ }
+};
+
+
+JID::JID(const char* jid) {
+ initializeFromString(String(jid));
+}
+
+JID::JID(const String& jid) {
+ initializeFromString(jid);
+}
+
+JID::JID(const String& node, const String& domain) : hasResource_(false) {
+ nameprepAndSetComponents(node, domain, "");
+}
+
+JID::JID(const String& node, const String& domain, const String& resource) : hasResource_(true) {
+ nameprepAndSetComponents(node, domain, resource);
+}
+
+void JID::initializeFromString(const String& jid) {
+ if (jid.beginsWith('@')) {
+ return;
+ }
+
+ String bare, resource;
+ size_t slashIndex = jid.find('/');
+ if (slashIndex != jid.npos()) {
+ hasResource_ = true;
+ bare = jid.getSubstring(0, slashIndex);
+ resource = jid.getSubstring(slashIndex + 1, jid.npos());
+ }
+ else {
+ hasResource_ = false;
+ bare = jid;
+ }
+ std::pair<String,String> nodeAndDomain = bare.getSplittedAtFirst('@');
+ if (nodeAndDomain.second.isEmpty()) {
+ nameprepAndSetComponents("", nodeAndDomain.first, resource);
+ }
+ else {
+ nameprepAndSetComponents(nodeAndDomain.first, nodeAndDomain.second, resource);
+ }
+}
+
+
+void JID::nameprepAndSetComponents(const String& node, const String& domain, const String& resource) {
+ node_ = StringPrepper::getNamePrepped(node);
+ domain_ = StringPrepper::getNodePrepped(domain);
+ resource_ = StringPrepper::getResourcePrepped(resource);
+}
+
+String JID::toString() const {
+ String string;
+ if (!node_.isEmpty()) {
+ string += node_ + "@";
+ }
+ string += domain_;
+ if (!isBare()) {
+ string += "/" + resource_;
+ }
+ return string;
+}
+
+int JID::compare(const Swift::JID& o, CompareType compareType) const {
+ if (node_ < o.node_) { return -1; }
+ if (node_ > o.node_) { return 1; }
+ if (domain_ < o.domain_) { return -1; }
+ if (domain_ > o.domain_) { return 1; }
+ if (compareType == WithResource) {
+ if (hasResource_ != o.hasResource_) {
+ return hasResource_ ? 1 : -1;
+ }
+ if (resource_ < o.resource_) { return -1; }
+ if (resource_ > o.resource_) { return 1; }
+ }
+ return 0;
+}
+
+} // namespace Swift
+
diff --git a/Swiften/JID/JID.h b/Swiften/JID/JID.h
new file mode 100644
index 0000000..ad89c85
--- /dev/null
+++ b/Swiften/JID/JID.h
@@ -0,0 +1,68 @@
+#ifndef SWIFTEN_JID_H
+#define SWIFTEN_JID_H
+
+#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);
+
+ private:
+ void initializeFromString(const String&);
+
+ private:
+ String node_;
+ String domain_;
+ bool hasResource_;
+ String resource_;
+ };
+}
+
+#endif
+
diff --git a/Swiften/JID/Makefile.inc b/Swiften/JID/Makefile.inc
new file mode 100644
index 0000000..f60cb3c
--- /dev/null
+++ b/Swiften/JID/Makefile.inc
@@ -0,0 +1,4 @@
+SWIFTEN_SOURCES += \
+ Swiften/JID/JID.cpp
+
+include Swiften/JID/UnitTest/Makefile.inc
diff --git a/Swiften/JID/UnitTest/JIDTest.cpp b/Swiften/JID/UnitTest/JIDTest.cpp
new file mode 100644
index 0000000..917f89f
--- /dev/null
+++ b/Swiften/JID/UnitTest/JIDTest.cpp
@@ -0,0 +1,310 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/JID/JID.h"
+
+using namespace Swift;
+
+class JIDTest : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(JIDTest);
+ CPPUNIT_TEST(testConstructorWithString);
+ CPPUNIT_TEST(testConstructorWithString_NoResource);
+ CPPUNIT_TEST(testConstructorWithString_NoNode);
+ CPPUNIT_TEST(testConstructorWithString_EmptyResource);
+ CPPUNIT_TEST(testConstructorWithString_OnlyDomain);
+ CPPUNIT_TEST(testConstructorWithString_UpperCaseNode);
+ CPPUNIT_TEST(testConstructorWithString_UpperCaseDomain);
+ CPPUNIT_TEST(testConstructorWithString_UpperCaseResource);
+ CPPUNIT_TEST(testConstructorWithString_EmptyNode);
+ CPPUNIT_TEST(testConstructorWithStrings);
+ CPPUNIT_TEST(testIsBare);
+ CPPUNIT_TEST(testIsBare_NotBare);
+ CPPUNIT_TEST(testToBare);
+ CPPUNIT_TEST(testToBare_EmptyNode);
+ CPPUNIT_TEST(testToBare_EmptyResource);
+ CPPUNIT_TEST(testToString);
+ CPPUNIT_TEST(testToString_EmptyNode);
+ CPPUNIT_TEST(testToString_EmptyResource);
+ CPPUNIT_TEST(testToString_NoResource);
+ CPPUNIT_TEST(testCompare_SmallerNode);
+ CPPUNIT_TEST(testCompare_LargerNode);
+ CPPUNIT_TEST(testCompare_SmallerDomain);
+ CPPUNIT_TEST(testCompare_LargerDomain);
+ CPPUNIT_TEST(testCompare_SmallerResource);
+ CPPUNIT_TEST(testCompare_LargerResource);
+ CPPUNIT_TEST(testCompare_Equal);
+ CPPUNIT_TEST(testCompare_EqualWithoutResource);
+ CPPUNIT_TEST(testCompare_NoResourceAndEmptyResource);
+ CPPUNIT_TEST(testCompare_EmptyResourceAndNoResource);
+ CPPUNIT_TEST(testEquals);
+ CPPUNIT_TEST(testEquals_NotEqual);
+ CPPUNIT_TEST(testEquals_WithoutResource);
+ CPPUNIT_TEST(testSmallerThan);
+ CPPUNIT_TEST(testSmallerThan_Equal);
+ CPPUNIT_TEST(testSmallerThan_Larger);
+ CPPUNIT_TEST(testHasResource);
+ CPPUNIT_TEST(testHasResource_NoResource);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ JIDTest() {}
+
+ void testConstructorWithString() {
+ JID testling("foo@bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(String("baz"), testling.getResource());
+ CPPUNIT_ASSERT(!testling.isBare());
+ }
+
+ void testConstructorWithString_NoResource() {
+ JID testling("foo@bar");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.getResource());
+ CPPUNIT_ASSERT(testling.isBare());
+ }
+
+ void testConstructorWithString_EmptyResource() {
+ JID testling("bar/");
+
+ CPPUNIT_ASSERT(testling.isValid());
+ CPPUNIT_ASSERT(!testling.isBare());
+ }
+
+ void testConstructorWithString_NoNode() {
+ JID testling("bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(String("baz"), testling.getResource());
+ CPPUNIT_ASSERT(!testling.isBare());
+ }
+
+ void testConstructorWithString_OnlyDomain() {
+ JID testling("bar");
+
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.getResource());
+ CPPUNIT_ASSERT(testling.isBare());
+ }
+
+ void testConstructorWithString_UpperCaseNode() {
+ JID testling("Fo\xCE\xA9@bar");
+
+ CPPUNIT_ASSERT_EQUAL(String("fo\xCF\x89"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ }
+
+ void testConstructorWithString_UpperCaseDomain() {
+ JID testling("Fo\xCE\xA9");
+
+ CPPUNIT_ASSERT_EQUAL(String("fo\xCF\x89"), testling.getDomain());
+ }
+
+ void testConstructorWithString_UpperCaseResource() {
+ JID testling("bar/Fo\xCE\xA9");
+
+ CPPUNIT_ASSERT_EQUAL(testling.getResource(), String("Fo\xCE\xA9"));
+ }
+
+ void testConstructorWithString_EmptyNode() {
+ JID testling("@bar");
+
+ CPPUNIT_ASSERT(!testling.isValid());
+ }
+
+ void testConstructorWithStrings() {
+ JID testling("foo", "bar", "baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo"), testling.getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.getDomain());
+ CPPUNIT_ASSERT_EQUAL(String("baz"), testling.getResource());
+ }
+
+ void testIsBare() {
+ CPPUNIT_ASSERT(JID("foo@bar").isBare());
+ }
+
+ void testIsBare_NotBare() {
+ CPPUNIT_ASSERT(!JID("foo@bar/baz").isBare());
+ }
+
+ void testToBare() {
+ JID testling("foo@bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo"), testling.toBare().getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.toBare().getDomain());
+ CPPUNIT_ASSERT(testling.toBare().isBare());
+ }
+
+ void testToBare_EmptyNode() {
+ JID testling("bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.toBare().getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.toBare().getDomain());
+ CPPUNIT_ASSERT(testling.toBare().isBare());
+ }
+
+ void testToBare_EmptyResource() {
+ JID testling("bar/");
+
+ CPPUNIT_ASSERT_EQUAL(String(""), testling.toBare().getNode());
+ CPPUNIT_ASSERT_EQUAL(String("bar"), testling.toBare().getDomain());
+ CPPUNIT_ASSERT(testling.toBare().isBare());
+ }
+
+ void testToString() {
+ JID testling("foo@bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo@bar/baz"), testling.toString());
+ }
+
+ void testToString_EmptyNode() {
+ JID testling("bar/baz");
+
+ CPPUNIT_ASSERT_EQUAL(String("bar/baz"), testling.toString());
+ }
+
+ void testToString_NoResource() {
+ JID testling("foo@bar");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo@bar"), testling.toString());
+ }
+
+ void testToString_EmptyResource() {
+ JID testling("foo@bar/");
+
+ CPPUNIT_ASSERT_EQUAL(String("foo@bar/"), testling.toString());
+ }
+
+ void testCompare_SmallerNode() {
+ JID testling1("a@c");
+ JID testling2("b@b");
+
+ CPPUNIT_ASSERT_EQUAL(-1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_LargerNode() {
+ JID testling1("c@a");
+ JID testling2("b@b");
+
+ CPPUNIT_ASSERT_EQUAL(1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_SmallerDomain() {
+ JID testling1("x@a/c");
+ JID testling2("x@b/b");
+
+ CPPUNIT_ASSERT_EQUAL(-1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_LargerDomain() {
+ JID testling1("x@b/b");
+ JID testling2("x@a/c");
+
+ CPPUNIT_ASSERT_EQUAL(1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_SmallerResource() {
+ JID testling1("x@y/a");
+ JID testling2("x@y/b");
+
+ CPPUNIT_ASSERT_EQUAL(-1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_LargerResource() {
+ JID testling1("x@y/b");
+ JID testling2("x@y/a");
+
+ CPPUNIT_ASSERT_EQUAL(1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_Equal() {
+ JID testling1("x@y/z");
+ JID testling2("x@y/z");
+
+ CPPUNIT_ASSERT_EQUAL(0, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_EqualWithoutResource() {
+ JID testling1("x@y/a");
+ JID testling2("x@y/b");
+
+ CPPUNIT_ASSERT_EQUAL(0, testling1.compare(testling2, JID::WithoutResource));
+ }
+
+ void testCompare_NoResourceAndEmptyResource() {
+ JID testling1("x@y/");
+ JID testling2("x@y");
+
+ CPPUNIT_ASSERT_EQUAL(1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testCompare_EmptyResourceAndNoResource() {
+ JID testling1("x@y");
+ JID testling2("x@y/");
+
+ CPPUNIT_ASSERT_EQUAL(-1, testling1.compare(testling2, JID::WithResource));
+ }
+
+ void testEquals() {
+ JID testling1("x@y/c");
+ JID testling2("x@y/c");
+
+ CPPUNIT_ASSERT(testling1.equals(testling2, JID::WithResource));
+ }
+
+ void testEquals_NotEqual() {
+ JID testling1("x@y/c");
+ JID testling2("x@y/d");
+
+ CPPUNIT_ASSERT(!testling1.equals(testling2, JID::WithResource));
+ }
+
+ void testEquals_WithoutResource() {
+ JID testling1("x@y/c");
+ JID testling2("x@y/d");
+
+ CPPUNIT_ASSERT(testling1.equals(testling2, JID::WithoutResource));
+ }
+
+ void testSmallerThan() {
+ JID testling1("x@y/c");
+ JID testling2("x@y/d");
+
+ CPPUNIT_ASSERT(testling1 < testling2);
+ }
+
+ void testSmallerThan_Equal() {
+ JID testling1("x@y/d");
+ JID testling2("x@y/d");
+
+ CPPUNIT_ASSERT(!(testling1 < testling2));
+ }
+
+ void testSmallerThan_Larger() {
+ JID testling1("x@y/d");
+ JID testling2("x@y/c");
+
+ CPPUNIT_ASSERT(!(testling1 < testling2));
+ }
+
+ void testHasResource() {
+ JID testling("x@y/d");
+
+ CPPUNIT_ASSERT(!testling.isBare());
+ }
+
+ void testHasResource_NoResource() {
+ JID testling("x@y");
+
+ CPPUNIT_ASSERT(testling.isBare());
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(JIDTest);
diff --git a/Swiften/JID/UnitTest/Makefile.inc b/Swiften/JID/UnitTest/Makefile.inc
new file mode 100644
index 0000000..e896fd7
--- /dev/null
+++ b/Swiften/JID/UnitTest/Makefile.inc
@@ -0,0 +1,2 @@
+UNITTEST_SOURCES += \
+ Swiften/JID/UnitTest/JIDTest.cpp