From 92614cf44bc98c3aae240a8089452fb950af5c7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Mon, 13 Jul 2009 21:01:20 +0200
Subject: Added PLAIN SASL message parsing.


diff --git a/Swiften/SASL/PLAINMessage.cpp b/Swiften/SASL/PLAINMessage.cpp
index c88d6cf..66f8cd0 100644
--- a/Swiften/SASL/PLAINMessage.cpp
+++ b/Swiften/SASL/PLAINMessage.cpp
@@ -2,9 +2,37 @@
 
 namespace Swift {
 
-PLAINMessage::PLAINMessage(const String& authcid, const String& password, const String& authzid) {
+PLAINMessage::PLAINMessage(const String& authcid, const String& password, const String& authzid) : authcid(authcid), authzid(authzid), password(password) {
+}
+
+PLAINMessage::PLAINMessage(const ByteArray& value) {
+	size_t i = 0;
+	while (i < value.getSize() && value[i] != '\0') {
+		authzid += value[i];
+		++i;
+	}
+	if (i == value.getSize()) {
+		return;
+	}
+	++i;
+	while (i < value.getSize() && value[i] != '\0') {
+		authcid += value[i];
+		++i;
+	}
+	if (i == value.getSize()) {
+		authcid = "";
+		return;
+	}
+	++i;
+	while (i < value.getSize()) {
+		password += value[i];
+		++i;
+	}
+}
+
+ByteArray PLAINMessage::getValue() const {
 	String s = authzid + '\0' + authcid + '\0' + password;
-	value_ = ByteArray(s.getUTF8Data(), s.getUTF8Size());
+	return ByteArray(s.getUTF8Data(), s.getUTF8Size());
 }
 
 }
diff --git a/Swiften/SASL/PLAINMessage.h b/Swiften/SASL/PLAINMessage.h
index 14a51f2..76de4f5 100644
--- a/Swiften/SASL/PLAINMessage.h
+++ b/Swiften/SASL/PLAINMessage.h
@@ -1,22 +1,31 @@
-#ifndef SASL_PLAINMESSAGE_H
-#define SASL_PLAINMESSAGE_H
+#pragma once
 
 #include "Swiften/Base/String.h"
 #include "Swiften/Base/ByteArray.h"
 
 namespace Swift {
-	class PLAINMessage
-	{
+	class PLAINMessage {
 		public:
 			PLAINMessage(const String& authcid, const String& password, const String& authzid = "");
+			PLAINMessage(const ByteArray& value);
 
-			const ByteArray& getValue() {
-				return value_;
+			ByteArray getValue() const;
+
+			const String& getAuthenticationID() const {
+				return authcid;
+			}
+
+			const String& getPassword() const {
+				return password;
+			}
+
+			const String& getAuthorizationID() const {
+				return authzid;
 			}
 
 		private:
-			ByteArray value_;
+			String authcid;
+			String authzid;
+			String password;
 	};
 }
-
-#endif
diff --git a/Swiften/SASL/UnitTest/PLAINMessageTest.cpp b/Swiften/SASL/UnitTest/PLAINMessageTest.cpp
index da9287a..6493bd5 100644
--- a/Swiften/SASL/UnitTest/PLAINMessageTest.cpp
+++ b/Swiften/SASL/UnitTest/PLAINMessageTest.cpp
@@ -8,22 +8,54 @@ using namespace Swift;
 class PLAINMessageTest : public CppUnit::TestFixture
 {
 		CPPUNIT_TEST_SUITE(PLAINMessageTest);
+		CPPUNIT_TEST(testGetValue_WithoutAuthzID);
+		CPPUNIT_TEST(testGetValue_WithAuthzID);
 		CPPUNIT_TEST(testConstructor_WithoutAuthzID);
 		CPPUNIT_TEST(testConstructor_WithAuthzID);
+		CPPUNIT_TEST(testConstructor_NoAuthcid);
+		CPPUNIT_TEST(testConstructor_NoPassword);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
 		PLAINMessageTest() {}
 
-		void testConstructor_WithoutAuthzID() {
+		void testGetValue_WithoutAuthzID() {
 			PLAINMessage message("user", "pass");
 			CPPUNIT_ASSERT_EQUAL(message.getValue(), ByteArray("\0user\0pass", 10));
 		}
 
-		void testConstructor_WithAuthzID() {
+		void testGetValue_WithAuthzID() {
 			PLAINMessage message("user", "pass", "authz");
 			CPPUNIT_ASSERT_EQUAL(message.getValue(), ByteArray("authz\0user\0pass", 15));
 		}
+
+		void testConstructor_WithoutAuthzID() {
+			PLAINMessage message(ByteArray("\0user\0pass", 10));
+
+			CPPUNIT_ASSERT_EQUAL(String(""), message.getAuthorizationID());
+			CPPUNIT_ASSERT_EQUAL(String("user"), message.getAuthenticationID());
+			CPPUNIT_ASSERT_EQUAL(String("pass"), message.getPassword());
+		}
+
+		void testConstructor_WithAuthzID() {
+			PLAINMessage message(ByteArray("authz\0user\0pass", 15));
+
+			CPPUNIT_ASSERT_EQUAL(String("authz"), message.getAuthorizationID());
+			CPPUNIT_ASSERT_EQUAL(String("user"), message.getAuthenticationID());
+			CPPUNIT_ASSERT_EQUAL(String("pass"), message.getPassword());
+		}
+
+		void testConstructor_NoAuthcid() {
+			PLAINMessage message(ByteArray("authzid", 7));
+
+			CPPUNIT_ASSERT_EQUAL(String(""), message.getAuthenticationID());
+		}
+
+		void testConstructor_NoPassword() {
+			PLAINMessage message(ByteArray("authzid\0authcid", 15));
+
+			CPPUNIT_ASSERT_EQUAL(String(""), message.getAuthenticationID());
+		}
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(PLAINMessageTest);
-- 
cgit v0.10.2-6-g49f6