| 1 |
# Copyright (C) 2003 Python Software Foundation |
| 2 |
import copy |
| 3 |
import operator |
| 4 |
import pickle |
| 5 |
import struct |
| 6 |
import unittest |
| 7 |
import plistlib |
| 8 |
import os |
| 9 |
import sys |
| 10 |
import json |
| 11 |
import datetime |
| 12 |
import codecs |
| 13 |
import subprocess |
| 14 |
import binascii |
| 15 |
import collections |
| 16 |
import zoneinfo |
| 17 |
from test import support |
| 18 |
from test.support import os_helper |
| 19 |
from io import BytesIO |
| 20 |
|
| 21 |
from plistlib import UID |
| 22 |
|
| 23 |
ALL_FORMATS=(plistlib.FMT_XML, plistlib.FMT_BINARY) |
| 24 |
|
| 25 |
# The testdata is generated using Mac/Tools/plistlib_generate_testdata.py |
| 26 |
# (which using PyObjC to control the Cocoa classes for generating plists) |
| 27 |
TESTDATA={ |
| 28 |
plistlib.FMT_XML: binascii.a2b_base64(b''' |
| 29 |
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NU |
| 30 |
WVBFIHBsaXN0IFBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VO |
| 31 |
IiAiaHR0cDovL3d3dy5hcHBsZS5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4w |
| 32 |
LmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo8ZGljdD4KCTxrZXk+YUJp |
| 33 |
Z0ludDwva2V5PgoJPGludGVnZXI+OTIyMzM3MjAzNjg1NDc3NTc2NDwvaW50 |
| 34 |
ZWdlcj4KCTxrZXk+YUJpZ0ludDI8L2tleT4KCTxpbnRlZ2VyPjkyMjMzNzIw |
| 35 |
MzY4NTQ3NzU4NTI8L2ludGVnZXI+Cgk8a2V5PmFEYXRlPC9rZXk+Cgk8ZGF0 |
| 36 |
ZT4yMDA0LTEwLTI2VDEwOjMzOjMzWjwvZGF0ZT4KCTxrZXk+YURpY3Q8L2tl |
| 37 |
eT4KCTxkaWN0PgoJCTx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Z0BnwGhAaUBpwGwAbkBwAHBAcIB |
| 100 |
xQHHAsQC0gAAAAAAAAIBAAAAAAAAADkAAAAAAAAAAAAAAAAAAALs'''), |
| 101 |
'KEYED_ARCHIVE': binascii.a2b_base64(b''' |
| 102 |
YnBsaXN0MDDUAQIDBAUGHB1YJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVy |
| 103 |
VCR0b3ASAAGGoKMHCA9VJG51bGzTCQoLDA0OVnB5dHlwZVYkY2xhc3NZTlMu |
| 104 |
c3RyaW5nEAGAAl8QE0tleUFyY2hpdmUgVUlEIFRlc3TTEBESExQZWiRjbGFz |
| 105 |
c25hbWVYJGNsYXNzZXNbJGNsYXNzaGludHNfEBdPQ19CdWlsdGluUHl0aG9u |
| 106 |
VW5pY29kZaQVFhcYXxAXT0NfQnVpbHRpblB5dGhvblVuaWNvZGVfEBBPQ19Q |
| 107 |
eXRob25Vbmljb2RlWE5TU3RyaW5nWE5TT2JqZWN0ohobXxAPT0NfUHl0aG9u |
| 108 |
U3RyaW5nWE5TU3RyaW5nXxAPTlNLZXllZEFyY2hpdmVy0R4fVHJvb3SAAQAI |
| 109 |
ABEAGgAjAC0AMgA3ADsAQQBIAE8AVgBgAGIAZAB6AIEAjACVAKEAuwDAANoA |
| 110 |
7QD2AP8BAgEUAR0BLwEyATcAAAAAAAACAQAAAAAAAAAgAAAAAAAAAAAAAAAA |
| 111 |
AAABOQ=='''), |
| 112 |
} |
| 113 |
|
| 114 |
XML_PLIST_WITH_ENTITY=b'''\ |
| 115 |
<?xml version="1.0" encoding="UTF-8"?> |
| 116 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" [ |
| 117 |
<!ENTITY entity "replacement text"> |
| 118 |
]> |
| 119 |
<plist version="1.0"> |
| 120 |
<dict> |
| 121 |
<key>A</key> |
| 122 |
<string>&entity;</string> |
| 123 |
</dict> |
| 124 |
</plist> |
| 125 |
''' |
| 126 |
|
| 127 |
INVALID_BINARY_PLISTS = [ |
| 128 |
('too short data', |
| 129 |
b'' |
| 130 |
), |
| 131 |
('too large offset_table_offset and offset_size = 1', |
| 132 |
b'\x00\x08' |
| 133 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 134 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 135 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 136 |
b'\x00\x00\x00\x00\x00\x00\x00\x2a' |
| 137 |
), |
| 138 |
('too large offset_table_offset and nonstandard offset_size', |
| 139 |
b'\x00\x00\x00\x08' |
| 140 |
b'\x00\x00\x00\x00\x00\x00\x03\x01' |
| 141 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 142 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 143 |
b'\x00\x00\x00\x00\x00\x00\x00\x2c' |
| 144 |
), |
| 145 |
('integer overflow in offset_table_offset', |
| 146 |
b'\x00\x08' |
| 147 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 148 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 149 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 150 |
b'\xff\xff\xff\xff\xff\xff\xff\xff' |
| 151 |
), |
| 152 |
('too large top_object', |
| 153 |
b'\x00\x08' |
| 154 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 155 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 156 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 157 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 158 |
), |
| 159 |
('integer overflow in top_object', |
| 160 |
b'\x00\x08' |
| 161 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 162 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 163 |
b'\xff\xff\xff\xff\xff\xff\xff\xff' |
| 164 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 165 |
), |
| 166 |
('too large num_objects and offset_size = 1', |
| 167 |
b'\x00\x08' |
| 168 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 169 |
b'\x00\x00\x00\x00\x00\x00\x00\xff' |
| 170 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 171 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 172 |
), |
| 173 |
('too large num_objects and nonstandard offset_size', |
| 174 |
b'\x00\x00\x00\x08' |
| 175 |
b'\x00\x00\x00\x00\x00\x00\x03\x01' |
| 176 |
b'\x00\x00\x00\x00\x00\x00\x00\xff' |
| 177 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 178 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 179 |
), |
| 180 |
('extremally large num_objects (32 bit)', |
| 181 |
b'\x00\x08' |
| 182 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 183 |
b'\x00\x00\x00\x00\x7f\xff\xff\xff' |
| 184 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 185 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 186 |
), |
| 187 |
('extremally large num_objects (64 bit)', |
| 188 |
b'\x00\x08' |
| 189 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 190 |
b'\x00\x00\x00\xff\xff\xff\xff\xff' |
| 191 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 192 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 193 |
), |
| 194 |
('integer overflow in num_objects', |
| 195 |
b'\x00\x08' |
| 196 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 197 |
b'\xff\xff\xff\xff\xff\xff\xff\xff' |
| 198 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 199 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 200 |
), |
| 201 |
('offset_size = 0', |
| 202 |
b'\x00\x08' |
| 203 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 204 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 205 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 206 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 207 |
), |
| 208 |
('ref_size = 0', |
| 209 |
b'\xa1\x01\x00\x08\x0a' |
| 210 |
b'\x00\x00\x00\x00\x00\x00\x01\x00' |
| 211 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 212 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 213 |
b'\x00\x00\x00\x00\x00\x00\x00\x0b' |
| 214 |
), |
| 215 |
('too large offset', |
| 216 |
b'\x00\x2a' |
| 217 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 218 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 219 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 220 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 221 |
), |
| 222 |
('integer overflow in offset', |
| 223 |
b'\x00\xff\xff\xff\xff\xff\xff\xff\xff' |
| 224 |
b'\x00\x00\x00\x00\x00\x00\x08\x01' |
| 225 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 226 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 227 |
b'\x00\x00\x00\x00\x00\x00\x00\x09' |
| 228 |
), |
| 229 |
('too large array size', |
| 230 |
b'\xaf\x00\x01\xff\x00\x08\x0c' |
| 231 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 232 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 233 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 234 |
b'\x00\x00\x00\x00\x00\x00\x00\x0d' |
| 235 |
), |
| 236 |
('extremally large array size (32-bit)', |
| 237 |
b'\xaf\x02\x7f\xff\xff\xff\x01\x00\x08\x0f' |
| 238 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 239 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 240 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 241 |
b'\x00\x00\x00\x00\x00\x00\x00\x10' |
| 242 |
), |
| 243 |
('extremally large array size (64-bit)', |
| 244 |
b'\xaf\x03\x00\x00\x00\xff\xff\xff\xff\xff\x01\x00\x08\x13' |
| 245 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 246 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 247 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 248 |
b'\x00\x00\x00\x00\x00\x00\x00\x14' |
| 249 |
), |
| 250 |
('integer overflow in array size', |
| 251 |
b'\xaf\x03\xff\xff\xff\xff\xff\xff\xff\xff\x01\x00\x08\x13' |
| 252 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 253 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 254 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 255 |
b'\x00\x00\x00\x00\x00\x00\x00\x14' |
| 256 |
), |
| 257 |
('too large reference index', |
| 258 |
b'\xa1\x02\x00\x08\x0a' |
| 259 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 260 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 261 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 262 |
b'\x00\x00\x00\x00\x00\x00\x00\x0b' |
| 263 |
), |
| 264 |
('integer overflow in reference index', |
| 265 |
b'\xa1\xff\xff\xff\xff\xff\xff\xff\xff\x00\x08\x11' |
| 266 |
b'\x00\x00\x00\x00\x00\x00\x01\x08' |
| 267 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 268 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 269 |
b'\x00\x00\x00\x00\x00\x00\x00\x12' |
| 270 |
), |
| 271 |
('too large bytes size', |
| 272 |
b'\x4f\x00\x23\x41\x08' |
| 273 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 274 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 275 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 276 |
b'\x00\x00\x00\x00\x00\x00\x00\x0c' |
| 277 |
), |
| 278 |
('extremally large bytes size (32-bit)', |
| 279 |
b'\x4f\x02\x7f\xff\xff\xff\x41\x08' |
| 280 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 281 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 282 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 283 |
b'\x00\x00\x00\x00\x00\x00\x00\x0f' |
| 284 |
), |
| 285 |
('extremally large bytes size (64-bit)', |
| 286 |
b'\x4f\x03\x00\x00\x00\xff\xff\xff\xff\xff\x41\x08' |
| 287 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 288 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 289 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 290 |
b'\x00\x00\x00\x00\x00\x00\x00\x13' |
| 291 |
), |
| 292 |
('integer overflow in bytes size', |
| 293 |
b'\x4f\x03\xff\xff\xff\xff\xff\xff\xff\xff\x41\x08' |
| 294 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 295 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 296 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 297 |
b'\x00\x00\x00\x00\x00\x00\x00\x13' |
| 298 |
), |
| 299 |
('too large ASCII size', |
| 300 |
b'\x5f\x00\x23\x41\x08' |
| 301 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 302 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 303 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 304 |
b'\x00\x00\x00\x00\x00\x00\x00\x0c' |
| 305 |
), |
| 306 |
('extremally large ASCII size (32-bit)', |
| 307 |
b'\x5f\x02\x7f\xff\xff\xff\x41\x08' |
| 308 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 309 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 310 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 311 |
b'\x00\x00\x00\x00\x00\x00\x00\x0f' |
| 312 |
), |
| 313 |
('extremally large ASCII size (64-bit)', |
| 314 |
b'\x5f\x03\x00\x00\x00\xff\xff\xff\xff\xff\x41\x08' |
| 315 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 316 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 317 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 318 |
b'\x00\x00\x00\x00\x00\x00\x00\x13' |
| 319 |
), |
| 320 |
('integer overflow in ASCII size', |
| 321 |
b'\x5f\x03\xff\xff\xff\xff\xff\xff\xff\xff\x41\x08' |
| 322 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 323 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 324 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 325 |
b'\x00\x00\x00\x00\x00\x00\x00\x13' |
| 326 |
), |
| 327 |
('invalid ASCII', |
| 328 |
b'\x51\xff\x08' |
| 329 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 330 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 331 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 332 |
b'\x00\x00\x00\x00\x00\x00\x00\x0a' |
| 333 |
), |
| 334 |
('too large UTF-16 size', |
| 335 |
b'\x6f\x00\x13\x20\xac\x00\x08' |
| 336 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 337 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 338 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 339 |
b'\x00\x00\x00\x00\x00\x00\x00\x0e' |
| 340 |
), |
| 341 |
('extremally large UTF-16 size (32-bit)', |
| 342 |
b'\x6f\x02\x4f\xff\xff\xff\x20\xac\x00\x08' |
| 343 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 344 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 345 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 346 |
b'\x00\x00\x00\x00\x00\x00\x00\x11' |
| 347 |
), |
| 348 |
('extremally large UTF-16 size (64-bit)', |
| 349 |
b'\x6f\x03\x00\x00\x00\xff\xff\xff\xff\xff\x20\xac\x00\x08' |
| 350 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 351 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 352 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 353 |
b'\x00\x00\x00\x00\x00\x00\x00\x15' |
| 354 |
), |
| 355 |
('integer overflow in UTF-16 size', |
| 356 |
b'\x6f\x03\xff\xff\xff\xff\xff\xff\xff\xff\x20\xac\x00\x08' |
| 357 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 358 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 359 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 360 |
b'\x00\x00\x00\x00\x00\x00\x00\x15' |
| 361 |
), |
| 362 |
('invalid UTF-16', |
| 363 |
b'\x61\xd8\x00\x08' |
| 364 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 365 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 366 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 367 |
b'\x00\x00\x00\x00\x00\x00\x00\x0b' |
| 368 |
), |
| 369 |
('non-hashable key', |
| 370 |
b'\xd1\x01\x01\xa0\x08\x0b' |
| 371 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 372 |
b'\x00\x00\x00\x00\x00\x00\x00\x02' |
| 373 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 374 |
b'\x00\x00\x00\x00\x00\x00\x00\x0c' |
| 375 |
), |
| 376 |
('too large datetime (datetime overflow)', |
| 377 |
b'\x33\x42\x50\x00\x00\x00\x00\x00\x00\x08' |
| 378 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 379 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 380 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 381 |
b'\x00\x00\x00\x00\x00\x00\x00\x11' |
| 382 |
), |
| 383 |
('too large datetime (timedelta overflow)', |
| 384 |
b'\x33\x42\xe0\x00\x00\x00\x00\x00\x00\x08' |
| 385 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 386 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 387 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 388 |
b'\x00\x00\x00\x00\x00\x00\x00\x11' |
| 389 |
), |
| 390 |
('invalid datetime (Infinity)', |
| 391 |
b'\x33\x7f\xf0\x00\x00\x00\x00\x00\x00\x08' |
| 392 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 393 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 394 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 395 |
b'\x00\x00\x00\x00\x00\x00\x00\x11' |
| 396 |
), |
| 397 |
('invalid datetime (NaN)', |
| 398 |
b'\x33\x7f\xf8\x00\x00\x00\x00\x00\x00\x08' |
| 399 |
b'\x00\x00\x00\x00\x00\x00\x01\x01' |
| 400 |
b'\x00\x00\x00\x00\x00\x00\x00\x01' |
| 401 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 402 |
b'\x00\x00\x00\x00\x00\x00\x00\x11' |
| 403 |
), |
| 404 |
] |
| 405 |
|
| 406 |
|
| 407 |
class TestPlistlib(unittest.TestCase): |
| 408 |
|
| 409 |
def tearDown(self): |
| 410 |
try: |
| 411 |
os.unlink(os_helper.TESTFN) |
| 412 |
except: |
| 413 |
pass |
| 414 |
|
| 415 |
def _create(self, fmt=None): |
| 416 |
pl = dict( |
| 417 |
aString="Doodah", |
| 418 |
aList=["A", "B", 12, 32.5, [1, 2, 3]], |
| 419 |
aFloat = 0.5, |
| 420 |
anInt = 728, |
| 421 |
aBigInt = 2 ** 63 - 44, |
| 422 |
aBigInt2 = 2 ** 63 + 44, |
| 423 |
aNegativeInt = -5, |
| 424 |
aNegativeBigInt = -80000000000, |
| 425 |
aDict=dict( |
| 426 |
anotherString="<hello & 'hi' there!>", |
| 427 |
aUnicodeValue='M\xe4ssig, Ma\xdf', |
| 428 |
aTrueValue=True, |
| 429 |
aFalseValue=False, |
| 430 |
deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]), |
| 431 |
), |
| 432 |
someData = b"<binary gunk>", |
| 433 |
someMoreData = b"<lots of binary gunk>\0\1\2\3" * 10, |
| 434 |
nestedData = [b"<lots of binary gunk>\0\1\2\3" * 10], |
| 435 |
aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), |
| 436 |
anEmptyDict = dict(), |
| 437 |
anEmptyList = list() |
| 438 |
) |
| 439 |
pl['\xc5benraa'] = "That was a unicode key." |
| 440 |
return pl |
| 441 |
|
| 442 |
def test_create(self): |
| 443 |
pl = self._create() |
| 444 |
self.assertEqual(pl["aString"], "Doodah") |
| 445 |
self.assertEqual(pl["aDict"]["aFalseValue"], False) |
| 446 |
|
| 447 |
def test_io(self): |
| 448 |
pl = self._create() |
| 449 |
with open(os_helper.TESTFN, 'wb') as fp: |
| 450 |
plistlib.dump(pl, fp) |
| 451 |
|
| 452 |
with open(os_helper.TESTFN, 'rb') as fp: |
| 453 |
pl2 = plistlib.load(fp) |
| 454 |
|
| 455 |
self.assertEqual(dict(pl), dict(pl2)) |
| 456 |
|
| 457 |
self.assertRaises(AttributeError, plistlib.dump, pl, 'filename') |
| 458 |
self.assertRaises(AttributeError, plistlib.load, 'filename') |
| 459 |
|
| 460 |
def test_invalid_type(self): |
| 461 |
pl = [ object() ] |
| 462 |
|
| 463 |
for fmt in ALL_FORMATS: |
| 464 |
with self.subTest(fmt=fmt): |
| 465 |
self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) |
| 466 |
|
| 467 |
def test_invalid_uid(self): |
| 468 |
with self.assertRaises(TypeError): |
| 469 |
UID("not an int") |
| 470 |
with self.assertRaises(ValueError): |
| 471 |
UID(2 ** 64) |
| 472 |
with self.assertRaises(ValueError): |
| 473 |
UID(-19) |
| 474 |
|
| 475 |
def test_int(self): |
| 476 |
for pl in [0, 2**8-1, 2**8, 2**16-1, 2**16, 2**32-1, 2**32, |
| 477 |
2**63-1, 2**64-1, 1, -2**63]: |
| 478 |
for fmt in ALL_FORMATS: |
| 479 |
with self.subTest(pl=pl, fmt=fmt): |
| 480 |
data = plistlib.dumps(pl, fmt=fmt) |
| 481 |
pl2 = plistlib.loads(data) |
| 482 |
self.assertIsInstance(pl2, int) |
| 483 |
self.assertEqual(pl, pl2) |
| 484 |
data2 = plistlib.dumps(pl2, fmt=fmt) |
| 485 |
self.assertEqual(data, data2) |
| 486 |
|
| 487 |
for fmt in ALL_FORMATS: |
| 488 |
for pl in (2 ** 64 + 1, 2 ** 127-1, -2**64, -2 ** 127): |
| 489 |
with self.subTest(pl=pl, fmt=fmt): |
| 490 |
self.assertRaises(OverflowError, plistlib.dumps, |
| 491 |
pl, fmt=fmt) |
| 492 |
|
| 493 |
def test_bytearray(self): |
| 494 |
for pl in (b'<binary gunk>', b"<lots of binary gunk>\0\1\2\3" * 10): |
| 495 |
for fmt in ALL_FORMATS: |
| 496 |
with self.subTest(pl=pl, fmt=fmt): |
| 497 |
data = plistlib.dumps(bytearray(pl), fmt=fmt) |
| 498 |
pl2 = plistlib.loads(data) |
| 499 |
self.assertIsInstance(pl2, bytes) |
| 500 |
self.assertEqual(pl2, pl) |
| 501 |
data2 = plistlib.dumps(pl2, fmt=fmt) |
| 502 |
self.assertEqual(data, data2) |
| 503 |
|
| 504 |
def test_bytes(self): |
| 505 |
pl = self._create() |
| 506 |
data = plistlib.dumps(pl) |
| 507 |
pl2 = plistlib.loads(data) |
| 508 |
self.assertEqual(dict(pl), dict(pl2)) |
| 509 |
data2 = plistlib.dumps(pl2) |
| 510 |
self.assertEqual(data, data2) |
| 511 |
|
| 512 |
def test_bytes_indent(self): |
| 513 |
header = ( |
| 514 |
b'<?xml version="1.0" encoding="UTF-8"?>\n' |
| 515 |
b'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n' |
| 516 |
b'<plist version="1.0">\n') |
| 517 |
data = [{'bytes': bytes(range(50))}] |
| 518 |
pl = plistlib.dumps(data) |
| 519 |
self.assertEqual(pl, header + |
| 520 |
b'<array>\n' |
| 521 |
b'\t<dict>\n' |
| 522 |
b'\t\t<key>bytes</key>\n' |
| 523 |
b'\t\t<data>\n' |
| 524 |
b'\t\tAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss\n' |
| 525 |
b'\t\tLS4vMDE=\n' |
| 526 |
b'\t\t</data>\n' |
| 527 |
b'\t</dict>\n' |
| 528 |
b'</array>\n' |
| 529 |
b'</plist>\n') |
| 530 |
|
| 531 |
def dumps_with_indent(data, indent): |
| 532 |
fp = BytesIO() |
| 533 |
writer = plistlib._PlistWriter(fp, indent=indent) |
| 534 |
writer.write(data) |
| 535 |
return fp.getvalue() |
| 536 |
|
| 537 |
pl = dumps_with_indent(data, b' ') |
| 538 |
self.assertEqual(pl, header + |
| 539 |
b'<array>\n' |
| 540 |
b' <dict>\n' |
| 541 |
b' <key>bytes</key>\n' |
| 542 |
b' <data>\n' |
| 543 |
b' AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=\n' |
| 544 |
b' </data>\n' |
| 545 |
b' </dict>\n' |
| 546 |
b'</array>\n' |
| 547 |
b'</plist>\n') |
| 548 |
|
| 549 |
pl = dumps_with_indent(data, b' \t') |
| 550 |
self.assertEqual(pl, header + |
| 551 |
b'<array>\n' |
| 552 |
b' \t<dict>\n' |
| 553 |
b' \t \t<key>bytes</key>\n' |
| 554 |
b' \t \t<data>\n' |
| 555 |
b' \t \tAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss\n' |
| 556 |
b' \t \tLS4vMDE=\n' |
| 557 |
b' \t \t</data>\n' |
| 558 |
b' \t</dict>\n' |
| 559 |
b'</array>\n' |
| 560 |
b'</plist>\n') |
| 561 |
|
| 562 |
pl = dumps_with_indent(data, b'\t ') |
| 563 |
self.assertEqual(pl, header + |
| 564 |
b'<array>\n' |
| 565 |
b'\t <dict>\n' |
| 566 |
b'\t \t <key>bytes</key>\n' |
| 567 |
b'\t \t <data>\n' |
| 568 |
b'\t \t AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp\n' |
| 569 |
b'\t \t KissLS4vMDE=\n' |
| 570 |
b'\t \t </data>\n' |
| 571 |
b'\t </dict>\n' |
| 572 |
b'</array>\n' |
| 573 |
b'</plist>\n') |
| 574 |
|
| 575 |
def test_loads_str_with_xml_fmt(self): |
| 576 |
pl = self._create() |
| 577 |
b = plistlib.dumps(pl) |
| 578 |
s = b.decode() |
| 579 |
self.assertIsInstance(s, str) |
| 580 |
pl2 = plistlib.loads(s) |
| 581 |
self.assertEqual(pl, pl2) |
| 582 |
|
| 583 |
def test_loads_str_with_binary_fmt(self): |
| 584 |
msg = "value must be bytes-like object when fmt is FMT_BINARY" |
| 585 |
with self.assertRaisesRegex(TypeError, msg): |
| 586 |
plistlib.loads('test', fmt=plistlib.FMT_BINARY) |
| 587 |
|
| 588 |
def test_indentation_array(self): |
| 589 |
data = [[[[[[[[{'test': b'aaaaaa'}]]]]]]]] |
| 590 |
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
| 591 |
|
| 592 |
def test_indentation_dict(self): |
| 593 |
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': b'aaaaaa'}}}}}}}}} |
| 594 |
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
| 595 |
|
| 596 |
def test_indentation_dict_mix(self): |
| 597 |
data = {'1': {'2': [{'3': [[[[[{'test': b'aaaaaa'}]]]]]}]}} |
| 598 |
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data) |
| 599 |
|
| 600 |
def test_uid(self): |
| 601 |
data = UID(1) |
| 602 |
self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=plistlib.FMT_BINARY)), data) |
| 603 |
dict_data = { |
| 604 |
'uid0': UID(0), |
| 605 |
'uid2': UID(2), |
| 606 |
'uid8': UID(2 ** 8), |
| 607 |
'uid16': UID(2 ** 16), |
| 608 |
'uid32': UID(2 ** 32), |
| 609 |
'uid63': UID(2 ** 63) |
| 610 |
} |
| 611 |
self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_BINARY)), dict_data) |
| 612 |
|
| 613 |
def test_uid_data(self): |
| 614 |
uid = UID(1) |
| 615 |
self.assertEqual(uid.data, 1) |
| 616 |
|
| 617 |
def test_uid_eq(self): |
| 618 |
self.assertEqual(UID(1), UID(1)) |
| 619 |
self.assertNotEqual(UID(1), UID(2)) |
| 620 |
self.assertNotEqual(UID(1), "not uid") |
| 621 |
|
| 622 |
def test_uid_hash(self): |
| 623 |
self.assertEqual(hash(UID(1)), hash(UID(1))) |
| 624 |
|
| 625 |
def test_uid_repr(self): |
| 626 |
self.assertEqual(repr(UID(1)), "UID(1)") |
| 627 |
|
| 628 |
def test_uid_index(self): |
| 629 |
self.assertEqual(operator.index(UID(1)), 1) |
| 630 |
|
| 631 |
def test_uid_pickle(self): |
| 632 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 633 |
self.assertEqual(pickle.loads(pickle.dumps(UID(19), protocol=proto)), UID(19)) |
| 634 |
|
| 635 |
def test_uid_copy(self): |
| 636 |
self.assertEqual(copy.copy(UID(1)), UID(1)) |
| 637 |
self.assertEqual(copy.deepcopy(UID(1)), UID(1)) |
| 638 |
|
| 639 |
def test_appleformatting(self): |
| 640 |
for fmt in ALL_FORMATS: |
| 641 |
with self.subTest(fmt=fmt): |
| 642 |
pl = plistlib.loads(TESTDATA[fmt]) |
| 643 |
data = plistlib.dumps(pl, fmt=fmt) |
| 644 |
self.assertEqual(data, TESTDATA[fmt], |
| 645 |
"generated data was not identical to Apple's output") |
| 646 |
|
| 647 |
def test_appleformattingfromliteral(self): |
| 648 |
self.maxDiff = None |
| 649 |
for fmt in ALL_FORMATS: |
| 650 |
with self.subTest(fmt=fmt): |
| 651 |
pl = self._create(fmt=fmt) |
| 652 |
pl2 = plistlib.loads(TESTDATA[fmt], fmt=fmt) |
| 653 |
self.assertEqual(dict(pl), dict(pl2), |
| 654 |
"generated data was not identical to Apple's output") |
| 655 |
pl2 = plistlib.loads(TESTDATA[fmt]) |
| 656 |
self.assertEqual(dict(pl), dict(pl2), |
| 657 |
"generated data was not identical to Apple's output") |
| 658 |
|
| 659 |
def test_bytesio(self): |
| 660 |
for fmt in ALL_FORMATS: |
| 661 |
with self.subTest(fmt=fmt): |
| 662 |
b = BytesIO() |
| 663 |
pl = self._create(fmt=fmt) |
| 664 |
plistlib.dump(pl, b, fmt=fmt) |
| 665 |
pl2 = plistlib.load(BytesIO(b.getvalue()), fmt=fmt) |
| 666 |
self.assertEqual(dict(pl), dict(pl2)) |
| 667 |
pl2 = plistlib.load(BytesIO(b.getvalue())) |
| 668 |
self.assertEqual(dict(pl), dict(pl2)) |
| 669 |
|
| 670 |
def test_keysort_bytesio(self): |
| 671 |
pl = collections.OrderedDict() |
| 672 |
pl['b'] = 1 |
| 673 |
pl['a'] = 2 |
| 674 |
pl['c'] = 3 |
| 675 |
|
| 676 |
for fmt in ALL_FORMATS: |
| 677 |
for sort_keys in (False, True): |
| 678 |
with self.subTest(fmt=fmt, sort_keys=sort_keys): |
| 679 |
b = BytesIO() |
| 680 |
|
| 681 |
plistlib.dump(pl, b, fmt=fmt, sort_keys=sort_keys) |
| 682 |
pl2 = plistlib.load(BytesIO(b.getvalue()), |
| 683 |
dict_type=collections.OrderedDict) |
| 684 |
|
| 685 |
self.assertEqual(dict(pl), dict(pl2)) |
| 686 |
if sort_keys: |
| 687 |
self.assertEqual(list(pl2.keys()), ['a', 'b', 'c']) |
| 688 |
else: |
| 689 |
self.assertEqual(list(pl2.keys()), ['b', 'a', 'c']) |
| 690 |
|
| 691 |
def test_keysort(self): |
| 692 |
pl = collections.OrderedDict() |
| 693 |
pl['b'] = 1 |
| 694 |
pl['a'] = 2 |
| 695 |
pl['c'] = 3 |
| 696 |
|
| 697 |
for fmt in ALL_FORMATS: |
| 698 |
for sort_keys in (False, True): |
| 699 |
with self.subTest(fmt=fmt, sort_keys=sort_keys): |
| 700 |
data = plistlib.dumps(pl, fmt=fmt, sort_keys=sort_keys) |
| 701 |
pl2 = plistlib.loads(data, dict_type=collections.OrderedDict) |
| 702 |
|
| 703 |
self.assertEqual(dict(pl), dict(pl2)) |
| 704 |
if sort_keys: |
| 705 |
self.assertEqual(list(pl2.keys()), ['a', 'b', 'c']) |
| 706 |
else: |
| 707 |
self.assertEqual(list(pl2.keys()), ['b', 'a', 'c']) |
| 708 |
|
| 709 |
def test_keys_no_string(self): |
| 710 |
pl = { 42: 'aNumber' } |
| 711 |
|
| 712 |
for fmt in ALL_FORMATS: |
| 713 |
with self.subTest(fmt=fmt): |
| 714 |
self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) |
| 715 |
|
| 716 |
b = BytesIO() |
| 717 |
self.assertRaises(TypeError, plistlib.dump, pl, b, fmt=fmt) |
| 718 |
|
| 719 |
def test_skipkeys(self): |
| 720 |
pl = { |
| 721 |
42: 'aNumber', |
| 722 |
'snake': 'aWord', |
| 723 |
} |
| 724 |
|
| 725 |
for fmt in ALL_FORMATS: |
| 726 |
with self.subTest(fmt=fmt): |
| 727 |
data = plistlib.dumps( |
| 728 |
pl, fmt=fmt, skipkeys=True, sort_keys=False) |
| 729 |
|
| 730 |
pl2 = plistlib.loads(data) |
| 731 |
self.assertEqual(pl2, {'snake': 'aWord'}) |
| 732 |
|
| 733 |
fp = BytesIO() |
| 734 |
plistlib.dump( |
| 735 |
pl, fp, fmt=fmt, skipkeys=True, sort_keys=False) |
| 736 |
data = fp.getvalue() |
| 737 |
pl2 = plistlib.loads(fp.getvalue()) |
| 738 |
self.assertEqual(pl2, {'snake': 'aWord'}) |
| 739 |
|
| 740 |
def test_tuple_members(self): |
| 741 |
pl = { |
| 742 |
'first': (1, 2), |
| 743 |
'second': (1, 2), |
| 744 |
'third': (3, 4), |
| 745 |
} |
| 746 |
|
| 747 |
for fmt in ALL_FORMATS: |
| 748 |
with self.subTest(fmt=fmt): |
| 749 |
data = plistlib.dumps(pl, fmt=fmt) |
| 750 |
pl2 = plistlib.loads(data) |
| 751 |
self.assertEqual(pl2, { |
| 752 |
'first': [1, 2], |
| 753 |
'second': [1, 2], |
| 754 |
'third': [3, 4], |
| 755 |
}) |
| 756 |
if fmt != plistlib.FMT_BINARY: |
| 757 |
self.assertIsNot(pl2['first'], pl2['second']) |
| 758 |
|
| 759 |
def test_list_members(self): |
| 760 |
pl = { |
| 761 |
'first': [1, 2], |
| 762 |
'second': [1, 2], |
| 763 |
'third': [3, 4], |
| 764 |
} |
| 765 |
|
| 766 |
for fmt in ALL_FORMATS: |
| 767 |
with self.subTest(fmt=fmt): |
| 768 |
data = plistlib.dumps(pl, fmt=fmt) |
| 769 |
pl2 = plistlib.loads(data) |
| 770 |
self.assertEqual(pl2, { |
| 771 |
'first': [1, 2], |
| 772 |
'second': [1, 2], |
| 773 |
'third': [3, 4], |
| 774 |
}) |
| 775 |
self.assertIsNot(pl2['first'], pl2['second']) |
| 776 |
|
| 777 |
def test_dict_members(self): |
| 778 |
pl = { |
| 779 |
'first': {'a': 1}, |
| 780 |
'second': {'a': 1}, |
| 781 |
'third': {'b': 2 }, |
| 782 |
} |
| 783 |
|
| 784 |
for fmt in ALL_FORMATS: |
| 785 |
with self.subTest(fmt=fmt): |
| 786 |
data = plistlib.dumps(pl, fmt=fmt) |
| 787 |
pl2 = plistlib.loads(data) |
| 788 |
self.assertEqual(pl2, { |
| 789 |
'first': {'a': 1}, |
| 790 |
'second': {'a': 1}, |
| 791 |
'third': {'b': 2 }, |
| 792 |
}) |
| 793 |
self.assertIsNot(pl2['first'], pl2['second']) |
| 794 |
|
| 795 |
def test_controlcharacters(self): |
| 796 |
for i in range(128): |
| 797 |
c = chr(i) |
| 798 |
testString = "string containing %s" % c |
| 799 |
if i >= 32 or c in "\r\n\t": |
| 800 |
# \r, \n and \t are the only legal control chars in XML |
| 801 |
data = plistlib.dumps(testString, fmt=plistlib.FMT_XML) |
| 802 |
if c != "\r": |
| 803 |
self.assertEqual(plistlib.loads(data), testString) |
| 804 |
else: |
| 805 |
with self.assertRaises(ValueError): |
| 806 |
plistlib.dumps(testString, fmt=plistlib.FMT_XML) |
| 807 |
plistlib.dumps(testString, fmt=plistlib.FMT_BINARY) |
| 808 |
|
| 809 |
def test_non_bmp_characters(self): |
| 810 |
pl = {'python': '\U0001f40d'} |
| 811 |
for fmt in ALL_FORMATS: |
| 812 |
with self.subTest(fmt=fmt): |
| 813 |
data = plistlib.dumps(pl, fmt=fmt) |
| 814 |
self.assertEqual(plistlib.loads(data), pl) |
| 815 |
|
| 816 |
def test_lone_surrogates(self): |
| 817 |
for fmt in ALL_FORMATS: |
| 818 |
with self.subTest(fmt=fmt): |
| 819 |
with self.assertRaises(UnicodeEncodeError): |
| 820 |
plistlib.dumps('\ud8ff', fmt=fmt) |
| 821 |
with self.assertRaises(UnicodeEncodeError): |
| 822 |
plistlib.dumps('\udcff', fmt=fmt) |
| 823 |
|
| 824 |
def test_nondictroot(self): |
| 825 |
for fmt in ALL_FORMATS: |
| 826 |
with self.subTest(fmt=fmt): |
| 827 |
test1 = "abc" |
| 828 |
test2 = [1, 2, 3, "abc"] |
| 829 |
result1 = plistlib.loads(plistlib.dumps(test1, fmt=fmt)) |
| 830 |
result2 = plistlib.loads(plistlib.dumps(test2, fmt=fmt)) |
| 831 |
self.assertEqual(test1, result1) |
| 832 |
self.assertEqual(test2, result2) |
| 833 |
|
| 834 |
def test_invalidarray(self): |
| 835 |
for i in ["<key>key inside an array</key>", |
| 836 |
"<key>key inside an array2</key><real>3</real>", |
| 837 |
"<true/><key>key inside an array3</key>"]: |
| 838 |
self.assertRaises(ValueError, plistlib.loads, |
| 839 |
("<plist><array>%s</array></plist>"%i).encode()) |
| 840 |
|
| 841 |
def test_invaliddict(self): |
| 842 |
for i in ["<key><true/>k</key><string>compound key</string>", |
| 843 |
"<key>single key</key>", |
| 844 |
"<string>missing key</string>", |
| 845 |
"<key>k1</key><string>v1</string><real>5.3</real>" |
| 846 |
"<key>k1</key><key>k2</key><string>double key</string>"]: |
| 847 |
self.assertRaises(ValueError, plistlib.loads, |
| 848 |
("<plist><dict>%s</dict></plist>"%i).encode()) |
| 849 |
self.assertRaises(ValueError, plistlib.loads, |
| 850 |
("<plist><array><dict>%s</dict></array></plist>"%i).encode()) |
| 851 |
|
| 852 |
def test_invalidinteger(self): |
| 853 |
self.assertRaises(ValueError, plistlib.loads, |
| 854 |
b"<plist><integer>not integer</integer></plist>") |
| 855 |
|
| 856 |
def test_invalidreal(self): |
| 857 |
self.assertRaises(ValueError, plistlib.loads, |
| 858 |
b"<plist><integer>not real</integer></plist>") |
| 859 |
|
| 860 |
def test_integer_notations(self): |
| 861 |
pl = b"<plist><integer>456</integer></plist>" |
| 862 |
value = plistlib.loads(pl) |
| 863 |
self.assertEqual(value, 456) |
| 864 |
|
| 865 |
pl = b"<plist><integer>0xa</integer></plist>" |
| 866 |
value = plistlib.loads(pl) |
| 867 |
self.assertEqual(value, 10) |
| 868 |
|
| 869 |
pl = b"<plist><integer>0123</integer></plist>" |
| 870 |
value = plistlib.loads(pl) |
| 871 |
self.assertEqual(value, 123) |
| 872 |
|
| 873 |
def test_xml_encodings(self): |
| 874 |
base = TESTDATA[plistlib.FMT_XML] |
| 875 |
|
| 876 |
for xml_encoding, encoding, bom in [ |
| 877 |
(b'utf-8', 'utf-8', codecs.BOM_UTF8), |
| 878 |
(b'utf-16', 'utf-16-le', codecs.BOM_UTF16_LE), |
| 879 |
(b'utf-16', 'utf-16-be', codecs.BOM_UTF16_BE), |
| 880 |
# Expat does not support UTF-32 |
| 881 |
#(b'utf-32', 'utf-32-le', codecs.BOM_UTF32_LE), |
| 882 |
#(b'utf-32', 'utf-32-be', codecs.BOM_UTF32_BE), |
| 883 |
]: |
| 884 |
|
| 885 |
pl = self._create(fmt=plistlib.FMT_XML) |
| 886 |
with self.subTest(encoding=encoding): |
| 887 |
data = base.replace(b'UTF-8', xml_encoding) |
| 888 |
data = bom + data.decode('utf-8').encode(encoding) |
| 889 |
pl2 = plistlib.loads(data) |
| 890 |
self.assertEqual(dict(pl), dict(pl2)) |
| 891 |
|
| 892 |
def test_dump_invalid_format(self): |
| 893 |
with self.assertRaises(ValueError): |
| 894 |
plistlib.dumps({}, fmt="blah") |
| 895 |
|
| 896 |
def test_load_invalid_file(self): |
| 897 |
with self.assertRaises(plistlib.InvalidFileException): |
| 898 |
plistlib.loads(b"these are not plist file contents") |
| 899 |
|
| 900 |
def test_modified_uid_negative(self): |
| 901 |
neg_uid = UID(1) |
| 902 |
neg_uid.data = -1 # dodge the negative check in the constructor |
| 903 |
with self.assertRaises(ValueError): |
| 904 |
plistlib.dumps(neg_uid, fmt=plistlib.FMT_BINARY) |
| 905 |
|
| 906 |
def test_modified_uid_huge(self): |
| 907 |
huge_uid = UID(1) |
| 908 |
huge_uid.data = 2 ** 64 # dodge the size check in the constructor |
| 909 |
with self.assertRaises(OverflowError): |
| 910 |
plistlib.dumps(huge_uid, fmt=plistlib.FMT_BINARY) |
| 911 |
|
| 912 |
def test_xml_plist_with_entity_decl(self): |
| 913 |
with self.assertRaisesRegex(plistlib.InvalidFileException, |
| 914 |
"XML entity declarations are not supported"): |
| 915 |
plistlib.loads(XML_PLIST_WITH_ENTITY, fmt=plistlib.FMT_XML) |
| 916 |
|
| 917 |
def test_load_aware_datetime(self): |
| 918 |
dt = plistlib.loads(b"<plist><date>2023-12-10T08:03:30Z</date></plist>", |
| 919 |
aware_datetime=True) |
| 920 |
self.assertEqual(dt.tzinfo, datetime.UTC) |
| 921 |
|
| 922 |
@unittest.skipUnless("America/Los_Angeles" in zoneinfo.available_timezones(), |
| 923 |
"Can't find timezone datebase") |
| 924 |
def test_dump_aware_datetime(self): |
| 925 |
dt = datetime.datetime(2345, 6, 7, 8, 9, 10, |
| 926 |
tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles")) |
| 927 |
for fmt in ALL_FORMATS: |
| 928 |
s = plistlib.dumps(dt, fmt=fmt, aware_datetime=True) |
| 929 |
loaded_dt = plistlib.loads(s, fmt=fmt, aware_datetime=True) |
| 930 |
self.assertEqual(loaded_dt.tzinfo, datetime.UTC) |
| 931 |
self.assertEqual(loaded_dt, dt) |
| 932 |
|
| 933 |
def test_dump_utc_aware_datetime(self): |
| 934 |
dt = datetime.datetime(2345, 6, 7, 8, 9, 10, tzinfo=datetime.UTC) |
| 935 |
for fmt in ALL_FORMATS: |
| 936 |
s = plistlib.dumps(dt, fmt=fmt, aware_datetime=True) |
| 937 |
loaded_dt = plistlib.loads(s, fmt=fmt, aware_datetime=True) |
| 938 |
self.assertEqual(loaded_dt.tzinfo, datetime.UTC) |
| 939 |
self.assertEqual(loaded_dt, dt) |
| 940 |
|
| 941 |
@unittest.skipUnless("America/Los_Angeles" in zoneinfo.available_timezones(), |
| 942 |
"Can't find timezone datebase") |
| 943 |
def test_dump_aware_datetime_without_aware_datetime_option(self): |
| 944 |
dt = datetime.datetime(2345, 6, 7, 8, |
| 945 |
tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles")) |
| 946 |
s = plistlib.dumps(dt, fmt=plistlib.FMT_XML, aware_datetime=False) |
| 947 |
self.assertIn(b"2345-06-07T08:00:00Z", s) |
| 948 |
|
| 949 |
def test_dump_utc_aware_datetime_without_aware_datetime_option(self): |
| 950 |
dt = datetime.datetime(2345, 6, 7, 8, tzinfo=datetime.UTC) |
| 951 |
s = plistlib.dumps(dt, fmt=plistlib.FMT_XML, aware_datetime=False) |
| 952 |
self.assertIn(b"2345-06-07T08:00:00Z", s) |
| 953 |
|
| 954 |
def test_dump_naive_datetime_with_aware_datetime_option(self): |
| 955 |
# Save a naive datetime with aware_datetime set to true. This will lead |
| 956 |
# to having different time as compared to the current machine's |
| 957 |
# timezone, which is UTC. |
| 958 |
dt = datetime.datetime(2003, 6, 7, 8, tzinfo=None) |
| 959 |
for fmt in ALL_FORMATS: |
| 960 |
s = plistlib.dumps(dt, fmt=fmt, aware_datetime=True) |
| 961 |
parsed = plistlib.loads(s, aware_datetime=False) |
| 962 |
expected = dt.astimezone(datetime.UTC).replace(tzinfo=None) |
| 963 |
self.assertEqual(parsed, expected) |
| 964 |
|
| 965 |
|
| 966 |
class TestBinaryPlistlib(unittest.TestCase): |
| 967 |
|
| 968 |
def build(self, *objects, offset_size=1, ref_size=1): |
| 969 |
data = [b'bplist00'] |
| 970 |
offset = 8 |
| 971 |
offsets = [] |
| 972 |
for x in objects: |
| 973 |
offsets.append(offset.to_bytes(offset_size, 'big')) |
| 974 |
data.append(x) |
| 975 |
offset += len(x) |
| 976 |
tail = struct.pack('>6xBBQQQ', offset_size, ref_size, |
| 977 |
len(objects), 0, offset) |
| 978 |
data.extend(offsets) |
| 979 |
data.append(tail) |
| 980 |
return b''.join(data) |
| 981 |
|
| 982 |
def decode(self, *objects, offset_size=1, ref_size=1): |
| 983 |
data = self.build(*objects, offset_size=offset_size, ref_size=ref_size) |
| 984 |
return plistlib.loads(data, fmt=plistlib.FMT_BINARY) |
| 985 |
|
| 986 |
def test_nonstandard_refs_size(self): |
| 987 |
# Issue #21538: Refs and offsets are 24-bit integers |
| 988 |
data = (b'bplist00' |
| 989 |
b'\xd1\x00\x00\x01\x00\x00\x02QaQb' |
| 990 |
b'\x00\x00\x08\x00\x00\x0f\x00\x00\x11' |
| 991 |
b'\x00\x00\x00\x00\x00\x00' |
| 992 |
b'\x03\x03' |
| 993 |
b'\x00\x00\x00\x00\x00\x00\x00\x03' |
| 994 |
b'\x00\x00\x00\x00\x00\x00\x00\x00' |
| 995 |
b'\x00\x00\x00\x00\x00\x00\x00\x13') |
| 996 |
self.assertEqual(plistlib.loads(data), {'a': 'b'}) |
| 997 |
|
| 998 |
def test_dump_duplicates(self): |
| 999 |
# Test effectiveness of saving duplicated objects |
| 1000 |
for x in (None, False, True, 12345, 123.45, 'abcde', 'абвгд', b'abcde', |
| 1001 |
datetime.datetime(2004, 10, 26, 10, 33, 33), |
| 1002 |
bytearray(b'abcde'), [12, 345], (12, 345), {'12': 345}): |
| 1003 |
with self.subTest(x=x): |
| 1004 |
data = plistlib.dumps([x]*1000, fmt=plistlib.FMT_BINARY) |
| 1005 |
self.assertLess(len(data), 1100, repr(data)) |
| 1006 |
|
| 1007 |
def test_identity(self): |
| 1008 |
for x in (None, False, True, 12345, 123.45, 'abcde', b'abcde', |
| 1009 |
datetime.datetime(2004, 10, 26, 10, 33, 33), |
| 1010 |
bytearray(b'abcde'), [12, 345], (12, 345), {'12': 345}): |
| 1011 |
with self.subTest(x=x): |
| 1012 |
data = plistlib.dumps([x]*2, fmt=plistlib.FMT_BINARY) |
| 1013 |
a, b = plistlib.loads(data) |
| 1014 |
if isinstance(x, tuple): |
| 1015 |
x = list(x) |
| 1016 |
self.assertEqual(a, x) |
| 1017 |
self.assertEqual(b, x) |
| 1018 |
self.assertIs(a, b) |
| 1019 |
|
| 1020 |
def test_cycles(self): |
| 1021 |
# recursive list |
| 1022 |
a = [] |
| 1023 |
a.append(a) |
| 1024 |
b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY)) |
| 1025 |
self.assertIs(b[0], b) |
| 1026 |
# recursive tuple |
| 1027 |
a = ([],) |
| 1028 |
a[0].append(a) |
| 1029 |
b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY)) |
| 1030 |
self.assertIs(b[0][0], b) |
| 1031 |
# recursive dict |
| 1032 |
a = {} |
| 1033 |
a['x'] = a |
| 1034 |
b = plistlib.loads(plistlib.dumps(a, fmt=plistlib.FMT_BINARY)) |
| 1035 |
self.assertIs(b['x'], b) |
| 1036 |
|
| 1037 |
def test_deep_nesting(self): |
| 1038 |
for N in [50, 300, 100_000]: |
| 1039 |
chunks = [b'\xa1' + (i + 1).to_bytes(4, 'big') for i in range(N)] |
| 1040 |
try: |
| 1041 |
result = self.decode(*chunks, b'\x54seed', offset_size=4, ref_size=4) |
| 1042 |
except RecursionError: |
| 1043 |
self.assertGreater(N, sys.getrecursionlimit()) |
| 1044 |
else: |
| 1045 |
for i in range(N): |
| 1046 |
self.assertIsInstance(result, list) |
| 1047 |
self.assertEqual(len(result), 1) |
| 1048 |
result = result[0] |
| 1049 |
self.assertEqual(result, 'seed') |
| 1050 |
|
| 1051 |
def test_large_timestamp(self): |
| 1052 |
# Issue #26709: 32-bit timestamp out of range |
| 1053 |
for ts in -2**31-1, 2**31: |
| 1054 |
with self.subTest(ts=ts): |
| 1055 |
d = (datetime.datetime(1970, 1, 1, 0, 0) + |
| 1056 |
datetime.timedelta(seconds=ts)) |
| 1057 |
data = plistlib.dumps(d, fmt=plistlib.FMT_BINARY) |
| 1058 |
self.assertEqual(plistlib.loads(data), d) |
| 1059 |
|
| 1060 |
def test_load_singletons(self): |
| 1061 |
self.assertIs(self.decode(b'\x00'), None) |
| 1062 |
self.assertIs(self.decode(b'\x08'), False) |
| 1063 |
self.assertIs(self.decode(b'\x09'), True) |
| 1064 |
self.assertEqual(self.decode(b'\x0f'), b'') |
| 1065 |
|
| 1066 |
def test_load_int(self): |
| 1067 |
self.assertEqual(self.decode(b'\x10\x00'), 0) |
| 1068 |
self.assertEqual(self.decode(b'\x10\xfe'), 0xfe) |
| 1069 |
self.assertEqual(self.decode(b'\x11\xfe\xdc'), 0xfedc) |
| 1070 |
self.assertEqual(self.decode(b'\x12\xfe\xdc\xba\x98'), 0xfedcba98) |
| 1071 |
self.assertEqual(self.decode(b'\x13\x01\x23\x45\x67\x89\xab\xcd\xef'), |
| 1072 |
0x0123456789abcdef) |
| 1073 |
self.assertEqual(self.decode(b'\x13\xfe\xdc\xba\x98\x76\x54\x32\x10'), |
| 1074 |
-0x123456789abcdf0) |
| 1075 |
|
| 1076 |
def test_unsupported(self): |
| 1077 |
unsupported = [*range(1, 8), *range(10, 15), |
| 1078 |
0x20, 0x21, *range(0x24, 0x33), *range(0x34, 0x40)] |
| 1079 |
for i in [0x70, 0x90, 0xb0, 0xc0, 0xe0, 0xf0]: |
| 1080 |
unsupported.extend(i + j for j in range(16)) |
| 1081 |
for token in unsupported: |
| 1082 |
with self.subTest(f'token {token:02x}'): |
| 1083 |
with self.assertRaises(plistlib.InvalidFileException): |
| 1084 |
self.decode(bytes([token]) + b'\x00'*16) |
| 1085 |
|
| 1086 |
def test_invalid_binary(self): |
| 1087 |
for name, data in INVALID_BINARY_PLISTS: |
| 1088 |
with self.subTest(name): |
| 1089 |
with self.assertRaises(plistlib.InvalidFileException): |
| 1090 |
plistlib.loads(b'bplist00' + data, fmt=plistlib.FMT_BINARY) |
| 1091 |
|
| 1092 |
def test_truncated_large_data(self): |
| 1093 |
self.addCleanup(os_helper.unlink, os_helper.TESTFN) |
| 1094 |
def check(data): |
| 1095 |
with open(os_helper.TESTFN, 'wb') as f: |
| 1096 |
f.write(data) |
| 1097 |
# buffered file |
| 1098 |
with open(os_helper.TESTFN, 'rb') as f: |
| 1099 |
with self.assertRaises(plistlib.InvalidFileException): |
| 1100 |
plistlib.load(f, fmt=plistlib.FMT_BINARY) |
| 1101 |
# unbuffered file |
| 1102 |
with open(os_helper.TESTFN, 'rb', buffering=0) as f: |
| 1103 |
with self.assertRaises(plistlib.InvalidFileException): |
| 1104 |
plistlib.load(f, fmt=plistlib.FMT_BINARY) |
| 1105 |
for w in range(20, 64): |
| 1106 |
s = 1 << w |
| 1107 |
# data |
| 1108 |
check(self.build(b'\x4f\x13' + s.to_bytes(8, 'big'))) |
| 1109 |
# ascii string |
| 1110 |
check(self.build(b'\x5f\x13' + s.to_bytes(8, 'big'))) |
| 1111 |
# unicode string |
| 1112 |
check(self.build(b'\x6f\x13' + s.to_bytes(8, 'big'))) |
| 1113 |
# array |
| 1114 |
check(self.build(b'\xaf\x13' + s.to_bytes(8, 'big'))) |
| 1115 |
# dict |
| 1116 |
check(self.build(b'\xdf\x13' + s.to_bytes(8, 'big'))) |
| 1117 |
# number of objects |
| 1118 |
check(b'bplist00' + struct.pack('>6xBBQQQ', 1, 1, s, 0, 8)) |
| 1119 |
|
| 1120 |
def test_load_aware_datetime(self): |
| 1121 |
data = (b'bplist003B\x04>\xd0d\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00' |
| 1122 |
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00' |
| 1123 |
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11') |
| 1124 |
self.assertEqual(plistlib.loads(data, aware_datetime=True), |
| 1125 |
datetime.datetime(2345, 6, 7, 8, tzinfo=datetime.UTC)) |
| 1126 |
|
| 1127 |
@unittest.skipUnless("America/Los_Angeles" in zoneinfo.available_timezones(), |
| 1128 |
"Can't find timezone datebase") |
| 1129 |
def test_dump_aware_datetime_without_aware_datetime_option(self): |
| 1130 |
dt = datetime.datetime(2345, 6, 7, 8, |
| 1131 |
tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles")) |
| 1132 |
msg = "can't subtract offset-naive and offset-aware datetimes" |
| 1133 |
with self.assertRaisesRegex(TypeError, msg): |
| 1134 |
plistlib.dumps(dt, fmt=plistlib.FMT_BINARY, aware_datetime=False) |
| 1135 |
|
| 1136 |
def test_dump_utc_aware_datetime_without_aware_datetime_option(self): |
| 1137 |
dt = datetime.datetime(2345, 6, 7, 8, tzinfo=datetime.UTC) |
| 1138 |
msg = "can't subtract offset-naive and offset-aware datetimes" |
| 1139 |
with self.assertRaisesRegex(TypeError, msg): |
| 1140 |
plistlib.dumps(dt, fmt=plistlib.FMT_BINARY, aware_datetime=False) |
| 1141 |
|
| 1142 |
|
| 1143 |
class TestKeyedArchive(unittest.TestCase): |
| 1144 |
def test_keyed_archive_data(self): |
| 1145 |
# This is the structure of a NSKeyedArchive packed plist |
| 1146 |
data = { |
| 1147 |
'$version': 100000, |
| 1148 |
'$objects': [ |
| 1149 |
'$null', { |
| 1150 |
'pytype': 1, |
| 1151 |
'$class': UID(2), |
| 1152 |
'NS.string': 'KeyArchive UID Test' |
| 1153 |
}, |
| 1154 |
{ |
| 1155 |
'$classname': 'OC_BuiltinPythonUnicode', |
| 1156 |
'$classes': [ |
| 1157 |
'OC_BuiltinPythonUnicode', |
| 1158 |
'OC_PythonUnicode', |
| 1159 |
'NSString', |
| 1160 |
'NSObject' |
| 1161 |
], |
| 1162 |
'$classhints': [ |
| 1163 |
'OC_PythonString', 'NSString' |
| 1164 |
] |
| 1165 |
} |
| 1166 |
], |
| 1167 |
'$archiver': 'NSKeyedArchiver', |
| 1168 |
'$top': { |
| 1169 |
'root': UID(1) |
| 1170 |
} |
| 1171 |
} |
| 1172 |
self.assertEqual(plistlib.loads(TESTDATA["KEYED_ARCHIVE"]), data) |
| 1173 |
|
| 1174 |
|
| 1175 |
class MiscTestCase(unittest.TestCase): |
| 1176 |
def test__all__(self): |
| 1177 |
not_exported = {"PlistFormat", "PLISTHEADER"} |
| 1178 |
support.check__all__(self, plistlib, not_exported=not_exported) |
| 1179 |
|
| 1180 |
@unittest.skipUnless(sys.platform == "darwin", "plutil utility is for Mac os") |
| 1181 |
class TestPlutil(unittest.TestCase): |
| 1182 |
file_name = "plutil_test.plist" |
| 1183 |
properties = { |
| 1184 |
"fname" : "H", |
| 1185 |
"lname":"A", |
| 1186 |
"marks" : {"a":100, "b":0x10} |
| 1187 |
} |
| 1188 |
exptected_properties = { |
| 1189 |
"fname" : "H", |
| 1190 |
"lname": "A", |
| 1191 |
"marks" : {"a":100, "b":16} |
| 1192 |
} |
| 1193 |
pl = { |
| 1194 |
"HexType" : 0x0100000c, |
| 1195 |
"IntType" : 0o123 |
| 1196 |
} |
| 1197 |
|
| 1198 |
@classmethod |
| 1199 |
def setUpClass(cls) -> None: |
| 1200 |
## Generate plist file with plistlib and parse with plutil |
| 1201 |
with open(cls.file_name,'wb') as f: |
| 1202 |
plistlib.dump(cls.properties, f, fmt=plistlib.FMT_BINARY) |
| 1203 |
|
| 1204 |
@classmethod |
| 1205 |
def tearDownClass(cls) -> None: |
| 1206 |
os.remove(cls.file_name) |
| 1207 |
|
| 1208 |
def get_lint_status(self): |
| 1209 |
return subprocess.run(['plutil', "-lint", self.file_name], capture_output=True, text=True).stdout |
| 1210 |
|
| 1211 |
def convert_to_json(self): |
| 1212 |
"""Convert binary file to json using plutil |
| 1213 |
""" |
| 1214 |
subprocess.run(['plutil', "-convert", 'json', self.file_name]) |
| 1215 |
|
| 1216 |
def convert_to_bin(self): |
| 1217 |
"""Convert file to binary using plutil |
| 1218 |
""" |
| 1219 |
subprocess.run(['plutil', "-convert", 'binary1', self.file_name]) |
| 1220 |
|
| 1221 |
def write_pl(self): |
| 1222 |
"""Write Hex properties to file using writePlist |
| 1223 |
""" |
| 1224 |
with open(self.file_name, 'wb') as f: |
| 1225 |
plistlib.dump(self.pl, f, fmt=plistlib.FMT_BINARY) |
| 1226 |
|
| 1227 |
def test_lint_status(self): |
| 1228 |
# check lint status of file using plutil |
| 1229 |
self.assertEqual(f"{self.file_name}: OK\n", self.get_lint_status()) |
| 1230 |
|
| 1231 |
def check_content(self): |
| 1232 |
# check file content with plutil converting binary to json |
| 1233 |
self.convert_to_json() |
| 1234 |
with open(self.file_name) as f: |
| 1235 |
ff = json.loads(f.read()) |
| 1236 |
self.assertEqual(ff, self.exptected_properties) |
| 1237 |
|
| 1238 |
def check_plistlib_parse(self): |
| 1239 |
# Generate plist files with plutil and parse with plistlib |
| 1240 |
self.convert_to_bin() |
| 1241 |
with open(self.file_name, 'rb') as f: |
| 1242 |
self.assertEqual(plistlib.load(f), self.exptected_properties) |
| 1243 |
|
| 1244 |
def test_octal_and_hex(self): |
| 1245 |
self.write_pl() |
| 1246 |
self.convert_to_json() |
| 1247 |
with open(self.file_name, 'r') as f: |
| 1248 |
p = json.loads(f.read()) |
| 1249 |
self.assertEqual(p.get("HexType"), 16777228) |
| 1250 |
self.assertEqual(p.get("IntType"), 83) |
| 1251 |
|
| 1252 |
|
| 1253 |
if __name__ == '__main__': |
| 1254 |
unittest.main() |