Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 1 | # Copyright (C) 2003-2013 Python Software Foundation |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
| 4 | import plistlib |
| 5 | import os |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 6 | import datetime |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 7 | import codecs |
| 8 | import binascii |
| 9 | import collections |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 10 | import struct |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 12 | from io import BytesIO |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 13 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 14 | ALL_FORMATS=(plistlib.FMT_XML, plistlib.FMT_BINARY) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 15 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 16 | # The testdata is generated using Mac/Tools/plistlib_generate_testdata.py |
| 17 | # (which using PyObjC to control the Cocoa classes for generating plists) |
| 18 | TESTDATA={ |
| 19 | plistlib.FMT_XML: binascii.a2b_base64(b''' |
| 20 | PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NU |
| 21 | WVBFIHBsaXN0IFBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VO |
| 22 | IiAiaHR0cDovL3d3dy5hcHBsZS5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4w |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0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RzkwCgljeUJ2WmlCaWFXNWhjbmtnWjNWdWF6NEFBUUlEUEd4 |
| 68 | dmRITWdiMllnWW1sdVlYSjVJR2QxYm1zK0FBRUNBdz09Cgk8L2RhdGE+Cgk8 |
| 69 | a2V5PsOFYmVucmFhPC9rZXk+Cgk8c3RyaW5nPlRoYXQgd2FzIGEgdW5pY29k |
| 70 | ZSBrZXkuPC9zdHJpbmc+CjwvZGljdD4KPC9wbGlzdD4K'''), |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 71 | plistlib.FMT_BINARY: binascii.a2b_base64(b''' |
Ronald Oussoren | 94e44a9 | 2014-02-06 11:19:18 +0100 | [diff] [blame] | 72 | YnBsaXN0MDDfEBABAgMEBQYHCAkKCwwNDg8QERITFCgpLzAxMjM0NTc2OFdh |
| 73 | QmlnSW50WGFCaWdJbnQyVWFEYXRlVWFEaWN0VmFGbG9hdFVhTGlzdF8QD2FO |
| 74 | ZWdhdGl2ZUJpZ0ludFxhTmVnYXRpdmVJbnRXYVN0cmluZ1thbkVtcHR5RGlj |
| 75 | dFthbkVtcHR5TGlzdFVhbkludFpuZXN0ZWREYXRhWHNvbWVEYXRhXHNvbWVN |
| 76 | b3JlRGF0YWcAxQBiAGUAbgByAGEAYRN/////////1BQAAAAAAAAAAIAAAAAA |
| 77 | AAAsM0GcuX30AAAA1RUWFxgZGhscHR5bYUZhbHNlVmFsdWVaYVRydWVWYWx1 |
| 78 | ZV1hVW5pY29kZVZhbHVlXWFub3RoZXJTdHJpbmdaZGVlcGVyRGljdAgJawBN |
| 79 | AOQAcwBzAGkAZwAsACAATQBhAN9fEBU8aGVsbG8gJiAnaGknIHRoZXJlIT7T |
| 80 | HyAhIiMkUWFRYlFjEBEjQEBAAAAAAACjJSYnEAEQAlR0ZXh0Iz/gAAAAAAAA |
| 81 | pSorLCMtUUFRQhAMoyUmLhADE////+1foOAAE//////////7VkRvb2RhaNCg |
| 82 | EQLYoTZPEPo8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmlu |
| 83 | YXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBv |
| 84 | ZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs |
| 85 | b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4A |
| 86 | AQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBn |
| 87 | dW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDTTxiaW5hcnkgZ3Vu |
| 88 | az5fEBdUaGF0IHdhcyBhIHVuaWNvZGUga2V5LgAIACsAMwA8AEIASABPAFUA |
| 89 | ZwB0AHwAiACUAJoApQCuALsAygDTAOQA7QD4AQQBDwEdASsBNgE3ATgBTwFn |
| 90 | AW4BcAFyAXQBdgF/AYMBhQGHAYwBlQGbAZ0BnwGhAaUBpwGwAbkBwAHBAcIB |
| 91 | xQHHAsQC0gAAAAAAAAIBAAAAAAAAADkAAAAAAAAAAAAAAAAAAALs'''), |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 92 | } |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 93 | |
| 94 | |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 95 | class TestPlistlib(unittest.TestCase): |
| 96 | |
| 97 | def tearDown(self): |
| 98 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 99 | os.unlink(support.TESTFN) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 100 | except: |
| 101 | pass |
| 102 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 103 | def _create(self, fmt=None): |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 104 | pl = dict( |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 105 | aString="Doodah", |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 106 | aList=["A", "B", 12, 32.5, [1, 2, 3]], |
| 107 | aFloat = 0.5, |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 108 | anInt = 728, |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 109 | aBigInt = 2 ** 63 - 44, |
Ronald Oussoren | 94e44a9 | 2014-02-06 11:19:18 +0100 | [diff] [blame] | 110 | aBigInt2 = 2 ** 63 + 44, |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 111 | aNegativeInt = -5, |
| 112 | aNegativeBigInt = -80000000000, |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 113 | aDict=dict( |
| 114 | anotherString="<hello & 'hi' there!>", |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 115 | aUnicodeValue='M\xe4ssig, Ma\xdf', |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 116 | aTrueValue=True, |
| 117 | aFalseValue=False, |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 118 | deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]), |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 119 | ), |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 120 | someData = b"<binary gunk>", |
| 121 | someMoreData = b"<lots of binary gunk>\0\1\2\3" * 10, |
| 122 | nestedData = [b"<lots of binary gunk>\0\1\2\3" * 10], |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 123 | aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), |
Hynek Schlawack | 52209d3 | 2012-05-29 12:04:54 +0200 | [diff] [blame] | 124 | anEmptyDict = dict(), |
| 125 | anEmptyList = list() |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 126 | ) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 127 | pl['\xc5benraa'] = "That was a unicode key." |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 128 | return pl |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 129 | |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 130 | def test_create(self): |
| 131 | pl = self._create() |
| 132 | self.assertEqual(pl["aString"], "Doodah") |
| 133 | self.assertEqual(pl["aDict"]["aFalseValue"], False) |
| 134 | |
| 135 | def test_io(self): |
| 136 | pl = self._create() |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 137 | with open(support.TESTFN, 'wb') as fp: |
| 138 | plistlib.dump(pl, fp) |
| 139 | |
| 140 | with open(support.TESTFN, 'rb') as fp: |
| 141 | pl2 = plistlib.load(fp) |
| 142 | |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 143 | self.assertEqual(dict(pl), dict(pl2)) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 144 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 145 | self.assertRaises(AttributeError, plistlib.dump, pl, 'filename') |
| 146 | self.assertRaises(AttributeError, plistlib.load, 'filename') |
| 147 | |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 148 | def test_invalid_type(self): |
| 149 | pl = [ object() ] |
| 150 | |
| 151 | for fmt in ALL_FORMATS: |
| 152 | with self.subTest(fmt=fmt): |
| 153 | self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) |
| 154 | |
| 155 | def test_int(self): |
| 156 | for pl in [0, 2**8-1, 2**8, 2**16-1, 2**16, 2**32-1, 2**32, |
Ronald Oussoren | 94e44a9 | 2014-02-06 11:19:18 +0100 | [diff] [blame] | 157 | 2**63-1, 2**64-1, 1, -2**63]: |
Ronald Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 158 | for fmt in ALL_FORMATS: |
| 159 | with self.subTest(pl=pl, fmt=fmt): |
| 160 | data = plistlib.dumps(pl, fmt=fmt) |
| 161 | pl2 = plistlib.loads(data) |
| 162 | self.assertIsInstance(pl2, int) |
| 163 | self.assertEqual(pl, pl2) |
| 164 | data2 = plistlib.dumps(pl2, fmt=fmt) |
| 165 | self.assertEqual(data, data2) |
| 166 | |
| 167 | for fmt in ALL_FORMATS: |
| 168 | for pl in (2 ** 64 + 1, 2 ** 127-1, -2**64, -2 ** 127): |
| 169 | with self.subTest(pl=pl, fmt=fmt): |
| 170 | self.assertRaises(OverflowError, plistlib.dumps, |
| 171 | pl, fmt=fmt) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 172 | |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 173 | def test_bytes(self): |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 174 | pl = self._create() |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 175 | data = plistlib.dumps(pl) |
| 176 | pl2 = plistlib.loads(data) |
| 177 | self.assertNotIsInstance(pl, plistlib._InternalDict) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 178 | self.assertEqual(dict(pl), dict(pl2)) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 179 | data2 = plistlib.dumps(pl2) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 180 | self.assertEqual(data, data2) |
| 181 | |
Ronald Oussoren | 326edfd | 2013-04-23 13:47:22 +0200 | [diff] [blame] | 182 | def test_indentation_array(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 183 | data = [[[[[[[[{'test': b'aaaaaa'}]]]]]]]] |
| 184 | self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
Ronald Oussoren | 326edfd | 2013-04-23 13:47:22 +0200 | [diff] [blame] | 185 | |
| 186 | def test_indentation_dict(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 187 | data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': b'aaaaaa'}}}}}}}}} |
| 188 | self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
Ronald Oussoren | 326edfd | 2013-04-23 13:47:22 +0200 | [diff] [blame] | 189 | |
| 190 | def test_indentation_dict_mix(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 191 | data = {'1': {'2': [{'3': [[[[[{'test': b'aaaaaa'}]]]]]}]}} |
| 192 | self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
Ronald Oussoren | 326edfd | 2013-04-23 13:47:22 +0200 | [diff] [blame] | 193 | |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 194 | def test_appleformatting(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 195 | for use_builtin_types in (True, False): |
| 196 | for fmt in ALL_FORMATS: |
| 197 | with self.subTest(fmt=fmt, use_builtin_types=use_builtin_types): |
| 198 | pl = plistlib.loads(TESTDATA[fmt], |
| 199 | use_builtin_types=use_builtin_types) |
| 200 | data = plistlib.dumps(pl, fmt=fmt) |
| 201 | self.assertEqual(data, TESTDATA[fmt], |
| 202 | "generated data was not identical to Apple's output") |
| 203 | |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 204 | |
| 205 | def test_appleformattingfromliteral(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 206 | self.maxDiff = None |
| 207 | for fmt in ALL_FORMATS: |
| 208 | with self.subTest(fmt=fmt): |
| 209 | pl = self._create(fmt=fmt) |
Serhiy Storchaka | 8966759 | 2014-07-23 18:49:31 +0300 | [diff] [blame] | 210 | pl2 = plistlib.loads(TESTDATA[fmt], fmt=fmt) |
| 211 | self.assertEqual(dict(pl), dict(pl2), |
| 212 | "generated data was not identical to Apple's output") |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 213 | pl2 = plistlib.loads(TESTDATA[fmt]) |
| 214 | self.assertEqual(dict(pl), dict(pl2), |
| 215 | "generated data was not identical to Apple's output") |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 217 | def test_bytesio(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 218 | for fmt in ALL_FORMATS: |
| 219 | with self.subTest(fmt=fmt): |
| 220 | b = BytesIO() |
| 221 | pl = self._create(fmt=fmt) |
| 222 | plistlib.dump(pl, b, fmt=fmt) |
Serhiy Storchaka | 8966759 | 2014-07-23 18:49:31 +0300 | [diff] [blame] | 223 | pl2 = plistlib.load(BytesIO(b.getvalue()), fmt=fmt) |
| 224 | self.assertEqual(dict(pl), dict(pl2)) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 225 | pl2 = plistlib.load(BytesIO(b.getvalue())) |
| 226 | self.assertEqual(dict(pl), dict(pl2)) |
| 227 | |
| 228 | def test_keysort_bytesio(self): |
| 229 | pl = collections.OrderedDict() |
| 230 | pl['b'] = 1 |
| 231 | pl['a'] = 2 |
| 232 | pl['c'] = 3 |
| 233 | |
| 234 | for fmt in ALL_FORMATS: |
| 235 | for sort_keys in (False, True): |
| 236 | with self.subTest(fmt=fmt, sort_keys=sort_keys): |
| 237 | b = BytesIO() |
| 238 | |
| 239 | plistlib.dump(pl, b, fmt=fmt, sort_keys=sort_keys) |
| 240 | pl2 = plistlib.load(BytesIO(b.getvalue()), |
| 241 | dict_type=collections.OrderedDict) |
| 242 | |
| 243 | self.assertEqual(dict(pl), dict(pl2)) |
| 244 | if sort_keys: |
| 245 | self.assertEqual(list(pl2.keys()), ['a', 'b', 'c']) |
| 246 | else: |
| 247 | self.assertEqual(list(pl2.keys()), ['b', 'a', 'c']) |
| 248 | |
| 249 | def test_keysort(self): |
| 250 | pl = collections.OrderedDict() |
| 251 | pl['b'] = 1 |
| 252 | pl['a'] = 2 |
| 253 | pl['c'] = 3 |
| 254 | |
| 255 | for fmt in ALL_FORMATS: |
| 256 | for sort_keys in (False, True): |
| 257 | with self.subTest(fmt=fmt, sort_keys=sort_keys): |
| 258 | data = plistlib.dumps(pl, fmt=fmt, sort_keys=sort_keys) |
| 259 | pl2 = plistlib.loads(data, dict_type=collections.OrderedDict) |
| 260 | |
| 261 | self.assertEqual(dict(pl), dict(pl2)) |
| 262 | if sort_keys: |
| 263 | self.assertEqual(list(pl2.keys()), ['a', 'b', 'c']) |
| 264 | else: |
| 265 | self.assertEqual(list(pl2.keys()), ['b', 'a', 'c']) |
| 266 | |
| 267 | def test_keys_no_string(self): |
| 268 | pl = { 42: 'aNumber' } |
| 269 | |
| 270 | for fmt in ALL_FORMATS: |
| 271 | with self.subTest(fmt=fmt): |
| 272 | self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) |
| 273 | |
| 274 | b = BytesIO() |
| 275 | self.assertRaises(TypeError, plistlib.dump, pl, b, fmt=fmt) |
| 276 | |
| 277 | def test_skipkeys(self): |
| 278 | pl = { |
| 279 | 42: 'aNumber', |
| 280 | 'snake': 'aWord', |
| 281 | } |
| 282 | |
| 283 | for fmt in ALL_FORMATS: |
| 284 | with self.subTest(fmt=fmt): |
| 285 | data = plistlib.dumps( |
| 286 | pl, fmt=fmt, skipkeys=True, sort_keys=False) |
| 287 | |
| 288 | pl2 = plistlib.loads(data) |
| 289 | self.assertEqual(pl2, {'snake': 'aWord'}) |
| 290 | |
| 291 | fp = BytesIO() |
| 292 | plistlib.dump( |
| 293 | pl, fp, fmt=fmt, skipkeys=True, sort_keys=False) |
| 294 | data = fp.getvalue() |
| 295 | pl2 = plistlib.loads(fp.getvalue()) |
| 296 | self.assertEqual(pl2, {'snake': 'aWord'}) |
| 297 | |
| 298 | def test_tuple_members(self): |
| 299 | pl = { |
| 300 | 'first': (1, 2), |
| 301 | 'second': (1, 2), |
| 302 | 'third': (3, 4), |
| 303 | } |
| 304 | |
| 305 | for fmt in ALL_FORMATS: |
| 306 | with self.subTest(fmt=fmt): |
| 307 | data = plistlib.dumps(pl, fmt=fmt) |
| 308 | pl2 = plistlib.loads(data) |
| 309 | self.assertEqual(pl2, { |
| 310 | 'first': [1, 2], |
| 311 | 'second': [1, 2], |
| 312 | 'third': [3, 4], |
| 313 | }) |
| 314 | self.assertIsNot(pl2['first'], pl2['second']) |
| 315 | |
| 316 | def test_list_members(self): |
| 317 | pl = { |
| 318 | 'first': [1, 2], |
| 319 | 'second': [1, 2], |
| 320 | 'third': [3, 4], |
| 321 | } |
| 322 | |
| 323 | for fmt in ALL_FORMATS: |
| 324 | with self.subTest(fmt=fmt): |
| 325 | data = plistlib.dumps(pl, fmt=fmt) |
| 326 | pl2 = plistlib.loads(data) |
| 327 | self.assertEqual(pl2, { |
| 328 | 'first': [1, 2], |
| 329 | 'second': [1, 2], |
| 330 | 'third': [3, 4], |
| 331 | }) |
| 332 | self.assertIsNot(pl2['first'], pl2['second']) |
| 333 | |
| 334 | def test_dict_members(self): |
| 335 | pl = { |
| 336 | 'first': {'a': 1}, |
| 337 | 'second': {'a': 1}, |
| 338 | 'third': {'b': 2 }, |
| 339 | } |
| 340 | |
| 341 | for fmt in ALL_FORMATS: |
| 342 | with self.subTest(fmt=fmt): |
| 343 | data = plistlib.dumps(pl, fmt=fmt) |
| 344 | pl2 = plistlib.loads(data) |
| 345 | self.assertEqual(pl2, { |
| 346 | 'first': {'a': 1}, |
| 347 | 'second': {'a': 1}, |
| 348 | 'third': {'b': 2 }, |
| 349 | }) |
| 350 | self.assertIsNot(pl2['first'], pl2['second']) |
Just van Rossum | bcc58e8 | 2003-07-01 20:22:30 +0000 | [diff] [blame] | 351 | |
Just van Rossum | 48ecacc | 2004-11-12 08:34:32 +0000 | [diff] [blame] | 352 | def test_controlcharacters(self): |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 353 | for i in range(128): |
| 354 | c = chr(i) |
| 355 | testString = "string containing %s" % c |
| 356 | if i >= 32 or c in "\r\n\t": |
| 357 | # \r, \n and \t are the only legal control chars in XML |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 358 | plistlib.dumps(testString, fmt=plistlib.FMT_XML) |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 359 | else: |
| 360 | self.assertRaises(ValueError, |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 361 | plistlib.dumps, |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 362 | testString) |
Just van Rossum | 48ecacc | 2004-11-12 08:34:32 +0000 | [diff] [blame] | 363 | |
| 364 | def test_nondictroot(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 365 | for fmt in ALL_FORMATS: |
| 366 | with self.subTest(fmt=fmt): |
| 367 | test1 = "abc" |
| 368 | test2 = [1, 2, 3, "abc"] |
| 369 | result1 = plistlib.loads(plistlib.dumps(test1, fmt=fmt)) |
| 370 | result2 = plistlib.loads(plistlib.dumps(test2, fmt=fmt)) |
| 371 | self.assertEqual(test1, result1) |
| 372 | self.assertEqual(test2, result2) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 373 | |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 374 | def test_invalidarray(self): |
| 375 | for i in ["<key>key inside an array</key>", |
| 376 | "<key>key inside an array2</key><real>3</real>", |
| 377 | "<true/><key>key inside an array3</key>"]: |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 378 | self.assertRaises(ValueError, plistlib.loads, |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 379 | ("<plist><array>%s</array></plist>"%i).encode()) |
| 380 | |
| 381 | def test_invaliddict(self): |
| 382 | for i in ["<key><true/>k</key><string>compound key</string>", |
| 383 | "<key>single key</key>", |
| 384 | "<string>missing key</string>", |
| 385 | "<key>k1</key><string>v1</string><real>5.3</real>" |
| 386 | "<key>k1</key><key>k2</key><string>double key</string>"]: |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 387 | self.assertRaises(ValueError, plistlib.loads, |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 388 | ("<plist><dict>%s</dict></plist>"%i).encode()) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 389 | self.assertRaises(ValueError, plistlib.loads, |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 390 | ("<plist><array><dict>%s</dict></array></plist>"%i).encode()) |
| 391 | |
| 392 | def test_invalidinteger(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 393 | self.assertRaises(ValueError, plistlib.loads, |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 394 | b"<plist><integer>not integer</integer></plist>") |
| 395 | |
| 396 | def test_invalidreal(self): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 397 | self.assertRaises(ValueError, plistlib.loads, |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 398 | b"<plist><integer>not real</integer></plist>") |
| 399 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 400 | def test_xml_encodings(self): |
| 401 | base = TESTDATA[plistlib.FMT_XML] |
| 402 | |
| 403 | for xml_encoding, encoding, bom in [ |
| 404 | (b'utf-8', 'utf-8', codecs.BOM_UTF8), |
| 405 | (b'utf-16', 'utf-16-le', codecs.BOM_UTF16_LE), |
| 406 | (b'utf-16', 'utf-16-be', codecs.BOM_UTF16_BE), |
| 407 | # Expat does not support UTF-32 |
| 408 | #(b'utf-32', 'utf-32-le', codecs.BOM_UTF32_LE), |
| 409 | #(b'utf-32', 'utf-32-be', codecs.BOM_UTF32_BE), |
| 410 | ]: |
| 411 | |
| 412 | pl = self._create(fmt=plistlib.FMT_XML) |
| 413 | with self.subTest(encoding=encoding): |
| 414 | data = base.replace(b'UTF-8', xml_encoding) |
| 415 | data = bom + data.decode('utf-8').encode(encoding) |
| 416 | pl2 = plistlib.loads(data) |
| 417 | self.assertEqual(dict(pl), dict(pl2)) |
| 418 | |
Serhiy Storchaka | 0652664 | 2014-05-23 16:13:33 +0300 | [diff] [blame] | 419 | def test_nonstandard_refs_size(self): |
| 420 | # Issue #21538: Refs and offsets are 24-bit integers |
| 421 | data = (b'bplist00' |
| 422 | b'\xd1\x00\x00\x01\x00\x00\x02QaQb' |
| 423 | b'\x00\x00\x08\x00\x00\x0f\x00\x00\x11' |
| 424 | b'\x00\x00\x00\x00\x00\x00' |
| 425 | b'\x03\x03' |
| 426 | b'\x00\x00\x00\x00\x00\x00\x00\x03' |
| 427 | b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 428 | b'\x00\x00\x00\x00\x00\x00\x00\x13') |
| 429 | self.assertEqual(plistlib.loads(data), {'a': 'b'}) |
| 430 | |
Serhiy Storchaka | 94ad49f | 2016-04-08 15:00:02 +0300 | [diff] [blame] | 431 | def test_large_timestamp(self): |
| 432 | # Issue #26709: 32-bit timestamp out of range |
| 433 | for ts in -2**31-1, 2**31: |
| 434 | with self.subTest(ts=ts): |
| 435 | d = (datetime.datetime.utcfromtimestamp(0) + |
| 436 | datetime.timedelta(seconds=ts)) |
| 437 | data = plistlib.dumps(d, fmt=plistlib.FMT_BINARY) |
| 438 | self.assertEqual(plistlib.loads(data), d) |
| 439 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 440 | |
| 441 | class TestPlistlibDeprecated(unittest.TestCase): |
| 442 | def test_io_deprecated(self): |
| 443 | pl_in = { |
| 444 | 'key': 42, |
| 445 | 'sub': { |
| 446 | 'key': 9, |
| 447 | 'alt': 'value', |
| 448 | 'data': b'buffer', |
| 449 | } |
| 450 | } |
| 451 | pl_out = plistlib._InternalDict({ |
| 452 | 'key': 42, |
| 453 | 'sub': plistlib._InternalDict({ |
| 454 | 'key': 9, |
| 455 | 'alt': 'value', |
| 456 | 'data': plistlib.Data(b'buffer'), |
| 457 | }) |
| 458 | }) |
| 459 | |
| 460 | self.addCleanup(support.unlink, support.TESTFN) |
| 461 | with self.assertWarns(DeprecationWarning): |
| 462 | plistlib.writePlist(pl_in, support.TESTFN) |
| 463 | |
| 464 | with self.assertWarns(DeprecationWarning): |
| 465 | pl2 = plistlib.readPlist(support.TESTFN) |
| 466 | |
| 467 | self.assertEqual(pl_out, pl2) |
| 468 | |
| 469 | os.unlink(support.TESTFN) |
| 470 | |
| 471 | with open(support.TESTFN, 'wb') as fp: |
| 472 | with self.assertWarns(DeprecationWarning): |
| 473 | plistlib.writePlist(pl_in, fp) |
| 474 | |
| 475 | with open(support.TESTFN, 'rb') as fp: |
| 476 | with self.assertWarns(DeprecationWarning): |
| 477 | pl2 = plistlib.readPlist(fp) |
| 478 | |
| 479 | self.assertEqual(pl_out, pl2) |
| 480 | |
| 481 | def test_bytes_deprecated(self): |
| 482 | pl = { |
| 483 | 'key': 42, |
| 484 | 'sub': { |
| 485 | 'key': 9, |
| 486 | 'alt': 'value', |
| 487 | 'data': b'buffer', |
| 488 | } |
| 489 | } |
| 490 | with self.assertWarns(DeprecationWarning): |
| 491 | data = plistlib.writePlistToBytes(pl) |
| 492 | |
| 493 | with self.assertWarns(DeprecationWarning): |
| 494 | pl2 = plistlib.readPlistFromBytes(data) |
| 495 | |
| 496 | self.assertIsInstance(pl2, plistlib._InternalDict) |
| 497 | self.assertEqual(pl2, plistlib._InternalDict( |
| 498 | key=42, |
| 499 | sub=plistlib._InternalDict( |
| 500 | key=9, |
| 501 | alt='value', |
| 502 | data=plistlib.Data(b'buffer'), |
| 503 | ) |
| 504 | )) |
| 505 | |
| 506 | with self.assertWarns(DeprecationWarning): |
| 507 | data2 = plistlib.writePlistToBytes(pl2) |
| 508 | self.assertEqual(data, data2) |
| 509 | |
| 510 | def test_dataobject_deprecated(self): |
| 511 | in_data = { 'key': plistlib.Data(b'hello') } |
| 512 | out_data = { 'key': b'hello' } |
| 513 | |
| 514 | buf = plistlib.dumps(in_data) |
| 515 | |
| 516 | cur = plistlib.loads(buf) |
| 517 | self.assertEqual(cur, out_data) |
Serhiy Storchaka | dd1bcdf | 2016-05-01 13:36:16 +0300 | [diff] [blame] | 518 | self.assertEqual(cur, in_data) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 519 | |
| 520 | cur = plistlib.loads(buf, use_builtin_types=False) |
Serhiy Storchaka | dd1bcdf | 2016-05-01 13:36:16 +0300 | [diff] [blame] | 521 | self.assertEqual(cur, out_data) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 522 | self.assertEqual(cur, in_data) |
| 523 | |
| 524 | with self.assertWarns(DeprecationWarning): |
| 525 | cur = plistlib.readPlistFromBytes(buf) |
Serhiy Storchaka | dd1bcdf | 2016-05-01 13:36:16 +0300 | [diff] [blame] | 526 | self.assertEqual(cur, out_data) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 527 | self.assertEqual(cur, in_data) |
| 528 | |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 529 | |
| 530 | def test_main(): |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 531 | support.run_unittest(TestPlistlib, TestPlistlibDeprecated) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 532 | |
| 533 | |
| 534 | if __name__ == '__main__': |
| 535 | test_main() |