<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_InfoCard
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: XmlParsingTest.php 8064 2008-02-16 10:58:39Z thomas $
 */

// Call Zend_InfoCard_XmlParsingTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "Zend_InfoCard_XmlParsingTest::main");
}

require_once dirname(dirname(dirname(__FILE__))) . '/TestHelper.php';
require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once 'Zend/InfoCard/Xml/EncryptedData.php';
require_once 'Zend/InfoCard/Xml/SecurityTokenReference.php';

class Zend_InfoCard_XmlParsingTest extends PHPUnit_Framework_TestCase
{
    protected $_xmlDocument;

    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main()
    {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("Zend_InfoCard_XmlParsingTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    public function setUp()
    {
        $this->tokenDocument = dirname(__FILE__) . '/_files/encryptedtoken.xml';
        $this->loadXmlDocument();
    }

    public function loadXmlDocument()
    {
        $this->_xmlDocument = file_get_contents($this->tokenDocument);
    }

    public function testEncryptedDataType()
    {
            $doc = file_get_contents(dirname(__FILE__) . '/_files/encryptedtoken_bad_type.xml');

            try {
                $encryptedData = Zend_InfoCard_Xml_EncryptedData::getInstance($doc);
                $this->fail("Exception not thrown as expected");
            } catch(Exception $e) {
                /* yay */
            }

            try {
                $encryptedData = Zend_InfoCard_Xml_EncryptedData::getInstance(10);
                $this->fail("Exception not thrown as expected");
            } catch(Exception $e) {
                /* yay */
            }

    }
    public function testEncryptedData()
    {
        $encryptedData = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument);

        $this->assertTrue($encryptedData instanceof Zend_InfoCard_Xml_EncryptedData_XmlEnc);
        $this->assertSame($encryptedData->getCipherValue(), ' ');

        $this->assertSame($encryptedData->getEncryptionMethod(), 'http://www.w3.org/2001/04/xmlenc#aes256-cbc');
        $this->assertTrue($encryptedData->getKeyInfo() instanceof Zend_InfoCard_Xml_KeyInfo_XmlDSig);
    }

    public function testTostring()
    {
        $encryptedData = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument);
        $key = $encryptedData->getKeyInfo();

        $this->assertTrue(is_string($key->__toString()));
    }

    public function testConversion()
    {
        $encryptedData = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument);

        $keyInfo = $encryptedData->getKeyInfo();

        $this->assertTrue($keyInfo instanceof SimpleXMLElement);

        $dom = Zend_InfoCard_Xml_KeyInfo_XmlDSig::convertToDOM($keyInfo);
        $this->assertTrue($dom instanceof DOMNode);
        $sxe = Zend_InfoCard_Xml_KeyInfo_XmlDSig::convertToObject($dom, 'Zend_InfoCard_Xml_KeyInfo_XmlDSig');

