You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/10/16 15:51:55 UTC

(shardingsphere) branch master updated: Add more test cases on MySQLAuthSwitchRequestPacket (#33270)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 7e9c88093d9 Add more test cases on MySQLAuthSwitchRequestPacket (#33270)
7e9c88093d9 is described below

commit 7e9c88093d95571a488ed0e875967fc1ab5603ee
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Oct 16 23:51:48 2024 +0800

    Add more test cases on MySQLAuthSwitchRequestPacket (#33270)
    
    * Add more test cases on MySQLAuthSwitchRequestPacket
    
    * Add more test cases on MySQLAuthMoreDataPacket
---
 .../handshake/MySQLAuthSwitchRequestPacket.java    |  3 +-
 .../handshake/MySQLAuthMoreDataPacketTest.java     | 51 ++++++++++++++++++++++
 .../MySQLAuthSwitchRequestPacketTest.java          | 19 ++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
index d7265f39ab8..63a2cc34048 100644
--- a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
+++ b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
@@ -47,8 +47,7 @@ public final class MySQLAuthSwitchRequestPacket extends MySQLPacket {
         Preconditions.checkArgument(HEADER == payload.readInt1(), "Header of MySQL auth switch request packet must be `0xfe`.");
         authPluginName = payload.readStringNul();
         String strAuthPluginData = payload.readStringNul();
-        authPluginData = new MySQLAuthenticationPluginData(Arrays.copyOfRange(strAuthPluginData.getBytes(), 0, 8),
-                Arrays.copyOfRange(strAuthPluginData.getBytes(), 8, 20));
+        authPluginData = new MySQLAuthenticationPluginData(Arrays.copyOfRange(strAuthPluginData.getBytes(), 0, 8), Arrays.copyOfRange(strAuthPluginData.getBytes(), 8, 20));
     }
     
     @Override
diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthMoreDataPacketTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthMoreDataPacketTest.java
new file mode 100644
index 00000000000..95bbf4afb42
--- /dev/null
+++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthMoreDataPacketTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.db.protocol.mysql.packet.handshake;
+
+import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class MySQLAuthMoreDataPacketTest {
+    
+    @Test
+    void assertNewWithInvalidHeader() {
+        MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
+        assertThrows(IllegalArgumentException.class, () -> new MySQLAuthMoreDataPacket(payload));
+    }
+    
+    @Test
+    void assertNewWithValidHeader() {
+        MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
+        when(payload.readInt1()).thenReturn(MySQLAuthMoreDataPacket.HEADER);
+        when(payload.readStringEOFByBytes()).thenReturn(new byte[0]);
+        MySQLAuthMoreDataPacket packet = new MySQLAuthMoreDataPacket(payload);
+        assertThat(packet.getPluginData(), is(new byte[0]));
+    }
+    
+    @Test
+    void assertWrite() {
+        assertThrows(UnsupportedSQLOperationException.class, () -> new MySQLAuthMoreDataPacket(new byte[0]).write(mock(MySQLPacketPayload.class)));
+    }
+}
diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
index 5d7bf127685..fbe2b7f0cb5 100644
--- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
+++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
@@ -23,7 +23,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -37,6 +41,21 @@ class MySQLAuthSwitchRequestPacketTest {
     @Mock
     private MySQLPacketPayload payload;
     
+    @Test
+    void assertNewWithInvalidHeader() {
+        MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
+        assertThrows(IllegalArgumentException.class, () -> new MySQLAuthSwitchRequestPacket(payload));
+    }
+    
+    @Test
+    void assertNewWithValidHeader() {
+        MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
+        when(payload.readInt1()).thenReturn(MySQLAuthSwitchRequestPacket.HEADER);
+        when(payload.readStringNul()).thenReturn("foo_auth");
+        MySQLAuthSwitchRequestPacket packet = new MySQLAuthSwitchRequestPacket(payload);
+        assertThat(packet.getAuthPluginName(), is("foo_auth"));
+    }
+    
     @Test
     void assertWrite() {
         when(authPluginData.getAuthenticationPluginData()).thenReturn(new byte[]{0x11, 0x22});