blob: 5a8201c3d0e9aa5ed675e4dd4d1fbdb31310e401 [file] [log] [blame]
Jack Jansenc0b2b722003-11-18 22:36:12 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
Jack Jansenc0b2b722003-11-18 22:36:12 +00004import os
Jack Jansenc0b2b722003-11-18 22:36:12 +00005from test import test_support
6import struct
R. David Murray59beec32009-03-30 19:04:00 +00007
8MacOS = test_support.import_module('MacOS')
9# The following should exist if MacOS does.
10import macostools
Jack Jansenc0b2b722003-11-18 22:36:12 +000011import applesingle
R. David Murray59beec32009-03-30 19:04:00 +000012import Carbon.File
Jack Jansenc0b2b722003-11-18 22:36:12 +000013
14AS_MAGIC=0x00051600
15AS_VERSION=0x00020000
16dataforkdata = 'hello\r\0world\n'
17resourceforkdata = 'goodbye\ncruel\0world\r'
18
Ronald Oussoren6c107482006-04-17 13:40:08 +000019applesingledata = struct.pack(">ll16sh", AS_MAGIC, AS_VERSION, "foo", 2) + \
20 struct.pack(">llllll", 1, 50, len(dataforkdata),
Jack Jansenc0b2b722003-11-18 22:36:12 +000021 2, 50+len(dataforkdata), len(resourceforkdata)) + \
22 dataforkdata + \
23 resourceforkdata
24TESTFN2 = test_support.TESTFN + '2'
25
26class 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 Peters58eb11c2004-01-18 20:29:55 +000059
Jack Jansenc0b2b722003-11-18 22:36:12 +000060 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
68def test_main():
69 test_support.run_unittest(TestApplesingle)
70
71
72if __name__ == '__main__':
73 test_main()