Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.exception.TronError;

@Slf4j(topic = "app")
public class LocalWitnesses {
Expand Down Expand Up @@ -78,13 +79,14 @@ private void validate(String privateKey) {

if (StringUtils.isBlank(privateKey)
|| privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
throw new IllegalArgumentException(
String.format("private key must be %d hex string, actual: %d",
ChainConstant.PRIVATE_KEY_LENGTH,
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()));
throw new TronError(String.format("private key must be %d hex string, actual: %d",
ChainConstant.PRIVATE_KEY_LENGTH,
StringUtils.isBlank(privateKey) ? 0 : privateKey.length()),
TronError.ErrCode.WITNESS_INIT);
}
if (!StringUtil.isHexadecimal(privateKey)) {
throw new IllegalArgumentException("private key must be hex string");
throw new TronError("private key must be hex string",
TronError.ErrCode.WITNESS_INIT);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@

package org.tron.core.config.args;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.collect.Lists;
import java.io.IOException;
import java.security.SecureRandom;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.LocalWitnesses;
import org.tron.common.utils.PublicMethod;
import org.tron.common.utils.StringUtil;
import org.tron.core.Constant;
import org.tron.core.exception.TronError;
import org.tron.core.exception.TronError.ErrCode;

public class LocalWitnessTest {

Expand All @@ -42,7 +49,7 @@ public void setLocalWitness() {
localWitness
.setPrivateKeys(
Lists.newArrayList(
PRIVATE_KEY));
PRIVATE_KEY));
}

@Test
Expand All @@ -52,13 +59,13 @@ public void whenSetNullPrivateKey() {
Assert.assertNotNull(localWitness.getPublicKey());
}

@Test(expected = IllegalArgumentException.class)
@Test(expected = TronError.class)
public void whenSetEmptyPrivateKey() {
localWitness.setPrivateKeys(Lists.newArrayList(""));
fail("private key must be 64-bits hex string");
}

@Test(expected = IllegalArgumentException.class)
@Test(expected = TronError.class)
public void whenSetBadFormatPrivateKey() {
localWitness.setPrivateKeys(Lists.newArrayList("a111"));
fail("private key must be 64-bits hex string");
Expand Down Expand Up @@ -104,62 +111,27 @@ public void testValidPrivateKeyWithPrefix() {
@Test
public void testInvalidPrivateKey() {
LocalWitnesses localWitnesses = new LocalWitnesses();
String expectedMessage = "private key must be 64 hex string";
assertTronError(localWitnesses, null, expectedMessage);
assertTronError(localWitnesses, "", expectedMessage);
assertTronError(localWitnesses, " ", expectedMessage);
assertTronError(localWitnesses, "11111", expectedMessage);
String expectedMessage2 = "private key must be hex string";
SecureRandom secureRandom = new SecureRandom();
byte[] keyBytes = new byte[31];
secureRandom.nextBytes(keyBytes);
final String privateKey = ByteArray.toHexString(keyBytes) + " ";
assertTronError(localWitnesses, privateKey, expectedMessage2);
final String privateKey2 = "xy" + ByteArray.toHexString(keyBytes);
assertTronError(localWitnesses, privateKey2, expectedMessage2);
}

try {
localWitnesses.addPrivateKeys(null);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}

try {
localWitnesses.addPrivateKeys("");
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}

try {
localWitnesses.addPrivateKeys(" ");
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}

try {
localWitnesses.addPrivateKeys("11111");
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be 64 hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}

try {
String privateKey = "11111111111111111111111111111111111111111111111111111111111111 ";
localWitnesses.addPrivateKeys(privateKey);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}

try {
String privateKey = "xy11111111111111111111111111111111111111111111111111111111111111";
localWitnesses.addPrivateKeys(privateKey);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("private key must be hex string"));
} catch (Exception e) {
fail("should IllegalArgumentException,actual exception: " + e.getClass().getSimpleName());
}
private void assertTronError(LocalWitnesses localWitnesses, String privateKey,
String expectedMessage) {
TronError thrown = assertThrows(TronError.class,
() -> localWitnesses.addPrivateKeys(privateKey));
assertEquals(ErrCode.WITNESS_INIT, thrown.getErrCode());
assertTrue(thrown.getMessage().contains(expectedMessage));
}

@Test
Expand Down