Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
name: PHP Tests
on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -53,3 +61,23 @@ jobs:

- name: Run tests
run: vendor/bin/phpunit --coverage-text --testdox

big-endian-unit-tests:
runs-on: ubuntu-latest
name: Unit tests (big-endian / s390x)
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: s390x

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest --ignore-platform-reqs

- name: Run unit tests under emulated big-endian arch
run: |
docker run --rm --platform linux/s390x -v "$PWD":/app -w /app php:8.4-cli \
sh -c 'docker-php-ext-install bcmath >/dev/null && php vendor/bin/phpunit --testsuite "Unit Suite"'
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ services:
ports:
- "3401:3306/tcp"
restart: unless-stopped

tests-big-endian:
container_name: php-s390x-tests
image: php:8.4-cli
platform: linux/s390x
profiles: ["big-endian"]
depends_on:
- replication-test-mysql
volumes:
- .:/app
working_dir: /app
environment:
- DB_HOST=replication-test-mysql
- DB_PORT=3306
- DB_USER=root
- DB_PASSWORD=root
command: sh -c "docker-php-ext-install bcmath sockets pdo_mysql >/dev/null && php vendor/bin/phpunit"
2 changes: 1 addition & 1 deletion src/MySQLReplication/BinLog/BinLogServerInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function make(string $data, string $version): self
}

