feat: update test cases
This commit is contained in:
parent
3f6375977c
commit
fbeba5b4fc
@ -9,19 +9,21 @@ from detection.GPTdetection import detectGPT
|
|||||||
|
|
||||||
class TestFinalTests(unittest.TestCase):
|
class TestFinalTests(unittest.TestCase):
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
shutil.rmtree("./tmp/repo", ignore_errors=True)
|
self.path = "./tmp/repo"
|
||||||
clone_repo("https://github.com/injetlee/Python.git", "./tmp/repo")
|
shutil.rmtree(self.path, ignore_errors=True)
|
||||||
|
clone_repo("https://github.com/injetlee/Python.git", self.path)
|
||||||
sampleRate = 0.1
|
sampleRate = 0.1
|
||||||
self.inject_reslt = inject_random_backdoor("./tmp/repo", sample_rate=sampleRate)
|
self.inject_reslt = inject_random_backdoor(self.path, sample_rate=sampleRate)
|
||||||
self.injectedNum = len(self.inject_reslt)
|
self.injectedNum = len(self.inject_reslt)
|
||||||
print(self.injectedNum)
|
print(self.injectedNum)
|
||||||
project_path = Path("./tmp/repo")
|
project_path = Path(self.path)
|
||||||
self.all_python_files = list(project_path.rglob("*.py"))
|
self.all_python_files = list(project_path.rglob("*.py"))
|
||||||
self.filesNum = len(self.all_python_files)
|
self.py_filesNum = len(self.all_python_files)
|
||||||
self.trueRate = self.injectedNum / self.filesNum
|
self.trueRate = self.injectedNum / self.py_filesNum
|
||||||
print(self.trueRate)
|
print(self.trueRate)
|
||||||
|
|
||||||
def test_final_tests(self):
|
# test backdoor code in python files
|
||||||
|
def test_final_tests_pycode(self):
|
||||||
detectedNum = 0
|
detectedNum = 0
|
||||||
possibly_dangerous_file = []
|
possibly_dangerous_file = []
|
||||||
for file in self.all_python_files:
|
for file in self.all_python_files:
|
||||||
@ -34,8 +36,8 @@ class TestFinalTests(unittest.TestCase):
|
|||||||
):
|
):
|
||||||
detectedNum += 1
|
detectedNum += 1
|
||||||
possibly_dangerous_file.append(file)
|
possibly_dangerous_file.append(file)
|
||||||
print(detectedNum / self.filesNum)
|
print(detectedNum / self.py_filesNum)
|
||||||
self.assertAlmostEqual(detectedNum / self.filesNum, self.trueRate, places=1)
|
self.assertAlmostEqual(detectedNum, self.py_filesNum, places=1)
|
||||||
GPTdetectedNum = 0
|
GPTdetectedNum = 0
|
||||||
|
|
||||||
for i in possibly_dangerous_file:
|
for i in possibly_dangerous_file:
|
||||||
@ -55,6 +57,17 @@ class TestFinalTests(unittest.TestCase):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
# test pickle files
|
||||||
|
pickle_detectedNum = 0
|
||||||
|
pickle_tureNum = len(list(Path(self.path).glob("*.pickle")))
|
||||||
|
|
||||||
|
self.assertAlmostEqual(pickle_detectedNum, pickle_tureNum, places=1)
|
||||||
|
|
||||||
|
# test pyc files
|
||||||
|
pyc_detectedNum = 0
|
||||||
|
pyc_tureNum = len(list(Path(self.path).glob("*.pyc")))
|
||||||
|
self.assertAlmostEqual(pyc_detectedNum, pyc_tureNum, places=1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user