Beispiel #1
0
def generate_calls_to_fxs(testcase_files):

    fcl = FunctionCallLines()
    for fullfilepath in testcase_files:

        filename = os.path.basename(fullfilepath)

        # do different things if its a c or cpp file
        match = re.search(
            "^(?P<root>CWE(\d+).*__.*_\d+)((?P<letter>[a-z]*)|_(bad|good\d+))(\.c|\.cpp)$",
            filename)

        if filename.endswith(".cpp"):
            root = match.group(
                "root")  # we don't use the letter in the namespace
            bad = "bad();"
            good = "good();"

            fcl.cpp_bad_lines.append("\tprintLine(\"Calling " + root + "::" +
                                     bad + "\");")
            fcl.cpp_bad_lines.append("\t" + root + "::" + bad + "\n")
            fcl.cpp_h_bad_lines.append("\tnamespace " + root + " { void " +
                                       bad + "}\n")

            fcl.cpp_good_lines.append("\tprintLine(\"Calling " + root + "::" +
                                      good + "\");")
            fcl.cpp_good_lines.append("\t" + root + "::" + good + "\n")
            fcl.cpp_h_good_lines.append("\tnamespace " + root + " { void " +
                                        good + "}\n")

        elif filename.endswith(".c"):
            # we only want to add the "a" files
            if match.group("letter") != "" and match.group("letter") != "a":
                py_common.print_with_timestamp("Ignored file: " + filename)
                continue

            root = match.group("root")
            bad = "_bad();"
            good = "_good();"

            fcl.c_bad_lines.append("\tprintLine(\"Calling " + root + bad +
                                   "\");")
            fcl.c_bad_lines.append("\t" + root + bad + "\n")
            fcl.c_h_bad_lines.append("\tvoid " + root + bad + "\n")

            # don't create good if template file contains point-flaw-badonly.
            file_contents = py_common.open_file_and_get_contents(fullfilepath)
            result = re.search("Template File: point-flaw-badonly",
                               file_contents, re.IGNORECASE)
            if result == None:
                fcl.c_good_lines.append("\tprintLine(\"Calling " + root +
                                        good + "\");")
                fcl.c_good_lines.append("\t" + root + good + "\n")
                fcl.c_h_good_lines.append("\tvoid " + root + good + "\n")

        else:
            raise Exception("filename ends with something we don't handle!: " +
                            fullfilepath)

    return fcl
Beispiel #2
0
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 generate_calls_to_linux_fxs(testcase_files):

	fcl = FunctionCallLines()
	for fullfilepath in testcase_files:

		filename = os.path.basename(fullfilepath)

		# we only want the non w32 and non wchar_t test cases
		if ('w32' not in filename) and ('wchar_t' not in filename):
			
			# do different things if its a c or cpp file
			match = re.search("^(?P<root>CWE(\d+).*__.*_\d+)((?P<letter>[a-z]*)|_(bad|good\d+))(\.c|\.cpp)$", filename)

			if filename.endswith(".cpp"):
				root = match.group("root") # we don't use the letter in the namespace 
				bad = "bad();"
				good = "good();"

				fcl.cpp_bad_lines.append("\tprintLine(\"Calling " + root + "::" + bad + "\");");
				fcl.cpp_bad_lines.append("\t" + root + "::" + bad + "\n")
				fcl.cpp_h_bad_lines.append("\tnamespace " + root + " { void " + bad + "}\n")

				fcl.cpp_good_lines.append("\tprintLine(\"Calling " + root + "::" + good + "\");");
				fcl.cpp_good_lines.append("\t" + root + "::" + good + "\n")
				fcl.cpp_h_good_lines.append("\tnamespace " + root + " { void " + good + "}\n")

			elif filename.endswith(".c"):
				# we only want to add the "a" files
				if match.group("letter") != "" and match.group("letter") != "a":
					py_common.print_with_timestamp("Ignored file: " + filename)
					continue

				root = match.group("root")
				bad = "_bad();"
				good = "_good();"

				fcl.c_bad_lines.append("\tprintLine(\"Calling " + root + bad + "\");");
				fcl.c_bad_lines.append("\t" + root + bad + "\n")
				fcl.c_h_bad_lines.append("\tvoid " + root + bad + "\n")

				# don't create good if template file contains point-flaw-badonly.
				file_contents = py_common.open_file_and_get_contents(fullfilepath)
				result = re.search("Template File: point-flaw-badonly", file_contents, re.IGNORECASE)
				if result == None:
					fcl.c_good_lines.append("\tprintLine(\"Calling " + root + good + "\");");
					fcl.c_good_lines.append("\t" + root + good + "\n")
					fcl.c_h_good_lines.append("\tvoid " + root + good + "\n")
				
			else:
				raise Exception("filename ends with something we don't handle!: " + fullfilepath)

	return fcl
