<?php


namespace Buckhill\U2F\tests\units\Enroll;

require('vendor/autoload.php');

use mageekguy\atoum;
use Buckhill\U2F\U2FHelper;
use function Buckhill\U2F\u2f;

class Response extends atoum
{
    public function testResponseCreatedOK()
    {
        $this
            ->given($response = \Buckhill\U2F\Enroll\Response::fromArray($this->getValidEnrollmentData()))
            ->and(
                $response->parse(),
                $response->verifySignature()
            )
            ->then
                ->object($response)->isInstanceOf(\Buckhill\U2F\Enroll\Response::class)

                ->object($response->getParser())->isInstanceOf(\Buckhill\U2F\Enroll\RequestParser::class)

                ->string(U2FHelper::pub2pem($response->getParser()->getPublicKey()))->isEqualTo($this->getExpectedPublicKey())

                ->string($response->getParser()->getPEMEncodedCertificate())->isEqualTo($this->getExpectedPEMCert())

                ->array($response->toArray())->isNotEmpty()
        ;

        return 1;
    }

    public function testResponseFromFunctionOK()
    {
        $this
            ->given($enroll = u2f()->enroll()->response($this->getValidEnrollmentData()))
            ->and(
                $enroll->parse(),
                $enroll->verifySignature()
            )
            ->then
                ->object($enroll)->isInstanceOf(\Buckhill\U2F\Enroll\Response::class)

                ->string(hash('sha256', $enroll->getKeyHandle(), true))->isEqualTo(hex2bin('19b79e5a2bb8ca6ff02a39bbef810738e295e679b099bcb2217229c457dce0e8'))

                ->string($enroll->getPublicKey())->isEqualTo($this->getExpectedPublicKey())

                ->string(hash('sha256', $enroll->getPublicKey(false), true))->isEqualTo(hex2bin('ba46fb771e0fdd39693d5243620981773829687cc17821bcc3a19c3ac69dd3a8'))

                ->string($enroll->getAttestationCertificate())->isEqualTo($this->getExpectedPEMCert())
        ;
    }

    public function testResponseFails()
    {
        $this->given($enroll = u2f()->enroll()->response($this->getValidEnrollmentData()))
            ->then
                ->exception(
                    function() use (&$enroll)
                    {
                        // Throws a \DomainException because there's no parser instance available
                        $enroll->getPublicKey();
                    }
                )
                    ->isIdenticalTo($this->exception)
                    ->hasMessage('No instance of RequestParser found. Invoke ::parse() method before retrieving the parser instance.')
            ;
    }


