blob: 2f3d53afbd1321345e6c26dfe4acd0fd3c890c89 [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"""
6import binhex
Fred Drake275dfda2001-05-22 21:01:14 +00007import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test import support
Roger E. Masse2a1c8341997-01-16 16:44:09 +00009
Roger E. Masse2a1c8341997-01-16 16:44:09 +000010
Fred Drake275dfda2001-05-22 21:01:14 +000011class BinHexTestCase(unittest.TestCase):
Roger E. Masse2a1c8341997-01-16 16:44:09 +000012
Fred Drake275dfda2001-05-22 21:01:14 +000013 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000014 self.fname1 = support.TESTFN + "1"
15 self.fname2 = support.TESTFN + "2"
Nick Coghlanb3c728f2011-03-16 21:26:40 -040016 self.fname3 = support.TESTFN + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"
Fred Drake004d5e62000-10-23 17:22:08 +000017
Fred Drake275dfda2001-05-22 21:01:14 +000018 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000019 support.unlink(self.fname1)
20 support.unlink(self.fname2)
Nick Coghlanb3c728f2011-03-16 21:26:40 -040021 support.unlink(self.fname3)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000022
Guido van Rossumdcee3c02007-05-22 22:25:42 +000023 DATA = b'Jack is my hero'
Roger E. Masse2a1c8341997-01-16 16:44:09 +000024
Fred Drake275dfda2001-05-22 21:01:14 +000025 def test_binhex(self):
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020026 with open(self.fname1, 'wb') as f:
27 f.write(self.DATA)
Roger E. Masse2a1c8341997-01-16 16:44:09 +000028
Fred Drake275dfda2001-05-22 21:01:14 +000029 binhex.binhex(self.fname1, self.fname2)
30
31 binhex.hexbin(self.fname2, self.fname1)
32
Serhiy Storchaka5b10b982019-03-05 10:06:26 +020033 with open(self.fname1, 'rb') as f:
34 finish = f.readline()
Fred Drake275dfda2001-05-22 21:01:14 +000035
36 self.assertEqual(self.DATA, finish)
37
Nick Coghlanb3c728f2011-03-16 21:26:40 -040038 def test_binhex_error_on_long_filename(self):
39 """
40 The testcase fails if no exception is raised when a filename parameter provided to binhex.binhex()
41 is too long, or if the exception raised in binhex.binhex() is not an instance of binhex.Error.
42 """
43 f3 = open(self.fname3, 'wb')
44 f3.close()
45
46 self.assertRaises(binhex.Error, binhex.binhex, self.fname3, self.fname2)
Fred Drake275dfda2001-05-22 21:01:14 +000047
Fred Drake2e2be372001-09-20 21:33:42 +000048def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000049 support.run_unittest(BinHexTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000050
51
52if __name__ == "__main__":
53 test_main()