blob: e915028dca3e272f240436e88d92410bf328988e [file] [log] [blame]
Jack Jansenc0b2b722003-11-18 22:36:12 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import macostools
5import Carbon.File
6import MacOS
7import os
Jack Jansenc0b2b722003-11-18 22:36:12 +00008from test import test_support
9import struct
10import applesingle
11
12AS_MAGIC=0x00051600
13AS_VERSION=0x00020000
14dataforkdata = 'hello\r\0world\n'
15resourceforkdata = 'goodbye\ncruel\0world\r'
16
Ronald Oussoren6c107482006-04-17 13:40:08 +000017applesingledata = struct.pack(">ll16sh", AS_MAGIC, AS_VERSION, "foo", 2) + \
18 struct.pack(">llllll", 1, 50, len(dataforkdata),
Jack Jansenc0b2b722003-11-18 22:36:12 +000019 2, 50+len(dataforkdata), len(resourceforkdata)) + \
20 dataforkdata + \
21 resourceforkdata
22TESTFN2 = test_support.TESTFN + '2'
23
24class TestApplesingle(unittest.TestCase):
25
26 def setUp(self):
27 fp = open(test_support.TESTFN, 'w')
28 fp.write(applesingledata)
29 fp.close()
30
31 def tearDown(self):
32 try:
33 os.unlink(test_support.TESTFN)
34 except:
35 pass
36 try:
37 os.unlink(TESTFN2)
38 except:
39 pass
40
41 def compareData(self, isrf, data):
42 if isrf:
43 fp = MacOS.openrf(TESTFN2, '*rb')
44 else:
45 fp = open(TESTFN2, 'rb')
46 filedata = fp.read(1000)
47 self.assertEqual(data, filedata)
48
49 def test_applesingle(self):
50 try:
51 os.unlink(TESTFN2)
52 except:
53 pass
54 applesingle.decode(test_support.TESTFN, TESTFN2)
55 self.compareData(False, dataforkdata)
56 self.compareData(True, resourceforkdata)
Tim Peters58eb11c2004-01-18 20:29:55 +000057
Jack Jansenc0b2b722003-11-18 22:36:12 +000058 def test_applesingle_resonly(self):
59 try:
60 os.unlink(TESTFN2)
61 except:
62 pass
63 applesingle.decode(test_support.TESTFN, TESTFN2, resonly=True)
64 self.compareData(False, resourceforkdata)
65
66def test_main():
67 test_support.run_unittest(TestApplesingle)
68
69
70if __name__ == '__main__':
71 test_main()