PHP Classes

File: test/unit/StreamTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Halite   test/unit/StreamTest.php   Download  
File: test/unit/StreamTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: Halite
Perform cryptography operations with libsodium
Author: By
Last change:
Date: 8 years ago
Size: 1,539 bytes
 

Contents

Class file image Download
<?php

use \ParagonIE\Halite\Alerts as CryptoException;
use \
ParagonIE\Halite\Stream\ReadOnlyFile;
use \
ParagonIE\Halite\Util;

/**
 * @backupGlobals disabled
 * @backupStaticAttributes disabled
 */
class StreamTest extends PHPUnit_Framework_TestCase
{
    public function
testFileHash()
    {
       
$filename = \tempnam('/tmp', 'x');
       
       
$buf = \Sodium\randombytes_buf(65537);
        \
file_put_contents($filename, $buf);
       
       
$fileOne = new ReadOnlyFile($filename);
       
$fp = \fopen($filename, 'rb');
       
$fileTwo = new ReadOnlyFile($fp);
       
       
$this->assertEquals(
           
$fileOne->getHash(),
           
$fileTwo->getHash()
        );
        \
fclose($fp);
    }
   
    public function
testFileRead()
    {
       
$filename = \tempnam('/tmp', 'x');
       
       
$buf = \Sodium\randombytes_buf(65537);
        \
file_put_contents($filename, $buf);
       
       
$fStream = new ReadOnlyFile($filename);
       
       
$this->assertEquals(
           
$fStream->readBytes(65537),
           
$buf
       
);
       
$fStream->reset(0);
       
        \
file_put_contents(
           
$filename,
           
Util::safeSubstr($buf, 0, 32768) . 'x' . Util::safeSubstr($buf, 32768)
        );
       
        try {
           
$x = $fStream->readBytes(65537);
            throw new \
Exception('fail');
        } catch (
CryptoException\FileModified $ex) {
           
$this->assertTrue(
               
$ex instanceof CryptoException\FileModified
           
);
        }
    }
}