public function testLengthEncodingExceptions() { $smallLength = -1; try { Communicator::encodeLength($smallLength); } catch (LengthException $e) { $this->assertEquals(LengthException::CODE_INVALID, $e->getCode(), "Length '{$smallLength}' must not be encodable."); $this->assertEquals($smallLength, $e->getLength(), 'Exception is misleading.'); } $largeLength = 0x800000000; try { Communicator::encodeLength($largeLength); } catch (LengthException $e) { $this->assertEquals(LengthException::CODE_BEYOND_SHEME, $e->getCode(), "Length '{$largeLength}' must not be encodable."); $this->assertEquals($largeLength, $e->getLength(), 'Exception is misleading.'); } }
fwrite(STDOUT, implode("\n", array(implode($cSep, array(str_pad('MODE', $cColumns['mode'], ' ', STR_PAD_RIGHT), str_pad('LENGTH', $cColumns['length'], ' ', STR_PAD_BOTH), str_pad('LENGTH', $cColumns['encodedLength'], ' ', STR_PAD_BOTH), ' CONTENTS')), implode($cSep, array(str_repeat(' ', $cColumns['mode']), str_pad('(decoded)', $cColumns['length'], ' ', STR_PAD_BOTH), str_pad('(encoded)', $cColumns['encodedLength'], ' ', STR_PAD_BOTH), '')), implode('-|-', array(str_repeat('-', $cColumns['mode']), str_repeat('-', $cColumns['length']), str_repeat('-', $cColumns['encodedLength']), str_repeat('-', $cColumns['contents']))))) . "\n"); $cRegexWrap = '/([^\\n]{1,' . $cColumns['contents'] . '})/sS'; $printWord = function ($mode, $word, $msg = '') use($cSep, $cColumns, $cRegexWrap, $cColors) { $wordFragments = preg_split($cRegexWrap, $word, null, PREG_SPLIT_DELIM_CAPTURE); for ($i = 0, $l = count($wordFragments); $i < $l; $i += 2) { unset($wordFragments[$i]); } if ('' !== $cColors['']) { $wordFragments = str_replace("", "[27@", $wordFragments); } $isAbnormal = 'ERR' === $mode || 'NOTE' === $mode; if ($isAbnormal) { $details = str_pad($msg, $cColumns['length'] + $cColumns['encodedLength'] + 3, ' ', STR_PAD_BOTH); } else { $length = strlen($word); $lengthBytes = RouterOS\Communicator::encodeLength($length); $encodedLength = ''; for ($i = 0, $l = strlen($lengthBytes); $i < $l; ++$i) { $encodedLength .= str_pad(dechex(ord($lengthBytes[$i])), 2, '0', STR_PAD_LEFT); } $details = str_pad($length, $cColumns['length'], ' ', STR_PAD_LEFT) . $cSep . str_pad('0x' . strtoupper($encodedLength), $cColumns['encodedLength'], ' ', STR_PAD_LEFT); } fwrite(STDOUT, $cColors[$mode] . str_pad($mode, $cColumns['mode'], ' ', STR_PAD_RIGHT) . $cColors[''] . "{$cSep}{$details}{$cSep}{$cColors[$mode]}" . implode("\n{$cColors['']}" . str_repeat(' ', $cColumns['mode']) . $cSep . implode($isAbnormal ? ' ' : $cSep, array(str_repeat(' ', $cColumns['length']), str_repeat(' ', $cColumns['encodedLength']))) . $cSep . $cColors[$mode], $wordFragments) . "\n{$cColors['']}"); }; } else { $printWord = function ($mode, $word, $msg = '') use($cColors) { if ('' !== $cColors['']) { $word = str_replace("", "[27@", $word); $msg = str_replace("", "[27@", $msg); } if ('ERR' === $mode || 'NOTE' === $mode) {