Skip to content

Commit 041e47e

Browse files
committed
increase the test coverage
1 parent 614b1a4 commit 041e47e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

common/src/main/java/org/tron/common/utils/StringUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,16 @@ public static ByteString hexString2ByteString(String hexString) {
4646
}
4747

4848
public static boolean isHexadecimal(String str) {
49+
if (str == null || str.length() == 0) {
50+
return false;
51+
}
52+
if (str.length() % 2 != 0) {
53+
return false;
54+
}
55+
4956
for (int i = 0; i < str.length(); i++) {
5057
char c = str.charAt(i);
51-
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
58+
if (Character.digit(c, 16) == -1) {
5259
return false;
5360
}
5461
}

framework/src/test/java/org/tron/core/config/args/LocalWitnessTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.rules.TemporaryFolder;
2727
import org.tron.common.utils.LocalWitnesses;
2828
import org.tron.common.utils.PublicMethod;
29+
import org.tron.common.utils.StringUtil;
2930
import org.tron.core.Constant;
3031

3132
public class LocalWitnessTest {
@@ -108,7 +109,7 @@ public void testInvalidPrivateKey() {
108109
localWitnesses.addPrivateKeys(null);
109110
fail("should throw IllegalArgumentException");
110111
} catch (IllegalArgumentException e) {
111-
Assert.assertTrue(e.getMessage().contains("private key must be"));
112+
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
112113
} catch (Exception e) {
113114
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
114115
}
@@ -117,7 +118,7 @@ public void testInvalidPrivateKey() {
117118
localWitnesses.addPrivateKeys("");
118119
fail("should throw IllegalArgumentException");
119120
} catch (IllegalArgumentException e) {
120-
Assert.assertTrue(e.getMessage().contains("private key must be"));
121+
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
121122
} catch (Exception e) {
122123
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
123124
}
@@ -126,7 +127,16 @@ public void testInvalidPrivateKey() {
126127
localWitnesses.addPrivateKeys(" ");
127128
fail("should throw IllegalArgumentException");
128129
} catch (IllegalArgumentException e) {
129-
Assert.assertTrue(e.getMessage().contains("private key must be"));
130+
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
131+
} catch (Exception e) {
132+
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
133+
}
134+
135+
try {
136+
localWitnesses.addPrivateKeys("11111");
137+
fail("should throw IllegalArgumentException");
138+
} catch (IllegalArgumentException e) {
139+
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
130140
} catch (Exception e) {
131141
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
132142
}
@@ -152,6 +162,16 @@ public void testInvalidPrivateKey() {
152162
}
153163
}
154164

165+
@Test
166+
public void testHexStringFormat() {
167+
Assert.assertTrue(StringUtil.isHexadecimal("0123456789abcdefABCDEF"));
168+
Assert.assertFalse(StringUtil.isHexadecimal(null));
169+
Assert.assertFalse(StringUtil.isHexadecimal(""));
170+
Assert.assertFalse(StringUtil.isHexadecimal("abc"));
171+
Assert.assertFalse(StringUtil.isHexadecimal(" "));
172+
Assert.assertFalse(StringUtil.isHexadecimal("123xyz"));
173+
}
174+
155175
@Test
156176
public void getPrivateKey() {
157177
Assert.assertEquals(Lists

0 commit comments

Comments
 (0)