<commit_before><?php

namespace Tests\Core\Service;

use Core\Exception\SecurityException;
use Core\Handler\SecurityHandler;
use Tests\Core\BaseTest;

class SecurityHandlerTest extends BaseTest
{
    /**
     *
     */
    public function testRestrictedDomains()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage("Restricted domains enabled, the domain your fetching from is not allowed:");
        $this->app['params'] = array_merge($this->app['params'], ['restricted_domains' => true]);
        $securityHandler = new SecurityHandler($this->app['params']);

        $securityHandler->checkRestrictedDomains(parent::PNG_TEST_IMAGE);
    }

    /**
     *
     */
    public function testCheckSecurityHashFail()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage(
            "Security Key enabled: Requested URL doesn't match with the hashed Security key !"
        );
        $this->app['params'] = array_merge($this->app['params'], ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']);
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $securityHandler->checkSecurityHash($options, $imageSrc);
    }

    /**
     *
     */
    public function testCheckSecurityIvMissing()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage("Security iv is not set in parameters.yml (security_iv)");
        $this->app['params'] = array_merge(
            $this->app['params'],
            ['security_key' => 'TestSecurityKey', 'security_iv' => '']
        );
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $securityHandler->checkSecurityHash($options, $imageSrc);
    }

    /**
     *
     */
    public function testCheckSecurityHashSuccess()
    {
        $this->app['params'] = array_merge($this->app['params'], ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']);
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $hash = $securityHandler->encrypt($options.'/'.$imageSrc);
        list($hashedOptions, $hashedImageSrc) = $securityHandler->checkSecurityHash($hash, $imageSrc);
        $this->assertEquals($hashedOptions, $options);
        $this->assertEquals($hashedImageSrc, $imageSrc);
    }

    /**
     *
     */
    public function testEncryptionDecryption()
    {
        $this->app['params'] = array_merge($this->app['params'], ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']);
        $securityHandler = new SecurityHandler($this->app['params']);
        $randomString = str_shuffle('AKALEOCJCNXMSOLWO5#KXMw');
        $hashedString = $securityHandler->encrypt($randomString);
        $decryptedString = $securityHandler->decrypt($hashedString);
        $this->assertEquals($decryptedString, $randomString);
    }
}<commit_msg>Fix phpcs warning<commit_after><?php

namespace Tests\Core\Service;

use Core\Exception\SecurityException;
use Core\Handler\SecurityHandler;
use Tests\Core\BaseTest;

class SecurityHandlerTest extends BaseTest
{
    /**
     *
     */
    public function testRestrictedDomains()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage("Restricted domains enabled, the domain your fetching from is not allowed:");
        $this->app['params'] = array_merge($this->app['params'], ['restricted_domains' => true]);
        $securityHandler = new SecurityHandler($this->app['params']);

        $securityHandler->checkRestrictedDomains(parent::PNG_TEST_IMAGE);
    }

    /**
     *
     */
    public function testCheckSecurityHashFail()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage(
            "Security Key enabled: Requested URL doesn't match with the hashed Security key !"
        );
        $this->app['params'] = array_merge(
            $this->app['params'],
            ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']
        );
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $securityHandler->checkSecurityHash($options, $imageSrc);
    }

    /**
     *
     */
    public function testCheckSecurityIvMissing()
    {
        $this->expectException(SecurityException::class);
        $this->expectExceptionMessage("Security iv is not set in parameters.yml (security_iv)");
        $this->app['params'] = array_merge(
            $this->app['params'],
            ['security_key' => 'TestSecurityKey', 'security_iv' => '']
        );
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $securityHandler->checkSecurityHash($options, $imageSrc);
    }

    /**
     *
     */
    public function testCheckSecurityHashSuccess()
    {
        $this->app['params'] = array_merge(
            $this->app['params'],
            ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']
        );
        $securityHandler = new SecurityHandler($this->app['params']);
        $options = parent::OPTION_URL;
        $imageSrc = parent::JPG_TEST_IMAGE;
        $hash = $securityHandler->encrypt($options.'/'.$imageSrc);
        list($hashedOptions, $hashedImageSrc) = $securityHandler->checkSecurityHash($hash, $imageSrc);
        $this->assertEquals($hashedOptions, $options);
        $this->assertEquals($hashedImageSrc, $imageSrc);
    }

    /**
     *
     */
    public function testEncryptionDecryption()
    {
        $this->app['params'] = array_merge(
            $this->app['params'],
            ['security_key' => 'TestSecurityKey', 'security_iv' => 'TestSecurityIVXXXX']
        );
        $securityHandler = new SecurityHandler($this->app['params']);
        $randomString = str_shuffle('AKALEOCJCNXMSOLWO5#KXMw');
        $hashedString = $securityHandler->encrypt($randomString);
        $decryptedString = $securityHandler->decrypt($hashedString);
        $this->assertEquals($decryptedString, $randomString);
    }
}
{'fraction_non_alphanumeric': 0.14918486619501692, 'fraction_numerical': 0.00030759766225776686, 'mean_word_length': 3.275476660092045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2639233', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1785, 'n_words': 287}