This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new dabd992e0 [improve] add common util test (#2001)
dabd992e0 is described below
commit dabd992e0e534f9b5188adb294ef1247078333d9
Author: YuLuo <[email protected]>
AuthorDate: Fri May 17 23:48:18 2024 +0800
[improve] add common util test (#2001)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../org/apache/hertzbeat/common/util/AesUtil.java | 5 +-
.../apache/hertzbeat/common/util/Base64Util.java | 5 ++
.../apache/hertzbeat/common/util/AesUtilTest.java | 73 +++++++++++++++-------
.../hertzbeat/common/util/Base64UtilTest.java | 23 ++++++-
.../apache/hertzbeat/common/util/JsonUtilTest.java | 34 ++++++----
5 files changed, 104 insertions(+), 36 deletions(-)
diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/AesUtil.java
b/common/src/main/java/org/apache/hertzbeat/common/util/AesUtil.java
index c47be5f88..2068397d9 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/AesUtil.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/AesUtil.java
@@ -40,7 +40,7 @@ public final class AesUtil {
private static final String ENCODE_RULES = "tomSun28HaHaHaHa";
/**
- * Default algorithm
+ * Default algorithm
*/
private static final String ALGORITHM_STR = "AES/CBC/PKCS5Padding";
@@ -143,7 +143,7 @@ public final class AesUtil {
public static boolean isCiphertext(String text, String decryptKey) {
// First use whether it is base64 to determine whether it has been
encrypted
if (Base64Util.isBase64(text)) {
- // If it is base64, decrypt directly to determine
+ // if it is base64, decrypt directly to determine
try {
SecretKeySpec keySpec = new
SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), AES);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
@@ -152,6 +152,7 @@ public final class AesUtil {
byte[] byteDecode = cipher.doFinal(bytesContent);
return byteDecode != null;
} catch (Exception e) {
+ log.warn("isCiphertext method error: {}", e.getMessage());
return false;
}
}
diff --git
a/common/src/main/java/org/apache/hertzbeat/common/util/Base64Util.java
b/common/src/main/java/org/apache/hertzbeat/common/util/Base64Util.java
index 1acf477bb..a70d277d5 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/Base64Util.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/Base64Util.java
@@ -28,6 +28,11 @@ public final class Base64Util {
}
public static boolean isBase64(String base64) {
+
+ if (base64 == null || base64.isEmpty()) {
+ return false;
+ }
+
try {
return Base64.getDecoder().decode(base64) != null;
} catch (Exception e) {
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/AesUtilTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/AesUtilTest.java
index 12a63eba2..7feb9f726 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/AesUtilTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/AesUtilTest.java
@@ -17,48 +17,77 @@
package org.apache.hertzbeat.common.util;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import static org.apache.hertzbeat.common.util.AesUtil.aesDecode;
+import static org.apache.hertzbeat.common.util.AesUtil.aesEncode;
+import static org.apache.hertzbeat.common.util.AesUtil.isCiphertext;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case for {@link AesUtil}
*/
class AesUtilTest {
- @BeforeEach
- void setUp() {
- }
-
- @AfterEach
- void tearDown() {
- }
+ private static final String ALGORITHM_STR = "AES/CBC/PKCS5Padding";
- @Test
- void setDefaultSecretKey() {
- }
+ private static final String AES = "AES";
- @Test
- void aesEncode() {
- }
+ private static final String VALID_KEY = "1234567890123456";
- @Test
- void aesDecode() {
- }
-
- @Test
- void isCiphertext() {
- }
+ private static final String ENCODE_RULES = "defaultsecretkey";
@Test
void testAesEncode() {
+ String originalText = "This is a secret message";
+ String encryptedText = aesEncode(originalText, VALID_KEY);
+ assertNotEquals(originalText, encryptedText);
+
+ String decryptedText = aesDecode(encryptedText, VALID_KEY);
+ assertEquals(originalText, decryptedText);
}
@Test
void testAesDecode() {
+ // Test with invalid key
+ String originalText = "This is a secret message";
+ String encryptedText = aesEncode(originalText, VALID_KEY);
+ String decryptedText = aesDecode(encryptedText, "invalidkey123456");
+ assertNotEquals(originalText, decryptedText);
+
+ // Test with default key
+ originalText = "This is a secret message";
+ encryptedText = aesEncode(originalText, VALID_KEY);
+ decryptedText = aesDecode(encryptedText, "invalidkey123456");
+ if (!decryptedText.equals(originalText)) {
+ decryptedText = aesDecode(encryptedText, ENCODE_RULES);
+ }
+ assertNotEquals(originalText, decryptedText);
}
@Test
void testIsCiphertext() {
+
+ // Test with valid key
+ String originalText = "This is a secret message";
+ String encryptedText = aesEncode(originalText, VALID_KEY);
+ assertTrue(isCiphertext(encryptedText, VALID_KEY));
+
+ // Test with plain text, normal text is not ciphertext
+ String plainText = "This is not encrypted";
+ assertFalse(isCiphertext(plainText, VALID_KEY));
+
+ // Test with invalid base64 text
+ String invalidBase64Text = "InvalidBase64";
+ assertFalse(isCiphertext(invalidBase64Text, VALID_KEY));
+
+ // Test with invalid key
+ originalText = "This is a secret message";
+ encryptedText = aesEncode(originalText, VALID_KEY);
+ String invalidKey = "6543210987654321";
+ assertFalse(isCiphertext(encryptedText, invalidKey));
}
+
}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/Base64UtilTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/Base64UtilTest.java
index 4f5f98acd..3df1e307f 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/Base64UtilTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/Base64UtilTest.java
@@ -19,12 +19,33 @@ package org.apache.hertzbeat.common.util;
import org.junit.jupiter.api.Test;
+import static org.apache.hertzbeat.common.util.Base64Util.isBase64;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
/**
* Test case for {@link Base64Util}
*/
class Base64UtilTest {
@Test
- void isBase64() {
+ void testIsBase64() {
+
+ String validBase64String = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5n";
+ assertTrue(isBase64(validBase64String));
+
+ String invalidBase64String = "This is not a valid Base64 string!";
+ assertFalse(isBase64(invalidBase64String));
+
+ String emptyString = "";
+ assertFalse(isBase64(emptyString));
+
+ assertFalse(isBase64(null));
+
+ String whitespaceString = " ";
+ assertFalse(isBase64(whitespaceString));
+
+ String paddedBase64String = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5n===";
+ assertFalse(isBase64(paddedBase64String));
}
}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
index de158aa0a..330c09880 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/JsonUtilTest.java
@@ -21,29 +21,25 @@ import com.fasterxml.jackson.core.type.TypeReference;
import java.util.ArrayList;
import java.util.List;
import org.apache.hertzbeat.common.entity.manager.TagItem;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import static org.apache.hertzbeat.common.util.JsonUtil.isJsonStr;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* Test case for {@link JsonUtil}
*/
class JsonUtilTest {
- @BeforeEach
- void setUp() {
- }
-
@Test
void toJson() {
List<TagItem> tagList = new ArrayList<>(4);
TagItem proTag = new TagItem("test", "pro");
tagList.add(proTag);
tagList.add(new TagItem("test", "dev"));
- System.out.println(JsonUtil.toJson(tagList));
- }
- @Test
- void fromJson() {
+
assertEquals("[{\"name\":\"test\",\"value\":\"pro\"},{\"name\":\"test\",\"value\":\"dev\"}]",
+ JsonUtil.toJson(tagList));
}
@Test
@@ -51,10 +47,26 @@ class JsonUtilTest {
String jsonStr =
"[{\"name\":\"test\",\"value\":\"pro\"},{\"name\":\"test\",\"value\":\"dev\"}]";
List<TagItem> tagItems = JsonUtil.fromJson(jsonStr, new
TypeReference<>() {
});
- System.out.println(tagItems);
+ assertEquals("[TagItem(name=test, value=pro), TagItem(name=test,
value=dev)]", tagItems.toString());
}
@Test
- void testFromJson1() {
+ void testIsJsonStr() {
+ String jsonString = "{\"name\":\"John\", \"age\":30";
+ assertFalse(isJsonStr(jsonString));
+
+ assertFalse(isJsonStr(""));
+
+ assertFalse(isJsonStr(null));
+
+ String whitespaceString = " ";
+ assertFalse(isJsonStr(whitespaceString));
+
+ jsonString = "This is just a plain string.";
+ assertFalse(isJsonStr(jsonString));
+
+ String jsonStringArrays = "[{\"name\":\"John\"}, {\"name\":\"Doe\"}]";
+ assertFalse(isJsonStr(jsonStringArrays));
}
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]