Kaynağa Gözat

Fix wrong CRC16 calculated on integer value.

This bug affected only our pure PHP implementation in Predis\Cluster\Hash\CRC16.

Closes #450.
Daniele Alessandri 7 yıl önce
ebeveyn
işleme
26b03bd6e8

+ 2 - 0
src/Cluster/Hash/CRC16.php

@@ -61,6 +61,8 @@ class CRC16 implements HashGeneratorInterface
         // CRC-CCITT-16 algorithm
         $crc = 0;
         $CCITT_16 = self::$CCITT_16;
+
+        $value = (string) $value;
         $strlen = strlen($value);
 
         for ($i = 0; $i < $strlen; ++$i) {

+ 11 - 0
tests/Predis/Cluster/Hash/CRC16Test.php

@@ -36,4 +36,15 @@ class CRC16Test extends PredisTestCase
         $this->assertSame(25343, $crc16->hash('key:008'));
         $this->assertSame(29406, $crc16->hash('key:009'));
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testHashGenerationWithIntegerValues()
+    {
+        $crc16 = new CRC16();
+
+        $this->assertSame(13907, $crc16->hash(0));
+        $this->assertSame(55177, $crc16->hash(1234));
+    }
 }

+ 11 - 0
tests/Predis/Cluster/Hash/PhpiredisCRC16Test.php

@@ -38,4 +38,15 @@ class PhpiredisCRC16Test extends PredisTestCase
         $this->assertSame(25343, $crc16->hash('key:008'));
         $this->assertSame(29406, $crc16->hash('key:009'));
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testHashGenerationWithIntegerValues()
+    {
+        $crc16 = new PhpiredisCRC16();
+
+        $this->assertSame(13907, $crc16->hash(0));
+        $this->assertSame(55177, $crc16->hash(1234));
+    }
 }