Procházet zdrojové kódy

Fix the numeric overflow bug of ping response data in the cluster module (#844)

- Change type of cluster ping data response from byte to int. This change is compatible with old versions

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao GitHub před 5 roky
rodič
revize
7997cf65fc
V databázi nebyl nalezen žádný známý klíč pro tento podpis ID GPG klíče: 4AEE18F83AFDEB23
6 změnil soubory, kde provedl 109 přidání a 2 odebrání
  1. +5
    -0
      sentinel-cluster/sentinel-cluster-client-default/pom.xml
  2. +6
    -1
      sentinel-cluster/sentinel-cluster-client-default/src/main/java/com/alibaba/csp/sentinel/cluster/client/codec/data/PingResponseDataDecoder.java
  3. +44
    -0
      sentinel-cluster/sentinel-cluster-client-default/src/test/java/com/alibaba/csp/sentinel/cluster/client/codec/data/PingResponseDataDecoderTest.java
  4. +5
    -0
      sentinel-cluster/sentinel-cluster-server-default/pom.xml
  5. +1
    -1
      sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/codec/data/PingResponseDataWriter.java
  6. +48
    -0
      sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/server/codec/data/PingResponseDataWriterTest.java

+ 5
- 0
sentinel-cluster/sentinel-cluster-client-default/pom.xml Zobrazit soubor

@@ -53,5 +53,10 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

+ 6
- 1
sentinel-cluster/sentinel-cluster-client-default/src/main/java/com/alibaba/csp/sentinel/cluster/client/codec/data/PingResponseDataDecoder.java Zobrazit soubor

@@ -27,9 +27,14 @@ public class PingResponseDataDecoder implements EntityDecoder<ByteBuf, Integer>


@Override @Override
public Integer decode(ByteBuf source) { public Integer decode(ByteBuf source) {
if (source.readableBytes() >= 1) {
int size = source.readableBytes();
if (size == 1) {
// Compatible with old version (< 1.7.0).
return (int) source.readByte(); return (int) source.readByte();
} }
if (size >= 4) {
return source.readInt();
}
return -1; return -1;
} }
} }

+ 44
- 0
sentinel-cluster/sentinel-cluster-client-default/src/test/java/com/alibaba/csp/sentinel/cluster/client/codec/data/PingResponseDataDecoderTest.java Zobrazit soubor

@@ -0,0 +1,44 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster.client.codec.data;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Eric Zhao
*/
public class PingResponseDataDecoderTest {

@Test
public void testDecodePingResponseData() {
ByteBuf buf = Unpooled.buffer();
PingResponseDataDecoder decoder = new PingResponseDataDecoder();

int big = Integer.MAX_VALUE;
buf.writeInt(big);
assertThat(decoder.decode(buf)).isEqualTo(big);

byte small = 12;
buf.writeByte(small);
assertThat(decoder.decode(buf)).isEqualTo(small);

buf.release();
}
}

+ 5
- 0
sentinel-cluster/sentinel-cluster-server-default/pom.xml Zobrazit soubor

@@ -52,5 +52,10 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

+ 1
- 1
sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/codec/data/PingResponseDataWriter.java Zobrazit soubor

@@ -31,6 +31,6 @@ public class PingResponseDataWriter implements EntityWriter<Integer, ByteBuf> {
if (entity == null || target == null) { if (entity == null || target == null) {
return; return;
} }
target.writeByte(entity);
target.writeInt(entity);
} }
} }

+ 48
- 0
sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/server/codec/data/PingResponseDataWriterTest.java Zobrazit soubor

@@ -0,0 +1,48 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster.server.codec.data;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test cases for {@link PingResponseDataWriter}.
*
* @author Eric Zhao
*/
public class PingResponseDataWriterTest {

@Test
public void testWritePingResponseAndParse() {
ByteBuf buf = Unpooled.buffer();
PingResponseDataWriter writer = new PingResponseDataWriter();

int small = 120;
writer.writeTo(small, buf);
assertThat(buf.readableBytes()).isGreaterThanOrEqualTo(4);
assertThat(buf.readInt()).isEqualTo(small);

int big = Integer.MAX_VALUE;
writer.writeTo(big, buf);
assertThat(buf.readableBytes()).isGreaterThanOrEqualTo(4);
assertThat(buf.readInt()).isEqualTo(big);

buf.release();
}
}

Načítá se…
Zrušit
Uložit