Skip to content

Commit

Permalink
This should be our last commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jan 26, 2018
1 parent 77ac0d2 commit 3728ff4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Asymmetric/Crypto.php
Expand Up @@ -59,6 +59,7 @@ final private function __construct()
* @return string Ciphertext
*
* @throws CannotPerformOperation
* @throws InvalidKey
* @throws InvalidMessage
* @throws InvalidDigestLength
* @throws InvalidType
Expand Down Expand Up @@ -135,6 +136,7 @@ public static function encryptWithAd(
*
* @throws CannotPerformOperation
* @throws InvalidDigestLength
* @throws InvalidKey
* @throws InvalidMessage
* @throws InvalidSignature
* @throws InvalidType
Expand Down
11 changes: 11 additions & 0 deletions src/Stream/MutableFile.php
Expand Up @@ -152,28 +152,34 @@ public function getSize(): int
*/
public function readBytes(int $num, bool $skipTests = false): string
{
// @codeCoverageIgnoreStart
if ($num < 0) {
throw new CannotPerformOperation('num < 0');
} elseif ($num === 0) {
return '';
}
// @codeCoverageIgnoreEnd
if (($this->pos + $num) > $this->stat['size']) {
throw new CannotPerformOperation('Out-of-bounds read');
}
$buf = '';
$remaining = $num;
do {
if ($remaining <= 0) {
// @codeCoverageIgnoreStart
break;
// @codeCoverageIgnoreEnd
}
/** @var int $bufSize */
$bufSize = \min($remaining, self::CHUNK);
/** @var string $read */
$read = \fread($this->fp, $bufSize);
if (!\is_string($read)) {
// @codeCoverageIgnoreStart
throw new FileAccessDenied(
'Could not read from the file'
);
// @codeCoverageIgnoreEnd
}
$buf .= $read;
$readSize = Binary::safeStrlen($read);
Expand Down Expand Up @@ -207,6 +213,7 @@ public function remainingBytes(): int
* @param int $i
* @return bool
* @throws CannotPerformOperation
* @codeCoverageIgnore
*/
public function reset(int $i = 0): bool
{
Expand Down Expand Up @@ -236,19 +243,23 @@ public function writeBytes(string $buf, int $num = null): int
if ($num === null || $num > $bufSize) {
$num = $bufSize;
}
// @codeCoverageIgnoreStart
if ($num < 0) {
throw new CannotPerformOperation('num < 0');
}
// @codeCoverageIgnoreEnd
$remaining = $num;
do {
if ($remaining <= 0) {
break;
}
$written = \fwrite($this->fp, $buf, $remaining);
if ($written === false) {
// @codeCoverageIgnoreStart
throw new FileAccessDenied(
'Could not write to the file'
);
// @codeCoverageIgnoreEnd
}
$buf = Binary::safeSubstr($buf, $written, null);
$this->pos += $written;
Expand Down
6 changes: 6 additions & 0 deletions test/unit/StreamTest.php
Expand Up @@ -176,6 +176,12 @@ public function testFileRead()
$mStream->reset(0);

$this->assertSame(0, $mStream->getPos());
$this->assertSame($size, $mStream->remainingBytes());

$mStream->reset(127);
$this->assertSame($size - 127, $mStream->remainingBytes());
$mStream->reset(0);

$this->assertSame($size, $mStream->getSize());
$this->assertSame(bin2hex($buffer), bin2hex($mStream->readBytes($size)));
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/Structure/MerkleTreeTest.php
Expand Up @@ -34,6 +34,10 @@ public function testExpectedBehavior()
'6781891a87aa476454b74dc635c5cdebfc8f887438829ce2e81423f54906c058',
$treeA->getRoot()
);
$this->assertSame(
hex2bin('6781891a87aa476454b74dc635c5cdebfc8f887438829ce2e81423f54906c058'),
$treeA->getRoot(true)
);
$treeB = new MerkleTree(
new Node('a'),
new Node('b'),
Expand Down
1 change: 1 addition & 0 deletions test/unit/Structure/NodeTest.php
Expand Up @@ -23,5 +23,6 @@ public function testHash()
$hash = sodium_crypto_generichash($stringData . $extra);

$this->assertSame(bin2hex($hash), $node->getExpandedNode($extra)->getHash());
$this->assertSame($hash, $node->getExpandedNode($extra)->getHash(true));
}
}

0 comments on commit 3728ff4

Please sign in to comment.