Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 1 | # Copyright (C) 2003 Python Software Foundation |
| 2 | |
| 3 | import unittest |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 4 | import os |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 5 | from test import test_support |
| 6 | import struct |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame^] | 7 | |
| 8 | MacOS = test_support.import_module('MacOS') |
| 9 | # The following should exist if MacOS does. |
| 10 | import macostools |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 11 | import applesingle |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame^] | 12 | import Carbon.File |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 13 | |
| 14 | AS_MAGIC=0x00051600 |
| 15 | AS_VERSION=0x00020000 |
| 16 | dataforkdata = 'hello\r\0world\n' |
| 17 | resourceforkdata = 'goodbye\ncruel\0world\r' |
| 18 | |
Ronald Oussoren | 6c10748 | 2006-04-17 13:40:08 +0000 | [diff] [blame] | 19 | applesingledata = struct.pack(">ll16sh", AS_MAGIC, AS_VERSION, "foo", 2) + \ |
| 20 | struct.pack(">llllll", 1, 50, len(dataforkdata), |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 21 | 2, 50+len(dataforkdata), len(resourceforkdata)) + \ |
| 22 | dataforkdata + \ |
| 23 | resourceforkdata |
| 24 | TESTFN2 = test_support.TESTFN + '2' |
| 25 | |
| 26 | class TestApplesingle(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | fp = open(test_support.TESTFN, 'w') |
| 30 | fp.write(applesingledata) |
| 31 | fp.close() |
| 32 | |
| 33 | def tearDown(self): |
| 34 | try: |
| 35 | os.unlink(test_support.TESTFN) |
| 36 | except: |
| 37 | pass |
| 38 | try: |
| 39 | os.unlink(TESTFN2) |
| 40 | except: |
| 41 | pass |
| 42 | |
| 43 | def compareData(self, isrf, data): |
| 44 | if isrf: |
| 45 | fp = MacOS.openrf(TESTFN2, '*rb') |
| 46 | else: |
| 47 | fp = open(TESTFN2, 'rb') |
| 48 | filedata = fp.read(1000) |
| 49 | self.assertEqual(data, filedata) |
| 50 | |
| 51 | def test_applesingle(self): |
| 52 | try: |
| 53 | os.unlink(TESTFN2) |
| 54 | except: |
| 55 | pass |
| 56 | applesingle.decode(test_support.TESTFN, TESTFN2) |
| 57 | self.compareData(False, dataforkdata) |
| 58 | self.compareData(True, resourceforkdata) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 59 | |
Jack Jansen | c0b2b72 | 2003-11-18 22:36:12 +0000 | [diff] [blame] | 60 | def test_applesingle_resonly(self): |
| 61 | try: |
| 62 | os.unlink(TESTFN2) |
| 63 | except: |
| 64 | pass |
| 65 | applesingle.decode(test_support.TESTFN, TESTFN2, resonly=True) |
| 66 | self.compareData(False, resourceforkdata) |
| 67 | |
| 68 | def test_main(): |
| 69 | test_support.run_unittest(TestApplesingle) |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | test_main() |