This is an automated email from the ASF dual-hosted git repository.
liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 7c1e43bf6f feat: add shenyu-common utils unit test (#6077)
7c1e43bf6f is described below
commit 7c1e43bf6fb8477352e7fb7f236255545f33e362
Author: shown <[email protected]>
AuthorDate: Thu Jul 31 09:05:00 2025 +0800
feat: add shenyu-common utils unit test (#6077)
* feat: add shenyu-common utils unit test
Signed-off-by: shown.Ji <[email protected]>
* fix
Signed-off-by: shown.Ji <[email protected]>
---------
Signed-off-by: shown.Ji <[email protected]>
Co-authored-by: aias00 <[email protected]>
---
.../apache/shenyu/common/utils/AesUtilsTest.java | 88 ++++++++++++++++++++++
.../shenyu/common/utils/NamespaceIDUtilsTest.java | 49 ++++++++++++
2 files changed, 137 insertions(+)
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/AesUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/AesUtilsTest.java
new file mode 100644
index 0000000000..c979dc7976
--- /dev/null
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/AesUtilsTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.shenyu.common.utils;
+
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AesUtilsTest {
+
+ @Test
+ void testCbcEncryptAndDecrypt() {
+
+ // 16-byte key
+ String secretKey = "1234567890123456";
+ // 16-byte IV
+ String iv = "1234567890123456";
+ String data = "Test data for AES";
+
+ // Encrypt the data
+ String encryptedData = AesUtils.cbcEncrypt(secretKey, iv, data);
+ assertNotNull(encryptedData, "Encrypted data should not be null");
+
+ // Decrypt the data
+ String decryptedData = AesUtils.cbcDecrypt(secretKey, iv,
encryptedData);
+ Assertions.assertNotNull(decryptedData, "Decrypted data should not be
null");
+ assertEquals(data, decryptedData, "Decrypted data should match the
original");
+ }
+
+ @Test
+ void testCbcEncryptWithInvalidKey() {
+
+ // Invalid key length
+ String invalidKey = "shortkey";
+ // 16-byte IV
+ String iv = "1234567890123456";
+ String data = "Test data for AES";
+
+ Exception exception = assertThrows(ShenyuException.class, () -> {
+ AesUtils.cbcEncrypt(invalidKey, iv, data);
+ });
+
+ // Update the assertion to match the actual exception message
+ assertTrue(exception.getMessage().contains("Invalid AES key length")
+ || exception.getMessage().contains("Given final block not
properly padded")
+ || exception.getMessage().contains("Key length not 128/192/256
bits"));
+ }
+
+ @Test
+ void testCbcDecryptWithInvalidData() {
+
+ // 16-byte key
+ String secretKey = "1234567890123456";
+ // 16-byte IV
+ String iv = "1234567890123456";
+ String invalidData = "InvalidBase64Data";
+
+ Exception exception = assertThrows(ShenyuException.class, () -> {
+ AesUtils.cbcDecrypt(secretKey, iv, invalidData);
+ });
+
+ // Update the assertion to match the actual exception message
+ assertTrue(exception.getMessage().contains("Illegal base64 character")
+ || exception.getMessage().contains("Input byte array has wrong
4-byte ending unit")
+ || exception.getMessage().contains("Last unit does not have
enough valid bits"));
+ }
+
+}
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/NamespaceIDUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/NamespaceIDUtilsTest.java
new file mode 100644
index 0000000000..9189dde3b0
--- /dev/null
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/NamespaceIDUtilsTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.shenyu.common.utils;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+class NamespaceIDUtilsTest {
+
+ @Test
+ void testGetInstance() {
+
+ NamespaceIDUtils instance1 = NamespaceIDUtils.getInstance();
+ NamespaceIDUtils instance2 = NamespaceIDUtils.getInstance();
+ assertNotNull(instance1, "Instance should not be null");
+ assertSame(instance1, instance2, "Instances should be the same
(singleton)");
+ }
+
+ @Test
+ void testGenerateNamespaceID() {
+
+ NamespaceIDUtils namespaceIDUtils = NamespaceIDUtils.getInstance();
+ String namespaceID1 = namespaceIDUtils.generateNamespaceID();
+ String namespaceID2 = namespaceIDUtils.generateNamespaceID();
+
+ assertNotNull(namespaceID1, "Generated namespace ID should not be
null");
+ assertNotNull(namespaceID2, "Generated namespace ID should not be
null");
+ assertNotEquals(namespaceID1, namespaceID2, "Generated namespace IDs
should be unique");
+ }
+
+}