tests/SecurityTest.php

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

<?php
require_once __DIR__ . '/../core/Security.php';

class SecurityTest {
    private $passed = 0;
    private $failed = 0;
    
    public function run() {
        echo "Running Security Tests...\n\n";
        
        $this->testSanitize();
        $this->testCleanXSS();
        $this->testHashPassword();
        $this->testVerifyPassword();
        $this->testGenerateToken();
        $this->testValidateEmail();
        $this->testValidateInt();
        $this->testTruncate();
        $this->testSlugify();
        $this->testRandomString();
        
        $this->printResults();
    }
    
    private function testSanitize() {
        $input = '  <script>alert("xss")</script>  ';
        $result = Security::sanitize($input);
        $expected = '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;';
        
        $this->assertEquals($expected, $result, 'Sanitize removes whitespace and encodes HTML');
    }
    
    private function testCleanXSS() {
        $input = '<script>alert("xss")</script>';
        $result = Security::cleanXSS($input);
        $expected = '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;';
        
        $this->assertEquals($expected, $result, 'CleanXSS encodes HTML entities');
    }
    
    private function testHashPassword() {
        $password = 'test123';
        $hash = Security::hashPassword($password);
        
        $this->assertTrue(strlen($hash) > 0, 'HashPassword returns a hash');
        $this->assertTrue($hash !== $password, 'HashPassword returns different value');
        $this->assertTrue(password_verify($password, $hash), 'HashPassword creates verifiable hash');
    }
    
    private function testVerifyPassword() {
        $password = 'test123';
        $hash = password_hash($password, PASSWORD_DEFAULT);
        
        $this->assertTrue(Security::verifyPassword($password, $hash), 'VerifyPassword returns true for correct password');
        $this->assertFalse(Security::verifyPassword('wrong', $hash), 'VerifyPassword returns false for wrong password');
    }
    
    private function testGenerateToken() {
        $token1 = Security::generateToken();
        $token2 = Security::generateToken();
后面还有 93 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。