test: 修改单元测试
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				Python application test / build (pull_request) Failing after 15s
				
					
					
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	Python application test / build (pull_request) Failing after 15s
				This commit is contained in:
		@@ -1,5 +1,9 @@
 | 
				
			|||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
from detection.backdoor_detection import find_dangerous_functions
 | 
					import os
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sys.path.append(os.path.abspath("../detection"))
 | 
				
			||||||
 | 
					from backdoor_detection import find_dangerous_functions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestBackdoorDetection(unittest.TestCase):
 | 
					class TestBackdoorDetection(unittest.TestCase):
 | 
				
			||||||
@@ -9,7 +13,8 @@ class TestBackdoorDetection(unittest.TestCase):
 | 
				
			|||||||
        exec('print("Hello")')  # high risk
 | 
					        exec('print("Hello")')  # high risk
 | 
				
			||||||
        eval('2 + 2')   # high risk
 | 
					        eval('2 + 2')   # high risk
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        results = find_dangerous_functions(content)
 | 
					        file_extension = ".py"
 | 
				
			||||||
 | 
					        results = find_dangerous_functions(content, file_extension)
 | 
				
			||||||
        self.assertIn((2, "os.system('rm -rf /')"), results["high"])
 | 
					        self.assertIn((2, "os.system('rm -rf /')"), results["high"])
 | 
				
			||||||
        self.assertIn((3, "exec('print(\"Hello\")')"), results["high"])
 | 
					        self.assertIn((3, "exec('print(\"Hello\")')"), results["high"])
 | 
				
			||||||
        self.assertIn((4, "eval('2 + 2')"), results["high"])
 | 
					        self.assertIn((4, "eval('2 + 2')"), results["high"])
 | 
				
			||||||
@@ -20,7 +25,8 @@ class TestBackdoorDetection(unittest.TestCase):
 | 
				
			|||||||
        import os
 | 
					        import os
 | 
				
			||||||
        os.popen('ls')  # medium risk
 | 
					        os.popen('ls')  # medium risk
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        results = find_dangerous_functions(content)
 | 
					        file_extension = ".py"
 | 
				
			||||||
 | 
					        results = find_dangerous_functions(content, file_extension)
 | 
				
			||||||
        self.assertIn((2, "subprocess.run(['ls', '-l'])"), results["medium"])
 | 
					        self.assertIn((2, "subprocess.run(['ls', '-l'])"), results["medium"])
 | 
				
			||||||
        self.assertIn((4, "os.popen('ls')"), results["medium"])
 | 
					        self.assertIn((4, "os.popen('ls')"), results["medium"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -29,7 +35,8 @@ class TestBackdoorDetection(unittest.TestCase):
 | 
				
			|||||||
        b = a + 5
 | 
					        b = a + 5
 | 
				
			||||||
        print('This should not be detected as risky.')
 | 
					        print('This should not be detected as risky.')
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        results = find_dangerous_functions(content)
 | 
					        file_extension = ".py"
 | 
				
			||||||
 | 
					        results = find_dangerous_functions(content, file_extension)
 | 
				
			||||||
        self.assertEqual(len(results["high"]), 0)
 | 
					        self.assertEqual(len(results["high"]), 0)
 | 
				
			||||||
        self.assertEqual(len(results["medium"]), 0)
 | 
					        self.assertEqual(len(results["medium"]), 0)
 | 
				
			||||||
        self.assertEqual(len(results["low"]), 0)
 | 
					        self.assertEqual(len(results["low"]), 0)
 | 
				
			||||||
@@ -40,7 +47,8 @@ class TestBackdoorDetection(unittest.TestCase):
 | 
				
			|||||||
        eval('2 + 2')  # This should be high risk
 | 
					        eval('2 + 2')  # This should be high risk
 | 
				
			||||||
        subprocess.run(['echo', 'hello'])  # This should be medium risk
 | 
					        subprocess.run(['echo', 'hello'])  # This should be medium risk
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        results = find_dangerous_functions(content)
 | 
					        file_extension = ".py"
 | 
				
			||||||
 | 
					        results = find_dangerous_functions(content, file_extension)
 | 
				
			||||||
        self.assertIn(
 | 
					        self.assertIn(
 | 
				
			||||||
            (3, "eval('2 + 2')"),
 | 
					            (3, "eval('2 + 2')"),
 | 
				
			||||||
            results["high"],
 | 
					            results["high"],
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user