//connection_id 4 bytes
$connectionId = BinaryDataReader::unpack('I', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
$connectionId = BinaryDataReader::unpack('V', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
++$i;

//auth_plugin_data_part_1
Expand Down
34 changes: 17 additions & 17 deletions src/MySQLReplication/BinLog/BinLogSocketConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getResponse(bool $checkResponse = true): string
if ($header === '') {
return '';
}
$dataLength = BinaryDataReader::unpack('L', $header[0] . $header[1] . $header[2] . chr(0))[1];
$dataLength = BinaryDataReader::unpack('V', $header[0] . $header[1] . $header[2] . chr(0))[1];
$isMaxDataLength = $dataLength === $this->binaryDataMaxLength;

$result = $this->socket->readFromSocket($dataLength);
Expand All @@ -78,7 +78,7 @@ public function getResponse(bool $checkResponse = true): string
if ($header === '') {
return $result;
}
$dataLength = BinaryDataReader::unpack('L', $header[0] . $header[1] . $header[2] . chr(0))[1];
$dataLength = BinaryDataReader::unpack('V', $header[0] . $header[1] . $header[2] . chr(0))[1];
$isMaxDataLength = $dataLength === $this->binaryDataMaxLength;
$next_result = $this->socket->readFromSocket($dataLength);
$result .= $next_result;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function sendSemiSyncAck(): void
. BinaryDataReader::pack64bit((int)$this->binLogCurrent->getBinLogPosition())
. $this->binLogCurrent->getBinFileName();

$this->socket->writeToSocket(pack('l', strlen($payload)) . $payload);
$this->socket->writeToSocket(pack('V', strlen($payload)) . $payload);

$this->logger->debug('Semi-sync ACK sent for ' . $this->binLogCurrent->getBinFileName() . ':' . $this->binLogCurrent->getBinLogPosition());
}
Expand All @@ -135,15 +135,15 @@ private function authenticate(BinLogAuthPluginMode $authPlugin): void
{
$this->logger->debug('Trying to authenticate user: ' . $this->config->user . ' using ' . $authPlugin->value . ' default plugin');

$data = pack('L', self::getCapabilities());
$data .= pack('L', $this->binaryDataMaxLength);
$data = pack('V', self::getCapabilities());
$data .= pack('V', $this->binaryDataMaxLength);
$data .= chr(33);
$data .= str_repeat(chr(0), 23);
$data .= $this->config->user . chr(0);
$auth = $this->getAuthData($authPlugin, $this->binLogServerInfo->salt);
$data .= chr(strlen($auth) & 0xFF) . $auth;
$data .= $authPlugin->value . chr(0);
$str = pack('L', strlen($data));
$str = pack('V', strlen($data));
$s = $str[0] . $str[1] . $str[2];
$data = $s . chr(1) . $data;

Expand Down Expand Up @@ -276,7 +276,7 @@ private function getBinlogStream(): void

private function executeSQL(string $sql): void
{
$this->socket->writeToSocket(pack('LC', strlen($sql) + 1, 0x03) . $sql);
$this->socket->writeToSocket(pack('VC', strlen($sql) + 1, 0x03) . $sql);
$this->getResponse();
}

Expand All @@ -290,7 +290,7 @@ private function registerSlave(): void
$userLength = strlen($this->config->user);
$passLength = strlen($this->config->password);

$data = pack('l', 18 + $hostLength + $userLength + $passLength);
$data = pack('V', 18 + $hostLength + $userLength + $passLength);
$data .= chr(self::COM_REGISTER_SLAVE);
$data .= pack('V', $this->config->slaveId);
$data .= pack('C', $hostLength);
Expand Down Expand Up @@ -325,15 +325,15 @@ private function setBinLogDumpGtid(string $gtid): void
{
$collection = GtidCollection::makeCollectionFromString($gtid);

$data = pack('l', 26 + $collection->getEncodedLength()) . chr(self::COM_BINLOG_DUMP_GTID);
$data .= pack('S', 0);
$data .= pack('I', $this->config->slaveId);
$data .= pack('I', 3);
$data = pack('V', 26 + $collection->getEncodedLength()) . chr(self::COM_BINLOG_DUMP_GTID);
$data .= pack('v', 0);
$data .= pack('V', $this->config->slaveId);
$data .= pack('V', 3);
$data .= chr(0);
$data .= chr(0);
$data .= chr(0);
$data .= BinaryDataReader::pack64bit(4);
$data .= pack('I', $collection->getEncodedLength());
$data .= pack('V', $collection->getEncodedLength());
$data .= $collection->getEncoded();

$this->socket->writeToSocket($data);
Expand Down Expand Up @@ -362,10 +362,10 @@ private function setBinLogDump(): void
$binFileName = $masterStatusDTO->file;
}

$data = pack('i', strlen($binFileName) + 11) . chr(self::COM_BINLOG_DUMP);
$data .= pack('I', $binFilePos);
$data = pack('V', strlen($binFileName) + 11) . chr(self::COM_BINLOG_DUMP);
$data .= pack('V', $binFilePos);
$data .= pack('v', 0);
$data .= pack('I', $this->config->slaveId);
$data .= pack('V', $this->config->slaveId);
$data .= $binFileName;

$this->socket->writeToSocket($data);
Expand All @@ -387,7 +387,7 @@ private function switchAuth(string $response): void

$this->logger->debug('Auth switch packet received, switching to ' . $authPluginSwitched->value);

$this->socket->writeToSocket(pack('L', (strlen($auth)) | (3 << 24)) . $auth);
$this->socket->writeToSocket(pack('V', (strlen($auth)) | (3 << 24)) . $auth);

// caching_sha2_password sends an AuthMoreData packet (0x01 status byte)
// followed by either fast_auth_success (0x03) or perform_full_authentication
Expand Down
20 changes: 12 additions & 8 deletions src/MySQLReplication/BinaryDataReader/BinaryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function advance(int $length): void

public function readInt16(): int
{
return self::unpack('s', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
$re = self::unpack('v', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];

return $re >= 0x8000 ? $re - 0x10000 : $re;
}

public function read(int $length): string
Expand Down Expand Up @@ -238,13 +240,13 @@ public function readUInt8(): int

public function readUInt32(): int
{
return self::unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
return self::unpack('V', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
}

public function readUInt40(): int
{
$data1 = self::unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
$data2 = self::unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
$data2 = self::unpack('V', $this->read(self::UNSIGNED_INT32_LENGTH))[1];

return $data1 + ($data2 << 8);
}
Expand All @@ -259,8 +261,8 @@ public function readUInt48(): int
public function readUInt56(): int
{
$data1 = self::unpack('C', $this->read(self::UNSIGNED_CHAR_LENGTH))[1];
$data2 = self::unpack('S', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
$data3 = self::unpack('I', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
$data2 = self::unpack('v', $this->read(self::UNSIGNED_SHORT_LENGTH))[1];
$data3 = self::unpack('V', $this->read(self::UNSIGNED_INT32_LENGTH))[1];

return $data1 + ($data2 << 8) + ($data3 << 24);
}
Expand Down Expand Up @@ -325,17 +327,19 @@ public function readInt40Be(): int

public function readInt32(): int
{
return self::unpack('i', $this->read(self::UNSIGNED_INT32_LENGTH))[1];
$re = self::unpack('V', $this->read(self::UNSIGNED_INT32_LENGTH))[1];

return $re >= 0x80000000 ? $re - 0x100000000 : $re;
}

public function readFloat(): float
{
return self::unpack('f', $this->read(self::UNSIGNED_FLOAT_LENGTH))[1];
return self::unpack('g', $this->read(self::UNSIGNED_FLOAT_LENGTH))[1];
}

public function readDouble(): float
{
return self::unpack('d', $this->read(self::UNSIGNED_DOUBLE_LENGTH))[1];
return self::unpack('e', $this->read(self::UNSIGNED_DOUBLE_LENGTH))[1];
}

public function readTableId(): string
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/BinLog/BinLogServerInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,6 @@ public function testShouldParseCharacterSetFromRawHandshake(): void
self::assertSame(45, $info->characterSet);
self::assertSame(10, $info->protocolVersion);
self::assertSame('8.0.28', $info->serverVersion);
self::assertSame(7, $info->connectionId);
}
}
61 changes: 50 additions & 11 deletions tests/Unit/BinaryDataReader/BinaryDataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function testShouldReadCodedBinary(): void
{
self::assertSame(0, $this->getBinaryRead(pack('C', ''))->readCodedBinary());
self::assertNull($this->getBinaryRead(pack('C', BinaryDataReader::NULL_COLUMN))->readCodedBinary());
self::assertSame(0, $this->getBinaryRead(pack('i', BinaryDataReader::UNSIGNED_SHORT_COLUMN))->readCodedBinary());
self::assertSame(0, $this->getBinaryRead(pack('i', BinaryDataReader::UNSIGNED_INT24_COLUMN))->readCodedBinary());
self::assertSame(0, $this->getBinaryRead(pack('V', BinaryDataReader::UNSIGNED_SHORT_COLUMN))->readCodedBinary());
self::assertSame(0, $this->getBinaryRead(pack('V', BinaryDataReader::UNSIGNED_INT24_COLUMN))->readCodedBinary());
}

public function testShouldThrowErrorOnUnknownCodedBinary(): void
{
$this->expectException(BinaryDataReaderException::class);

$this->getBinaryRead(pack('i', 255))
$this->getBinaryRead(pack('V', 255))
->readCodedBinary();
}

Expand Down Expand Up @@ -59,10 +59,10 @@ public static function dataProviderForUInt(): array
[1, pack('c', 1), 1],
[2, pack('v', 9999), 9999],
[3, pack('CCC', 160, 190, 15), 1031840],
[4, pack('I', 123123543), 123123543],
[5, pack('CI', 71, 2570258120), 657986078791],
[4, pack('V', 123123543), 123123543],
[5, pack('CV', 71, 2570258120), 657986078791],
[6, pack('v3', 2570258120, 2570258120, 2570258120), 7456176998088],
[7, pack('CSI', 66, 7890, 2570258120), 43121775657013826],
[7, pack('CvV', 66, 7890, 2570258120), 43121775657013826],
[8, pack('C8', 1, 2, 3, 4, 5, 6, 7, 8), 578437695752307201],
];
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public function testShouldThrowErrorOnReadUIntBySizeNotSupported(): void

public static function dataProviderForBeInt(): array
{
return [[1, pack('c', 4), 4], [2, pack('n', 9999), 9999], [3, pack('CCC', 160, 190, 15), -6242801], [4, pack('i', 123123543), 1471632903], [5, pack('NC', 71, 2570258120), 18376]];
return [[1, pack('c', 4), 4], [2, pack('n', 9999), 9999], [3, pack('CCC', 160, 190, 15), -6242801], [4, pack('N', 123123543), 123123543], [5, pack('NC', 71, 2570258120), 18376]];
}

#[DataProvider('dataProviderForBeInt')] public function testShouldReadIntBeBySize(int $size, string $data, int $expected): void
Expand All @@ -112,7 +112,7 @@ public function testShouldThrowErrorOnReadIntBeBySizeNotSupported(): void
public function testShouldReadInt16(): void
{
$expected = 1000;
self::assertSame($expected, $this->getBinaryRead(pack('s', $expected))->readInt16());
self::assertSame($expected, $this->getBinaryRead(pack('v', $expected))->readInt16());
}

public function testShouldUnreadAdvance(): void
Expand Down Expand Up @@ -152,20 +152,20 @@ public function testShouldReadLengthCodedPascalString(): void
public function testShouldReadInt32(): void
{
$expected = 777333;
self::assertSame($expected, $this->getBinaryRead(pack('i', $expected))->readInt32());
self::assertSame($expected, $this->getBinaryRead(pack('V', $expected))->readInt32());
}

public function testShouldReadFloat(): void
{
$expected = 0.001;
// we need to add round as php have problem with precision in floats
self::assertSame($expected, round($this->getBinaryRead(pack('f', $expected))->readFloat(), 3));
self::assertSame($expected, round($this->getBinaryRead(pack('g', $expected))->readFloat(), 3));
}

public function testShouldReadDouble(): void
{
$expected = 1321312312.143567586;
self::assertSame($expected, $this->getBinaryRead(pack('d', $expected))->readDouble());
self::assertSame($expected, $this->getBinaryRead(pack('e', $expected))->readDouble());
}

public function testShouldReadTableId(): void
Expand Down Expand Up @@ -197,4 +197,43 @@ private function getBinaryRead(string $data): BinaryDataReader
{
return new BinaryDataReader($data);
}

// The fixtures below are hand-built little-endian byte strings (never via pack('s'/'i'/'f'/'d'),
// which are native-byte-order and would silently mask the same bug on a little-endian host).
// Byte patterns are asymmetric so a big-endian misread produces a different, wrong value.

public function testShouldReadInt16LittleEndian(): void
{
self::assertSame(-32767, $this->getBinaryRead("\x01\x80")->readInt16());
}

public function testShouldReadInt32LittleEndian(): void
{
self::assertSame(-2147483647, $this->getBinaryRead("\x01\x00\x00\x80")->readInt32());
}

public function testShouldReadUInt32LittleEndian(): void
{
self::assertSame(2147483649, $this->getBinaryRead("\x01\x00\x00\x80")->readUInt32());
}

public function testShouldReadUInt40LittleEndian(): void
{
self::assertSame(591292919723, $this->getBinaryRead("\xAB\xEF\xCD\xAB\x89")->readUInt40());
}

public function testShouldReadUInt56LittleEndian(): void
{
self::assertSame(38750972784150017, $this->getBinaryRead("\x01\x06\x80\xEF\xCD\xAB\x89")->readUInt56());
}

public function testShouldReadFloatLittleEndian(): void
{
self::assertSame(-1.5, $this->getBinaryRead("\x00\x00\xC0\xBF")->readFloat());
}

public function testShouldReadDoubleLittleEndian(): void
{
self::assertSame(-1.5, $this->getBinaryRead("\x00\x00\x00\x00\x00\x00\xF8\xBF")->readDouble());
}
}
4 changes: 2 additions & 2 deletions tests/Unit/JsonBinaryDecoder/JsonBinaryDecoderServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testShouldParseLiteralFalse(): void
public function testShouldParseInt16Scalar(): void
{
// type=INT16(5) + int16 value 42
$binary = pack('C', JsonBinaryDecoderService::INT16) . pack('s', 42);
$binary = pack('C', JsonBinaryDecoderService::INT16) . pack('v', 42);
$service = JsonBinaryDecoderService::makeJsonBinaryDecoder($binary);
self::assertSame('"42"', $service->parseToString());
}
Expand All @@ -53,7 +53,7 @@ public function testShouldParseUInt16Scalar(): void
public function testShouldParseDouble(): void
{
// type=DOUBLE(11) + double value
$binary = pack('C', JsonBinaryDecoderService::DOUBLE) . pack('d', 3.14);
$binary = pack('C', JsonBinaryDecoderService::DOUBLE) . pack('e', 3.14);
$service = JsonBinaryDecoderService::makeJsonBinaryDecoder($binary);
$result = $service->parseToString();
self::assertStringContainsString('3.14', $result);
Expand Down
Loading