PHP Classes
elePHPant
Icontem

File: tests/compat/NamespacedTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  PHP Sodium Compat  >  tests/compat/NamespacedTest.php  >  Download  
File: tests/compat/NamespacedTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Sodium Compat
Cryptographic functions of libsodium in pure PHP
Author: By
Last change: Make 32-bit twice as fast.
Date: 8 months ago
Size: 3,839 bytes
 

Contents

Class file image Download
<?php

/**
 * Class SodiumCompatTest
 */
class NamespacedTest extends PHPUnit_Framework_TestCase
{
    public function
setUp()
    {
        if (
PHP_VERSION_ID < 50300) {
           
$this->markTestSkipped('PHP < 5.3.0; skipping PHP 5.3+ compatibility test suite.');
        }
       
ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true;
    }

   
/**
     * @covers ParagonIE_Sodium_Compat::crypto_secretbox()
     */
   
public function testCryptoSecretBox()
    {
       
$key = str_repeat("\x80", 32);
       
$nonce = str_repeat("\x00", 24);
       
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

       
$this->assertSame(
           
ParagonIE_Sodium_Core_Util::substr(
               
bin2hex(
                   
call_user_func_array(
                        array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                        array(
$message, $nonce, $key)
                    )
                ),
               
0, 32
           
),
           
ParagonIE_Sodium_Core_Util::substr(
               
bin2hex(ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key)),
               
0, 32
           
),
           
'secretbox - short messages'
       
);
       
$this->assertSame(
           
$message,
           
ParagonIE_Sodium_Compat::crypto_secretbox_open(
               
call_user_func_array(
                    array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                    array(
$message, $nonce, $key)
                ),
               
$nonce,
               
$key
           
)
        );
       
$this->assertSame(
           
$message,
           
call_user_func_array(
                array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox_open'),
                array(
                   
ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key),
                   
$nonce,
                   
$key
               
)
            )
        );
       
$message = str_repeat('a', 97);
       
$this->assertSame(
           
bin2hex(
               
call_user_func_array(
                    array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                    array(
$message, $nonce, $key)
                )
            ),
           
bin2hex(ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key)),
           
'secretbox - long messages (multiple of 16)'
       
);

       
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

       
$message = str_repeat($message, 16);

       
$this->assertSame(
           
bin2hex(
               
call_user_func_array(
                    array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                    array(
$message, $nonce, $key)
                )
            ),
           
bin2hex(ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key)),
           
'secretbox - long messages (multiple of 16)'
       
);

       
$message .= 'a';

       
$this->assertSame(
           
bin2hex(
               
call_user_func_array(
                    array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                    array(
$message, $nonce, $key)
                )
            ),
           
bin2hex(ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key)),
           
'secretbox - long messages (NOT a multiple of 16)'
       
);

       
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

       
$this->assertSame(
           
bin2hex(
               
call_user_func_array(
                    array(
'\\ParagonIE\\Sodium\\Compat', 'crypto_secretbox'),
                    array(
$message, $nonce, $key)
                )
            ),
           
bin2hex(ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key)),
           
'secretbox - medium messages'
       
);
    }
}