BackDoorBuster/tests/test_final_tests.py

35 lines
1.3 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 setUp(self) -> None:
shutil.rmtree("./tmp/repo", ignore_errors=True)
clone_repo("https://github.com/TheAlgorithms/Python.git", "./tmp/repo")
sampleRate = 0.1
self.injectedNum = inject_random_backdoor("./tmp/repo", sample_rate=sampleRate)
project_path = Path("./tmp/repo")
self.all_python_files = list(project_path.rglob("*.py"))
self.filesNum = len(self.all_python_files)
self.trueRate = self.injectedNum / self.filesNum
def test_final_tests(self):
detectedNum = 0
for file in self.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:
detectedNum += 1
print(detectedNum / self.filesNum)
self.assertAlmostEqual(detectedNum / self.filesNum, self.trueRate, places=1)
if __name__ == "__main__":
unittest.main()