Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 18, 2019
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
5 changes: 5 additions & 0 deletions sentinel-cluster/sentinel-cluster-client-default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ public class PingResponseDataDecoder implements EntityDecoder<ByteBuf, Integer>

@Override
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();
}
if (size >= 4) {
return source.readInt();
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions sentinel-cluster/sentinel-cluster-server-default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public void writeTo(Integer entity, ByteBuf target) {
if (entity == null || target == null) {
return;
}
target.writeByte(entity);
target.writeInt(entity);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}