﻿<?php

declare(strict_types=1);

/**
 * Hack Detection Service Tester v1.1
 * Enhanced with protocol injection testing capabilities
 */

final class HackDetectionTester {
    private const TEST_KEY_HEADER = 'X-Security-Test';
    private string $testKey;
    private array $testResults = [];
    private float $startTime;

    public function __construct() {
        $this->startTime = microtime(true);
        $this->initializeTestEnvironment();
    }

    private function initializeTestEnvironment(): void {
        $testKeyFile = __DIR__ . '/.test_key';
        
        if (!file_exists($testKeyFile)) {
            $this->testKey = $this->generateSecureTestKey();
            file_put_contents($testKeyFile, $this->testKey, LOCK_EX);
            chmod($testKeyFile, 0600);
            echo "Generated new test key: " . substr($this->testKey, 0, 16) . "...\n";
        } else {
            $this->testKey = trim(file_get_contents($testKeyFile));
        }

        $_SERVER['HTTP_X_SECURITY_TEST'] = $this->testKey;
    }

    private function generateSecureTestKey(): string {
        return bin2hex(random_bytes(32));
    }

    /**
     * Run comprehensive test suite including protocol injection tests
     */
    public function runAllTests(): array {
        echo "Starting Hack Detection Service Tests...\n";
        echo "Test Key: " . substr($this->testKey, 0, 16) . "...\n\n";

        $this->testPatternValidation();
        $this->testDetectionVectors();
        $this->testProtocolInjectionSpecific();
        $this->testFalsePositives();
        $this->testPerformance();
        $this->testSecurity();

        return $this->generateTestReport();
    }

    /**
     * Specialized protocol injection tests
     */
    private function testProtocolInjectionSpecific(): void {
        echo "2a. Testing Protocol Injection Specific Vectors...\n";

        $protocolTests = [
            [
                'category' => 'php_wrappers',
                'tests' => [
                    ['input' => "php://filter/convert.base64-encode/resource=index.php", 'expected_score' => 98],
                    ['input' => "php://input", 'expected_score' => 97],
                    ['input' => "phar:///tmp/exploit.phar", 'expected_score' => 96],
                    ['input' => "expect://whoami", 'expected_score' => 99],
                ]
            ],
            [
                'category' => 'data_uris',
                'tests' => [
                    ['input' => "data:text/html,<script>alert('XSS')</script>", 'expected_score' => 90],
                    ['input' => "data://text/plain;base64,PD9waHAgc3lzdGVtKCdpZCcpOw==", 'expected_score' => 92],
                    ['input' => "data:text/javascript,eval('alert(1)')", 'expected_score' => 79],
                ]
            ],
            [
                'category' => 'network_protocols',
                'tests' => [
                    ['input' => "gopher://internal:3306/_test", 'expected_score' => 89],
                    ['input' => "ftp://attacker:password@evil.com/shell.php", 'expected_score' => 88],
                    ['input' => "http://169.254.169.254/latest/meta-data/", 'expected_score' => 92],
                ]
            ],
            [
                'category' => 'evasion_techniques',
                'tests' => [
                    ['input' => "p%68p://filter/convert.base64-encode/resource=config.php", 'expected_score' => 86],
                    ['input' => "%70%68%70://input", 'expected_score' => 87],
                    ['input' => "PhP://filter/read=convert.base64-encode", 'expected_score' => 80],
                ]
            ]
        ];

        $this->testResults['protocol_injection'] = [
            'categories_tested' => count($protocolTests),
            'total_tests' => 0,
            'passed_tests' => 0,
            'category_results' => []
        ];

        foreach ($protocolTests as $category) {
            $categoryResults = [
                'category' => $category['category'],
                'tests' => [],
                'passed' => 0,
                'failed' => 0
            ];

            foreach ($category['tests'] as $test) {
                $this->setupTestEnvironment([
                    'get' => ['input' => $test['input']],
                    'server' => [
                        'REQUEST_URI' => '/test?input=' . urlencode($test['input']),
                        'HTTP_USER_AGENT' => 'Security-Tester/1.0'
                    ]
                ]);

                $result = HackDetectionService::detectHackAttempt();
                $passed = $result['confidence_score'] >= $test['expected_score'];

                $testResult = [
                    'input' => $test['input'],
                    'expected_score' => $test['expected_score'],
                    'actual_score' => $result['confidence_score'],
                    'passed' => $passed,
                    'detected_vectors' => $result['detected_vectors']
                ];

                $categoryResults['tests'][] = $testResult;

                if ($passed) {
                    $categoryResults['passed']++;
                    $this->testResults['protocol_injection']['passed_tests']++;
                } else {
                    $categoryResults['failed']++;
                }

                $this->testResults['protocol_injection']['total_tests']++;
            }

            $this->testResults['protocol_injection']['category_results'][] = $categoryResults;
        }

        echo "   ✓ Tested " . $this->testResults['protocol_injection']['total_tests'] . " protocol injection vectors\n";
    }

