<commit_before><?php
/**
 * @todo test all other functionalities of Bindings, currently tests a small part of redirection
 */
class EngineBlock_Test_Corto_Module_BindingsTest extends PHPUnit_Framework_TestCase
{
    private $bindings;

    public function setup()
    {
        $proxyServer = Phake::mock('EngineBlock_Corto_ProxyServer');
        $this->bindings = new EngineBlock_Corto_Module_Bindings($proxyServer);
    }

    /**
     * @expectedException EngineBlock_Corto_Module_Bindings_UnsupportedBindingException
     */
    public function testResponseRedirectIsNotSupported()
    {
        $message = array(
            '__' => array(
                'ProtocolBinding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
                'paramname' => 'SAMLResponse'
            )
        );
        $remoteEntity = array();
        $this->bindings->send($message, $remoteEntity);
    }

    /**
     * @param string $xmlFile
     * @param string $certificateFile
     *
     * @dataProvider responseFilePathsProvider
     */
    public function testInvalidNameId($xmlFile, $certificateFile)
    {
        $server = Phake::mock('EngineBlock_Corto_ProxyServer');
        $bindings = new EngineBlock_Corto_Module_Bindings($server);
        $xml2array = new EngineBlock_Corto_XmlToArray();
        $xml = file_get_contents($xmlFile);

        $element = $xml2array->xml2array($xml);

        $publicKey = openssl_pkey_get_public($certificateFile);
        $this->assertTrue(
            $bindings->_verifySignatureXMLElement(
                $publicKey,
                $xml,
                $element['saml:Assertion']
            )
        );
    }

    /**
     * Provides a list of paths to response xml files
     * 
     * @return array
     */
    public function responseFilePathsProvider()
    {
        $responseFiles = array();
        $responsesDir = new DirectoryIterator(TEST_RESOURCES_DIR . '/saml/responses');
        /** @var $responseFile DirectoryIterator */
        foreach($responsesDir as $responseFile) {
            if ($responseFile->isFile() && !$responseFile->isDot()) {
                $extension = substr($responseFile->getFilename(), -3);
                $fileNameWithoutExtension = substr($responseFile->getFilename(), 0, -4);
                if ($extension == 'pem') {
                    $responseFiles[$fileNameWithoutExtension]['certificateFile'] = $responseFile->getRealPath();
                } elseif ($extension == 'xml') {
                    $responseFiles[$fileNameWithoutExtension]['responseXmlFile'] = $responseFile->getRealPath();
                }
            }
        }

        return $responseFiles;
    }
}<commit_msg>Issue #6 Test now uses mocked proxyserver with mocked log<commit_after><?php
/**
 * @todo test all other functionalities of Bindings, currently tests a small part of redirection
 */
class EngineBlock_Test_Corto_Module_BindingsTest extends PHPUnit_Framework_TestCase
{
    private $bindings;

    public function setup()
    {
        $proxyServer = Phake::mock('EngineBlock_Corto_ProxyServer');
        $log = Phake::mock('EngineBlock_Log');
        Phake::when($proxyServer)->getSessionLog()->thenReturn($log);
        $this->bindings = new EngineBlock_Corto_Module_Bindings($proxyServer);
    }

    /**
     * @expectedException EngineBlock_Corto_Module_Bindings_UnsupportedBindingException
     */
    public function testResponseRedirectIsNotSupported()
    {
        $message = array(
            '__' => array(
                'ProtocolBinding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
                'paramname' => 'SAMLResponse'
            )
        );
        $remoteEntity = array();
        $this->bindings->send($message, $remoteEntity);
    }

    /**
     * @param string $xmlFile
     * @param string $certificateFile
     *
     * @dataProvider responseFilePathsProvider
     */
    public function testInvalidNameId($xmlFile, $certificateFile)
    {
        $xml2array = new EngineBlock_Corto_XmlToArray();
        $xml = file_get_contents($xmlFile);

        $element = $xml2array->xml2array($xml);

        $publicKey = openssl_pkey_get_public($certificateFile);
        $this->assertTrue(
            $this->bindings->_verifySignatureXMLElement(
                $publicKey,
                $xml,
                $element['saml:Assertion']
            )
        );
    }

    /**
     * Provides a list of paths to response xml files
     * 
     * @return array
     */
    public function responseFilePathsProvider()
    {
        $responseFiles = array();
        $responsesDir = new DirectoryIterator(TEST_RESOURCES_DIR . '/saml/responses');
        /** @var $responseFile DirectoryIterator */
        foreach($responsesDir as $responseFile) {
            if ($responseFile->isFile() && !$responseFile->isDot()) {
                $extension = substr($responseFile->getFilename(), -3);
                $fileNameWithoutExtension = substr($responseFile->getFilename(), 0, -4);
                if ($extension == 'pem') {
                    $responseFiles[$fileNameWithoutExtension]['certificateFile'] = $responseFile->getRealPath();
                } elseif ($extension == 'xml') {
                    $responseFiles[$fileNameWithoutExtension]['responseXmlFile'] = $responseFile->getRealPath();
                }
            }
        }

        return $responseFiles;
    }
}
{'fraction_non_alphanumeric': 0.11862835959221502, 'fraction_numerical': 0.003151065801668211, 'mean_word_length': 2.445721583652618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '3400790', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1458, 'n_words': 255}