summaryrefslogtreecommitdiffstats
blob: fae259f6637f94d11b3dfe0782097a0911a41ec9 (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
/*
 * Copyright (c) 2014-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

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

#include <Swiften/Base/DateTime.h>
#include <Swiften/Elements/Delay.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Parser/PayloadParsers/ForwardedParser.h>
#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>

using namespace Swift;

class ForwardedParserTest : public CppUnit::TestFixture
{
		CPPUNIT_TEST_SUITE(ForwardedParserTest);
		CPPUNIT_TEST(testParseIQ);
		CPPUNIT_TEST(testParseMessage);
		CPPUNIT_TEST(testParseMessageNoDelay);
		CPPUNIT_TEST(testParsePresence);
		CPPUNIT_TEST_SUITE_END();

	public:
		void testParseIQ() {
			PayloadsParserTester parser;
			CPPUNIT_ASSERT(parser.parse(
				"<forwarded xmlns=\"urn:xmpp:forward:0\">"
					"<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>"
					"<iq xmlns=\"jabber:client\" type=\"get\" from=\"kindanormal@example.com/IM\" to=\"stupidnewbie@example.com\" id=\"id0\"/>"
				"</forwarded>"));

			boost::shared_ptr<Forwarded> payload = parser.getPayload<Forwarded>();
			CPPUNIT_ASSERT(!!payload);
			CPPUNIT_ASSERT(payload->getDelay());
			CPPUNIT_ASSERT_EQUAL(std::string("2010-07-10T23:08:25Z"), dateTimeToString(payload->getDelay()->getStamp()));

			boost::shared_ptr<IQ> iq = boost::dynamic_pointer_cast<IQ>(payload->getStanza());
			CPPUNIT_ASSERT(!!iq);
			CPPUNIT_ASSERT_EQUAL(JID("stupidnewbie@example.com"), iq->getTo());
			CPPUNIT_ASSERT_EQUAL(JID("kindanormal@example.com/IM"), iq->getFrom());
			CPPUNIT_ASSERT_EQUAL(std::string("id0"), iq->getID());
			CPPUNIT_ASSERT_EQUAL(IQ::Get, iq->getType());
		}

		void testParseMessage() {
			PayloadsParserTester parser;
			CPPUNIT_ASSERT(parser.parse(
				"<forwarded xmlns=\"urn:xmpp:forward:0\">"
					"<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>"
					"<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">"
						"<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>"
					"</message>"
				"</forwarded>"));

			boost::shared_ptr<Forwarded> payload = parser.getPayload<Forwarded>();
			CPPUNIT_ASSERT(!!payload);
			CPPUNIT_ASSERT(payload->getDelay());
			CPPUNIT_ASSERT_EQUAL(std::string("2010-07-10T23:08:25Z"), dateTimeToString(payload->getDelay()->getStamp()));

			boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(payload->getStanza());
			CPPUNIT_ASSERT(!!message);
			const std::string expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.";
			CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody().get());
			CPPUNIT_ASSERT_EQUAL(Message::Chat, message->getType());
			CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), message->getTo());
			CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), message->getFrom());
		}

		void testParseMessageNoDelay() {
			PayloadsParserTester parser;
			CPPUNIT_ASSERT(parser.parse(
				"<forwarded xmlns=\"urn:xmpp:forward:0\">"
					"<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">"
						"<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>"
					"</message>"
				"</forwarded>"));

			boost::shared_ptr<Forwarded> payload = parser.getPayload<Forwarded>();
			CPPUNIT_ASSERT(!!payload);
			CPPUNIT_ASSERT(!payload->getDelay());

			boost::shared_ptr<Message> message = boost::dynamic_pointer_cast<Message>(payload->getStanza());
			CPPUNIT_ASSERT(!!message);
			const std::string expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.";
			CPPUNIT_ASSERT_EQUAL(expectedBody, message->getBody().get());
			CPPUNIT_ASSERT_EQUAL(Message::Chat, message->getType());
			CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.lit/balcony"), message->getTo());
			CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.lit/orchard"), message->getFrom());
		}

		void testParsePresence() {
			PayloadsParserTester parser;
			CPPUNIT_ASSERT(parser.parse(
				"<forwarded xmlns=\"urn:xmpp:forward:0\">"
					"<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>"
					"<presence xmlns=\"jabber:client\" from=\"alice@wonderland.lit/rabbithole\" to=\"madhatter@wonderland.lit\" type=\"unavailable\"/>"
				"</forwarded>"));

			boost::shared_ptr<Forwarded> payload = parser.getPayload<Forwarded>();
			CPPUNIT_ASSERT(!!payload);
			CPPUNIT_ASSERT(payload->getDelay());
			CPPUNIT_ASSERT_EQUAL(std::string("2010-07-10T23:08:25Z"), dateTimeToString(payload->getDelay()->getStamp()));

			boost::shared_ptr<Presence> presence = boost::dynamic_pointer_cast<Presence>(payload->getStanza());
			CPPUNIT_ASSERT(!!presence);
			CPPUNIT_ASSERT_EQUAL(JID("madhatter@wonderland.lit"), presence->getTo());
			CPPUNIT_ASSERT_EQUAL(JID("alice@wonderland.lit/rabbithole"), presence->getFrom());
			CPPUNIT_ASSERT_EQUAL(Presence::Unavailable, presence->getType());
		}
};

CPPUNIT_TEST_SUITE_REGISTRATION(ForwardedParserTest);