blob: efc1654a6b7107a2e6cf2b90cc97b9c4df3ab629 [file] [log] [blame]
Guido van Rossum7b8f1ab1999-10-19 17:48:54 +00001"""Test script for the binhex C module
Roger E. Masse2a1c8341997-01-16 16:44:09 +00002
3 Uses the mechanism of the python binhex module
Fred Drake275dfda2001-05-22 21:01:14 +00004 Based on an original test by Roger E. Masse.
Roger E. Masse2a1c8341997-01-16 16:44:09 +00005"""
Fred Drake275dfda2001-05-22 21:01:14 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Hai Shi883bc632020-07-06 17:12:49 +08008from test.support import import_helper
9from test.support import os_helper
10from test.support import warnings_helper
Roger E. Masse2a1c8341997-01-16 16:44:09 +000011
Hai Shi883bc632020-07-06 17:12:49 +080012
13with warnings_helper.check_warnings(('', DeprecationWarning)):
14 binhex = import_helper.import_fresh_module('binhex')
Victor Stinnerbeea26b2020-01-22 20:44:22 +010015
Roger E. Masse2a1c8341997-01-16 16:44:09 +000016
Fred Drake275dfda2001-05-22 21:01:14 +000017class BinHexTestCase(unittest.TestCase):
Roger E. Masse2a1c8341997-01-16 16:44:09 +000018
Fred Drake275dfda2001-05-22 21:01:14 +000019 def setUp(self):
Serhiy Storchaka700cfa82020-06-25 17:56:31 +030020 # binhex supports only file names encodable to Latin1
Hai Shi883bc632020-07-06 17:12:49 +080021 self.fname1 = os_helper.TESTFN_ASCII + "1"
22 self.fname2 = os_helper.TESTFN_ASCII + "2"
23 self.fname3 = os_helper.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
Fred Drake004d5e62000-10-23 17:22:08 +000024
Fred Drake275dfda2001-05-22 21:01:14 +000025 def tearDown(self):
Hai Shi883bc632020-07-06 17:12:49 +080026 os_helper.unlink(self.fname1)
27 os_helper.unlink(self.fname2)
28 os_helper.unlink(self.fname3)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000029
Guido van Rossumdcee3c02007-05-22 22:25:42 +000030 DATA = b'Jack is my hero'
Roger E. Masse2a1c8341997-01-16 16:44:09 +000031
Fred Drake275dfda2001-05-22 21:01:14 +000032 def test_binhex(self):
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020033 with open(self.fname1, 'wb') as f:
34 f.write(self.DATA)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000035
Fred Drake275dfda2001-05-22 21:01:14 +000036 binhex.binhex(self.fname1, self.fname2)
37
38 binhex.hexbin(self.fname2, self.fname1)
39
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020040 with open(self.fname1, 'rb') as f:
41 finish = f.readline()
Fred Drake275dfda2001-05-22 21:01:14 +000042
43 self.assertEqual(self.DATA, finish)
44
Nick Coghlanb3c728f2011-03-16 21:26:40 -040045 def test_binhex_error_on_long_filename(self):
46 """
47 The testcase fails if no exception is raised when a filename parameter provided to binhex.binhex()
48 is too long, or if the exception raised in binhex.binhex() is not an instance of binhex.Error.
49 """
50 f3 = open(self.fname3, 'wb')
51 f3.close()
52
53 self.assertRaises(binhex.Error, binhex.binhex, self.fname3, self.fname2)
Fred Drake275dfda2001-05-22 21:01:14 +000054
Ronald Oussoren2165cea2020-11-01 10:08:48 +010055 def test_binhex_line_endings(self):
56 # bpo-29566: Ensure the line endings are those for macOS 9
57 with open(self.fname1, 'wb') as f:
58 f.write(self.DATA)
59
60 binhex.binhex(self.fname1, self.fname2)
61
62 with open(self.fname2, 'rb') as fp:
63 contents = fp.read()
64
65 self.assertNotIn(b'\n', contents)
66
Fred Drake2e2be372001-09-20 21:33:42 +000067def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000068 support.run_unittest(BinHexTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000069
70
71if __name__ == "__main__":
72 test_main()