blob: e40e95cb1271fcf4679b21a96d7f169a184da819 [file] [log] [blame]
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import dbus, logging
from autotest_lib.client.common_lib import error
sample = {
'pdu' :
'parsed' :
{'text' : 'Test',
'number' : '+16175046925',
'timestamp' : '110908141801-04',
'smsc' : '+14044550010'
sample_multipart = {
'pdu' :
'parsed' :
{'text' : 'Test of some long text but without any difficult characters'
' included in the message. This needs to be over the length threshold'
' for the local software to do the split.',
'number' : '+16175046925',
'timestamp' : '110919163047-04',
'smsc' : '+12063130028'
class SmsStore(object):
'''SMS content management - this maintains an internal model of the
index->PDU mapping that the fakemodem program should be returning so
that tests can add and remove individual PDUs and handles generating
the correct set of responses, including the complete SMS list.
def __init__(self, fakemodem):
self.fakemodem = fakemodem
self.smsdict = {}
self.fakemodem.SetResponse('\+CMGR=', '', '+CMS ERROR: 321')
self.fakemodem.SetResponse('\+CMGD=', '', '+CMS ERROR: 321')
def sms_insert(self, index, pdu):
'''Add a SMS to the fake modem's list.'''
smsc_len = int(pdu[0:1], 16)
mlen = len(pdu)/2 - smsc_len - 1
self.fakemodem.SetResponse('\+CMGD=%d' % (index), '', '')
self.fakemodem.SetResponse('\+CMGR=%d' % (index),
'+CMGR: 1,,%d\r\n%s' % (mlen, pdu), '')
self.fakemodem.SetResponse('\+CMGR=', '', '+CMS ERROR: 321')
self.fakemodem.SetResponse('\+CMGD=', '', '+CMS ERROR: 321')
self.smsdict[index] = pdu
def sms_receive(self, index, pdu):
'''Add a SMS to the fake modem's list, like sms_insert(), and generate
an unsolicited new-sms message.'''
self.sms_insert(index, pdu)
self.fakemodem.SendUnsolicited('+CMTI: "ME",%d'%(index))
def sms_remove(self, index):
'''Remove a SMS from the fake modem's list'''
self.fakemodem.RemoveResponse('\+CMGR=%d' % (index))
self.fakemodem.RemoveResponse('\+CMGD=%d' % (index))
del self.smsdict[index]
def _sms_regen_list(self):
response = ''
keys = self.smsdict.keys()
for i in keys:
pdu = self.smsdict[i]
smsc_len = int(pdu[0:1],16)
mlen = len(pdu)/2 - smsc_len - 1
response = response + '+CMGL: %d,1,,%d\r\n%s\r\n' % (i, mlen, pdu)
self.fakemodem.SetResponse('\+CMGL=4', response, '')
class SmsTest(object):
def __init__(self, gsmsms):
self.gsmsms = gsmsms
def compare(self, expected, got):
'''Compare two SMS dictionaries, discounting the index number if
not specified in the first.'''
if expected == got:
return True
if 'index' in expected:
return False
if 'index' not in got:
return False
got = dict(got)
del got['index']
return expected == got
def compare_list(self, expected_list, got_list):
if len(expected_list) != len(got_list):
return False
# There must be a more Pythonic way to do this
for (expected,got) in zip(expected_list, got_list):
if, got) == False:
return False
return True
def test_get(self, index, expected):
sms = self.gsmsms.Get(index)
except dbus.DBusException, db:
if expected is not None:
if expected is None:'Got %s' % sms)
raise error.TestFail('SMS.Get(%d) succeeded unexpectedly' %
if, sms) == False:'Got %s, expected %s' % (sms, expected))
raise error.TestFail('SMS.Get(%d) did not match expected values' %
def test_delete(self, index, expected_success):
if expected_success == False:
raise error.TestFail('SMS.Delete(%d) succeeded unexpectedly' %
except dbus.DBusException, db:
if expected_success:
def test_list(self, expected_list):
sms_list = self.gsmsms.List()
if self.compare_list(expected_list, sms_list) == False:'Got %s, expected %s' % (sms_list, expected_list))
raise error.TestFail('SMS.List() did not match expected values')
def test_has_none(self):
'''Test that the SMS interface has no messages.'''
self.test_get(1, None)
self.test_delete(1, False)
self.test_delete(2, False)
def test_has_one(self, parsed_sms):
'''Test that the SMS interface has exactly one message at index 1
As a side effect, deletes the message.'''
self.test_get(1, parsed_sms)
self.test_get(2, None)
self.test_delete(2, False)
self.test_delete(1, True)