Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 1 | # Copyright (C) 2003 Python Software Foundation |
| 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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 8 | |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 9 | |
| 10 | # This test data was generated through Cocoa's NSDictionary class |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 11 | TESTDATA = b"""<?xml version="1.0" encoding="UTF-8"?> |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 12 | <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \ |
| 13 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 14 | <plist version="1.0"> |
| 15 | <dict> |
| 16 | <key>aDate</key> |
| 17 | <date>2004-10-26T10:33:33Z</date> |
| 18 | <key>aDict</key> |
| 19 | <dict> |
| 20 | <key>aFalseValue</key> |
| 21 | <false/> |
| 22 | <key>aTrueValue</key> |
| 23 | <true/> |
| 24 | <key>aUnicodeValue</key> |
| 25 | <string>M\xc3\xa4ssig, Ma\xc3\x9f</string> |
| 26 | <key>anotherString</key> |
| 27 | <string><hello & 'hi' there!></string> |
| 28 | <key>deeperDict</key> |
| 29 | <dict> |
| 30 | <key>a</key> |
| 31 | <integer>17</integer> |
| 32 | <key>b</key> |
| 33 | <real>32.5</real> |
| 34 | <key>c</key> |
| 35 | <array> |
| 36 | <integer>1</integer> |
| 37 | <integer>2</integer> |
| 38 | <string>text</string> |
| 39 | </array> |
| 40 | </dict> |
| 41 | </dict> |
| 42 | <key>aFloat</key> |
| 43 | <real>0.5</real> |
| 44 | <key>aList</key> |
| 45 | <array> |
| 46 | <string>A</string> |
| 47 | <string>B</string> |
| 48 | <integer>12</integer> |
| 49 | <real>32.5</real> |
| 50 | <array> |
| 51 | <integer>1</integer> |
| 52 | <integer>2</integer> |
| 53 | <integer>3</integer> |
| 54 | </array> |
| 55 | </array> |
| 56 | <key>aString</key> |
| 57 | <string>Doodah</string> |
| 58 | <key>anInt</key> |
| 59 | <integer>728</integer> |
| 60 | <key>nestedData</key> |
| 61 | <array> |
| 62 | <data> |
| 63 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r |
| 64 | PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5 |
| 65 | IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi |
| 66 | aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz |
| 67 | IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID |
| 68 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== |
| 69 | </data> |
| 70 | </array> |
| 71 | <key>someData</key> |
| 72 | <data> |
| 73 | PGJpbmFyeSBndW5rPg== |
| 74 | </data> |
| 75 | <key>someMoreData</key> |
| 76 | <data> |
| 77 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8 |
| 78 | bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs |
| 79 | b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv |
| 80 | dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90 |
| 81 | cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== |
| 82 | </data> |
| 83 | <key>\xc3\x85benraa</key> |
| 84 | <string>That was a unicode key.</string> |
| 85 | </dict> |
| 86 | </plist> |
Neal Norwitz | eaa16f9 | 2007-08-30 06:37:08 +0000 | [diff] [blame] | 87 | """.replace(b" " * 8, b"\t") # Apple as well as plistlib.py output hard tabs |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 88 | |
| 89 | |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 90 | class TestPlistlib(unittest.TestCase): |
| 91 | |
| 92 | def tearDown(self): |
| 93 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 94 | os.unlink(support.TESTFN) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 95 | except: |
| 96 | pass |
| 97 | |
| 98 | def _create(self): |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 99 | pl = dict( |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 100 | aString="Doodah", |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 101 | aList=["A", "B", 12, 32.5, [1, 2, 3]], |
| 102 | aFloat = 0.5, |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 103 | anInt = 728, |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 104 | aDict=dict( |
| 105 | anotherString="<hello & 'hi' there!>", |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 106 | aUnicodeValue='M\xe4ssig, Ma\xdf', |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 107 | aTrueValue=True, |
| 108 | aFalseValue=False, |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 109 | deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]), |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 110 | ), |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 111 | someData = plistlib.Data(b"<binary gunk>"), |
| 112 | someMoreData = plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10), |
| 113 | nestedData = [plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10)], |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 114 | aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 115 | ) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 116 | pl['\xc5benraa'] = "That was a unicode key." |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 117 | return pl |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 118 | |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 119 | def test_create(self): |
| 120 | pl = self._create() |
| 121 | self.assertEqual(pl["aString"], "Doodah") |
| 122 | self.assertEqual(pl["aDict"]["aFalseValue"], False) |
| 123 | |
| 124 | def test_io(self): |
| 125 | pl = self._create() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 126 | plistlib.writePlist(pl, support.TESTFN) |
| 127 | pl2 = plistlib.readPlist(support.TESTFN) |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 128 | self.assertEqual(dict(pl), dict(pl2)) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 130 | def test_bytes(self): |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 131 | pl = self._create() |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 132 | data = plistlib.writePlistToBytes(pl) |
| 133 | pl2 = plistlib.readPlistFromBytes(data) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 134 | self.assertEqual(dict(pl), dict(pl2)) |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 135 | data2 = plistlib.writePlistToBytes(pl2) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 136 | self.assertEqual(data, data2) |
| 137 | |
| 138 | def test_appleformatting(self): |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 139 | pl = plistlib.readPlistFromBytes(TESTDATA) |
| 140 | data = plistlib.writePlistToBytes(pl) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 141 | self.assertEqual(data, TESTDATA, |
| 142 | "generated data was not identical to Apple's output") |
| 143 | |
| 144 | def test_appleformattingfromliteral(self): |
| 145 | pl = self._create() |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 146 | pl2 = plistlib.readPlistFromBytes(TESTDATA) |
Just van Rossum | fc93e17 | 2004-10-26 11:02:08 +0000 | [diff] [blame] | 147 | self.assertEqual(dict(pl), dict(pl2), |
| 148 | "generated data was not identical to Apple's output") |
| 149 | |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 150 | def test_bytesio(self): |
| 151 | from io import BytesIO |
| 152 | b = BytesIO() |
Just van Rossum | bcc58e8 | 2003-07-01 20:22:30 +0000 | [diff] [blame] | 153 | pl = self._create() |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 154 | plistlib.writePlist(pl, b) |
| 155 | pl2 = plistlib.readPlist(BytesIO(b.getvalue())) |
Just van Rossum | bcc58e8 | 2003-07-01 20:22:30 +0000 | [diff] [blame] | 156 | self.assertEqual(dict(pl), dict(pl2)) |
| 157 | |
Just van Rossum | 48ecacc | 2004-11-12 08:34:32 +0000 | [diff] [blame] | 158 | def test_controlcharacters(self): |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 159 | for i in range(128): |
| 160 | c = chr(i) |
| 161 | testString = "string containing %s" % c |
| 162 | if i >= 32 or c in "\r\n\t": |
| 163 | # \r, \n and \t are the only legal control chars in XML |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 164 | plistlib.writePlistToBytes(testString) |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 165 | else: |
| 166 | self.assertRaises(ValueError, |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 167 | plistlib.writePlistToBytes, |
Just van Rossum | 2dae764 | 2004-11-12 09:36:12 +0000 | [diff] [blame] | 168 | testString) |
Just van Rossum | 48ecacc | 2004-11-12 08:34:32 +0000 | [diff] [blame] | 169 | |
| 170 | def test_nondictroot(self): |
| 171 | test1 = "abc" |
| 172 | test2 = [1, 2, 3, "abc"] |
Guido van Rossum | cd869d8 | 2007-08-07 14:26:40 +0000 | [diff] [blame] | 173 | result1 = plistlib.readPlistFromBytes(plistlib.writePlistToBytes(test1)) |
| 174 | result2 = plistlib.readPlistFromBytes(plistlib.writePlistToBytes(test2)) |
Just van Rossum | 48ecacc | 2004-11-12 08:34:32 +0000 | [diff] [blame] | 175 | self.assertEqual(test1, result1) |
| 176 | self.assertEqual(test2, result2) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 180 | support.run_unittest(TestPlistlib) |
Jack Jansen | 838e76a | 2003-02-25 12:58:58 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | if __name__ == '__main__': |
| 184 | test_main() |