2013-11-21 10:46:49 -04:00
|
|
|
# Copyright (C) 2003-2013 Python Software Foundation
|
2003-02-25 08:58:58 -04:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import plistlib
|
|
|
|
import os
|
2004-10-26 08:02:08 -03:00
|
|
|
import datetime
|
2013-11-21 10:46:49 -04:00
|
|
|
import codecs
|
|
|
|
import binascii
|
|
|
|
import collections
|
2014-01-15 06:32:35 -04:00
|
|
|
import struct
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2013-11-21 10:46:49 -04:00
|
|
|
from io import BytesIO
|
2003-02-25 08:58:58 -04:00
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
ALL_FORMATS=(plistlib.FMT_XML, plistlib.FMT_BINARY)
|
2004-10-26 08:02:08 -03:00
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
# The testdata is generated using Mac/Tools/plistlib_generate_testdata.py
|
|
|
|
# (which using PyObjC to control the Cocoa classes for generating plists)
|
|
|
|
TESTDATA={
|
|
|
|
plistlib.FMT_XML: binascii.a2b_base64(b'''
|
|
|
|
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NU
|
|
|
|
WVBFIHBsaXN0IFBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VO
|
|
|
|
IiAiaHR0cDovL3d3dy5hcHBsZS5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4w
|
2014-01-15 06:32:35 -04:00
|
|
|
LmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo8ZGljdD4KCTxrZXk+YUJp
|
|
|
|
Z0ludDwva2V5PgoJPGludGVnZXI+OTIyMzM3MjAzNjg1NDc3NTc2NDwvaW50
|
2014-02-06 06:19:18 -04:00
|
|
|
ZWdlcj4KCTxrZXk+YUJpZ0ludDI8L2tleT4KCTxpbnRlZ2VyPjkyMjMzNzIw
|
|
|
|
MzY4NTQ3NzU4NTI8L2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zxsb3RzIG9mIGJpbmFyeSBn
|
|
|
|
dW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDTTxiaW5hcnkgZ3Vu
|
|
|
|
az5fEBdUaGF0IHdhcyBhIHVuaWNvZGUga2V5LgAIACsAMwA8AEIASABPAFUA
|
|
|
|
ZwB0AHwAiACUAJoApQCuALsAygDTAOQA7QD4AQQBDwEdASsBNgE3ATgBTwFn
|
|
|
|
AW4BcAFyAXQBdgF/AYMBhQGHAYwBlQGbAZ0BnwGhAaUBpwGwAbkBwAHBAcIB
|
|
|
|
xQHHAsQC0gAAAAAAAAIBAAAAAAAAADkAAAAAAAAAAAAAAAAAAALs'''),
|
2013-11-21 10:46:49 -04:00
|
|
|
}
|
2004-10-26 08:02:08 -03:00
|
|
|
|
|
|
|
|
2003-02-25 08:58:58 -04:00
|
|
|
class TestPlistlib(unittest.TestCase):
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
2008-05-20 18:35:26 -03:00
|
|
|
os.unlink(support.TESTFN)
|
2003-02-25 08:58:58 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
def _create(self, fmt=None):
|
2004-10-26 08:02:08 -03:00
|
|
|
pl = dict(
|
2003-02-25 08:58:58 -04:00
|
|
|
aString="Doodah",
|
2004-10-26 08:02:08 -03:00
|
|
|
aList=["A", "B", 12, 32.5, [1, 2, 3]],
|
|
|
|
aFloat = 0.5,
|
2003-02-25 08:58:58 -04:00
|
|
|
anInt = 728,
|
2014-01-15 06:32:35 -04:00
|
|
|
aBigInt = 2 ** 63 - 44,
|
2014-02-06 06:19:18 -04:00
|
|
|
aBigInt2 = 2 ** 63 + 44,
|
2014-01-15 06:32:35 -04:00
|
|
|
aNegativeInt = -5,
|
|
|
|
aNegativeBigInt = -80000000000,
|
2004-10-26 08:02:08 -03:00
|
|
|
aDict=dict(
|
|
|
|
anotherString="<hello & 'hi' there!>",
|
2007-05-02 16:09:54 -03:00
|
|
|
aUnicodeValue='M\xe4ssig, Ma\xdf',
|
2003-02-25 08:58:58 -04:00
|
|
|
aTrueValue=True,
|
|
|
|
aFalseValue=False,
|
2004-10-26 08:02:08 -03:00
|
|
|
deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]),
|
2003-02-25 08:58:58 -04:00
|
|
|
),
|
2013-11-21 10:46:49 -04:00
|
|
|
someData = b"<binary gunk>",
|
|
|
|
someMoreData = b"<lots of binary gunk>\0\1\2\3" * 10,
|
|
|
|
nestedData = [b"<lots of binary gunk>\0\1\2\3" * 10],
|
2004-10-26 08:02:08 -03:00
|
|
|
aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
|
2012-05-29 07:04:54 -03:00
|
|
|
anEmptyDict = dict(),
|
|
|
|
anEmptyList = list()
|
2003-02-25 08:58:58 -04:00
|
|
|
)
|
2007-05-02 16:09:54 -03:00
|
|
|
pl['\xc5benraa'] = "That was a unicode key."
|
2003-02-25 08:58:58 -04:00
|
|
|
return pl
|
2003-03-07 13:30:48 -04:00
|
|
|
|
2003-02-25 08:58:58 -04:00
|
|
|
def test_create(self):
|
|
|
|
pl = self._create()
|
|
|
|
self.assertEqual(pl["aString"], "Doodah")
|
|
|
|
self.assertEqual(pl["aDict"]["aFalseValue"], False)
|
|
|
|
|
|
|
|
def test_io(self):
|
|
|
|
pl = self._create()
|
2013-11-21 10:46:49 -04:00
|
|
|
with open(support.TESTFN, 'wb') as fp:
|
|
|
|
plistlib.dump(pl, fp)
|
|
|
|
|
|
|
|
with open(support.TESTFN, 'rb') as fp:
|
|
|
|
pl2 = plistlib.load(fp)
|
|
|
|
|
2003-03-07 13:30:48 -04:00
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
2003-02-25 08:58:58 -04:00
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(AttributeError, plistlib.dump, pl, 'filename')
|
|
|
|
self.assertRaises(AttributeError, plistlib.load, 'filename')
|
|
|
|
|
2014-01-15 06:32:35 -04:00
|
|
|
def test_invalid_type(self):
|
|
|
|
pl = [ object() ]
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt)
|
|
|
|
|
|
|
|
def test_int(self):
|
|
|
|
for pl in [0, 2**8-1, 2**8, 2**16-1, 2**16, 2**32-1, 2**32,
|
2014-02-06 06:19:18 -04:00
|
|
|
2**63-1, 2**64-1, 1, -2**63]:
|
2014-01-15 06:32:35 -04:00
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(pl=pl, fmt=fmt):
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertIsInstance(pl2, int)
|
|
|
|
self.assertEqual(pl, pl2)
|
|
|
|
data2 = plistlib.dumps(pl2, fmt=fmt)
|
|
|
|
self.assertEqual(data, data2)
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
for pl in (2 ** 64 + 1, 2 ** 127-1, -2**64, -2 ** 127):
|
|
|
|
with self.subTest(pl=pl, fmt=fmt):
|
|
|
|
self.assertRaises(OverflowError, plistlib.dumps,
|
|
|
|
pl, fmt=fmt)
|
2013-11-21 10:46:49 -04:00
|
|
|
|
2007-08-07 11:26:40 -03:00
|
|
|
def test_bytes(self):
|
2004-10-26 08:02:08 -03:00
|
|
|
pl = self._create()
|
2013-11-21 10:46:49 -04:00
|
|
|
data = plistlib.dumps(pl)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertNotIsInstance(pl, plistlib._InternalDict)
|
2004-10-26 08:02:08 -03:00
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
2013-11-21 10:46:49 -04:00
|
|
|
data2 = plistlib.dumps(pl2)
|
2004-10-26 08:02:08 -03:00
|
|
|
self.assertEqual(data, data2)
|
|
|
|
|
2013-04-23 08:47:22 -03:00
|
|
|
def test_indentation_array(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
data = [[[[[[[[{'test': b'aaaaaa'}]]]]]]]]
|
|
|
|
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
|
2013-04-23 08:47:22 -03:00
|
|
|
|
|
|
|
def test_indentation_dict(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': b'aaaaaa'}}}}}}}}}
|
|
|
|
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
|
2013-04-23 08:47:22 -03:00
|
|
|
|
|
|
|
def test_indentation_dict_mix(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
data = {'1': {'2': [{'3': [[[[[{'test': b'aaaaaa'}]]]]]}]}}
|
|
|
|
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
|
2013-04-23 08:47:22 -03:00
|
|
|
|
2004-10-26 08:02:08 -03:00
|
|
|
def test_appleformatting(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
for use_builtin_types in (True, False):
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt, use_builtin_types=use_builtin_types):
|
|
|
|
pl = plistlib.loads(TESTDATA[fmt],
|
|
|
|
use_builtin_types=use_builtin_types)
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt)
|
|
|
|
self.assertEqual(data, TESTDATA[fmt],
|
|
|
|
"generated data was not identical to Apple's output")
|
|
|
|
|
2004-10-26 08:02:08 -03:00
|
|
|
|
|
|
|
def test_appleformattingfromliteral(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
self.maxDiff = None
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
pl = self._create(fmt=fmt)
|
2014-07-23 12:49:31 -03:00
|
|
|
pl2 = plistlib.loads(TESTDATA[fmt], fmt=fmt)
|
|
|
|
self.assertEqual(dict(pl), dict(pl2),
|
|
|
|
"generated data was not identical to Apple's output")
|
2013-11-21 10:46:49 -04:00
|
|
|
pl2 = plistlib.loads(TESTDATA[fmt])
|
|
|
|
self.assertEqual(dict(pl), dict(pl2),
|
|
|
|
"generated data was not identical to Apple's output")
|
2004-10-26 08:02:08 -03:00
|
|
|
|
2007-08-07 11:26:40 -03:00
|
|
|
def test_bytesio(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
b = BytesIO()
|
|
|
|
pl = self._create(fmt=fmt)
|
|
|
|
plistlib.dump(pl, b, fmt=fmt)
|
2014-07-23 12:49:31 -03:00
|
|
|
pl2 = plistlib.load(BytesIO(b.getvalue()), fmt=fmt)
|
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
2013-11-21 10:46:49 -04:00
|
|
|
pl2 = plistlib.load(BytesIO(b.getvalue()))
|
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
|
|
|
|
|
|
|
def test_keysort_bytesio(self):
|
|
|
|
pl = collections.OrderedDict()
|
|
|
|
pl['b'] = 1
|
|
|
|
pl['a'] = 2
|
|
|
|
pl['c'] = 3
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
for sort_keys in (False, True):
|
|
|
|
with self.subTest(fmt=fmt, sort_keys=sort_keys):
|
|
|
|
b = BytesIO()
|
|
|
|
|
|
|
|
plistlib.dump(pl, b, fmt=fmt, sort_keys=sort_keys)
|
|
|
|
pl2 = plistlib.load(BytesIO(b.getvalue()),
|
|
|
|
dict_type=collections.OrderedDict)
|
|
|
|
|
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
|
|
|
if sort_keys:
|
|
|
|
self.assertEqual(list(pl2.keys()), ['a', 'b', 'c'])
|
|
|
|
else:
|
|
|
|
self.assertEqual(list(pl2.keys()), ['b', 'a', 'c'])
|
|
|
|
|
|
|
|
def test_keysort(self):
|
|
|
|
pl = collections.OrderedDict()
|
|
|
|
pl['b'] = 1
|
|
|
|
pl['a'] = 2
|
|
|
|
pl['c'] = 3
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
for sort_keys in (False, True):
|
|
|
|
with self.subTest(fmt=fmt, sort_keys=sort_keys):
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt, sort_keys=sort_keys)
|
|
|
|
pl2 = plistlib.loads(data, dict_type=collections.OrderedDict)
|
|
|
|
|
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
|
|
|
if sort_keys:
|
|
|
|
self.assertEqual(list(pl2.keys()), ['a', 'b', 'c'])
|
|
|
|
else:
|
|
|
|
self.assertEqual(list(pl2.keys()), ['b', 'a', 'c'])
|
|
|
|
|
|
|
|
def test_keys_no_string(self):
|
|
|
|
pl = { 42: 'aNumber' }
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt)
|
|
|
|
|
|
|
|
b = BytesIO()
|
|
|
|
self.assertRaises(TypeError, plistlib.dump, pl, b, fmt=fmt)
|
|
|
|
|
|
|
|
def test_skipkeys(self):
|
|
|
|
pl = {
|
|
|
|
42: 'aNumber',
|
|
|
|
'snake': 'aWord',
|
|
|
|
}
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
data = plistlib.dumps(
|
|
|
|
pl, fmt=fmt, skipkeys=True, sort_keys=False)
|
|
|
|
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertEqual(pl2, {'snake': 'aWord'})
|
|
|
|
|
|
|
|
fp = BytesIO()
|
|
|
|
plistlib.dump(
|
|
|
|
pl, fp, fmt=fmt, skipkeys=True, sort_keys=False)
|
|
|
|
data = fp.getvalue()
|
|
|
|
pl2 = plistlib.loads(fp.getvalue())
|
|
|
|
self.assertEqual(pl2, {'snake': 'aWord'})
|
|
|
|
|
|
|
|
def test_tuple_members(self):
|
|
|
|
pl = {
|
|
|
|
'first': (1, 2),
|
|
|
|
'second': (1, 2),
|
|
|
|
'third': (3, 4),
|
|
|
|
}
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertEqual(pl2, {
|
|
|
|
'first': [1, 2],
|
|
|
|
'second': [1, 2],
|
|
|
|
'third': [3, 4],
|
|
|
|
})
|
|
|
|
self.assertIsNot(pl2['first'], pl2['second'])
|
|
|
|
|
|
|
|
def test_list_members(self):
|
|
|
|
pl = {
|
|
|
|
'first': [1, 2],
|
|
|
|
'second': [1, 2],
|
|
|
|
'third': [3, 4],
|
|
|
|
}
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertEqual(pl2, {
|
|
|
|
'first': [1, 2],
|
|
|
|
'second': [1, 2],
|
|
|
|
'third': [3, 4],
|
|
|
|
})
|
|
|
|
self.assertIsNot(pl2['first'], pl2['second'])
|
|
|
|
|
|
|
|
def test_dict_members(self):
|
|
|
|
pl = {
|
|
|
|
'first': {'a': 1},
|
|
|
|
'second': {'a': 1},
|
|
|
|
'third': {'b': 2 },
|
|
|
|
}
|
|
|
|
|
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
data = plistlib.dumps(pl, fmt=fmt)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertEqual(pl2, {
|
|
|
|
'first': {'a': 1},
|
|
|
|
'second': {'a': 1},
|
|
|
|
'third': {'b': 2 },
|
|
|
|
})
|
|
|
|
self.assertIsNot(pl2['first'], pl2['second'])
|
2003-07-01 17:22:30 -03:00
|
|
|
|
2004-11-12 04:34:32 -04:00
|
|
|
def test_controlcharacters(self):
|
2004-11-12 05:36:12 -04:00
|
|
|
for i in range(128):
|
|
|
|
c = chr(i)
|
|
|
|
testString = "string containing %s" % c
|
|
|
|
if i >= 32 or c in "\r\n\t":
|
|
|
|
# \r, \n and \t are the only legal control chars in XML
|
2013-11-21 10:46:49 -04:00
|
|
|
plistlib.dumps(testString, fmt=plistlib.FMT_XML)
|
2004-11-12 05:36:12 -04:00
|
|
|
else:
|
|
|
|
self.assertRaises(ValueError,
|
2013-11-21 10:46:49 -04:00
|
|
|
plistlib.dumps,
|
2004-11-12 05:36:12 -04:00
|
|
|
testString)
|
2004-11-12 04:34:32 -04:00
|
|
|
|
|
|
|
def test_nondictroot(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
for fmt in ALL_FORMATS:
|
|
|
|
with self.subTest(fmt=fmt):
|
|
|
|
test1 = "abc"
|
|
|
|
test2 = [1, 2, 3, "abc"]
|
|
|
|
result1 = plistlib.loads(plistlib.dumps(test1, fmt=fmt))
|
|
|
|
result2 = plistlib.loads(plistlib.dumps(test2, fmt=fmt))
|
|
|
|
self.assertEqual(test1, result1)
|
|
|
|
self.assertEqual(test2, result2)
|
2003-02-25 08:58:58 -04:00
|
|
|
|
2011-05-28 06:19:19 -03:00
|
|
|
def test_invalidarray(self):
|
|
|
|
for i in ["<key>key inside an array</key>",
|
|
|
|
"<key>key inside an array2</key><real>3</real>",
|
|
|
|
"<true/><key>key inside an array3</key>"]:
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(ValueError, plistlib.loads,
|
2011-05-28 06:19:19 -03:00
|
|
|
("<plist><array>%s</array></plist>"%i).encode())
|
|
|
|
|
|
|
|
def test_invaliddict(self):
|
|
|
|
for i in ["<key><true/>k</key><string>compound key</string>",
|
|
|
|
"<key>single key</key>",
|
|
|
|
"<string>missing key</string>",
|
|
|
|
"<key>k1</key><string>v1</string><real>5.3</real>"
|
|
|
|
"<key>k1</key><key>k2</key><string>double key</string>"]:
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(ValueError, plistlib.loads,
|
2011-05-28 06:19:19 -03:00
|
|
|
("<plist><dict>%s</dict></plist>"%i).encode())
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(ValueError, plistlib.loads,
|
2011-05-28 06:19:19 -03:00
|
|
|
("<plist><array><dict>%s</dict></array></plist>"%i).encode())
|
|
|
|
|
|
|
|
def test_invalidinteger(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(ValueError, plistlib.loads,
|
2011-05-28 06:19:19 -03:00
|
|
|
b"<plist><integer>not integer</integer></plist>")
|
|
|
|
|
|
|
|
def test_invalidreal(self):
|
2013-11-21 10:46:49 -04:00
|
|
|
self.assertRaises(ValueError, plistlib.loads,
|
2011-05-28 06:19:19 -03:00
|
|
|
b"<plist><integer>not real</integer></plist>")
|
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
def test_xml_encodings(self):
|
|
|
|
base = TESTDATA[plistlib.FMT_XML]
|
|
|
|
|
|
|
|
for xml_encoding, encoding, bom in [
|
|
|
|
(b'utf-8', 'utf-8', codecs.BOM_UTF8),
|
|
|
|
(b'utf-16', 'utf-16-le', codecs.BOM_UTF16_LE),
|
|
|
|
(b'utf-16', 'utf-16-be', codecs.BOM_UTF16_BE),
|
|
|
|
# Expat does not support UTF-32
|
|
|
|
#(b'utf-32', 'utf-32-le', codecs.BOM_UTF32_LE),
|
|
|
|
#(b'utf-32', 'utf-32-be', codecs.BOM_UTF32_BE),
|
|
|
|
]:
|
|
|
|
|
|
|
|
pl = self._create(fmt=plistlib.FMT_XML)
|
|
|
|
with self.subTest(encoding=encoding):
|
|
|
|
data = base.replace(b'UTF-8', xml_encoding)
|
|
|
|
data = bom + data.decode('utf-8').encode(encoding)
|
|
|
|
pl2 = plistlib.loads(data)
|
|
|
|
self.assertEqual(dict(pl), dict(pl2))
|
|
|
|
|
2014-05-23 10:13:33 -03:00
|
|
|
def test_nonstandard_refs_size(self):
|
|
|
|
# Issue #21538: Refs and offsets are 24-bit integers
|
|
|
|
data = (b'bplist00'
|
|
|
|
b'\xd1\x00\x00\x01\x00\x00\x02QaQb'
|
|
|
|
b'\x00\x00\x08\x00\x00\x0f\x00\x00\x11'
|
|
|
|
b'\x00\x00\x00\x00\x00\x00'
|
|
|
|
b'\x03\x03'
|
|
|
|
b'\x00\x00\x00\x00\x00\x00\x00\x03'
|
|
|
|
b'\x00\x00\x00\x00\x00\x00\x00\x00'
|
|
|
|
b'\x00\x00\x00\x00\x00\x00\x00\x13')
|
|
|
|
self.assertEqual(plistlib.loads(data), {'a': 'b'})
|
|
|
|
|
2016-04-08 09:00:02 -03:00
|
|
|
def test_large_timestamp(self):
|
|
|
|
# Issue #26709: 32-bit timestamp out of range
|
|
|
|
for ts in -2**31-1, 2**31:
|
|
|
|
with self.subTest(ts=ts):
|
|
|
|
d = (datetime.datetime.utcfromtimestamp(0) +
|
|
|
|
datetime.timedelta(seconds=ts))
|
|
|
|
data = plistlib.dumps(d, fmt=plistlib.FMT_BINARY)
|
|
|
|
self.assertEqual(plistlib.loads(data), d)
|
|
|
|
|
2013-11-21 10:46:49 -04:00
|
|
|
|
|
|
|
class TestPlistlibDeprecated(unittest.TestCase):
|
|
|
|
def test_io_deprecated(self):
|
|
|
|
pl_in = {
|
|
|
|
'key': 42,
|
|
|
|
'sub': {
|
|
|
|
'key': 9,
|
|
|
|
'alt': 'value',
|
|
|
|
'data': b'buffer',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pl_out = plistlib._InternalDict({
|
|
|
|
'key': 42,
|
|
|
|
'sub': plistlib._InternalDict({
|
|
|
|
'key': 9,
|
|
|
|
'alt': 'value',
|
|
|
|
'data': plistlib.Data(b'buffer'),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addCleanup(support.unlink, support.TESTFN)
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
plistlib.writePlist(pl_in, support.TESTFN)
|
|
|
|
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
pl2 = plistlib.readPlist(support.TESTFN)
|
|
|
|
|
|
|
|
self.assertEqual(pl_out, pl2)
|
|
|
|
|
|
|
|
os.unlink(support.TESTFN)
|
|
|
|
|
|
|
|
with open(support.TESTFN, 'wb') as fp:
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
plistlib.writePlist(pl_in, fp)
|
|
|
|
|
|
|
|
with open(support.TESTFN, 'rb') as fp:
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
pl2 = plistlib.readPlist(fp)
|
|
|
|
|
|
|
|
self.assertEqual(pl_out, pl2)
|
|
|
|
|
|
|
|
def test_bytes_deprecated(self):
|
|
|
|
pl = {
|
|
|
|
'key': 42,
|
|
|
|
'sub': {
|
|
|
|
'key': 9,
|
|
|
|
'alt': 'value',
|
|
|
|
'data': b'buffer',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
data = plistlib.writePlistToBytes(pl)
|
|
|
|
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
pl2 = plistlib.readPlistFromBytes(data)
|
|
|
|
|
|
|
|
self.assertIsInstance(pl2, plistlib._InternalDict)
|
|
|
|
self.assertEqual(pl2, plistlib._InternalDict(
|
|
|
|
key=42,
|
|
|
|
sub=plistlib._InternalDict(
|
|
|
|
key=9,
|
|
|
|
alt='value',
|
|
|
|
data=plistlib.Data(b'buffer'),
|
|
|
|
)
|
|
|
|
))
|
|
|
|
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
data2 = plistlib.writePlistToBytes(pl2)
|
|
|
|
self.assertEqual(data, data2)
|
|
|
|
|
|
|
|
def test_dataobject_deprecated(self):
|
|
|
|
in_data = { 'key': plistlib.Data(b'hello') }
|
|
|
|
out_data = { 'key': b'hello' }
|
|
|
|
|
|
|
|
buf = plistlib.dumps(in_data)
|
|
|
|
|
|
|
|
cur = plistlib.loads(buf)
|
|
|
|
self.assertEqual(cur, out_data)
|
|
|
|
self.assertNotEqual(cur, in_data)
|
|
|
|
|
|
|
|
cur = plistlib.loads(buf, use_builtin_types=False)
|
|
|
|
self.assertNotEqual(cur, out_data)
|
|
|
|
self.assertEqual(cur, in_data)
|
|
|
|
|
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
cur = plistlib.readPlistFromBytes(buf)
|
|
|
|
self.assertNotEqual(cur, out_data)
|
|
|
|
self.assertEqual(cur, in_data)
|
|
|
|
|
2003-02-25 08:58:58 -04:00
|
|
|
|
|
|
|
def test_main():
|
2013-11-21 10:46:49 -04:00
|
|
|
support.run_unittest(TestPlistlib, TestPlistlibDeprecated)
|
2003-02-25 08:58:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|