<commit_before><?php

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use Base32\Base32;
use OTPHP\TOTP;

class TOTPTest extends \PHPUnit_Framework_TestCase
{
    public function testCustomParameter()
    {
        $otp = new TOTP();

        $otp->setLabel('<EMAIL>')
            ->setIssuer('My Project')
            ->setDigest('sha512')
            ->setDigits(8)
            ->setIssuerIncludedAsParameter(true)
            ->setSecret('<KEY>')
            ->setInterval(20)
            ->setParameter('foo', 'bar.baz');

        $this->assertEquals('otpauth://totp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&digits=8&foo=bar.baz&issuer=My%20Project&period=20&secret=<KEY>', $otp->getProvisioningUri());
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Interval must be at least 1.
     */
    public function testIntervalIsNotNumeric()
    {
        $otp = new TOTP();
        $otp->setInterval('foo');
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Interval must be at least 1.
     */
    public function testIntervalIsNot1OrMore()
    {
        $otp = new TOTP();
        $otp->setInterval(-500);
    }

    public function testGetProvisioningUri()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals('otpauth://totp/My%20Project%3Aalice%40foo.bar?secret=<KEY>', $otp->getProvisioningUri());
    }

    public function testGenerateOtpAt()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals('855783', $otp->at(0));
        $this->assertEquals('762124', $otp->at(319690800));
        $this->assertEquals('139664', $otp->at(1301012137));
    }

    public function testGenerateOtpNow()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals($otp->now(), $otp->at(time()));
    }

    public function testVerifyOtpNow()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $totp = $otp->at(time());
        $this->assertTrue($otp->verify($totp));
    }

    public function testVerifyOtp()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertTrue($otp->verify('855783', 0));
        $this->assertTrue($otp->verify('762124', 319690800));
        $this->assertTrue($otp->verify('139664', 1301012137));
<commit_msg>Update TOTPTest.php<commit_after><?php

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use Base32\Base32;
use OTPHP\TOTP;

class TOTPTest extends \PHPUnit_Framework_TestCase
{
    public function testCustomParameter()
    {
        $otp = new TOTP();

        $otp->setLabel('<EMAIL>')
            ->setIssuer('My Project')
            ->setDigest('sha512')
            ->setDigits(8)
            ->setIssuerIncludedAsParameter(true)
            ->setSecret('<KEY>')
            ->setInterval(20)
            ->setParameter('foo', 'bar.baz');

        $this->assertEquals('otpauth://totp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&digits=8&foo=bar.baz&issuer=My%20Project&period=20&secret=<KEY>', $otp->getProvisioningUri());
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Interval must be at least 1.
     */
    public function testIntervalIsNotNumeric()
    {
        $otp = new TOTP();
        $otp->setInterval('foo');
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Interval must be at least 1.
     */
    public function testIntervalIsNot1OrMore()
    {
        $otp = new TOTP();
        $otp->setInterval(-500);
    }

    public function testGetProvisioningUri()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals('otpauth://totp/My%20Project%3Aalice%40foo.bar?secret=<KEY>', $otp->getProvisioningUri());
    }

    public function testGenerateOtpAt()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals('855783', $otp->at(0));
        $this->assertEquals('762124', $otp->at(319690800));
        $this->assertEquals('139664', $otp->at(1301012137));
    }

    public function testGenerateOtpAt()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertFalse($otp->verify('0'));
        $this->assertFalse($otp->verify('00'));
        $this->assertFalse($otp->verify('000'));
        $this->assertFalse($otp->verify('0000'));
        $this->assertFalse($otp->verify('00000'));
    }

    public function testGenerateOtpNow()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertEquals($otp->now(), $otp->at(time()));
    }

    public function testVerifyOtpNow()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $totp = $otp->at(time());
        $this->assertTrue($otp->verify($totp));
    }

    public function testVerifyOtp()
    {
        $otp = $this->createTOTP(6, 'sha1', 30);

        $this->assertTrue($otp->verify('855783', 0));
        $this->assertTrue($otp->verify('762124', 319690800));
        $this->assertTrue($otp->verify('139664', 1301012137));

{'fraction_non_alphanumeric': 0.1690245718540581, 'fraction_numerical': 0.054914370811615786, 'mean_word_length': 2.797173144876325, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 96, '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': '4695788', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1787, 'n_words': 295}