feature/国内GPT-文心一言

This commit is contained in:
2024-05-16 21:15:22 +08:00
parent 9d6f054478
commit dd45c467a3
2 changed files with 110 additions and 79 deletions

View File

@@ -1,34 +1,48 @@
import unittest
import warnings
import os
from detection.cngptdetection import cndetectGPT
from detection.cngptdetection import detectGPT # 导入调用百度 ai 模型的函数
class TestBackdoorDetection(unittest.TestCase):
def test_gpt_risk_detection(self):
"""
if os.getenv("BAIDU_API_KEY") is None or os.getenv("BAIDU_SECRET_KEY") is None:
warnings.warn("BAIDU_API_KEY or BAIDU_SECRET_KEY is not set, test skipped.", UserWarning)
self.skipTest("BAIDU_API_KEY or BAIDU_SECRET_KEY is not set")
"""
content = """import os
os.system('rm -rf /') # high risk
exec('print("Hello")') # high risk
eval('2 + 2') # high risk
"""
results = cndetectGPT(content)
self.assertEqual(len(results["high"]), 3)
results1 = detectGPT(content)
self.assertEqual(len(results1["high"]), 3)
def test_gpt_no_risk_detection(self):
"""
if os.getenv("BAIDU_API_KEY") is None or os.getenv("BAIDU_SECRET_KEY") is None:
warnings.warn("BAIDU_API_KEY or BAIDU_SECRET_KEY is not set, test skipped.", UserWarning)
self.skipTest("BAIDU_API_KEY or BAIDU_SECRET_KEY is not set")
"""
content = """a = 10
b = a + 5
print('This should not be detected as risky.')
"""
results = cndetectGPT(content)
self.assertEqual(len(results["high"]), 0)
self.assertEqual(len(results["medium"]), 0)
self.assertEqual(len(results["low"]), 0)
results2 = detectGPT(content)
self.assertEqual(len(results2["high"]), 0)
self.assertEqual(len(results2["medium"]), 0)
self.assertEqual(len(results2["low"]), 0)
def test_gpt_env_no_set(self):
if os.getenv("BAIDU_API_KEY") is None or os.getenv("BAIDU_SECRET_KEY") is None:
self.skipTest("BAIDU_API_KEY or BAIDU_SECRET_KEY is not set")
"""
if os.getenv("BAIDU_API_KEY") is not None or os.getenv("BAIDU_SECRET_KEY") is not None:
self.skipTest("BAIDU_API_KEY or BAIDU_SECRET_KEY is set")
"""
content = "print('test test')"
with self.assertRaises(ValueError):
cndetectGPT(content)
detectGPT(content)
if __name__ == "__main__":