提交 a97537a5 编写于 作者: T tristaZero

Merge branch 'dev' of ssh://github.com/shardingjdbc/sharding-jdbc into dev

......@@ -98,7 +98,7 @@ public final class ShardingProxy {
}
private void groupsEpoll(final ServerBootstrap bootstrap) {
workerGroup = new EpollEventLoopGroup(RULE_REGISTRY.getAcceptorSize());
workerGroup = new EpollEventLoopGroup();
userGroup = new EpollEventLoopGroup(RULE_REGISTRY.getAcceptorSize());
bootstrap.group(bossGroup, workerGroup)
.channel(EpollServerSocketChannel.class)
......@@ -111,7 +111,7 @@ public final class ShardingProxy {
}
private void groupsNio(final ServerBootstrap bootstrap) {
workerGroup = new NioEventLoopGroup(RULE_REGISTRY.getAcceptorSize());
workerGroup = new NioEventLoopGroup();
userGroup = new NioEventLoopGroup(RULE_REGISTRY.getAcceptorSize());
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
......
......@@ -34,7 +34,7 @@ public abstract class FrontendHandler extends ChannelInboundHandlerAdapter {
private boolean authorized;
@Setter
private BackendConnection backendConnection;
private volatile BackendConnection backendConnection;
@Override
public final void channelActive(final ChannelHandlerContext context) {
......
......@@ -17,6 +17,7 @@
package io.shardingsphere.proxy.runtime;
import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.AccessLevel;
......@@ -33,6 +34,7 @@ public final class ChannelRegistry {
private static final ChannelRegistry INSTANCE = new ChannelRegistry();
// TODO :wangkai do not use cache, should use map, and add unregister feature
private final Cache<String, Integer> connectionIds = CacheBuilder.newBuilder().build();
/**
......@@ -61,6 +63,8 @@ public final class ChannelRegistry {
* @return connectionId database connection ID
*/
public int getConnectionId(final String channelId) {
return connectionIds.getIfPresent(channelId);
Integer result = connectionIds.getIfPresent(channelId);
Preconditions.checkNotNull(result, String.format("Can not get connection id via channel id: %s", channelId));
return result;
}
}
......@@ -69,31 +69,6 @@ public final class CommandPacketFactory {
return new ComStmtClosePacket(sequenceId, payload);
case COM_PING:
return new ComPingPacket(sequenceId);
case COM_SLEEP:
case COM_CREATE_DB:
case COM_DROP_DB:
case COM_REFRESH:
case COM_SHUTDOWN:
case COM_STATISTICS:
case COM_PROCESS_INFO:
case COM_CONNECT:
case COM_PROCESS_KILL:
case COM_DEBUG:
case COM_TIME:
case COM_DELAYED_INSERT:
case COM_CHANGE_USER:
case COM_BINLOG_DUMP:
case COM_TABLE_DUMP:
case COM_CONNECT_OUT:
case COM_REGISTER_SLAVE:
case COM_STMT_SEND_LONG_DATA:
case COM_STMT_RESET:
case COM_SET_OPTION:
case COM_STMT_FETCH:
case COM_DAEMON:
case COM_BINLOG_DUMP_GTID:
case COM_RESET_CONNECTION:
return new UnsupportedCommandPacket(sequenceId, type);
default:
return new UnsupportedCommandPacket(sequenceId, type);
}
......
......@@ -33,23 +33,17 @@ import java.util.Arrays;
@Getter
public final class AuthorityHandler {
private static final RuleRegistry RULE_REGISTRY = RuleRegistry.getInstance();
private final AuthPluginData authPluginData;
public AuthorityHandler() {
authPluginData = new AuthPluginData();
}
private final AuthPluginData authPluginData = new AuthPluginData();
/**
* Login into sharding proxy.
* Login.
*
* @param username connection username.
* @param authResponse connection auth response.
* @return login success or failure.
* @param username connection username
* @param authResponse connection auth response
* @return login success or failure
*/
public boolean login(final String username, final byte[] authResponse) {
ProxyAuthority proxyAuthority = RULE_REGISTRY.getProxyAuthority();
ProxyAuthority proxyAuthority = RuleRegistry.getInstance().getProxyAuthority();
if (Strings.isNullOrEmpty(proxyAuthority.getPassword())) {
return proxyAuthority.getUsername().equals(username);
}
......
......@@ -18,7 +18,6 @@
package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
......@@ -29,11 +28,19 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.NONE)
public final class ConnectionIdGenerator {
@Getter
private static ConnectionIdGenerator instance = new ConnectionIdGenerator();
private static final ConnectionIdGenerator INSTANCE = new ConnectionIdGenerator();
private int currentId;
/**
* Get instance.
*
* @return instance
*/
public static ConnectionIdGenerator getInstance() {
return INSTANCE;
}
/**
* Get next connection id.
*
......
......@@ -34,7 +34,6 @@ import lombok.Getter;
* @author zhangliang
* @author linjiaqi
*/
@Getter
public final class HandshakePacket implements MySQLPacket {
private final int protocolVersion = ServerInfo.PROTOCOL_VERSION;
......@@ -43,16 +42,17 @@ public final class HandshakePacket implements MySQLPacket {
private final int capabilityFlagsLower = CapabilityFlag.calculateHandshakeCapabilityFlagsLower();
private final int characterSet = ServerInfo.CHARSET;
private final StatusFlag statusFlag = StatusFlag.SERVER_STATUS_AUTOCOMMIT;
private final int capabilityFlagsUpper = CapabilityFlag.calculateHandshakeCapabilityFlagsUpper();
@Getter
private final int sequenceId;
@Getter
private final int connectionId;
@Getter
private final AuthPluginData authPluginData;
public HandshakePacket(final int connectionId, final AuthPluginData authPluginData) {
......
......@@ -20,8 +20,8 @@ package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* Handshake response above MySQL 4.1 packet protocol.
......@@ -31,23 +31,25 @@ import lombok.Getter;
* @author zhangliang
* @author wangkai
*/
@AllArgsConstructor
@Getter
@RequiredArgsConstructor
public final class HandshakeResponse41Packet implements MySQLPacket {
@Getter
private final int sequenceId;
private int capabilityFlags;
private final int capabilityFlags;
private int maxPacketSize;
private final int maxPacketSize;
private int characterSet;
private final int characterSet;
private String username;
@Getter
private final String username;
private byte[] authResponse;
@Getter
private final byte[] authResponse;
private String database;
private final String database;
public HandshakeResponse41Packet(final MySQLPacketPayload payload) {
sequenceId = payload.readInt1();
......@@ -56,25 +58,23 @@ public final class HandshakeResponse41Packet implements MySQLPacket {
characterSet = payload.readInt1();
payload.skipReserved(23);
username = payload.readStringNul();
readAuthResponse(payload);
readDatabase(payload);
authResponse = readAuthResponse(payload);
database = readDatabase(payload);
}
private void readAuthResponse(final MySQLPacketPayload payload) {
private byte[] readAuthResponse(final MySQLPacketPayload payload) {
if (0 != (capabilityFlags & CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue())) {
authResponse = payload.readStringLenencByBytes();
} else if (0 != (capabilityFlags & CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue())) {
return payload.readStringLenencByBytes();
}
if (0 != (capabilityFlags & CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue())) {
int length = payload.readInt1();
authResponse = payload.readStringFixByBytes(length);
} else {
authResponse = payload.readStringNulByBytes();
return payload.readStringFixByBytes(length);
}
return payload.readStringNulByBytes();
}
private void readDatabase(final MySQLPacketPayload payload) {
if (0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue())) {
database = payload.readStringNul();
}
private String readDatabase(final MySQLPacketPayload payload) {
return 0 != (capabilityFlags & CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue()) ? payload.readStringNul() : null;
}
@Override
......
......@@ -18,25 +18,11 @@
package io.shardingsphere.proxy;
import io.shardingsphere.proxy.transport.AllTransportTests;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.AuthPluginDataTest;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.AuthorityHandlerTest;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.ConnectionIdGeneratorTest;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakePacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.HandshakeResponse41PacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.handshake.RandomGeneratorTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
AllTransportTests.class,
AuthorityHandlerTest.class,
AuthPluginDataTest.class,
ConnectionIdGeneratorTest.class,
HandshakePacketTest.class,
HandshakeResponse41PacketTest.class,
RandomGeneratorTest.class
})
@SuiteClasses(AllTransportTests.class)
public final class AllTests {
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package io.shardingsphere.proxy.runtime;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses(ChannelRegistryTest.class)
public final class AllRuntimeTests {
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package io.shardingsphere.proxy.runtime;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ChannelRegistryTest {
@Test
public void assertGetConnectionIdIfPresent() {
ChannelRegistry.getInstance().putConnectionId("0x0a", 1000);
assertThat(ChannelRegistry.getInstance().getConnectionId("0x0a"), is(1000));
}
@Test(expected = NullPointerException.class)
public void assertGetConnectionIdIfAbsent() {
ChannelRegistry.getInstance().getConnectionId("0x0b");
}
}
......@@ -17,7 +17,9 @@
package io.shardingsphere.proxy.transport;
import io.shardingsphere.proxy.transport.common.codec.PacketCodecFactoryTest;
import io.shardingsphere.proxy.runtime.AllRuntimeTests;
import io.shardingsphere.proxy.transport.common.codec.AllCommonCodecTests;
import io.shardingsphere.proxy.transport.mysql.codec.MySQLPacketCodecTest;
import io.shardingsphere.proxy.transport.mysql.constant.AllMySQLConstantTests;
import io.shardingsphere.proxy.transport.mysql.packet.AllMySQLPacketTests;
import org.junit.runner.RunWith;
......@@ -26,8 +28,10 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
PacketCodecFactoryTest.class,
AllMySQLConstantTests.class,
AllRuntimeTests.class,
AllCommonCodecTests.class,
AllMySQLConstantTests.class,
MySQLPacketCodecTest.class,
AllMySQLPacketTests.class
})
public final class AllTransportTests {
......
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package io.shardingsphere.proxy.transport.common.codec;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
PacketCodecFactoryTest.class,
PacketCodecTest.class
})
public final class AllCommonCodecTests {
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package io.shardingsphere.proxy.transport.common.codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.shardingsphere.proxy.transport.common.codec.fixture.PacketCodecFixture;
import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.LinkedList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class PacketCodecTest {
@Mock
private ChannelHandlerContext context;
@Mock
private ByteBuf byteBuf;
@Test
public void assertDecodeWithValidHeader() {
when(byteBuf.readableBytes()).thenReturn(1);
new PacketCodecFixture().decode(context, byteBuf, new LinkedList<>());
}
@Test
public void assertDecodeWithInvalidHeader() {
when(byteBuf.readableBytes()).thenReturn(-1);
new PacketCodecFixture().decode(context, byteBuf, new LinkedList<>());
verify(context, times(0)).read();
}
@Test
public void assertEncode() {
DatabasePacket databasePacket = mock(DatabasePacket.class);
new PacketCodecFixture().encode(context, databasePacket, byteBuf);
verify(context).write(databasePacket);
}
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
package io.shardingsphere.proxy.transport.common.codec.fixture;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.shardingsphere.proxy.transport.common.codec.PacketCodec;
import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import java.util.List;
public final class PacketCodecFixture extends PacketCodec<DatabasePacket> {
@Override
protected boolean isValidHeader(final int readableBytes) {
return readableBytes > 0;
}
@Override
protected void doDecode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes) {
context.read();
}
@Override
protected void doEncode(final ChannelHandlerContext context, final DatabasePacket message, final ByteBuf out) {
context.write(message);
}
}
......@@ -17,57 +17,79 @@
package io.shardingsphere.proxy.transport.mysql.codec;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacket;
import io.shardingsphere.proxy.transport.mysql.packet.command.admin.quit.ComQuitPacket;
import org.junit.Before;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.LinkedList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class MySQLPacketCodecTest {
@RunWith(MockitoJUnitRunner.class)
public final class MySQLPacketCodecTest {
private MySQLPacketCodec mySQLPacketCodec;
private ChannelHandlerContext channelHandlerContext;
@Mock
private ChannelHandlerContext context;
@Mock
private ByteBuf byteBuf;
@Before
public void init() {
mySQLPacketCodec = new MySQLPacketCodec();
channelHandlerContext = mock(ChannelHandlerContext.class);
byteBuf = mock(ByteBuf.class);
@Test
public void assertIsValidHeader() {
assertTrue(new MySQLPacketCodec().isValidHeader(50));
}
@Test
public void assertMySQLPacketDoDecode() {
final List<Object> out = Lists.newArrayList();
public void assertIsInvalidHeader() {
assertFalse(new MySQLPacketCodec().isValidHeader(3));
}
@Test
public void assertDoDecode() {
when(byteBuf.markReaderIndex()).thenReturn(byteBuf);
when(byteBuf.markReaderIndex().readMedium()).thenReturn(50);
when(byteBuf.readRetainedSlice(anyInt())).thenReturn(byteBuf);
mySQLPacketCodec.doDecode(channelHandlerContext, byteBuf, out, 54);
when(byteBuf.readMediumLE()).thenReturn(50);
when(byteBuf.readRetainedSlice(51)).thenReturn(byteBuf);
List<Object> out = new LinkedList<>();
new MySQLPacketCodec().doDecode(context, byteBuf, out, 54);
assertThat(out.size(), is(1));
}
@Test
public void assertDoDecodeWithStickyPacket() {
when(byteBuf.markReaderIndex()).thenReturn(byteBuf);
when(byteBuf.readMediumLE()).thenReturn(50);
List<Object> out = new LinkedList<>();
new MySQLPacketCodec().doDecode(context, byteBuf, out, 40);
assertTrue(out.isEmpty());
}
@Test
public void assertMySQLPacketDoEncode() {
final MySQLPacket message = new ComQuitPacket(10);
when(channelHandlerContext.alloc()).thenReturn(mock(ByteBufAllocator.class));
when(channelHandlerContext.alloc().buffer()).thenReturn(byteBuf);
when(byteBuf.writeMediumLE(anyInt())).thenReturn(byteBuf);
when(byteBuf.writeByte(anyInt())).thenReturn(byteBuf);
when(byteBuf.writeBytes(ArgumentMatchers.<ByteBuf>any())).thenReturn(byteBuf);
mySQLPacketCodec.doEncode(channelHandlerContext, message, byteBuf);
public void assertDoEncode() {
ByteBufAllocator byteBufAllocator = mock(ByteBufAllocator.class);
when(context.alloc()).thenReturn(byteBufAllocator);
ByteBuf payloadByteBuf = mock(ByteBuf.class);
when(byteBufAllocator.buffer()).thenReturn(payloadByteBuf);
when(payloadByteBuf.readableBytes()).thenReturn(50);
MySQLPacket actualMessage = mock(MySQLPacket.class);
when(actualMessage.getSequenceId()).thenReturn(1);
new MySQLPacketCodec().doEncode(context, actualMessage, byteBuf);
verify(actualMessage).write(ArgumentMatchers.<MySQLPacketPayload>any());
verify(byteBuf).writeMediumLE(50);
verify(byteBuf).writeByte(1);
verify(byteBuf).writeBytes(payloadByteBuf);
}
}
......@@ -31,6 +31,7 @@ import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.execu
import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.execute.ComStmtExecutePacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.execute.NullBitmapTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.prepare.ComStmtPrepareOKPacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.binary.prepare.ComStmtPreparePacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.text.TextResultSetRowPacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.text.fieldlist.ComFieldListPacketTest;
import io.shardingsphere.proxy.transport.mysql.packet.command.query.text.query.ComQueryPacketTest;
......@@ -52,6 +53,7 @@ import org.junit.runners.Suite.SuiteClasses;
TextResultSetRowPacketTest.class,
ComFieldListPacketTest.class,
ComQueryPacketTest.class,
ComStmtPreparePacketTest.class,
ComStmtPrepareOKPacketTest.class,
BinaryResultSetRowPacketTest.class,
ComStmtExecutePacketTest.class,
......
......@@ -23,12 +23,22 @@ import io.shardingsphere.proxy.transport.mysql.packet.generic.EofPacket;
import org.junit.Test;
import java.util.Arrays;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class QueryResponsePacketsTest {
@Test
public void assertGetColumnDefinition41Packets() {
QueryResponsePackets actual = createQueryResponsePackets();
assertThat(actual.getColumnDefinition41Packets().size(), is(2));
Iterator<ColumnDefinition41Packet> actualColumnDefinition41Packets = actual.getColumnDefinition41Packets().iterator();
assertThat(actualColumnDefinition41Packets.next().getSequenceId(), is(2));
assertThat(actualColumnDefinition41Packets.next().getSequenceId(), is(3));
}
@Test
public void assertGetColumnCount() {
assertThat(createQueryResponsePackets().getColumnCount(), is(2));
......
......@@ -28,7 +28,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -42,7 +41,6 @@ public final class ComStmtClosePacketTest {
when(payload.readInt4()).thenReturn(1);
Optional<CommandResponsePackets> actual = new ComStmtClosePacket(1, payload).execute();
assertFalse(actual.isPresent());
verify(payload).readInt4();
}
@Test
......@@ -51,6 +49,5 @@ public final class ComStmtClosePacketTest {
ComStmtClosePacket actual = new ComStmtClosePacket(1, payload);
assertThat(actual.getSequenceId(), is(1));
actual.write(payload);
verify(payload).readInt4();
}
}
......@@ -24,6 +24,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.math.BigDecimal;
import java.sql.Timestamp;
import static org.hamcrest.CoreMatchers.is;
......@@ -134,7 +135,8 @@ public final class BinaryProtocolValueTest {
@Test
public void assertWriteInt8() {
new BinaryProtocolValue(ColumnType.MYSQL_TYPE_LONGLONG, payload).write(1L);
verify(payload).writeInt8(1L);
new BinaryProtocolValue(ColumnType.MYSQL_TYPE_LONGLONG, payload).write(new BigDecimal(1L));
verify(payload, times(2)).writeInt8(1L);
}
@Test
......
......@@ -105,9 +105,6 @@ public final class ComStmtExecutePacketTest {
assertThat(actualResultValue.getSequenceId(), is(2));
assertThat(((BinaryResultSetRowPacket) actualResultValue).getData(), is(Collections.<Object>singletonList(99999L)));
assertFalse(packet.next());
verify(backendHandler).execute();
verify(backendHandler, times(2)).next();
verify(backendHandler).getResultValue();
}
private void setBackendHandler(final ComStmtExecutePacket packet, final BackendHandler backendHandler) throws ReflectiveOperationException {
......
......@@ -26,7 +26,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -34,7 +33,7 @@ import static org.mockito.Mockito.when;
public final class EofPacketTest {
@Mock
private MySQLPacketPayload packetPayload;
private MySQLPacketPayload payload;
@Test
public void assertNewEofPacketWithSequenceId() {
......@@ -46,21 +45,19 @@ public final class EofPacketTest {
@Test
public void assertNewEofPacketWithMySQLPacketPayload() {
when(packetPayload.readInt1()).thenReturn(1, EofPacket.HEADER);
when(packetPayload.readInt2()).thenReturn(0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
EofPacket actual = new EofPacket(packetPayload);
when(payload.readInt1()).thenReturn(1, EofPacket.HEADER);
when(payload.readInt2()).thenReturn(0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
EofPacket actual = new EofPacket(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getWarnings(), is(0));
assertThat(actual.getStatusFlags(), is(StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
verify(packetPayload, times(2)).readInt1();
verify(packetPayload, times(2)).readInt2();
}
@Test
public void assertWrite() {
new EofPacket(1).write(packetPayload);
verify(packetPayload).writeInt1(EofPacket.HEADER);
verify(packetPayload).writeInt2(0);
verify(packetPayload).writeInt2(StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
new EofPacket(1).write(payload);
verify(payload).writeInt1(EofPacket.HEADER);
verify(payload).writeInt2(0);
verify(payload).writeInt2(StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
}
}
......@@ -28,7 +28,6 @@ import java.sql.SQLException;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -36,7 +35,7 @@ import static org.mockito.Mockito.when;
public final class ErrPacketTest {
@Mock
private MySQLPacketPayload packetPayload;
private MySQLPacketPayload payload;
@Test
public void assertNewErrPacketWithServerErrorCode() {
......@@ -58,30 +57,25 @@ public final class ErrPacketTest {
@Test
public void assertNewErrPacketWithMySQLPacketPayload() {
when(packetPayload.readInt1()).thenReturn(1, ErrPacket.HEADER);
when(packetPayload.readInt2()).thenReturn(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorCode());
when(packetPayload.readStringFix(1)).thenReturn("#");
when(packetPayload.readStringFix(5)).thenReturn(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getSqlState());
when(packetPayload.readStringEOF()).thenReturn(String.format(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorMessage(), "root", "localhost", "root"));
ErrPacket actual = new ErrPacket(packetPayload);
when(payload.readInt1()).thenReturn(1, ErrPacket.HEADER);
when(payload.readInt2()).thenReturn(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorCode());
when(payload.readStringFix(1)).thenReturn("#");
when(payload.readStringFix(5)).thenReturn(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getSqlState());
when(payload.readStringEOF()).thenReturn(String.format(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorMessage(), "root", "localhost", "root"));
ErrPacket actual = new ErrPacket(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorCode()));
assertThat(actual.getSqlState(), is(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getSqlState()));
assertThat(actual.getErrorMessage(), is(String.format(ServerErrorCode.ER_ACCESS_DENIED_ERROR.getErrorMessage(), "root", "localhost", "root")));
verify(packetPayload, times(2)).readInt1();
verify(packetPayload).readInt2();
verify(packetPayload).readStringFix(1);
verify(packetPayload).readStringFix(5);
verify(packetPayload).readStringEOF();
}
@Test
public void assertWrite() {
new ErrPacket(1, new SQLException("no reason", "X999", -1)).write(packetPayload);
verify(packetPayload).writeInt1(ErrPacket.HEADER);
verify(packetPayload).writeInt2(-1);
verify(packetPayload).writeStringFix("#");
verify(packetPayload).writeStringFix("X999");
verify(packetPayload).writeStringEOF("no reason");
new ErrPacket(1, new SQLException("no reason", "X999", -1)).write(payload);
verify(payload).writeInt1(ErrPacket.HEADER);
verify(payload).writeInt2(-1);
verify(payload).writeStringFix("#");
verify(payload).writeStringFix("X999");
verify(payload).writeStringEOF("no reason");
}
}
......@@ -26,7 +26,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -68,10 +67,6 @@ public final class OKPacketTest {
assertThat(actual.getLastInsertId(), is(9999L));
assertThat(actual.getWarnings(), is(1));
assertThat(actual.getInfo(), is("no info"));
verify(packetPayload, times(2)).readInt1();
verify(packetPayload, times(2)).readIntLenenc();
verify(packetPayload, times(2)).readInt2();
verify(packetPayload).readStringEOF();
}
@Test
......
......@@ -18,7 +18,6 @@
package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import com.google.common.primitives.Bytes;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
......@@ -26,35 +25,21 @@ import static org.junit.Assert.assertThat;
public final class AuthPluginDataTest {
private AuthPluginData authPluginData;
private final byte[] part1 = {106, 105, 55, 122, 117, 98, 115, 109};
private final byte[] part2 = {68, 102, 53, 122, 65, 49, 84, 79, 85, 115, 116, 113};
@Before
public void setUp() {
authPluginData = new AuthPluginData(part1, part2);
}
@Test
public void assertGetAuthPluginData() {
assertThat(authPluginData.getAuthPluginData(), is(Bytes.concat(part1, part2)));
}
@Test
public void assertGetAuthPluginDataPart1() {
assertThat(authPluginData.getAuthPluginDataPart1(), is(part1));
}
@Test
public void assertGetAuthPluginDataPart2() {
assertThat(authPluginData.getAuthPluginDataPart2(), is(part2));
byte[] actualPart1 = {106, 105, 55, 122, 117, 98, 115, 109};
byte[] actualPart2 = {68, 102, 53, 122, 65, 49, 84, 79, 85, 115, 116, 113};
AuthPluginData actual = new AuthPluginData(actualPart1, actualPart2);
assertThat(actual.getAuthPluginDataPart1(), is(actualPart1));
assertThat(actual.getAuthPluginDataPart2(), is(actualPart2));
assertThat(actual.getAuthPluginData(), is(Bytes.concat(actualPart1, actualPart2)));
}
@Test
public void assertGetAuthPluginDataWithoutArguments() {
AuthPluginData authPluginData = new AuthPluginData();
assertThat(authPluginData.getAuthPluginData().length, is(20));
AuthPluginData actual = new AuthPluginData();
assertThat(actual.getAuthPluginDataPart1().length, is(8));
assertThat(actual.getAuthPluginDataPart2().length, is(12));
assertThat(actual.getAuthPluginData().length, is(20));
}
}
......@@ -31,8 +31,6 @@ import static org.junit.Assert.assertTrue;
public final class AuthorityHandlerTest {
private final RuleRegistry ruleRegistry = RuleRegistry.getInstance();
private final AuthorityHandler authorityHandler = new AuthorityHandler();
private final byte[] part1 = {84, 85, 115, 77, 68, 116, 85, 78};
......@@ -40,36 +38,36 @@ public final class AuthorityHandlerTest {
private final byte[] part2 = {83, 121, 75, 81, 87, 56, 120, 112, 73, 109, 77, 69};
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
reviseRuleRegistry();
reviseAuthorityHandler();
public void setUp() throws ReflectiveOperationException {
initProxyAuthorityForRuleRegistry();
initAuthPluginDataForAuthorityHandler();
}
private void reviseRuleRegistry() throws NoSuchFieldException, IllegalAccessException {
private void initProxyAuthorityForRuleRegistry() throws ReflectiveOperationException {
ProxyAuthority proxyAuthority = new ProxyAuthority();
proxyAuthority.setUsername("root");
proxyAuthority.setPassword("root");
Field field = ruleRegistry.getClass().getDeclaredField("proxyAuthority");
Field field = RuleRegistry.class.getDeclaredField("proxyAuthority");
field.setAccessible(true);
field.set(ruleRegistry, proxyAuthority);
field.set(RuleRegistry.getInstance(), proxyAuthority);
}
private void reviseAuthorityHandler() throws NoSuchFieldException, IllegalAccessException {
private void initAuthPluginDataForAuthorityHandler() throws ReflectiveOperationException {
AuthPluginData authPluginData = new AuthPluginData(part1, part2);
Field field = authorityHandler.getClass().getDeclaredField("authPluginData");
Field field = AuthorityHandler.class.getDeclaredField("authPluginData");
field.setAccessible(true);
field.set(authorityHandler, authPluginData);
}
@Test
public void assertLogin() {
public void assertLoginWithPassword() {
RuleRegistry.getInstance().getProxyAuthority().setUsername("root");
RuleRegistry.getInstance().getProxyAuthority().setPassword("root");
byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
assertTrue(authorityHandler.login("root", authResponse));
}
@Test
public void assertLoginWithoutPassword() {
ruleRegistry.getProxyAuthority().setPassword("");
RuleRegistry.getInstance().getProxyAuthority().setUsername("root");
byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
assertTrue(authorityHandler.login("root", authResponse));
}
......
......@@ -17,6 +17,7 @@
package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -25,34 +26,29 @@ import java.lang.reflect.Field;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class ConnectionIdGeneratorTest {
private final ConnectionIdGenerator generator = ConnectionIdGenerator.getInstance();
@Before
public void setUp() throws ReflectiveOperationException {
@After
public void resetConnectionId() throws ReflectiveOperationException {
setCurrentConnectionId(0);
}
private void setCurrentConnectionId(final int connectionId) throws ReflectiveOperationException {
Field field = ConnectionIdGenerator.class.getDeclaredField("currentId");
field.setAccessible(true);
field.set(ConnectionIdGenerator.getInstance(), 0);
field.set(ConnectionIdGenerator.getInstance(), connectionId);
}
@Test
public void assertNextId() {
assertEquals(generator.nextId(), 1);
}
@Test
public void assertMaxNextId() throws NoSuchFieldException, IllegalAccessException {
Field currentId = generator.getClass().getDeclaredField("currentId");
currentId.setAccessible(true);
currentId.setInt(generator, 2147483647);
assertThat(generator.nextId(), is(1));
assertEquals(ConnectionIdGenerator.getInstance().nextId(), 1);
}
@Test
public void assertGetInstance() {
assertTrue(null != ConnectionIdGenerator.getInstance());
public void assertMaxNextId() throws ReflectiveOperationException {
setCurrentConnectionId(Integer.MAX_VALUE);
assertThat(ConnectionIdGenerator.getInstance().nextId(), is(1));
}
}
......@@ -17,91 +17,59 @@
package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag;
import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo;
import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class HandshakePacketTest {
private HandshakePacket handshakePacket;
@Mock
private MySQLPacketPayload payload;
private final byte[] part1 = {106, 105, 55, 122, 117, 98, 115, 109};
private final byte[] part2 = {68, 102, 53, 122, 65, 49, 84, 79, 85, 115, 116, 113};
@Before
public void setUp() {
AuthPluginData authPluginData = new AuthPluginData(part1, part2);
handshakePacket = new HandshakePacket(1, authPluginData);
}
@Test
public void assertWrite() {
ByteBuf byteBuf = mock(ByteBuf.class);
MySQLPacketPayload payload = new MySQLPacketPayload(byteBuf);
when(byteBuf.writeByte(anyInt())).thenReturn(byteBuf);
when(byteBuf.writeBytes(byteBuf)).thenReturn(byteBuf);
handshakePacket.write(payload);
assertThat(handshakePacket.getSequenceId(), is(0));
}
@Test
public void assertGetProtocolVersion() {
assertThat(handshakePacket.getProtocolVersion(), is(ServerInfo.PROTOCOL_VERSION));
public void assertNewWithPayload() {
when(payload.readInt1()).thenReturn(1, ServerInfo.PROTOCOL_VERSION, ServerInfo.CHARSET, 0);
when(payload.readStringNul()).thenReturn(ServerInfo.SERVER_VERSION, new String(part1), new String(part2));
when(payload.readInt4()).thenReturn(1000);
when(payload.readInt2()).thenReturn(
CapabilityFlag.calculateHandshakeCapabilityFlagsLower(), StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), CapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
HandshakePacket actual = new HandshakePacket(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getConnectionId(), is(1000));
assertThat(actual.getAuthPluginData().getAuthPluginDataPart1(), is(part1));
assertThat(actual.getAuthPluginData().getAuthPluginDataPart2(), is(part2));
verify(payload).skipReserved(10);
}
@Test
public void assertGetServerVersion() {
assertThat(handshakePacket.getServerVersion(), is(ServerInfo.SERVER_VERSION));
}
@Test
public void assertGetCapabilityFlagsLower() {
assertThat(handshakePacket.getCapabilityFlagsLower(), is(CapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
}
@Test
public void assertGetCapabilityFlagsUpper() {
assertThat(handshakePacket.getCapabilityFlagsUpper(), is(CapabilityFlag.calculateHandshakeCapabilityFlagsUpper()));
}
@Test
public void assertGetCharacterSet() {
assertThat(handshakePacket.getCharacterSet(), is(ServerInfo.CHARSET));
}
@Test
public void assertGetStatusFlag() {
assertThat(handshakePacket.getStatusFlag(), is(StatusFlag.SERVER_STATUS_AUTOCOMMIT));
}
@Test
public void assertGetSequenceId() {
assertThat(handshakePacket.getSequenceId(), is(0));
}
@Test
public void assertGetConnectionId() {
assertThat(handshakePacket.getConnectionId(), is(1));
}
@Test
public void assertGetAuthPluginData() {
byte[] actual = Bytes.concat(part1, part2);
assertTrue(Arrays.equals(handshakePacket.getAuthPluginData().getAuthPluginData(), actual));
public void assertWrite() {
AuthPluginData authPluginData = new AuthPluginData(part1, part2);
new HandshakePacket(1000, authPluginData).write(payload);
verify(payload).writeInt1(ServerInfo.PROTOCOL_VERSION);
verify(payload).writeStringNul(ServerInfo.SERVER_VERSION);
verify(payload).writeInt4(1000);
verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart1()));
verify(payload).writeInt2(CapabilityFlag.calculateHandshakeCapabilityFlagsLower());
verify(payload).writeInt1(ServerInfo.CHARSET);
verify(payload).writeInt2(StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
verify(payload).writeInt2(CapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
verify(payload).writeInt1(0);
verify(payload).writeReserved(10);
verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart2()));
}
}
......@@ -17,79 +17,122 @@
package io.shardingsphere.proxy.transport.mysql.packet.handshake;
import io.netty.buffer.ByteBuf;
import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag;
import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyByte;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class HandshakeResponse41PacketTest {
private HandshakeResponse41Packet handshakeResponse41Packet;
@Mock
private MySQLPacketPayload payload;
@Before
public void setUp() {
ByteBuf byteBuf = mock(ByteBuf.class);
payload = new MySQLPacketPayload(byteBuf);
when(byteBuf.writeByte(anyInt())).thenReturn(byteBuf);
when(byteBuf.writeBytes(byteBuf)).thenReturn(byteBuf);
byte b = 0;
when(byteBuf.readByte()).thenReturn(b);
when(byteBuf.readIntLE()).thenReturn(0);
when(byteBuf.bytesBefore((byte) 0)).thenReturn(0);
when(byteBuf.skipBytes(1)).thenReturn(byteBuf);
when(byteBuf.readBytes(anyByte())).thenReturn(byteBuf);
handshakeResponse41Packet = new HandshakeResponse41Packet(payload);
}
@Test
public void testWrite() {
handshakeResponse41Packet.write(payload);
assertThat(handshakeResponse41Packet.getSequenceId(), is(0));
}
private HandshakeResponse41Packet handshakeResponse41Packet;
@Test
public void testGetSequenceId() {
assertThat(handshakeResponse41Packet.getSequenceId(), is(0));
}
// @Before
// public void setUp() {
// ByteBuf byteBuf = mock(ByteBuf.class);
// payload = new MySQLPacketPayload(byteBuf);
// when(byteBuf.writeByte(anyInt())).thenReturn(byteBuf);
// when(byteBuf.writeBytes(byteBuf)).thenReturn(byteBuf);
// byte b = 0;
// when(byteBuf.readByte()).thenReturn(b);
// when(byteBuf.readIntLE()).thenReturn(0);
// when(byteBuf.bytesBefore((byte) 0)).thenReturn(0);
// when(byteBuf.skipBytes(1)).thenReturn(byteBuf);
// when(byteBuf.readBytes(anyByte())).thenReturn(byteBuf);
// handshakeResponse41Packet = new HandshakeResponse41Packet(payload);
// }
@Test
public void testGetCapabilityFlags() {
assertThat(handshakeResponse41Packet.getCapabilityFlags(), is(0));
public void assertNewWithPayloadWithDatabase() {
when(payload.readInt1()).thenReturn(1, ServerInfo.CHARSET);
when(payload.readInt4()).thenReturn(CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root", "sharding_db");
when(payload.readStringNulByBytes()).thenReturn(new byte[] {1});
HandshakeResponse41Packet actual = new HandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
verify(payload, times(2)).readInt1();
verify(payload, times(2)).readInt4();
verify(payload, times(2)).readStringNul();
verify(payload).readStringNulByBytes();
verify(payload).skipReserved(23);
}
@Test
public void testGetMaxPacketSize() {
assertThat(handshakeResponse41Packet.getMaxPacketSize(), is(0));
public void assertNewWithPayloadWithClientPluginAuthLenencClientData() {
when(payload.readInt1()).thenReturn(1, ServerInfo.CHARSET);
when(payload.readInt4()).thenReturn(CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringLenencByBytes()).thenReturn(new byte[] {1});
HandshakeResponse41Packet actual = new HandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
verify(payload, times(2)).readInt1();
verify(payload, times(2)).readInt4();
verify(payload).readStringNul();
verify(payload).readStringLenencByBytes();
verify(payload).skipReserved(23);
}
@Test
public void testGetCharacterSet() {
assertThat(handshakeResponse41Packet.getCharacterSet(), is(0));
public void assertNewWithPayloadWithClientSecureConnection() {
when(payload.readInt1()).thenReturn(1, ServerInfo.CHARSET, 1);
when(payload.readInt4()).thenReturn(CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue(), 1000);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringFixByBytes(1)).thenReturn(new byte[] {1});
HandshakeResponse41Packet actual = new HandshakeResponse41Packet(payload);
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getUsername(), is("root"));
assertThat(actual.getAuthResponse(), is(new byte[] {1}));
verify(payload).skipReserved(23);
}
@Test
public void testGetUsername() {
assertThat(handshakeResponse41Packet.getUsername(), is(""));
public void assertWriteWithDatabase() {
new HandshakeResponse41Packet(1, CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue(), 100, ServerInfo.CHARSET, "root", new byte[] {1}, "sharding_db").write(payload);
verify(payload).writeInt4(CapabilityFlag.CLIENT_CONNECT_WITH_DB.getValue());
verify(payload).writeInt4(100);
verify(payload).writeInt1(ServerInfo.CHARSET);
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeStringNul(new String(new byte[] {1}));
verify(payload).writeStringNul("sharding_db");
}
@Test
public void testGetAuthResponse() {
byte[] expected = {};
assertThat(handshakeResponse41Packet.getAuthResponse(), is(expected));
public void assertWriteWithClientPluginAuthLenencClientData() {
new HandshakeResponse41Packet(1, CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue(), 100, ServerInfo.CHARSET, "root", new byte[] {1}, null).write(payload);
verify(payload).writeInt4(CapabilityFlag.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.getValue());
verify(payload).writeInt4(100);
verify(payload).writeInt1(ServerInfo.CHARSET);
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeStringLenenc(new String(new byte[] {1}));
}
@Test
public void testGetDatabase() {
assertNull(handshakeResponse41Packet.getDatabase());
public void assertWriteWithClientSecureConnection() {
new HandshakeResponse41Packet(1, CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue(), 100, ServerInfo.CHARSET, "root", new byte[] {1}, null).write(payload);
verify(payload).writeInt4(CapabilityFlag.CLIENT_SECURE_CONNECTION.getValue());
verify(payload).writeInt4(100);
verify(payload).writeInt1(ServerInfo.CHARSET);
verify(payload).writeReserved(23);
verify(payload).writeStringNul("root");
verify(payload).writeInt1(1);
verify(payload).writeBytes(new byte[] {1});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册