Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/test-security-check.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2017 The Bitcoin Core developers | # Copyright (c) 2015-2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
""" | """ | ||||
Test script for security-check.py | Test script for security-check.py | ||||
""" | """ | ||||
import os | import os | ||||
import subprocess | import subprocess | ||||
import unittest | import unittest | ||||
from typing import List | |||||
from utils import determine_wellknown_cmd | from utils import determine_wellknown_cmd | ||||
def write_testcode(filename): | def write_testcode(filename): | ||||
with open(filename, "w", encoding="utf8") as f: | with open(filename, "w", encoding="utf8") as f: | ||||
f.write(""" | f.write(""" | ||||
#include <stdio.h> | #include <stdio.h> | ||||
int main() | int main() | ||||
{ | { | ||||
printf("the quick brown fox jumps over the lazy god\\n"); | printf("the quick brown fox jumps over the lazy god\\n"); | ||||
return 0; | return 0; | ||||
} | } | ||||
""") | """) | ||||
def clean_files(source, executable): | def clean_files(source, executable): | ||||
os.remove(source) | os.remove(source) | ||||
os.remove(executable) | os.remove(executable) | ||||
def call_security_check(cc, source, executable, options): | def call_security_check(cc, source, executable, options): | ||||
subprocess.run([*cc, source, "-o", executable] + options, check=True) | # This should behave the same as AC_TRY_LINK, so arrange well-known flags | ||||
# in the same order as autoconf would. | |||||
# | |||||
# See the definitions for ac_link in autoconf's lib/autoconf/c.m4 file for | |||||
# reference. | |||||
env_flags: List[str] = [] | |||||
for var in ["CFLAGS", "CPPFLAGS", "LDFLAGS"]: | |||||
env_flags += filter(None, os.environ.get(var, "").split(" ")) | |||||
subprocess.run([*cc, source, "-o", executable] + env_flags + options, check=True) | |||||
p = subprocess.run( | p = subprocess.run( | ||||
["./contrib/devtools/security-check.py", executable], | ["./contrib/devtools/security-check.py", executable], | ||||
stdout=subprocess.PIPE, | stdout=subprocess.PIPE, | ||||
universal_newlines=True, | universal_newlines=True, | ||||
) | ) | ||||
return p.returncode, p.stdout.rstrip() | return p.returncode, p.stdout.rstrip() | ||||
▲ Show 20 Lines • Show All 282 Lines • Show Last 20 Lines |