    /**
     * @return array
     */
    protected function getValidEnrollmentData()
    {
        return [
            'registrationData' => 'BQSl1E1Tc924vsXAXCoCWTyTz9vLCbMwGwdnSYkn3KbfJpxVltmTfkwUmcLW_mAmckCWyyJxJ8XosrngElQi3f0ZQOMb99megSY-O_Q_oyazo2vkkJFpFgadwRrvLhAF9qQbFwIPulxSP36bIbTL998HBS-qnl0ihWZi0Iodia52o1QwggIuMIIBGKADAgECAgQKYwv_MAsGCSqGSIb3DQEBCzAuMSwwKgYDVQQDEyNZdWJpY28gVTJGIFJvb3QgQ0EgU2VyaWFsIDQ1NzIwMDYzMTAgFw0xNDA4MDEwMDAwMDBaGA8yMDUwMDkwNDAwMDAwMFowKTEnMCUGA1UEAwweWXViaWNvIFUyRiBFRSBTZXJpYWwgMTc0MjYzMjk1MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEpCNkXbqLI-1s2eXki5Mqz99FZMdXMfHPUgdFEGtJupYPrQ8KpQk97jOQhEWoFrg2fN2FVxLzSeVm5jcA1CbQn6MmMCQwIgYJKwYBBAGCxAoCBBUxLjMuNi4xLjQuMS40MTQ4Mi4xLjIwCwYJKoZIhvcNAQELA4IBAQBlObAyoc_ESNIHrhSbCra0YMqmUBwTOvCaDqSBLwo-QkpcpK71PUfpcEz34sAT1w3jyhSKsUWYMiVNtlFdTZWbao7052tJUVj6p1sX2i2LwIoCY8e4llJbqKLuo0Tn10cpRo_iUNimeFEOq21ZJRqWHYs8MlBSk71uwk-SA1kCEFRAxhlcv8xBktFiRf6V3uWVM05fm7uf_hjkQyv6Wo7JVUL2VDok_rGhr2rpmn35j1mYsJ5yuty4fr6ksOADM9xzyrGELY5HUHRlKkj1EGBxPQqqdUTS2rUoQN6iQ-oXcyCYNGQn779ralXTABh88Le0LnsG0F0f9RcQHzFlUWG0MEUCICPn5mejgZZ7HAc12IsG_ZFHk2euSJs2Ic5FI5PeBijaAiEAr2zv0YnjK14njrwM5HarglOevsffedupMAzcA_z35WI',
            'challenge' => 'RFpVZFhzdjkyZWdxOVY4NUdEZmN1ZFp6RndiQVJCMTg',
            'version' => 'U2F_V2',
            'appId' => 'https://server.mfa.web',
            'clientData' => 'eyJ0eXAiOiJuYXZpZ2F0b3IuaWQuZmluaXNoRW5yb2xsbWVudCIsImNoYWxsZW5nZSI6IlJGcFZaRmh6ZGpreVpXZHhPVlk0TlVkRVptTjFaRnA2Um5kaVFWSkNNVGciLCJvcmlnaW4iOiJodHRwczovL3NlcnZlci5tZmEud2ViIiwiY2lkX3B1YmtleSI6IiJ9'
        ];
    }


    /**
     * @return string
     */
    protected function getExpectedPublicKey()
    {
        return <<<EOF
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEpdRNU3PduL7FwFwqAlk8k8/bywmz
MBsHZ0mJJ9ym3yacVZbZk35MFJnC1v5gJnJAlssicSfF6LK54BJUIt39GQ==
-----END PUBLIC KEY-----
EOF;

    }


    /**
     * @return string
     */
    protected function getExpectedPEMCert()
    {
        return <<<EOF
-----BEGIN CERTIFICATE-----
MIICLjCCARigAwIBAgIECmML/zALBgkqhkiG9w0BAQswLjEsMCoGA1UEAxMjWXVi
aWNvIFUyRiBSb290IENBIFNlcmlhbCA0NTcyMDA2MzEwIBcNMTQwODAxMDAwMDAw
WhgPMjA1MDA5MDQwMDAwMDBaMCkxJzAlBgNVBAMMHll1YmljbyBVMkYgRUUgU2Vy
aWFsIDE3NDI2MzI5NTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABKQjZF26iyPt
bNnl5IuTKs/fRWTHVzHxz1IHRRBrSbqWD60PCqUJPe4zkIRFqBa4NnzdhVcS80nl
ZuY3ANQm0J+jJjAkMCIGCSsGAQQBgsQKAgQVMS4zLjYuMS40LjEuNDE0ODIuMS4y
MAsGCSqGSIb3DQEBCwOCAQEAZTmwMqHPxEjSB64Umwq2tGDKplAcEzrwmg6kgS8K
PkJKXKSu9T1H6XBM9+LAE9cN48oUirFFmDIlTbZRXU2Vm2qO9OdrSVFY+qdbF9ot
i8CKAmPHuJZSW6ii7qNE59dHKUaP4lDYpnhRDqttWSUalh2LPDJQUpO9bsJPkgNZ
AhBUQMYZXL/MQZLRYkX+ld7llTNOX5u7n/4Y5EMr+lqOyVVC9lQ6JP6xoa9q6Zp9
+Y9ZmLCecrrcuH6+pLDgAzPcc8qxhC2OR1B0ZSpI9RBgcT0KqnVE0tq1KEDeokPq
F3MgmDRkJ++/a2pV0wAYfPC3tC57BtBdH/UXEB8xZVFhtA==
-----END CERTIFICATE-----
EOF;

    }
}