def update_namespace_name(file, namespace): """ Update the namespace name for the file. """ contents = py_common.open_file_and_get_contents(file) contents = contents.replace("$TestCaseNamespace$", namespace) py_common.write_file(file, contents)
def update_and_rename_Main_csproj(file_path, cwe_name, csproj_guid, testcasesupport_csproj_guid): is_split = False if os.path.basename(file_path).startswith('s'): is_split = True cwe = cwe_name if is_split: cwe += "_" + os.path.basename(file_path) src_file = os.path.join(file_path, "Main.csproj") dest_file = os.path.join(file_path, cwe + ".csproj") # If the project file already exists, delete it first or the rename function won't work on Windows if os.path.isfile(dest_file): os.remove(dest_file) os.rename(src_file, dest_file) contents = py_common.open_file_and_get_contents(dest_file) contents = contents.replace("$CWE$", cwe) contents = contents.replace("$CWE_ROOT$", cwe_name) contents = contents.replace("$CWE_CSPROJ_GUID$", csproj_guid) contents = contents.replace("$TCS_CSPROJ_GUID$", testcasesupport_csproj_guid) test_case_files = py_common.find_files_in_dir(file_path, "^CWE.*\.cs$") items = "" for test_case_file in test_case_files: # This check is necessary due to CWE468 as this CWE contains 2 identical .cs files, # however they are in different directories, so we need to ensure they both get added # correctly by adding the path to the 2nd .cs file (HelperClass) test_case_file_dir = os.path.basename(os.path.dirname(test_case_file)) if test_case_file_dir.lower().startswith('cwe') or re.match( 's\d\d', test_case_file_dir, re.IGNORECASE): items += ' <Compile Include="' + os.path.basename( test_case_file) + '" />\n' else: items += ' <Compile Include="' + os.path.join( test_case_file_dir, os.path.basename(test_case_file)) + '" />\n' contents = contents.replace("$FILE_NAMES$", items) if os.path.basename(file_path).startswith('s'): contents = contents.replace("$SPLIT_DIR$", '..\\') else: contents = contents.replace("$SPLIT_DIR$", '') py_common.write_file(dest_file, contents)
def update_AssemblyInfo_cs(file_path, cwe_name, csproj_guid): is_split = False if os.path.basename(file_path).startswith('s'): is_split = True cwe = cwe_name if is_split: cwe += "_" + os.path.basename(file_path) assemblyinfo_dot_cs = os.path.join(file_path, "Properties", "AssemblyInfo.cs") contents = py_common.open_file_and_get_contents(assemblyinfo_dot_cs) contents = contents.replace("$CWE$", cwe) # Have to remove the leading { and trailing } contents = contents.replace("$CWE_CSPROJ_GUID$", csproj_guid.lower()[1:-1]) py_common.write_file(assemblyinfo_dot_cs, contents)
def update_file(file_path, file, tag_start, tag_end, lines): full_file_path = os.path.join(file_path, file) file_contents = py_common.open_file_and_get_contents(full_file_path) # get contents from start of file up to tag, get contents from end tag # to EOF up_to_tag_start = file_contents.split(tag_start)[0] tag_end_to_eof = file_contents.split(tag_end)[1] auto_gen_content = "\n".join(lines) # re-build the file with the modified content between the tags modified_file_contents = up_to_tag_start + \ tag_start + "\n" + \ auto_gen_content + "\n" + \ "\t" + tag_end + \ tag_end_to_eof # write out the new file outfile = os.path.join(file_path, file) py_common.write_file(outfile, modified_file_contents)
def update_and_rename_Main_sln(file_path, cwe_name, csproj_guid, testcasesupport_csproj_guid): is_split = False if os.path.basename(file_path).startswith('s'): is_split = True cwe = cwe_name if is_split: cwe += "_" + os.path.basename(file_path) src_file = os.path.join(file_path, "Main.sln") dest_file = os.path.join(file_path, cwe + ".sln") # If the solution file already exists, delete it first or the rename function won't work on Windows if os.path.isfile(dest_file): os.remove(dest_file) os.rename(src_file, dest_file) contents = py_common.open_file_and_get_contents(dest_file) contents = contents.replace("$CWE$", cwe) # generate fake project GUID cwe_project_guid = build_guid(cwe + ".sln") contents = contents.replace("$CWE_PROJECT_GUID$", cwe_project_guid) contents = contents.replace("$CWE_CSPROJ_GUID$", csproj_guid) contents = contents.replace("$TCS_CSPROJ_GUID$", testcasesupport_csproj_guid) if is_split: contents = contents.replace("$SPLIT_DIR$", '..\\') else: contents = contents.replace("$SPLIT_DIR$", '') py_common.write_file(dest_file, contents)
if not os.path.exists("testcases"): py_common.print_with_timestamp( "Wrong working directory; could not find testcases directory") exit() global testregex # Only include the tests cases that are relevant, that don't involve sockets #testregex = "CWE(122|XXX|YYY|ZZZ)+((?!socket).)*(\.c)$" testregex = "CWE(665)+((?!socket).)*(\.c)$" # update main_linux.cpp/testcases.h # Only take the test cases we care about, and only the .c tests testcase_files = asan_update_main_cpp_and_testcases_h.build_list_of_primary_c_cpp_testcase_files( "testcases", [testregex]) fcl = asan_update_main_cpp_and_testcases_h.generate_calls_to_fxs( testcase_files) linux_fcl = asan_update_main_cpp_and_testcases_h.generate_calls_to_linux_fxs( testcase_files) asan_update_main_cpp_and_testcases_h.update_main_cpp( "testcasesupport", "main_linux.c", linux_fcl) # update testcases.h to include all functions (including w32-only) asan_update_main_cpp_and_testcases_h.update_testcases_h( "testcasesupport", "testcases.h", fcl) dirs = get_directory_names_to_compile("testcases") makefile_contents = create_makefile(dirs) py_common.write_file("Makefile_asan", makefile_contents)
cpp_files_exist = check_if_cpp_files_exist(sub_dir) if use_debug: bat_contents = create_batch_file( cwe, split_debug_compile_flags, split_debug_flags, c_files_exist, cpp_files_exist, is_dir_split) else: bat_contents = create_batch_file(cwe, split_compile_flags, split_linker_flags, c_files_exist, cpp_files_exist, is_dir_split) bat_filename = cwe + ".bat" bat_fullpath = os.path.join(sub_dir, bat_filename) py_common.write_file(bat_fullpath, bat_contents) linux_testcase_exists = False for file in testcase_files: if ('w32' not in file) and ('wchar_t' not in file): linux_testcase_exists = True break # only generate main_linux.cpp and Makefile if there are Linux test cases for this CWE if linux_testcase_exists: shutil.copy("testcasesupport\\main_linux.cpp", sub_dir) linux_fcl = update_main_cpp_and_testcases_h.generate_calls_to_linux_fxs( testcase_files) update_main_cpp_and_testcases_h.update_main_cpp( sub_dir, "main_linux.cpp", linux_fcl)
def update_package_name(file, package): lines = py_common.open_file_and_get_lines(file) lines.pop(0) lines.insert(0, "package " + package + ";\n") py_common.write_file(file, "".join(lines))
cwe = this_cwe_dir[cwe_index:unders_index] sub_dir_number = os.path.basename(sub_dir) cwe = cwe + "_" + sub_dir_number # check if any .c files exist to compile c_files_exist = check_if_c_files_exist(sub_dir) cpp_files_exist = check_if_cpp_files_exist(sub_dir) if use_debug: bat_contents = create_batch_file(cwe, split_debug_compile_flags, split_debug_flags, c_files_exist, cpp_files_exist, is_dir_split) else: bat_contents = create_batch_file(cwe, split_compile_flags, split_linker_flags, c_files_exist, cpp_files_exist, is_dir_split) bat_filename = cwe + ".bat" bat_fullpath = os.path.join(sub_dir, bat_filename) py_common.write_file(bat_fullpath, bat_contents) linux_testcase_exists = False for file in testcase_files: if ('w32' not in file) and ('wchar_t' not in file): linux_testcase_exists = True break; # only generate main_linux.cpp and Makefile if there are Linux test cases for this CWE if linux_testcase_exists: shutil.copy("testcasesupport/main_linux.cpp", sub_dir); linux_fcl = update_main_cpp_and_testcases_h.generate_calls_to_linux_fxs(testcase_files) update_main_cpp_and_testcases_h.update_main_cpp(sub_dir, "main_linux.cpp", linux_fcl) # no need to update testcases.h
dirs = set() for file in files: base_dir = os.path.dirname(file) dirs.add(base_dir) return dirs if __name__ == "__main__": # check if ./testcases directory exists, if not, we are running # from wrong working directory if not os.path.exists("testcases"): print_with_timestamp("Wrong working directory; could not find testcases directory") exit() # update main.cpp/testcases.h to call only this cwe's testcases testcase_files = update_main_cpp_and_testcases_h.build_list_of_primary_c_cpp_testcase_files("testcases", None) fcl = update_main_cpp_and_testcases_h.generate_calls_to_fxs(testcase_files) update_main_cpp_and_testcases_h.update_main_cpp("testcasesupport", "main.cpp", fcl) update_main_cpp_and_testcases_h.update_testcases_h("testcasesupport", "testcases.h", fcl) dirs = get_directory_names_to_compile("testcases") dirs = dirs.union(get_directory_names_to_compile("testcasesupport")) linker_flags = create_per_cwe_files.linker_flags.replace("..\\..\\", "") compile_flags = linker_flags + " /c" bat_contents = create_batch_file(dirs, compile_flags, linker_flags) py_common.write_file("compile_all.bat", bat_contents)
dirs = set() for file in files: base_dir = os.path.dirname(file) dirs.add(base_dir) return dirs if __name__ == "__main__": # check if ./testcases directory exists, if not, we are running # from wrong working directory if not os.path.exists("testcases"): py_common.print_with_timestamp("Wrong working directory; could not find testcases directory") exit() # update main_linux.cpp/testcases.h testcase_files = update_main_cpp_and_testcases_h.build_list_of_primary_c_cpp_testcase_files("testcases", None) fcl = update_main_cpp_and_testcases_h.generate_calls_to_fxs(testcase_files) linux_fcl = update_main_cpp_and_testcases_h.generate_calls_to_linux_fxs(testcase_files) update_main_cpp_and_testcases_h.update_main_cpp("testcasesupport", "main_linux.cpp", linux_fcl) # update testcases.h to include all functions (including w32-only) update_main_cpp_and_testcases_h.update_testcases_h("testcasesupport", "testcases.h", fcl) dirs = get_directory_names_to_compile("testcases") makefile_contents = create_makefile(dirs) py_common.write_file("Makefile_all", makefile_contents)
return dirs if __name__ == "__main__": # check if ./testcases directory exists, if not, we are running # from wrong working directory if not os.path.exists("testcases"): print_with_timestamp( "Wrong working directory; could not find testcases directory") exit() # update main.cpp/testcases.h to call only this cwe's testcases testcase_files = update_main_cpp_and_testcases_h.build_list_of_primary_c_cpp_testcase_files( "testcases", None) fcl = update_main_cpp_and_testcases_h.generate_calls_to_fxs(testcase_files) update_main_cpp_and_testcases_h.update_main_cpp("testcasesupport", "main.cpp", fcl) update_main_cpp_and_testcases_h.update_testcases_h("testcasesupport", "testcases.h", fcl) dirs = get_directory_names_to_compile("testcases") dirs = dirs.union(get_directory_names_to_compile("testcasesupport")) linker_flags = create_per_cwe_files.linker_flags.replace("..\\..\\", "") compile_flags = linker_flags + " /c" bat_contents = create_batch_file(dirs, compile_flags, linker_flags) py_common.write_file("compile_all.bat", bat_contents)