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

#include <QA/Checker/IO.h>

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

#include <Swiften/Base/ByteArray.h>
#include <Swiften/LinkLocal/LinkLocalServiceInfo.h>

using namespace Swift;

class LinkLocalServiceInfoTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(LinkLocalServiceInfoTest);
        CPPUNIT_TEST(testGetTXTRecord);
        CPPUNIT_TEST(testCreateFromTXTRecord);
        CPPUNIT_TEST(testCreateFromTXTRecord_InvalidSize);
        CPPUNIT_TEST(testGetTXTRecordCreateFromTXTRecord_RoundTrip);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testGetTXTRecord() {
            LinkLocalServiceInfo info;
            info.setFirstName("Remko");
            info.setLastName("Tron\xc3\xe7on");
            info.setStatus(LinkLocalServiceInfo::Away);

            auto txtRecord = info.toTXTRecord();
            CPPUNIT_ASSERT(txtRecord);
            CPPUNIT_ASSERT_EQUAL(createByteArray("\x09txtvers=1\x09" + std::string("1st=Remko\x0dlast=Tron\xc3\xe7on\x0bstatus=away")), *txtRecord);
        }

        void testCreateFromTXTRecord() {
            LinkLocalServiceInfo info = LinkLocalServiceInfo::createFromTXTRecord(createByteArray("\x09txtvers=1\x09" + std::string("1st=Remko\x0dlast=Tron\xc3\xe7on\x0bstatus=away")));

            CPPUNIT_ASSERT_EQUAL(std::string("Remko"), info.getFirstName());
            CPPUNIT_ASSERT_EQUAL(std::string("Tron\xc3\xe7on"), info.getLastName());
            CPPUNIT_ASSERT_EQUAL(LinkLocalServiceInfo::Away, info.getStatus());
        }

        void testCreateFromTXTRecord_InvalidSize() {
            LinkLocalServiceInfo info = LinkLocalServiceInfo::createFromTXTRecord(createByteArray("\x10last=a"));

            CPPUNIT_ASSERT_EQUAL(std::string("a"), info.getLastName());
        }

        void testGetTXTRecordCreateFromTXTRecord_RoundTrip() {
            LinkLocalServiceInfo info;
            info.setFirstName("Remko");
            info.setLastName("Tron\xc3\xe7on");
            info.setEMail("remko-email@swift.im");
            info.setJID(JID("remko-jid@swift.im"));
            info.setMessage("I'm busy");
            info.setNick("el-tramo");
            info.setStatus(LinkLocalServiceInfo::DND);
            info.setPort(1234);

            auto txtRecord = info.toTXTRecord();
            CPPUNIT_ASSERT(txtRecord);
            LinkLocalServiceInfo info2 = LinkLocalServiceInfo::createFromTXTRecord(*txtRecord);
            CPPUNIT_ASSERT_EQUAL(info.getFirstName(), info2.getFirstName());
            CPPUNIT_ASSERT_EQUAL(info.getLastName(), info2.getLastName());
            CPPUNIT_ASSERT_EQUAL(info.getEMail(), info2.getEMail());
            CPPUNIT_ASSERT_EQUAL(info.getJID(), info2.getJID());
            CPPUNIT_ASSERT_EQUAL(info.getMessage(), info2.getMessage());
            CPPUNIT_ASSERT_EQUAL(info.getNick(), info2.getNick());
            CPPUNIT_ASSERT(info.getStatus() == info2.getStatus());
            CPPUNIT_ASSERT(info.getPort() == info2.getPort());
        }

        void testToTXTRecordWithInvalidParameter() {
            LinkLocalServiceInfo info;
            info.setFirstName(std::string(256, 'x'));
            auto txtRecord = info.toTXTRecord();
            CPPUNIT_ASSERT(!txtRecord);
        }
};

CPPUNIT_TEST_SUITE_REGISTRATION(LinkLocalServiceInfoTest);