summaryrefslogtreecommitdiffstats
blob: f4066356ba02f2b326e3d7659bf0b01e2bbdf1f0 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * 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 "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(testConstructorWithString_IllegalResource);
		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(std::string("foo"), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
			CPPUNIT_ASSERT_EQUAL(std::string("baz"), testling.getResource());
			CPPUNIT_ASSERT(!testling.isBare());
		}

		void testConstructorWithString_NoResource() {
			JID testling("foo@bar");

			CPPUNIT_ASSERT_EQUAL(std::string("foo"), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
			CPPUNIT_ASSERT_EQUAL(std::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(std::string(""), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
			CPPUNIT_ASSERT_EQUAL(std::string("baz"), testling.getResource());
			CPPUNIT_ASSERT(!testling.isBare());
		}

		void testConstructorWithString_OnlyDomain() {
			JID testling("bar");

			CPPUNIT_ASSERT_EQUAL(std::string(""), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
			CPPUNIT_ASSERT_EQUAL(std::string(""), testling.getResource());
			CPPUNIT_ASSERT(testling.isBare());
		}

		void testConstructorWithString_UpperCaseNode() {
			JID testling("Fo\xCE\xA9@bar");

			CPPUNIT_ASSERT_EQUAL(std::string("fo\xCF\x89"), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
		}

		void testConstructorWithString_UpperCaseDomain() {
			JID testling("Fo\xCE\xA9");

			CPPUNIT_ASSERT_EQUAL(std::string("fo\xCF\x89"), testling.getDomain());
		}

		void testConstructorWithString_UpperCaseResource() {
			JID testling("bar/Fo\xCE\xA9");

			CPPUNIT_ASSERT_EQUAL(testling.getResource(), std::string("Fo\xCE\xA9"));
		}

		void testConstructorWithString_EmptyNode() {
			JID testling("@bar");

			CPPUNIT_ASSERT(!testling.isValid());
		}

		void testConstructorWithString_IllegalResource() {
			JID testling("foo@bar.com/\xd8\xb1\xd9\x85\xd9\x82\xd9\x87\x20\xd8\xaa\xd8\xb1\xd9\x86\xd8\xb3\x20");

			CPPUNIT_ASSERT(!testling.isValid());
		}

		void testConstructorWithStrings() {
			JID testling("foo", "bar", "baz");

			CPPUNIT_ASSERT_EQUAL(std::string("foo"), testling.getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getDomain());
			CPPUNIT_ASSERT_EQUAL(std::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(std::string("foo"), testling.toBare().getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.toBare().getDomain());
			CPPUNIT_ASSERT(testling.toBare().isBare());
		}

		void testToBare_EmptyNode() {
			JID testling("bar/baz");

			CPPUNIT_ASSERT_EQUAL(std::string(""), testling.toBare().getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.toBare().getDomain());
			CPPUNIT_ASSERT(testling.toBare().isBare());
		}

		void testToBare_EmptyResource() {
			JID testling("bar/");

			CPPUNIT_ASSERT_EQUAL(std::string(""), testling.toBare().getNode());
			CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.toBare().getDomain());
			CPPUNIT_ASSERT(testling.toBare().isBare());
		}

		void testToString() {
			JID testling("foo@bar/baz");

			CPPUNIT_ASSERT_EQUAL(std::string("foo@bar/baz"), testling.toString());
		}

		void testToString_EmptyNode() {
			JID testling("bar/baz");

			CPPUNIT_ASSERT_EQUAL(std::string("bar/baz"), testling.toString());
		}

		void testToString_NoResource() {
			JID testling("foo@bar");

			CPPUNIT_ASSERT_EQUAL(std::string("foo@bar"), testling.toString());
		}

		void testToString_EmptyResource() {
			JID testling("foo@bar/");

			CPPUNIT_ASSERT_EQUAL(std::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);