blob: 591f32a4f0f7f02266373b485cb138f484c28b14 [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
Roger E. Masse2a1c8341997-01-16 16:44:09 +00008
Victor Stinnerbeea26b2020-01-22 20:44:22 +01009with support.check_warnings(('', DeprecationWarning)):
Victor Stinner9c24e2e2020-06-10 19:54:29 +020010 binhex = support.import_fresh_module('binhex')
Victor Stinnerbeea26b2020-01-22 20:44:22 +010011
Roger E. Masse2a1c8341997-01-16 16:44:09 +000012
Fred Drake275dfda2001-05-22 21:01:14 +000013class BinHexTestCase(unittest.TestCase):
Roger E. Masse2a1c8341997-01-16 16:44:09 +000014
Fred Drake275dfda2001-05-22 21:01:14 +000015 def setUp(self):
Serhiy Storchaka700cfa82020-06-25 17:56:31 +030016 # binhex supports only file names encodable to Latin1
17 self.fname1 = support.TESTFN_ASCII + "1"
18 self.fname2 = support.TESTFN_ASCII + "2"
19 self.fname3 = support.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
Fred Drake004d5e62000-10-23 17:22:08 +000020
Fred Drake275dfda2001-05-22 21:01:14 +000021 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000022 support.unlink(self.fname1)
23 support.unlink(self.fname2)
Nick Coghlanb3c728f2011-03-16 21:26:40 -040024 support.unlink(self.fname3)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000025
Guido van Rossumdcee3c02007-05-22 22:25:42 +000026 DATA = b'Jack is my hero'
Roger E. Masse2a1c8341997-01-16 16:44:09 +000027
Fred Drake275dfda2001-05-22 21:01:14 +000028 def test_binhex(self):
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020029 with open(self.fname1, 'wb') as f:
30 f.write(self.DATA)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000031
Fred Drake275dfda2001-05-22 21:01:14 +000032 binhex.binhex(self.fname1, self.fname2)
33
34 binhex.hexbin(self.fname2, self.fname1)
35
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020036 with open(self.fname1, 'rb') as f:
37 finish = f.readline()
Fred Drake275dfda2001-05-22 21:01:14 +000038
39 self.assertEqual(self.DATA, finish)
40
Nick Coghlanb3c728f2011-03-16 21:26:40 -040041 def test_binhex_error_on_long_filename(self):
42 """
43 The testcase fails if no exception is raised when a filename parameter provided to binhex.binhex()
44 is too long, or if the exception raised in binhex.binhex() is not an instance of binhex.Error.
45 """
46 f3 = open(self.fname3, 'wb')
47 f3.close()
48
49 self.assertRaises(binhex.Error, binhex.binhex, self.fname3, self.fname2)
Fred Drake275dfda2001-05-22 21:01:14 +000050
Fred Drake2e2be372001-09-20 21:33:42 +000051def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000052 support.run_unittest(BinHexTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000053
54
55if __name__ == "__main__":
56 test_main()