1# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import os
5from test import test_support
6import struct
7
8MacOS = test_support.import_module('MacOS')
9# The following should exist if MacOS does.
10import applesingle
11
12AS_MAGIC=0x00051600
13AS_VERSION=0x00020000
14dataforkdata = 'hello\r\0world\n'
15resourceforkdata = 'goodbye\ncruel\0world\r'
16
17applesingledata = struct.pack(">ll16sh", AS_MAGIC, AS_VERSION, "foo", 2) + \
18    struct.pack(">llllll", 1, 50, len(dataforkdata),
19        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)
57
58    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()
72