Debian Codegraph @

tests/test_invalid_binary.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env python
2 '''
3 PEXPECT LICENSE
4
5 This license is approved by the OSI and FSF as GPL-compatible.
6 http://opensource.org/licenses/isc-license.txt
7
8 Copyright (c) 2012, Noah Spurrier <[email protected]>
9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 '''
21 import time
22 import unittest
23 from ptyprocess import PtyProcess, PtyProcessUnicode
24 import errno
25 import os
26 import stat
27 import tempfile
28
29 class InvalidBinaryChars(unittest.TestCase):
30
31 def test_invalid_binary(self):
32 '''This tests that we correctly handle the case where we attempt to
33 spawn a child process but the exec call fails'''
34
35 # Create a file that should fail the exec call
36 dirpath = tempfile.mkdtemp()
37 fullpath = os.path.join(dirpath, "test")
38
39 with open(fullpath, 'wb') as f:
40 # Add some constant so it will never be executable
41 # - Not 0x54AD (Windows PE)
42 # - Not 0x7FEF (ELF)
43 # - Not 0410 or 0413 (a.out)
44 # - Not 0x2321 (script)
45 file_start = b'\x00\x00'
46 file_data = file_start + os.urandom(1022)
47 f.write(file_data)
48
49 # Make it executable
50 st = os.stat(fullpath)
51 os.chmod(fullpath, st.st_mode | stat.S_IEXEC)
52
53 # TODO Verify this does what is intended on Windows
54 try:
55 child = PtyProcess.spawn([fullpath])
56 # If we get here then an OSError was not raised
57 child.close()
58 raise AssertionError("OSError was not raised")
59 except OSError as err:
60 if errno.ENOEXEC == err.errno:
61 # This is what should happen
62 pass
63 else:
64 # Re-raise the original error to fail the test
65 raise
66 finally:
67 os.unlink(fullpath)
68 os.rmdir(dirpath)
69
70 if __name__ == '__main__':
71 unittest.main()
72
73 suite = unittest.makeSuite(InvalidBinaryChars,'test')
74