33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
import unittest
|
|
import os
|
|
import shutil
|
|
|
|
from detection.utils import read_file_content
|
|
from .final_tests_util import *
|
|
from detection.Regexdetection import find_dangerous_functions
|
|
from detection.GPTdetection import detectGPT
|
|
|
|
|
|
class TestFinalTests(unittest.TestCase):
|
|
def test_final_tests(self):
|
|
shutil.rmtree("./tmp/repo", ignore_errors=True)
|
|
clone_repo("https://github.com/TheAlgorithms/Python.git", "./tmp/repo")
|
|
sampleRate = 0.1
|
|
injectedNum = inject_random_backdoor("./tmp/repo",sample_rate=sampleRate)
|
|
project_path = Path("./tmp/repo")
|
|
all_python_files = list(project_path.rglob("*.py"))
|
|
filesNum = len(all_python_files)
|
|
trueRate = injectedNum / filesNum
|
|
detectedNum = 0
|
|
for file in all_python_files:
|
|
content = read_file_content(str(file))
|
|
results = find_dangerous_functions(content, ".py")
|
|
if len(results["high"]) > 0 or len(results["medium"]) > 0 or len(results["low"]) > 0:
|
|
print(str(file))
|
|
detectedNum += 1
|
|
shutil.rmtree("./tmp/repo",ignore_errors=True)
|
|
self.assertAlmostEquals(detectedNum / filesNum, trueRate, places=1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |