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