summaryrefslogtreecommitdiff
path: root/tests/test_stanza_xep_0047.py
blob: 6aa2314b524510046016b430d8e03c578fbbb273 (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
from sleekxmpp.test import *
from sleekxmpp.plugins.xep_0047 import Data


class TestIBB(SleekTest):

    def setUp(self):
        register_stanza_plugin(Iq, Data)

    def testInvalidBase64MidEqual(self):
        """
        Test detecting invalid base64 data with = inside the
        character data instead of at the end.
        """
        iq = Iq(xml=ET.fromstring("""
          <iq type="set" id="0" to="tester@localhost">
            <data xmlns="http://jabber.org/protocol/ibb" seq="0">
              ABC=DEFGH
            </data>
          </iq>
        """))

        errored = False

        try:
            data = iq['ibb_data']['data']
        except XMPPError:
            errored = True

        self.assertTrue(errored, "ABC=DEFGH did not raise base64 error")

    def testInvalidBase64PrefixEqual(self):
        """
        Test detecting invalid base64 data with = as a prefix
        to the character data.
        """
        iq = Iq(xml=ET.fromstring("""
          <iq type="set" id="0" to="tester@localhost">
            <data xmlns="http://jabber.org/protocol/ibb" seq="0">
              =ABCDEFGH
            </data>
          </iq>
        """))

        errored = False

        try:
            data = iq['ibb_data']['data']
        except XMPPError:
            errored = True

        self.assertTrue(errored, "=ABCDEFGH did not raise base64 error")

    def testInvalidBase64Alphabet(self):
        """
        Test detecting invalid base64 data with characters
        outside of the base64 alphabet.
        """
        iq = Iq(xml=ET.fromstring("""
          <iq type="set" id="0" to="tester@localhost">
            <data xmlns="http://jabber.org/protocol/ibb" seq="0">
              ABCD?EFGH
            </data>
          </iq>
        """))

        errored = False

        try:
            data = iq['ibb_data']['data']
        except XMPPError:
            errored = True

        self.assertTrue(errored, "ABCD?EFGH did not raise base64 error")

    def testConvertData(self):
        """Test that data is converted to base64"""
        iq = Iq()
        iq['type'] = 'set'
        iq['ibb_data']['seq'] = 0
        iq['ibb_data']['data'] = 'sleekxmpp'

        self.check(iq, """
          <iq type="set">
            <data xmlns="http://jabber.org/protocol/ibb" seq="0">c2xlZWt4bXBw</data>
          </iq>
        """)


suite = unittest.TestLoader().loadTestsFromTestCase(TestIBB)