Browse Source

a lot of tunes for correct work on 32 bit operation systems

pull/8/head
Paul Rock 7 years ago
parent
commit
ecc2bd87c2
  1. 9
      src/APILengthCoDec.php
  2. 4
      src/Helpers/BinaryStringHelper.php
  3. 17
      tests/APILengthCoDecTest.php
  4. 18
      tests/Helpers/BinaryStringHelperTest.php

9
src/APILengthCoDec.php

@ -15,7 +15,13 @@ use RouterOS\Helpers\BinaryStringHelper;
*/
class APILengthCoDec
{
public static function encodeLength(int $length): string
/**
* Encode string to length of string
*
* @param int|float $length
* @return string
*/
public static function encodeLength($length): string
{
// Encode the length :
// - if length <= 0x7F (binary : 01111111 => 7 bits set to 1)
@ -163,6 +169,7 @@ class APILengthCoDec
// PHP5 windows versions of php, even on 64 bits systems was impacted
// see : https://stackoverflow.com/questions/27865340/php-int-size-returns-4-but-my-operating-system-is-64-bit
// How can we test it ?
// @codeCoverageIgnoreStart
throw new \OverflowException("Your system is using 32 bits integers, cannot decode this value ($firstByte) on this system");
// @codeCoverageIgnoreEnd

4
src/Helpers/BinaryStringHelper.php

@ -23,10 +23,10 @@ class BinaryStringHelper
* Compatible with 8, 16, 32, 64 etc.. bits systems
*
* @see https://en.wikipedia.org/wiki/Endianness
* @param int $value the integer value to be converted
* @param int|float $value the integer value to be converted
* @return string the binary string
*/
public static function IntegerToNBOBinaryString(int $value): string
public static function IntegerToNBOBinaryString($value): string
{
// Initialize an empty string
$buffer = '';

17
tests/APILengthCoDecTest.php

@ -11,6 +11,7 @@ use RouterOS\Helpers\BinaryStringHelper;
/**
* Limit code coverage to the class
*
* @coversDefaultClass \RouterOS\APILengthCoDec
*/
class APILengthCoDecTest extends TestCase
@ -25,7 +26,7 @@ class APILengthCoDecTest extends TestCase
APILengthCoDec::encodeLength($length);
}
public function encodeLengthNegativeProvider()
public function encodeLengthNegativeProvider(): array
{
return [
[-1],
@ -39,10 +40,10 @@ class APILengthCoDecTest extends TestCase
*/
public function test__encodeLength($expected, $length)
{
$this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString($expected), APILengthCoDec::encodeLength($length));
$this->assertEquals(BinaryStringHelper::IntegerToNBOBinaryString((int) $expected), APILengthCoDec::encodeLength($length));
}
public function encodedLengthProvider()
public function encodedLengthProvider(): array
{
// [encoded length value, length value]
return [
@ -54,7 +55,7 @@ class APILengthCoDecTest extends TestCase
[0x9C42, 0x1C42], // Arbitrary median value for 2 bytes encoded length
[0xBFFF, 0x3FFF], // High limit value for 2 bytes encoded length
[0xC04000, 0x4000], // Low limit value for 3 bytesv
[0xC04000, 0x4000], // Low limit value for 3 bytes
[0xCAD73B, 0xAD73B], // Arbitrary median value for 3 bytes encoded length
[0xDFFFFF, 0x1FFFFF], // High limit value for 3 bytes encoded length
@ -83,19 +84,19 @@ class APILengthCoDecTest extends TestCase
/**
* @dataProvider decodeLengthControlWordProvider
* @covers ::decodeLength
* @expectedException UnexpectedValueException
* @expectedException \UnexpectedValueException
*/
public function test_decodeLengthControlWord(string $encodedLength)
{
APILengthCoDec::decodeLength(new StringStream($encodedLength));
}
public function decodeLengthControlWordProvider()
public function decodeLengthControlWordProvider(): array
{
// Control bytes : 5 most signficants its sets to 1
// Control bytes: 5 most significance its sets to 1
return [
[chr(0xF8)], // minimum
[chr(0xFC)], // arbitraty value
[chr(0xFC)], // arbitrary value
[chr(0xFF)], // maximum
];
}

18
tests/Helpers/BinaryStringHelperTest.php

@ -1,4 +1,5 @@
<?php
namespace RouterOS\Tests\Helpers;
use PHPUnit\Framework\TestCase;
@ -7,7 +8,8 @@ use RouterOS\Helpers\BinaryStringHelper;
/**
* Limit code coverage to the class
* @coversDefaultClass RouterOS\Helpers\BinaryStringHelper
*
* @coversDefaultClass \RouterOS\Helpers\BinaryStringHelper
*/
class BinaryStringHelperTest extends TestCase
{
@ -15,16 +17,16 @@ class BinaryStringHelperTest extends TestCase
* @dataProvider IntegerToNBOBinaryStringProvider
* @covers ::IntegerToNBOBinaryString
*/
public function test__IntegerToNBOBinaryString(int $value, string $expected)
public function test__IntegerToNBOBinaryString($value, $expected)
{
$this->assertEquals($expected, BinaryStringHelper::IntegerToNBOBinaryString($value));
}
public function IntegerToNBOBinaryStringProvider()
public function IntegerToNBOBinaryStringProvider(): array
{
return [
[0, chr(0)], // lower boundary value
[0xFFFFFFFF, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)], // 32 bits maximal value
[0xFFFFFFFF, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 32 bits maximal value
// strange behaviour :
// TypeError: Argument 1 passed to RouterOS\Tests\Helpers\BinaryStringHelperTest::test__IntegerToNBOBinaryString() must be of the type integer, float given
@ -33,11 +35,11 @@ class BinaryStringHelperTest extends TestCase
// [0xFFFFFFFFFFFFFFFF, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)],
// -1 is encoded with 0xFFFFFFF.....
// 64 bits maximal value (on a 64 bits system)
[-1, chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF).chr(0xFF)], // 64 bits upper boundary value
// Let's try random value
[0x390DDD99, chr(0x39).chr(0x0D).chr(0xDD).chr(0x99)],
// 64 bits maximal value (on a 64 bits system only)
[-1, chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF) . chr(0xFF)], // 64 bits upper boundary value
// Let's try random value
[0x390DDD99, chr(0x39) . chr(0x0D) . chr(0xDD) . chr(0x99)],
];
}
}
Loading…
Cancel
Save