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 015ecf0e26b Add OpenGaussMacCalculatorTest (#37604)
015ecf0e26b is described below

commit 015ecf0e26bfada57fb172b36774d6d7beeb29fa
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jan 1 16:18:45 2026 +0800

    Add OpenGaussMacCalculatorTest (#37604)
---
 AGENTS.md                                          |  3 +-
 .../authentication/OpenGaussMacCalculatorTest.java | 62 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/AGENTS.md b/AGENTS.md
index d17a87bbc14..c9e3f481742 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -37,6 +37,8 @@ This guide is written **for AI coding agents only**. Follow 
it literally; improv
 - **Dedicated and scoped tests**: each public production method must be 
covered by dedicated test methods; each test method covers only one scenario 
and invokes the target public method at most once (repeat only when the same 
scenario needs extra assertions), and different branches/inputs belong in 
separate test methods.
 - **Parameterized tests naming**: all parameterized tests must set an explicit 
`name` including the index (e.g., `"[{index}] foo={0}"`) so scenarios are 
distinguishable in reports.
 - **Mocking Rule**: default to mocks; see Mocking & SPI Guidance for 
static/constructor mocking and spy avoidance details.
+- **Reflection Rule**: when tests must touch fields or methods via reflection, 
use `Plugins.getMemberAccessor()`—direct reflection APIs are forbidden.
+
 ## Tool Usage Guide
 
 ### Exa - Web Search
@@ -226,7 +228,6 @@ Always state which topology, registry, and engine versions 
(e.g., MySQL 5.7 vs 8
 - Name tests after the production method under test; never probe private 
helpers directly—document unreachable branches instead.
 - Mock heavy dependencies (database/cache/registry/network) and prefer mocking 
over building deep object graphs.
 - For static/constructor mocking, use `@ExtendWith(AutoMockExtension.class)` 
with `@StaticMockSettings`; avoid hand-written `mockStatic`/`mockConstruction` 
unless you documented why the extension cannot be used.
-- When constructors hide collaborators, use `Plugins.getMemberAccessor()` to 
inject mocks and document why SPI creation is bypassed.
 - When static methods or constructors need mocking, prefer 
`@ExtendWith(AutoMockExtension.class)` with `@StaticMockSettings` (or the 
extension’s constructor-mocking support); when a class is listed in 
`@StaticMockSettings`, do not call `mockStatic`/`mockConstruction` 
directly—stub via `when(...)` instead. Only if AutoMockExtension cannot be used 
and the reason is documented in the plan may you fall back to 
`mockStatic`/`mockConstruction`, wrapped in try-with-resources.
 - Before coding tests, follow the Coverage & Branch Checklist to map 
inputs/branches to planned assertions.
 - When a component is available via SPI (e.g., `TypedSPILoader`, 
`DatabaseTypedSPILoader`, `PushDownMetaDataRefresher`), obtain the instance 
through SPI by default; note any exceptions in the plan.
diff --git 
a/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/authentication/OpenGaussMacCalculatorTest.java
 
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/authentication/OpenGaussMacCalculatorTest.java
new file mode 100644
index 00000000000..e923e14e7ba
--- /dev/null
+++ 
b/database/protocol/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/protocol/opengauss/packet/authentication/OpenGaussMacCalculatorTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.database.protocol.opengauss.packet.authentication;
+
+import org.apache.shardingsphere.infra.util.string.HexStringUtils;
+import org.junit.jupiter.api.Test;
+import org.mockito.internal.configuration.plugins.Plugins;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class OpenGaussMacCalculatorTest {
+    
+    @Test
+    void assertRequestServerMac() throws ReflectiveOperationException {
+        OpenGaussAuthenticationHexData authHexData = new 
OpenGaussAuthenticationHexData();
+        
Plugins.getMemberAccessor().set(OpenGaussAuthenticationHexData.class.getDeclaredField("salt"),
 authHexData, "73616c74");
+        
Plugins.getMemberAccessor().set(OpenGaussAuthenticationHexData.class.getDeclaredField("nonce"),
 authHexData, "6e6f6e6365");
+        String actual = OpenGaussMacCalculator.requestServerMac("password", 
authHexData, 4096);
+        assertThat(actual, 
is("788471142739dac2d5f7d6de58af1425aec57db7a4461e6ba78d2f93af132445"));
+    }
+    
+    @Test
+    void assertRequestClientMac() {
+        byte[] actual = OpenGaussMacCalculator.requestClientMac("password", 
"73616c74", 4096);
+        assertThat(HexStringUtils.toHexString(actual), 
is("8e795a7388652dea59019ec44c5dcc39e7c6a90e1fa1c260cbde4a2e57f1c933"));
+    }
+    
+    @Test
+    void assertRequestClientMacWithEmptySalt() {
+        assertThrows(IllegalArgumentException.class, () -> 
OpenGaussMacCalculator.requestClientMac("password", "", 4096));
+    }
+    
+    @Test
+    void assertCalculateClientMac() {
+        byte[] storedKey = OpenGaussMacCalculator.requestClientMac("password", 
"73616c74", 4096);
+        byte[] actual = 
OpenGaussMacCalculator.calculateClientMac("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
 "6e6f6e6365", storedKey);
+        assertThat(HexStringUtils.toHexString(actual), 
is("8503aa194430c80f5c156328ff53a90b1c3ddb9190ffae48eaba46e7e377aba7"));
+    }
+    
+    @Test
+    void assertCalculateClientMacWithDifferentLength() {
+        byte[] storedKey = OpenGaussMacCalculator.requestClientMac("password", 
"73616c74", 4096);
+        assertThrows(IllegalArgumentException.class, () -> 
OpenGaussMacCalculator.calculateClientMac("00", "6e6f6e6365", storedKey));
+    }
+}

Reply via email to