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

#include <cppunit/extensions/HelperMacros.h>

#include <Swiften/SASL/WindowsServicePrincipalName.h>

using namespace Swift;

class WindowsServicePrincipalNameTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(WindowsServicePrincipalNameTest);
        CPPUNIT_TEST(testServiceClass);
        CPPUNIT_TEST(testServiceName);
        CPPUNIT_TEST(testInstanceName);
        CPPUNIT_TEST(testInstancePort);
        CPPUNIT_TEST(testReferrer);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testServiceClass() {
            WindowsServicePrincipalName spn("mlink.adlon.isode.net");

            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net"));

            spn.setServiceClass("ldap");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("ldap/mlink.adlon.isode.net"));

            spn.setServiceClass("中文");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("中文/mlink.adlon.isode.net"));

            try {
                spn.setServiceClass("");
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }

            try {
                spn.setServiceClass("xm/pp"); /* Foward slash not allowed */
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }
        }

        void testServiceName() {
            try {
                WindowsServicePrincipalName spn("");
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }

            try {
                WindowsServicePrincipalName spn2("mlink/adlon.isode.net"); /* Foward slash not allowed */
                spn2.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }

            WindowsServicePrincipalName spn3("mlinkÄ.adlon.isode.net");
            CPPUNIT_ASSERT_EQUAL(spn3.toString(), std::string("xmpp/mlinkÄ.adlon.isode.net"));
        }

        void testInstanceName() {
            WindowsServicePrincipalName spn("adlon.isode.net");

            spn.setInstanceName("mlink.adlon.isode.net");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net/adlon.isode.net"));

            spn.setInstanceName("127.0.0.1");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/127.0.0.1/adlon.isode.net"));

            spn.setInstanceName("");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/adlon.isode.net"));

            spn.setInstanceName("cañón.adlon.isode.net");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/cañón.adlon.isode.net/adlon.isode.net"));

            try {
                spn.setInstanceName("mlink/adlon.isode.net"); /* Foward slash not allowed */
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }
        }

        void testInstancePort() {
            WindowsServicePrincipalName spn("adlon.isode.net");

            spn.setInstanceName("mlink.adlon.isode.net");
            spn.setInstancePort(55222);
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net:55222/adlon.isode.net"));

            spn.setInstancePort(0);
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/mlink.adlon.isode.net/adlon.isode.net"));

            WindowsServicePrincipalName spn2("mlink.adlon.isode.net");

            spn2.setInstancePort(55222);
            CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net:55222"));

            spn2.setInstancePort(0);
            CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net"));
        }

        void testReferrer() {
            WindowsServicePrincipalName spn("127.0.0.1");

            spn.setReferrer("referrer.net");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/127.0.0.1/referrer.net"));

            spn.setInstancePort(6222);
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/127.0.0.1:6222/referrer.net"));

            spn.setReferrer("हिन्दी.net");
            CPPUNIT_ASSERT_EQUAL(spn.toString(), std::string("xmpp/127.0.0.1:6222/हिन्दी.net"));

            try {
                spn.setReferrer("referrer/net"); /* Foward slash not allowed */
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }

            try {
                spn.setReferrer(""); /* seems like you must have referrer with an IP */
                spn.toString();
                CPPUNIT_ASSERT(false);
            } catch (std::runtime_error) {
                /* expected */
            }

            WindowsServicePrincipalName spn2("mlink.adlon.isode.net");

            spn2.setReferrer("referrer.net"); /* Referrer ignored if service name is not IP */
            CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net"));

            spn2.setReferrer("referrer/net"); /* Referrer ignored if service name is not IP */
            CPPUNIT_ASSERT_EQUAL(spn2.toString(), std::string("xmpp/mlink.adlon.isode.net"));

            WindowsServicePrincipalName spn3("adlon.isode.net");

            spn3.setInstanceName("mlink.adlon.isode.net");
            spn3.setInstancePort(6222);
            spn3.setReferrer("referrer.net"); /* Referrer ignored if service name is not IP */
            CPPUNIT_ASSERT_EQUAL(spn3.toString(), std::string("xmpp/mlink.adlon.isode.net:6222/adlon.isode.net"));

        }
};

CPPUNIT_TEST_SUITE_REGISTRATION(WindowsServicePrincipalNameTest);