1"""Test script for the binhex C module 2 3 Uses the mechanism of the python binhex module 4 Based on an original test by Roger E. Masse. 5""" 6import unittest 7from test import support 8from test.support import import_helper 9from test.support import os_helper 10from test.support import warnings_helper 11 12 13with warnings_helper.check_warnings(('', DeprecationWarning)): 14 binhex = import_helper.import_fresh_module('binhex') 15 16 17class BinHexTestCase(unittest.TestCase): 18 19 def setUp(self): 20 # binhex supports only file names encodable to Latin1 21 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__" 24 25 def tearDown(self): 26 os_helper.unlink(self.fname1) 27 os_helper.unlink(self.fname2) 28 os_helper.unlink(self.fname3) 29 30 DATA = b'Jack is my hero' 31 32 def test_binhex(self): 33 with open(self.fname1, 'wb') as f: 34 f.write(self.DATA) 35 36 binhex.binhex(self.fname1, self.fname2) 37 38 binhex.hexbin(self.fname2, self.fname1) 39 40 with open(self.fname1, 'rb') as f: 41 finish = f.readline() 42 43 self.assertEqual(self.DATA, finish) 44 45 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) 54 55 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 67def test_main(): 68 support.run_unittest(BinHexTestCase) 69 70 71if __name__ == "__main__": 72 test_main() 73