File: test_sms.py

package info (click to toggle)
python-gammu 3.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,024 kB
  • sloc: ansic: 11,446; python: 3,323; sql: 527; makefile: 6
file content (184 lines) | stat: -rw-r--r-- 6,376 bytes parent folder | download | duplicates (2)
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
# vim: expandtab sw=4 ts=4 sts=4:
#
# Copyright © 2003 - 2018 Michal Čihař <michal@cihar.com>
#
# This file is part of python-gammu <https://wammu.eu/python-gammu/>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import binascii
import datetime
import os
import sys
import unittest

import gammu

PDU_DATA = binascii.unhexlify(
    b"079124602009999002AB098106845688F8907080517375809070805183018000"
)

MESSAGE = (
    ".........1.........2.........3.........4.........5.........6.........7"
    ".........8.........9........0.........1.........2.........3.........4"
    ".........5.........6.........7.........8.........9........0.........1"
    ".........2.........3.........4.........5.........6.........7.........8"
    ".........9........0"
)
UNICODE = (
    ".........1ě........2..ř......3...žš....4....ý....5....á....6....á....7"
    ".........8.........9........0.........1.........2.........3.........4"
    ".........5.........6.........7.........8.........9........0.........1"
    ".........2.........3.........4.........5.........6.........7.........8"
    ".........9....č...0"
)
GSM = (
    ".........1$........2..Ø......3...åÅ....4....Λ....5....Æ....6....ñ....7"
    ".........8.........9........0.........1.........2.........3.........4"
    ".........5.........6.........7.........8.........9........0.........1"
    ".........2.........3.........4.........5.........6.........7.........8"
    ".........9....¥€..0"
)


class PDUTest(unittest.TestCase):
    def setUp(self):
        if "GAMMU_DEBUG" in os.environ:
            gammu.SetDebugFile(sys.stderr)
            gammu.SetDebugLevel("textall")

    def test_decode(self):
        sms = gammu.DecodePDU(PDU_DATA)
        self.assertEqual(sms["Number"], "604865888")
        self.assertEqual(sms["Text"], "Delivered")

    def do_smstest(self, smsinfo, expected):
        # encode SMSes
        sms = gammu.EncodeSMS(smsinfo)

        # decode back SMSes
        decodedsms = gammu.DecodeSMS(sms)

        # compare text
        self.assertEqual(decodedsms["Entries"][0]["Buffer"], expected)

        # do conversion to PDU
        pdu = [gammu.EncodePDU(s) for s in sms]

        # Convert back
        pdusms = [gammu.DecodePDU(p) for p in pdu]

        # decode back SMS from PDU
        decodedsms = gammu.DecodeSMS(pdusms)

        # compare PDU results
        self.assertEqual(decodedsms["Entries"][0]["Buffer"], expected)

    def test_encode_plain(self):
        smsinfo = {"Entries": [{"ID": "ConcatenatedTextLong", "Buffer": MESSAGE}]}
        self.do_smstest(smsinfo, MESSAGE)

    def test_encode_gsm(self):
        smsinfo = {"Entries": [{"ID": "ConcatenatedTextLong", "Buffer": GSM}]}
        self.do_smstest(smsinfo, GSM)

    def test_encode_unicode(self):
        smsinfo = {
            "Entries": [{"ID": "ConcatenatedTextLong", "Buffer": UNICODE}],
            "Unicode": True,
        }
        self.do_smstest(smsinfo, UNICODE)

    def test_link(self):
        # SMS info about message
        smsinfo = {"Entries": [{"ID": "ConcatenatedTextLong", "Buffer": MESSAGE}]}

        # encode SMS
        sms = gammu.EncodeSMS(smsinfo)

        # link SMS
        linked = gammu.LinkSMS([[sms[0]], [sms[1]]], True)

        # decode back SMS
        decodedsms = gammu.DecodeSMS(linked[0])

        # compare results
        self.assertTrue(decodedsms["Entries"][0]["Buffer"], MESSAGE)

    def test_mms_decode(self):
        message = [
            {
                "RejectDuplicates": 0,
                "SMSCDateTime": datetime.datetime(2010, 7, 22, 17, 4, 11),
                "Class": -1,
                "Name": "",
                "InboxFolder": 0,
                "Text": (
                    b"\x04\x06\x03\xbe\xaf\x84\x8c\x82\x981277970059\x00\x8d\x92"
                    b"\x89\x19\x80\x16\x0433707520030/TYPE=PLMN\x00\x96yBO\x00"
                    b'\x8a\x80\x8e\x01"\x88\x04\x81\x02\x0b\xb8\x83'
                    b"http://mmsc.labmctel.fr:9090/m33\x00"
                ),
                "SMSC": {
                    "DefaultNumber": "",
                    "Format": "Text",
                    "Number": "+33700065030",
                    "Validity": "NA",
                    "Location": 0,
                    "Name": "",
                },
                "ReplaceMessage": 0,
                "Coding": "8bit",
                "Number": "33707520030",
                "DateTime": datetime.datetime(2010, 7, 1, 9, 40, 21),
                "DeliveryStatus": 0,
                "State": "UnSent",
                "MessageReference": 0,
                "Length": 99,
                "Location": 0,
                "Memory": "",
                "ReplyViaSameSMSC": 0,
                "UDH": {
                    "Text": b"\x06\x05\x04\x0b\x84#\xf0",
                    "ID16bit": -1,
                    "AllParts": -1,
                    "ID8bit": -1,
                    "PartNumber": -1,
                    "Type": "UserUDH",
                },
                "Type": "Deliver",
                "Folder": 2,
            }
        ]

        decoded = gammu.DecodeSMS(message)
        self.assertEqual(
            decoded["Entries"][0]["MMSIndicator"]["Address"],
            "http://mmsc.labmctel.fr:9090/m33",
        )

    def test_counter(self):
        self.assertEqual(gammu.SMSCounter("foobar"), (1, 154))

    def test_counter_long(self):
        self.assertEqual(
            gammu.SMSCounter(
                "foobar fjsa;kjfkasdjfkljsklfjaskdljfkljasdfkljqilui143uu51o2"
                "3rjhskdf jasdklfjasdklf jasdfkljasdlkfj;asd;lfjaskdljf431ou9"
                "83jdfaskljfklsdjdkljasfl sdfjasdfkl jafklsda"
            ),
            (2, 156),
        )