        $this->assertTrue($sxe instanceof Zend_InfoCard_Xml_KeyInfo_XmlDSig);
    }

    public function testEncryptedDataKeyInfo()
    {
        $keyinfo = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument)->getKeyInfo();

        $this->assertTrue($keyinfo instanceof Zend_InfoCard_Xml_KeyInfo_XmlDSig);
        $this->assertTrue($keyinfo->getEncryptedKey() instanceof Zend_InfoCard_Xml_EncryptedKey);
    }

    public function testEncryptedKey()
    {
        $enckey = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument)->getKeyInfo()->getEncryptedKey();

        $this->assertTrue($enckey instanceof Zend_InfoCard_Xml_EncryptedKey);

        $this->assertSame($enckey->getEncryptionMethod(), 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p');
        $this->assertSame($enckey->getDigestMethod(), 'http://www.w3.org/2000/09/xmldsig#sha1');
        $this->assertSame($enckey->getCipherValue(), 'AIgtBEv9lGMikyHjV/b5mQ5LbLyupNtRH8hl5I6tJsZI5CYP32BLo9FgxAY5ZReEv+XZbqcs5KORBvTbMkP6l7MY32WJGPBDDMSB7k6DshryoZqlmGMbjt2g1nM7xOuwwfru1jC7t+qCBXL4PPBpHDhHzAW7u8tB8LQCU6GklIFa1+GoZbQ00BY/OoPbE3rxhxgGPAHXfYPLjGALIkYo9czeTO/zfcydHl5Xcyp/PsskSOUhNFcftxG+fQELb/oqc50ldBWlxBM/qU7fLI4KRfUag3J5sanCUsgiYdF0iQNfiYnUKLa9ThDHjHUQnB5EEt77cM2/DKQkyExMBBgYcRo9GzqyLXiDCYWVatCQU6rAD8NkBBpFs8W/0QXIV1J/S3DuZS3Eo4x27gRlT5YfUeO7jAZvwqy51WHNXwq13QTV2AOGfvpK3054sZm+10jdfAq6tgYdShQgO2kHRGP1q9vAC3SfD49mP9q+AemJrAkiR2HZTxkEQ+AttdfPhc2dzdLXp+ukQdqpL/xlywIp+KIim+YVjhO+Bi92rRn5Kl0h7q6MkpoTGI1F+akmNhD6VmB1Nd0G6e4AGTisuyd+vygEH7fsZhZuiMSknajfgPgazKiLUihwRvfk4FJm18Ju97tXcl6LhIJpkOcq7sI25GhWz0mHX1ErOf/949pcozo=');

        $this->assertTrue($enckey->getKeyInfo() instanceof Zend_InfoCard_Xml_KeyInfo_Default);
    }

    public function testEncryptedKeyKeyInfo()
    {
        $keyinfo = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument)->getKeyInfo()->getEncryptedKey()->getKeyInfo();

        $this->assertTrue($keyinfo instanceof Zend_InfoCard_Xml_KeyInfo_Default);
        $this->assertTrue($keyinfo->getSecurityTokenReference() instanceof Zend_InfoCard_Xml_SecurityTokenReference);
    }

    public function testSecurityTokenReference()
    {
        $sectoken = Zend_InfoCard_Xml_EncryptedData::getInstance($this->_xmlDocument)->getKeyInfo()
                                                                                      ->getEncryptedKey()
                                                                                      ->getKeyInfo()
                                                                                      ->getSecurityTokenReference();
        $this->assertTrue($sectoken instanceof Zend_InfoCard_Xml_SecurityTokenReference);

        $this->assertSame($sectoken->getKeyThumbprintType(), 'http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#ThumbprintSHA1');
        $this->assertSame($sectoken->getKeyThumbprintEncodingType(), 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary');
        $this->assertSame($sectoken->getKeyReference(false), '/OCqQ7Np25sOiA+4OsFh1R6qIeY=');
    }

    public function testEncryptedKeyErrors()
    {
        try {
            Zend_InfoCard_Xml_EncryptedKey::getInstance(10);
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }

        $doc = file_get_contents(dirname(__FILE__) . "/_files/encryptedkey_bad_block.xml");

        try {
            Zend_InfoCard_Xml_EncryptedKey::getInstance($doc);
            $this->fail("Expected Exception not thrown");
        } catch(Exception $e) {
            /* yay */
        }



        $doc = file_get_contents(dirname(__FILE__) . "/_files/encryptedkey_missing_enc_algo.xml");
        $ek = Zend_InfoCard_Xml_EncryptedKey::getInstance($doc);

        try {
            $ek->getEncryptionMethod();
            $this->fail("Expected Exception not thrown");
        } catch(Exception $e) {
            /* yay */
        }

    }

    public function testKeyInfo()
    {
        try {
            Zend_InfoCard_Xml_KeyInfo::getInstance("<foo/>");
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }

        try {
            Zend_InfoCard_Xml_KeyInfo::getInstance(10);
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }
    }

    public function testSecurityTokenReferenceErrors()
    {
        try {
            Zend_InfoCard_Xml_SecurityTokenReference::getInstance("<foo/>");
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }

        try {
            Zend_InfoCard_Xml_SecurityTokenReference::getInstance(10);
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }

        $doc = file_get_contents(dirname(__FILE__) . "/_files/security_token_bad_keyref.xml");

        try {
            $si = Zend_InfoCard_Xml_SecurityTokenReference::getInstance($doc);
            $si->getKeyReference();
            $this->fail("Expected Exception Not thrown");
        } catch(Exception $e) {
            /* yay */
        }
    }

}

// Call Zend_InfoCard_XmlParsingTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "Zend_InfoCard_XmlParsingTest::main") {
    Zend_InfoCard_XmlParsingTest::main();
}
