Dash Core  0.12.2.1
P2P Digital Currency
test-security-check.py
Go to the documentation of this file.
1 #!/usr/bin/python2
2 '''
3 Test script for security-check.py
4 '''
5 from __future__ import division,print_function
6 import subprocess
7 import sys
8 import unittest
9 
10 def write_testcode(filename):
11  with open(filename, 'w') as f:
12  f.write('''
13  #include <stdio.h>
14  int main()
15  {
16  printf("the quick brown fox jumps over the lazy god\\n");
17  return 0;
18  }
19  ''')
20 
21 def call_security_check(cc, source, executable, options):
22  subprocess.check_call([cc,source,'-o',executable] + options)
23  p = subprocess.Popen(['./security-check.py',executable], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
24  (stdout, stderr) = p.communicate()
25  return (p.returncode, stdout.rstrip())
26 
27 class TestSecurityChecks(unittest.TestCase):
28  def test_ELF(self):
29  source = 'test1.c'
30  executable = 'test1'
31  cc = 'gcc'
32  write_testcode(source)
33 
34  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-zexecstack','-fno-stack-protector','-Wl,-znorelro']),
35  (1, executable+': failed PIE NX RELRO Canary'))
36  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fno-stack-protector','-Wl,-znorelro']),
37  (1, executable+': failed PIE RELRO Canary'))
38  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro']),
39  (1, executable+': failed PIE RELRO'))
40  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-znorelro','-pie','-fPIE']),
41  (1, executable+': failed RELRO'))
42  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,-znoexecstack','-fstack-protector-all','-Wl,-zrelro','-Wl,-z,now','-pie','-fPIE']),
43  (0, ''))
44 
45  def test_PE(self):
46  source = 'test1.c'
47  executable = 'test1.exe'
48  cc = 'i686-w64-mingw32-gcc'
49  write_testcode(source)
50 
51  self.assertEqual(call_security_check(cc, source, executable, []),
52  (1, executable+': failed PIE NX'))
53  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat']),
54  (1, executable+': failed PIE'))
55  self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--dynamicbase']),
56  (0, ''))
57 
58 if __name__ == '__main__':
59  unittest.main()
60 
def call_security_check(cc, source, executable, options)
def write_testcode(filename)