import subprocess from subprocess import Popen, PIPE, check_output import sys # Get the command-line argument for the number of incorrect implementations num_incorrect = int(sys.argv[1]) # Get the token and check if it matches file = open("test_cases.cpp", "r") if file.readline().strip() != "// butterfly": # The token does not match. Print HTML message and exit. print("

You do not have the secret token. Do not pass Go. Do not collect $200.

") file.close() raise SystemExit file.close() # Compile testing file try: # This is Python's way of calling the command line. We use it to compile the C++ files. subprocess.check_output(["gcc -std=c++1y -c test_cases.cpp"],stdin=None,stderr=subprocess.STDOUT,shell=True) except subprocess.CalledProcessError as e: # There were compiler errors in test_cases.cpp. Print out the error message and exit the program. print("

",e.output,"

") raise SystemExit # Link the testing file with the correct implementation's object file to produce an executable. try: # This is Python's way of calling the command line. We use it to compile the C++ files. subprocess.check_output(["gcc -std=c++1y -c password_correct.cpp"],stdin=None,stderr=subprocess.STDOUT,shell=True) except subprocess.CalledProcessError as e: # There were compiler errors in password_correct.cpp. Print out the error message and exit the program. print("

",e.output,"

") raise SystemExit # Create the executable file subprocess.call('g++ -o correct.out test_cases.o password_correct.o', stdin=None, stdout=None, stderr=None, shell=True) # Link the testing file with each incorrect implementation's object file to produce an executable. for i in range(num_incorrect): try: # Compile the C++ files subprocess.check_output(["gcc -std=c++1y -c password_incorrect"+str(i+1)+".cpp"],stdin=None,stderr=subprocess.STDOUT,shell=True) # This is Python's way of calling the command line. We use it to compile the C++ files. # Create the executable files subprocess.call('g++ -o incorrect'+str(i+1)+'.out test_cases.o password_incorrect'+str(i+1)+'.o', stdin=None, stdout=None, stderr=None, shell=True) except subprocess.CalledProcessError as e: # There were compiler errors in password_correct.cpp. Print out the error message and exit the program. print("

",e.output,"

") raise SystemExit # Create lists for found and unfound bugs found_bug = [] unfound_bug = [] # Store the correct implementation's output in result p = Popen(['./correct.out'], shell=True, stdout=PIPE, stdin=PIPE) result = p.stdout.read() # For each incorrect implementation, compare its output to result. for i in range(num_incorrect): pi = Popen(['./incorrect'+str(i+1)+'.out'], shell=True, stdout=PIPE, stdin=PIPE) resulti = pi.stdout.read() if result == resulti: # If the outputs match, the bug was not found. unfound_bug += chr(ord('A')+i) else: # If the outputs do not match, the bug was found. found_bug += chr(ord('A')+i) # Print results to HTML print("

Found bugs:") print(found_bug) print("

") print("Unfound bugs:") print(unfound_bug) print("

")