summaryrefslogtreecommitdiffstats
blob: f7ed80fc435a7e0a5eb2b131074423f41466b042 (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
/*
 * Copyright (c) 2011-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/Parser/BOSHBodyExtractor.h>
#include <Swiften/Parser/PlatformXMLParserFactory.h>

using namespace Swift;

class BOSHBodyExtractorTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(BOSHBodyExtractorTest);
        CPPUNIT_TEST(testGetBody);
        CPPUNIT_TEST(testGetBody_EmptyContent);
        CPPUNIT_TEST(testGetBody_EmptyContent2);
        CPPUNIT_TEST(testGetBody_EmptyElementEmptyContent);
        CPPUNIT_TEST(testGetBody_InvalidStartTag);
        CPPUNIT_TEST(testGetBody_InvalidStartTag2);
        CPPUNIT_TEST(testGetBody_IncompleteStartTag);
        CPPUNIT_TEST(testGetBody_InvalidEndTag);
        CPPUNIT_TEST(testGetBody_InvalidEndTag2);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testGetBody() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body a1='a\"1' a2=\"a'2\" boo='bar'   >"
                        "foo <message> <body> bar"
                    "</body   >  "));

            CPPUNIT_ASSERT(testling.getBody());
            CPPUNIT_ASSERT_EQUAL(std::string("a\"1"), testling.getBody()->attributes.getAttribute("a1"));
            CPPUNIT_ASSERT_EQUAL(std::string("foo <message> <body> bar"), testling.getBody()->content);
        }

        void testGetBody_EmptyContent() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body foo='bar'/>"));

            CPPUNIT_ASSERT(testling.getBody());
            CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getBody()->attributes.getAttribute("foo"));
            CPPUNIT_ASSERT(testling.getBody()->content.empty());
        }

        void testGetBody_EmptyContent2() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body foo='bar'></body>"));

            CPPUNIT_ASSERT(testling.getBody());
            CPPUNIT_ASSERT_EQUAL(std::string("bar"), testling.getBody()->attributes.getAttribute("foo"));
            CPPUNIT_ASSERT(testling.getBody()->content.empty());
        }

        void testGetBody_EmptyElementEmptyContent() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body/>"));

            CPPUNIT_ASSERT(testling.getBody());
        }

        void testGetBody_InvalidStartTag() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<bodi></body>"));

            CPPUNIT_ASSERT(!testling.getBody());
        }

        void testGetBody_InvalidStartTag2() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<bodyy></body>"));

            CPPUNIT_ASSERT(!testling.getBody());
        }

        void testGetBody_IncompleteStartTag() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body"));

            CPPUNIT_ASSERT(!testling.getBody());
        }

        void testGetBody_InvalidEndTag() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body></bodi>"));

            CPPUNIT_ASSERT(!testling.getBody());
        }

        void testGetBody_InvalidEndTag2() {
            BOSHBodyExtractor testling(&parserFactory, createByteArray(
                    "<body><b/body>"));

            CPPUNIT_ASSERT(!testling.getBody());
        }

    private:
        PlatformXMLParserFactory parserFactory;
};

CPPUNIT_TEST_SUITE_REGISTRATION(BOSHBodyExtractorTest);