File: test_write.py

package info (click to toggle)
python-biplist 1.0.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: python: 1,419; makefile: 3
file content (365 lines) | stat: -rw-r--r-- 13,412 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-

import datetime
import io
import os
import subprocess
import sys
import tempfile
import unittest

from biplist import *
from biplist import PlistWriter
from test_utils import *

try:
    unicode
    unicodeStr = lambda x: x.decode('utf-8')
    toUnicode = lambda x: x.decode('unicode-escape')
except NameError:
    long = int
    unicode = str
    unicodeStr = lambda x: x
    toUnicode = lambda x: x
try:
    xrange
except NameError:
    xrange = range

# Ensure integers are always shown as '1L' regardless of size on Python 2
def repr_(x):
    if isinstance(x, int):
        x = long(x)
    return repr(x)

class TestWritePlist(unittest.TestCase):
    def setUp(self):
        pass
    
    def roundTrip(self, case, xml=False, expected=None, reprTest=True):
        # reprTest may fail randomly if True and the values being encoded include a dictionary with more
        # than one key.
        
        # convert to plist string
        plist = writePlistToString(case, binary=(not xml))
        self.assertTrue(len(plist) > 0)
        
        # confirm that lint is happy with the result
        self.lintPlist(plist)        
        
        # convert back
        readResult = readPlistFromString(plist)
        
        # test equality
        if reprTest is True:
            self.assertEqual(repr_(case if expected is None else expected), repr_(readResult))
        else:
            self.assertEqual((case if expected is None else expected), readResult)
        
        # write to file
        plistFile = tempfile.NamedTemporaryFile(mode='wb+', suffix='.plist')
        writePlist(case, plistFile, binary=(xml is False))
        plistFile.seek(0)
        
        # confirm that lint is happy with the result
        self.lintPlist(plistFile)
        
        # read back from file
        fileResult = readPlist(plistFile)
        
        # test equality
        if reprTest is True:
            self.assertEqual(repr(case if expected is None else expected), repr(fileResult))
        else:
            self.assertEqual((case if expected is None else expected), fileResult)
    
    def lintPlist(self, plist):
        if os.access('/usr/bin/plutil', os.X_OK):
            plistFile = None
            plistFilePath = None
            
            if hasattr(plist, 'name'):
                plistFilePath = plist.name
            else:
                if hasattr(plist, 'read'):
                    plistFile = tempfile.NamedTemporaryFile('w%s' % ('b' if 'b' in plist.mode else ''))
                    plistFile.write(plist.read())
                else:
                    plistFile = tempfile.NamedTemporaryFile('w%s' % ('b' if isinstance(plist, bytes) else ''))
                    plistFile.write(plist)
                plistFilePath = plistFile.name
                plistFile.flush()

            status, output = run_command(['/usr/bin/plutil', '-lint', plistFilePath])
            if status != 0:
                self.fail("plutil verification failed (status %d): %s" % (status, output))
    
    def testXMLPlist(self):
        self.roundTrip({'hello':'world'}, xml=True)

    def testXMLPlistWithData(self):
        for binmode in (True, False):
            binplist = writePlistToString({'data': Data(b'\x01\xac\xf0\xff')}, binary=binmode)
            plist = readPlistFromString(binplist)
            self.assertTrue(isinstance(plist['data'], (Data, bytes)), \
                "unable to encode then decode Data into %s plist" % ("binary" if binmode else "XML"))

    def testConvertToXMLPlistWithData(self):
        binplist = writePlistToString({'data': Data(b'\x01\xac\xf0\xff')})
        plist = readPlistFromString(binplist)
        xmlplist = writePlistToString(plist, binary=False)
        self.assertTrue(len(xmlplist) > 0, "unable to convert plist with Data from binary to XML")
    
    def testBoolRoot(self):
        self.roundTrip(True)
        self.roundTrip(False)
    
    def testDuplicate(self):
        l = ["foo" for i in xrange(0, 100)]
        self.roundTrip(l)
        
    def testListRoot(self):
        self.roundTrip([1, 2, 3])
    
    def testDictRoot(self):
        self.roundTrip({'a':1, 'B':'d'}, reprTest=False)
    
    def mixedNumericTypesHelper(self, cases):
        result = readPlistFromString(writePlistToString(cases))
        for i in xrange(0, len(cases)):
            self.assertTrue(cases[i] == result[i])
            self.assertEqual(type(cases[i]), type(result[i]), "Type mismatch on %d: %s != %s" % (i, repr(cases[i]), repr(result[i])))
    
    def testBoolsAndIntegersMixed(self):
        self.mixedNumericTypesHelper([0, 1, True, False, None])
        self.mixedNumericTypesHelper([False, True, 0, 1, None])
        self.roundTrip({'1':[True, False, 1, 0], '0':[1, 2, 0, {'2':[1, 0, False]}]}, reprTest=False)
        self.roundTrip([1, 1, 1, 1, 1, True, True, True, True])
    
    def testFloatsAndIntegersMixed(self):
        self.mixedNumericTypesHelper([0, 1, 1.0, 0.0, None])
        self.mixedNumericTypesHelper([0.0, 1.0, 0, 1, None])
        self.roundTrip({'1':[1.0, 0.0, 1, 0], '0':[1, 2, 0, {'2':[1, 0, 0.0]}]}, reprTest=False)
        self.roundTrip([1, 1, 1, 1, 1, 1.0, 1.0, 1.0, 1.0])
    
    def testSetRoot(self):
        self.roundTrip(set((1, 2, 3)))
    
    def testDatetime(self):
        now = datetime.datetime.utcnow()
        now = now.replace(microsecond=0)
        self.roundTrip([now])
    
    def testFloat(self):
        self.roundTrip({'aFloat':1.23})
    
    def testTuple(self):
        result = writePlistToString({'aTuple':(1, 2.0, 'a'), 'dupTuple':('a', 'a', 'a', 'b', 'b')})
        self.assertTrue(len(result) > 0)
        readResult = readPlistFromString(result)
        self.assertEqual(readResult['aTuple'], [1, 2.0, 'a'])
        self.assertEqual(readResult['dupTuple'], ['a', 'a', 'a', 'b', 'b'])
    
    def testComplicated(self):
        root = {'preference':[1, 2, {'hi there':['a', 1, 2, {'yarrrr':123}]}]}
        self.lintPlist(writePlistToString(root))
        self.roundTrip(root)
    
    def testBytes(self):
        self.roundTrip(b'0')
        self.roundTrip(b'')
        
        self.roundTrip([b'0'])
        self.roundTrip([b''])
        
        self.roundTrip({'a': b'0'})
        self.roundTrip({'a': b''})
    
    def testString(self):
        self.roundTrip('')
        self.roundTrip('a')
        self.roundTrip('1')
        
        self.roundTrip([''])
        self.roundTrip(['a'])
        self.roundTrip(['1'])
        
        self.roundTrip({'a':''})
        self.roundTrip({'a':'a'})
        self.roundTrip({'1':'a'})
        
        self.roundTrip({'a':'a'})
        self.roundTrip({'a':'1'})
    
    def testUnicode(self):
        # defaulting to 1 byte strings
        if str != unicode:
            self.roundTrip(unicodeStr(r''), expected='')
            self.roundTrip(unicodeStr(r'a'), expected='a')
            
            self.roundTrip([unicodeStr(r'a')], expected=['a'])
            
            self.roundTrip({'a':unicodeStr(r'a')}, expected={'a':'a'})
            self.roundTrip({unicodeStr(r'a'):'a'}, expected={'a':'a'})
            self.roundTrip({unicodeStr(r''):unicodeStr(r'')}, expected={'':''})
        
        # TODO: need a 4-byte unicode character
        self.roundTrip(unicodeStr(r'ü'))
        self.roundTrip([unicodeStr(r'ü')])
        self.roundTrip({'a':unicodeStr(r'ü')})
        self.roundTrip({unicodeStr(r'ü'):'a'})
        
        self.roundTrip(toUnicode('\u00b6'))
        self.roundTrip([toUnicode('\u00b6')])
        self.roundTrip({toUnicode('\u00b6'):toUnicode('\u00b6')})
        
        self.roundTrip(toUnicode('\u1D161'))
        self.roundTrip([toUnicode('\u1D161')])
        self.roundTrip({toUnicode('\u1D161'):toUnicode('\u1D161')})
        
        # Smiley face emoji
        self.roundTrip(toUnicode('\U0001f604'))
        self.roundTrip([toUnicode('\U0001f604'), toUnicode('\U0001f604')])
        self.roundTrip({toUnicode('\U0001f604'):toUnicode('\U0001f604')})
    
    def testNone(self):
        self.roundTrip(None)
        self.roundTrip({'1':None})
        self.roundTrip([None, None, None])
    
    def testBools(self):
        self.roundTrip(True)
        self.roundTrip(False)
        
        self.roundTrip([True, False])
        
        self.roundTrip({'a':True, 'b':False}, reprTest=False)
    
    def testUniques(self):
        root = {'hi':'there', 'halloo':'there'}
        self.roundTrip(root, reprTest=False)
    
    def testAllEmpties(self):
        '''Primarily testint that an empty unicode and bytes are not mixed up'''
        self.roundTrip([unicodeStr(''), '', b'', [], {}], expected=['', '', b'', [], {}])
    
    def testLargeDict(self):
        d = dict((str(x), str(x)) for x in xrange(0, 1000))
        self.roundTrip(d, reprTest=False)
        
    def testWriteToFile(self):
        for is_binary in [True, False]:
            with tempfile.NamedTemporaryFile(mode='w%s' % ('b' if is_binary else ''), suffix='.plist') as plistFile:
                # clear out the created file
                os.unlink(plistFile.name)
                self.assertFalse(os.path.exists(plistFile.name))
                
                # write to disk
                writePlist([1, 2, 3], plistFile.name, binary=is_binary)
                self.assertTrue(os.path.exists(plistFile.name))
                
                with open(plistFile.name, 'r%s' % ('b' if is_binary else '')) as f:
                    fileContents = f.read()
                    self.lintPlist(fileContents)
    
    def testBadKeys(self):
        try:
            self.roundTrip({None:1})
            self.fail("None is not a valid key in Cocoa.")
        except InvalidPlistException as e:
            pass
        try:
            self.roundTrip({Data(b"hello world"):1})
            self.fail("Data is not a valid key in Cocoa.")
        except InvalidPlistException as e:
            pass
        try:
            self.roundTrip({1:1})
            self.fail("Number is not a valid key in Cocoa.")
        except InvalidPlistException as e:
            pass
    
    def testIntBoundaries(self):
        edges = [0xff, 0xffff, 0xffffffff]
        for edge in edges:
            cases = [edge, edge-1, edge+1, edge-2, edge+2, edge*2, edge/2]
            self.roundTrip(cases)
        edges = [-pow(2, 7), pow(2, 7) - 1, 
                 -pow(2, 15), pow(2, 15) - 1, 
                 -pow(2, 31), pow(2, 31) - 1, 
                 -pow(2, 63), pow(2, 64) - 1]
        self.roundTrip(edges, reprTest=False)
        
        ioBytes = io.BytesIO()
        writer = PlistWriter(ioBytes)
        bytes = [(1, [pow(2, 7) - 1]),
                 (2, [pow(2, 15) - 1]),
                 (4, [pow(2, 31) - 1]),
                 (8, [-pow(2, 7), -pow(2, 15), -pow(2, 31), -pow(2, 63), pow(2, 63) - 1]),
                 (16, [pow(2, 64) - 1])
            ]
        for bytelen, tests in bytes:
            for test in tests:
                got = writer.intSize(test)
                self.assertEqual(bytelen, got, "Byte size is wrong. Expected %d, got %d" % (bytelen, got))
        
        bytes_lists = [list(x) for x in bytes]
        self.roundTrip(bytes_lists, reprTest=False)
        
        try:
            self.roundTrip([0x10000000000000000, pow(2, 64)])
            self.fail("2^64 should be too large for Core Foundation to handle.")
        except InvalidPlistException as e:
            pass
    
    def testUnicode2(self):
        unicodeRoot = toUnicode("Mirror's Edge\u2122 for iPad")
        self.roundTrip(unicodeRoot)
        unicodeStrings = [toUnicode("Mirror's Edge\u2122 for iPad"), toUnicode('Weightbot \u2014 Track your Weight in Style')]
        self.roundTrip(unicodeStrings)
        self.roundTrip({toUnicode(""):toUnicode("")}, expected={'':''})
        self.roundTrip(toUnicode(""), expected='')
    
    def testWriteData(self):
        self.roundTrip(Data(b"woohoo"))

    def testEmptyData(self):
        data = Data(b'')
        binplist = writePlistToString(data)
        plist = readPlistFromString(binplist)
        self.assertEqual(plist, data)
        self.assertEqual(type(plist), type(data))
        
    def testUidWrite(self):
        self.roundTrip({'$version': 100000, 
            '$objects': 
                ['$null', 
                 {'$class': Uid(3), 'somekey': Uid(2)}, 
                 'object value as string', 
                 {'$classes': ['Archived', 'NSObject'], '$classname': 'Archived'}
                 ], 
            '$top': {'root': Uid(1)}, '$archiver': 'NSKeyedArchiver'}, reprTest=False)
    
    def testUidRoundTrip(self):
        # Per https://github.com/wooster/biplist/issues/9
        self.roundTrip(Uid(1))
        self.roundTrip([Uid(1), 1])
        self.roundTrip([1, Uid(1)])
        self.roundTrip([Uid(1), Uid(1)])
    
    def testRecursiveWrite(self):
        # Apple libraries disallow recursive containers, so we should fail on
        # trying to write those.
        root = []
        child = [root]
        root.extend(child)
        try:
            writePlistToString(root)
            self.fail("Should not be able to write plists with recursive containers.")
        except InvalidPlistException as e:
            pass
        except:
            self.fail("Should get an invalid plist exception for recursive containers.")

if __name__ == '__main__':
    unittest.main()