Beispiel #4
0
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)
Beispiel #5
0
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)
Beispiel #6
0
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_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)
Beispiel #8
0
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)
Beispiel #9
0
def generate_calls_to_linux_fxs(testcase_files):

    fcl = FunctionCallLines()
    for fullfilepath in testcase_files:

        filename = os.path.basename(fullfilepath)

        # we only want the non w32 and non wchar_t test cases
        if ('w32' not in filename) and ('wchar_t' not in filename):

            # do different things if its a c or cpp file
            match = re.search(
                "^(?P<root>CWE(\d+).*__.*_\d+)((?P<letter>[a-z]*)|_(bad|good\d+))(\.c|\.cpp)$",
                filename)

            if filename.endswith(".cpp"):
                root = match.group(
                    "root")  # we don't use the letter in the namespace
                bad = "bad();"
                good = "good();"

                fcl.cpp_bad_lines.append("\tprintLine(\"Calling " + root +
                                         "::" + bad + "\");")
                fcl.cpp_bad_lines.append("\t" + root + "::" + bad + "\n")
                fcl.cpp_h_bad_lines.append("\tnamespace " + root + " { void " +
                                           bad + "}\n")

                fcl.cpp_good_lines.append("\tprintLine(\"Calling " + root +
                                          "::" + good + "\");")
                fcl.cpp_good_lines.append("\t" + root + "::" + good + "\n")
                fcl.cpp_h_good_lines.append("\tnamespace " + root +
                                            " { void " + good + "}\n")

            elif filename.endswith(".c"):
                # we only want to add the "a" files
                if match.group("letter") != "" and match.group(
                        "letter") != "a":
                    py_common.print_with_timestamp("Ignored file: " + filename)
                    continue

                root = match.group("root")
                bad = "_bad();"
                good = "_good();"

                fcl.c_bad_lines.append("\tprintLine(\"Calling " + root + bad +
                                       "\");")
                fcl.c_bad_lines.append("\tchildproc = fork();")
                fcl.c_bad_lines.append("\tif(childproc > 0) {")
                fcl.c_bad_lines.append(
                    "\t\twaitpid(childproc, &returnCode, 0);")
                fcl.c_bad_lines.append(
                    "\t\tif(WIFEXITED(returnCode) && (WEXITSTATUS(returnCode) == 1)) {"
                )
                fcl.c_bad_lines.append("\t\t\tprintf(\"" + root + bad +
                                       " passed\\n\");")
                fcl.c_bad_lines.append("\t\t\tpassedBadTests++;")
                fcl.c_bad_lines.append(
                    "\t\t} else if(WIFSIGNALED(returnCode) && WTERMSIG(returnCode) == SIGALRM) {"
                )
                fcl.c_bad_lines.append("\t\t\tprintf(\"" + root + bad +
                                       " timed out\\n\");")
                fcl.c_bad_lines.append("\t\t\ttimedOutBadTests++;")
                fcl.c_bad_lines.append("\t\t} else {")
                fcl.c_bad_lines.append("\t\t\tprintf(\"" + root + bad +
                                       " failed\\n\");")
                fcl.c_bad_lines.append("\t\t\tfprintf(stderr, \"%s\\n\", \"" +
                                       root + bad + "\");")
                fcl.c_bad_lines.append("\t\t\tfailedBadTests++;")
                fcl.c_bad_lines.append("\t\t}")
                fcl.c_bad_lines.append("\t} else {")
                fcl.c_bad_lines.append("\t\talarm(ALERT_TIMEOUT);")
                fcl.c_bad_lines.append("\t\t" + root + bad + "\n\t\texit(0);")
                fcl.c_bad_lines.append("\t}\n\ttotalBadTests++;\n")
                fcl.c_h_bad_lines.append("\tvoid " + root + bad + "\n")

                # don't create good if template file contains point-flaw-badonly.
                file_contents = py_common.open_file_and_get_contents(
                    fullfilepath)
                result = re.search("Template File: point-flaw-badonly",
                                   file_contents, re.IGNORECASE)
                if result == None:
                    fcl.c_good_lines.append("\tprintLine(\"Calling " + root +
                                            good + "\");")
                    fcl.c_good_lines.append("\tchildproc = fork();")
                    fcl.c_good_lines.append("\tif(childproc > 0) {")
                    fcl.c_good_lines.append(
                        "\t\twaitpid(childproc, &returnCode, 0);")
                    fcl.c_good_lines.append(
                        "\t\tif(WIFEXITED(returnCode) && WEXITSTATUS(returnCode) == 1) {"
                    )
                    fcl.c_good_lines.append("\t\t\tprintf(\"" + root + good +
                                            " failed\\n\");")
                    fcl.c_good_lines.append(
                        "\t\t\tfprintf(stderr, \"%s\\n\", \"" + root + good +
                        "\");")
                    fcl.c_good_lines.append("\t\t\tfailedGoodTests++;")
                    fcl.c_good_lines.append(
                        "\t\t} else if(WIFSIGNALED(returnCode) && WTERMSIG(returnCode) == SIGALRM) {"
                    )
                    fcl.c_good_lines.append("\t\t\tprintf(\"" + root + good +
                                            "timed out\\n\");")
                    fcl.c_good_lines.append("\t\t\ttimedOutGoodTests++;")
                    fcl.c_good_lines.append("\t\t} else {")
                    fcl.c_good_lines.append("\t\t\tprintf(\"" + root + good +
                                            " passed\\n\");")
                    fcl.c_good_lines.append("\t\t\tpassedGoodTests++;")
                    fcl.c_good_lines.append("\t\t}")
                    fcl.c_good_lines.append("\t} else {")
                    fcl.c_good_lines.append("\t\talarm(ALERT_TIMEOUT);")
                    fcl.c_good_lines.append("\t\t" + root + good +
                                            "\n\t\texit(0);")
                    fcl.c_good_lines.append("\t}\n\ttotalGoodTests++;\n")
                    fcl.c_h_good_lines.append("\tvoid " + root + good + "\n")

            else:
                raise Exception(
                    "filename ends with something we don't handle!: " +
                    fullfilepath)

    fcl.c_bad_lines.append(
        "printf(\"Total Bad Tests: %d\\nPassed: %d\\nFailed: %d\\n\", totalBadTests, passedBadTests, failedBadTests);"
    )
    fcl.c_bad_lines.append(
        "printf(\"Total Good Tests: %d\\nPassed: %d\\nFailed: %d\\n\", totalGoodTests, passedGoodTests, failedGoodTests);"
    )
    fcl.c_bad_lines.append(
        "printf(\"Total Bad Time Outs: %d\\nTotal Good Time Outs: %d\\n\", timedOutBadTests, timedOutGoodTests);"
    )
    return fcl