    /**
     * Test protocol injection false positives
     */
    private function testProtocolInjectionFalsePositives(): void {
        echo "3a. Testing Protocol Injection False Positives...\n";

        $legitimateProtocols = [
            [
                'description' => 'Legitimate image data URI',
                'input' => [
                    'get' => ['img' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='],
                    'server' => [
                        'REQUEST_URI' => '/image?img=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
                        'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                    ]
                ],
                'should_detect' => false
            ],
            [
                'description' => 'Legitimate PDF file reference',
                'input' => [
                    'get' => ['file' => 'file:///home/user/documents/report.pdf'],
                    'server' => [
                        'REQUEST_URI' => '/open?file=file:///home/user/documents/report.pdf',
                        'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
                    ]
                ],
                'should_detect' => false
            ],
            [
                'description' => 'External website redirect',
                'input' => [
                    'get' => ['url' => 'https://trusted-site.com/page'],
                    'server' => [
                        'REQUEST_URI' => '/redirect?url=https://trusted-site.com/page',
                        'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
                    ]
                ],
                'should_detect' => false
            ],
            [
                'description' => 'Text data URI for configuration',
                'input' => [
                    'get' => ['config' => 'data:text/plain;charset=utf-8,setting1=value1;setting2=value2'],
                    'server' => [
                        'REQUEST_URI' => '/config?config=data:text/plain;charset=utf-8,setting1=value1;setting2=value2',
                        'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                    ]
                ],
                'should_detect' => false
            ]
        ];

        $this->testResults['protocol_false_positives'] = [
            'total_tests' => count($legitimateProtocols),
            'passed_tests' => 0,
            'failed_tests' => 0,
            'test_results' => []
        ];

        foreach ($legitimateProtocols as $test) {
            $this->setupTestEnvironment($test['input']);
            
            $result = HackDetectionService::detectHackAttempt();
            $passed = $result['is_hack'] === $test['should_detect'];

            $testResult = [
                'description' => $test['description'],
                'expected_detection' => $test['should_detect'],
                'actual_detection' => $result['is_hack'],
                'confidence_score' => $result['confidence_score'],
                'passed' => $passed
            ];

            $this->testResults['protocol_false_positives']['test_results'][] = $testResult;

            if ($passed) {
                $this->testResults['protocol_false_positives']['passed_tests']++;
            } else {
                $this->testResults['protocol_false_positives']['failed_tests']++;
            }
        }

        echo "   ✓ Tested " . count($legitimateProtocols) . " legitimate protocol uses\n";
    }

    // ... (rest of the methods remain the same as previous implementation)

    private function testPatternValidation(): void {
        echo "1. Testing Pattern Validation...\n";

        $patternDetails = HackDetectionService::getPatternDetails();
        
        $this->testResults['pattern_validation'] = [
            'total_categories' => count($patternDetails),
            'categories_tested' => [],
            'test_cases_found' => 0,
            'regex_errors' => 0
        ];

        foreach ($patternDetails as $category => $details) {
            $categoryResults = [
                'category' => $category,
                'patterns_count' => $details['total_patterns'],
                'test_cases_count' => count($details['test_cases'] ?? []),
                'score_range' => $details['score_range'],
                'test_results' => []
            ];

            // Run test cases for this category
            if (!empty($details['test_cases'])) {
                foreach ($details['test_cases'] as $testCase) {
                    $testResult = $this->runTestCase($category, $testCase);
                    $categoryResults['test_results'][] = $testResult;
                    
                    if (!$testResult['passed']) {
                        $categoryResults['failed_tests'] = ($categoryResults['failed_tests'] ?? 0) + 1;
                    }
                }
            }

            $this->testResults['pattern_validation']['categories_tested'][] = $categoryResults;
            $this->testResults['pattern_validation']['test_cases_found'] += count($details['test_cases'] ?? []);
        }

        echo "   ✓ Tested " . count($patternDetails) . " pattern categories\n";
    }

    private function runTestCase(string $category, array $testCase): array {
        $this->setupTestEnvironment($testCase['input'] ?? []);
        $result = HackDetectionService::detectHackAttempt();
        $expectedScore = $testCase['expected_score'] ?? 0;
        $actualScore = $result['confidence_score'];
        
        $passed = $this->evaluateTestResult($testCase, $result);

        return [
            'description' => $testCase['description'] ?? 'Unknown test',
            'expected_score' => $expectedScore,
            'actual_score' => $actualScore,
            'passed' => $passed,
            'detected_vectors' => $result['detected_vectors'],
            'risk_level' => $result['risk_level'],
            'is_hack' => $result['is_hack']
        ];
    }

    private function setupTestEnvironment(array $input): void {
        $_GET = $input['get'] ?? [];
        $_POST = $input['post'] ?? [];
        $_COOKIE = $input['cookie'] ?? [];
        $_SERVER = array_merge($_SERVER, $input['server'] ?? []);
        $_FILES = $input['files'] ?? [];
        $_SERVER['HTTP_X_SECURITY_TEST'] = $this->testKey;
    }

    private function evaluateTestResult(array $testCase, array $result): bool {
        $expectedScore = $testCase['expected_score'] ?? 0;
        $expectedDetection = $testCase['should_detect'] ?? ($expectedScore > 0);
        
        $actualScore = $result['confidence_score'];
        $actualDetection = $result['is_hack'];

        $scoreTolerance = 10;
        $scoreMatch = abs($expectedScore - $actualScore) <= $scoreTolerance;
        $detectionMatch = $expectedDetection === $actualDetection;

        return $scoreMatch && $detectionMatch;
    }

    private function testDetectionVectors(): void {
        echo "2. Testing Detection Vectors...\n";

        $vectors = [
            'sql_injection' => [
                ['input' => "1' OR '1'='1", 'expected_score' => 85],
                ['input' => "1; DROP TABLE users", 'expected_score' => 95],
            ],
            'xss_attack' => [
                ['input' => "<script>alert('XSS')</script>", 'expected_score' => 90],
                ['input' => "<img src=x onerror=alert(1)>", 'expected_score' => 85]
            ],
            'rce_attempt' => [
                ['input' => "'; system('id');", 'expected_score' => 95],
                ['input' => "| ls -la", 'expected_score' => 80]
            ]
        ];

        $this->testResults['detection_vectors'] = [
            'vectors_tested' => count($vectors),
            'total_tests' => 0,
            'passed_tests' => 0
        ];

        foreach ($vectors as $vectorName => $tests) {
            $vectorResults = [
                'vector' => $vectorName,
                'tests' => [],
                'passed' => 0,
                'failed' => 0
            ];

            foreach ($tests as $test) {
                $this->setupTestEnvironment([
                    'get' => ['input' => $test['input']],
                    'server' => [
                        'REQUEST_URI' => '/test',
                        'HTTP_USER_AGENT' => 'Security-Tester/1.0'
                    ]
                ]);

                $result = HackDetectionService::detectHackAttempt();
                $passed = $result['confidence_score'] >= $test['expected_score'];

                $vectorResults['tests'][] = [
                    'input' => $test['input'],
                    'expected_score' => $test['expected_score'],
                    'actual_score' => $result['confidence_score'],
                    'passed' => $passed,
                    'detected' => $result['is_hack']
                ];

                if ($passed) {
                    $vectorResults['passed']++;
                    $this->testResults['detection_vectors']['passed_tests']++;
                } else {
                    $vectorResults['failed']++;
                }

                $this->testResults['detection_vectors']['total_tests']++;
            }

            $this->testResults['detection_vectors']['vector_results'][] = $vectorResults;
        }

        echo "   ✓ Tested " . count($vectors) . " detection vectors\n";
    }

    private function testFalsePositives(): void {
        echo "3. Testing False Positive Prevention...\n";

        $legitimateRequests = [
            [
                'description' => 'Normal search query',
                'input' => [
                    'get' => ['q' => 'normal search term'],
                    'server' => [
                        'REQUEST_URI' => '/search?q=normal+search+term',
                        'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                    ]
                ],
                'should_detect' => false
            ]
        ];

        $this->testResults['false_positive_tests'] = [
            'total_tests' => count($legitimateRequests),
            'passed_tests' => 0,
            'failed_tests' => 0,
            'test_results' => []
        ];

        foreach ($legitimateRequests as $test) {
            $this->setupTestEnvironment($test['input']);
            
            $result = HackDetectionService::detectHackAttempt();
            $passed = $result['is_hack'] === $test['should_detect'];

            $testResult = [
                'description' => $test['description'],
                'expected_detection' => $test['should_detect'],
                'actual_detection' => $result['is_hack'],
                'confidence_score' => $result['confidence_score'],
                'passed' => $passed
            ];

            $this->testResults['false_positive_tests']['test_results'][] = $testResult;

            if ($passed) {
                $this->testResults['false_positive_tests']['passed_tests']++;
            } else {
                $this->testResults['false_positive_tests']['failed_tests']++;
            }
        }

        echo "   ✓ Tested " . count($legitimateRequests) . " legitimate requests\n";
    }

    private function testPerformance(): void {
        echo "4. Testing Performance...\n";

        $performanceTests = [];
        $totalTime = 0;
        $testCount = 10;

        for ($i = 0; $i < $testCount; $i++) {
            $this->setupTestEnvironment([
                'get' => ['test' => 'performance'],
                'server' => [
                    'REQUEST_URI' => '/test/performance',
                    'HTTP_USER_AGENT' => 'Performance-Tester/1.0'
                ]
            ]);

            $startTime = microtime(true);
            $result = HackDetectionService::detectHackAttempt();
            $endTime = microtime(true);

            $executionTime = $endTime - $startTime;
            $totalTime += $executionTime;

            $performanceTests[] = [
                'iteration' => $i + 1,
                'execution_time' => $executionTime,
                'memory_usage' => memory_get_peak_usage(true),
                'vectors_detected' => count($result['detected_vectors'])
            ];
        }

        $this->testResults['performance_tests'] = [
            'iterations' => $testCount,
            'average_time' => $totalTime / $testCount,
            'total_time' => $totalTime,
            'tests' => $performanceTests,
            'memory_usage' => memory_get_peak_usage(true)
        ];

        echo "   ✓ Completed " . $testCount . " performance iterations\n";
    }

    private function testSecurity(): void {
        echo "5. Testing Security Measures...\n";

        $securityTests = [];

        // Test 1: Verify test mode requires valid key
        $originalKey = $_SERVER['HTTP_X_SECURITY_TEST'];
        $_SERVER['HTTP_X_SECURITY_TEST'] = 'invalid-key';
        $resultWithoutKey = HackDetectionService::detectHackAttempt();
        $_SERVER['HTTP_X_SECURITY_TEST'] = $originalKey;

        $securityTests[] = [
            'test' => 'Test mode requires valid key',
            'passed' => !$resultWithoutKey['test_mode'],
            'description' => 'System should not be in test mode with invalid key'
        ];

        $this->testResults['security_tests'] = [
            'total_tests' => count($securityTests),
            'passed_tests' => count(array_filter($securityTests, fn($test) => $test['passed'])),
            'tests' => $securityTests
        ];

        echo "   ✓ Completed " . count($securityTests) . " security tests\n";
    }

    private function generateTestReport(): array {
        $endTime = microtime(true);
        $totalTime = $endTime - $this->startTime;

        $report = [
            'test_summary' => [
                'total_execution_time' => $totalTime,
                'memory_peak_usage' => memory_get_peak_usage(true),
                'test_timestamp' => date('Y-m-d H:i:s'),
                'php_version' => PHP_VERSION,
                'test_mode_verified' => true
            ],
            'detailed_results' => $this->testResults
        ];

        $totalTests = 0;
        $passedTests = 0;

        foreach ($this->testResults as $category => $results) {
            if (isset($results['passed_tests']) && isset($results['total_tests'])) {
                $totalTests += $results['total_tests'];
                $passedTests += $results['passed_tests'];
            }
        }

        $report['test_summary']['total_tests'] = $totalTests;
        $report['test_summary']['passed_tests'] = $passedTests;
        $report['test_summary']['success_rate'] = $totalTests > 0 ? ($passedTests / $totalTests) * 100 : 0;

        $this->printReport($report);

        return $report;
    }

    private function printReport(array $report): void {
        echo "\n" . str_repeat("=", 60) . "\n";
        echo "HACK DETECTION SERVICE TEST REPORT\n";
        echo str_repeat("=", 60) . "\n";

        $summary = $report['test_summary'];
        
        printf("Execution Time: %.3f seconds\n", $summary['total_execution_time']);
        printf("Memory Usage: %.2f MB\n", $summary['memory_peak_usage'] / 1024 / 1024);
        printf("PHP Version: %s\n", $summary['php_version']);
        printf("Total Tests: %d\n", $summary['total_tests']);
        printf("Passed Tests: %d\n", $summary['passed_tests']);
        printf("Success Rate: %.1f%%\n", $summary['success_rate']);

        echo "\nDETAILED RESULTS:\n";
        echo str_repeat("-", 40) . "\n";

        foreach ($report['detailed_results'] as $category => $results) {
            $categoryName = str_replace('_', ' ', ucfirst($category));
            printf("%-25s: ", $categoryName);

            if (isset($results['passed_tests']) && isset($results['total_tests'])) {
                printf("%d/%d passed\n", $results['passed_tests'], $results['total_tests']);
            } else {
                echo "Completed\n";
            }
        }

        echo str_repeat("=", 60) . "\n";

        if ($summary['success_rate'] < 90) {
            echo "RECOMMENDATIONS:\n";
            echo "- Review failed test cases\n";
            echo "- Check pattern validation results\n";
            echo "- Verify detection thresholds\n";
        } else {
            echo "✓ All tests completed successfully. System is ready for production.\n";
        }
    }

    public function runSpecificTest(string $testCategory): array {
        switch ($testCategory) {
            case 'patterns':
                $this->testPatternValidation();
                break;
            case 'vectors':
                $this->testDetectionVectors();
                break;
            case 'protocols':
                $this->testProtocolInjectionSpecific();
                break;
            case 'falsepositives':
                $this->testFalsePositives();
                break;
            case 'performance':
                $this->testPerformance();
                break;
            case 'security':
                $this->testSecurity();
                break;
            default:
                throw new InvalidArgumentException("Unknown test category: $testCategory");
        }

        return $this->testResults[$testCategory . '_tests'] ?? [];
    }

    public function cleanup(): void {
        $_GET = $_POST = $_COOKIE = $_FILES = [];
        $_SERVER = array_filter($_SERVER, fn($key) => !str_starts_with($key, 'HTTP_X_SECURITY'), ARRAY_FILTER_USE_KEY);
        $this->testResults = [];
    }
}

// Command line interface
if (php_sapi_name() === 'cli' && basename(__FILE__) === basename($_SERVER['argv'][0] ?? '')) {
    $tester = new HackDetectionTester();
    
    $options = getopt('', ['category:', 'help']);
    
    if (isset($options['help'])) {
        echo "Hack Detection Service Tester\n";
        echo "Usage: php HackDetectionTester.php [--category=name]\n";
        echo "Categories: patterns, vectors, protocols, falsepositives, performance, security\n";
        exit(0);
    }
    
    if (isset($options['category'])) {
        $result = $tester->runSpecificTest($options['category']);
    } else {
        $result = $tester->runAllTests();
    }
    
    $tester->cleanup();
    exit($result['test_summary']['success_rate'] >= 90 ? 0 : 1);
}