fix: 添加容错代码
Some checks failed
Python application test / build (pull_request) Failing after 12m17s

This commit is contained in:
2024-04-29 11:52:46 +08:00
parent 7a420b9bf8
commit d771976b35
2 changed files with 18 additions and 2 deletions

View File

@@ -1,7 +1,9 @@
import unittest
import warnings
from detection.backdoor_detection import find_dangerous_functions
from detection.GPTdetection import *
from detection.GPTdetection import detectGPT
import os
class TestBackdoorDetection(unittest.TestCase):
@@ -57,6 +59,9 @@ class TestBackdoorDetection(unittest.TestCase):
)
def test_gpt_risk_detection(self):
if os.getenv("OPENAI_API_KEY") is None:
warnings.warn("OPENAI_API_KEY is not set, test skipped.", UserWarning)
self.skipTest("OPENAI_API_KEY is not set")
content = """import os
os.system('rm -rf /') # high risk
exec('print("Hello")') # high risk
@@ -66,6 +71,9 @@ class TestBackdoorDetection(unittest.TestCase):
self.assertEqual(len(results["high"]), 3)
def test_gpt_no_risk_detection(self):
if os.getenv("OPENAI_API_KEY") is None:
warnings.warn("OPENAI_API_KEY is not set, test skipped.", UserWarning)
self.skipTest("OPENAI_API_KEY is not set")
content = """a = 10
b = a + 5
print('This should not be detected as risky.')
@@ -75,6 +83,11 @@ class TestBackdoorDetection(unittest.TestCase):
self.assertEqual(len(results["medium"]), 0)
self.assertEqual(len(results["low"]), 0)
def test_gpt_env_no_set(self):
content = "print('test test')"
with self.assertRaises(ValueError):
detectGPT(content)
if __name__ == "__main__":
unittest.main()