Skip to content
Open
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 @@ -235,7 +235,7 @@ public boolean validate() throws ContractValidateException {
if (marketAccountOrderCapsule != null
&& marketAccountOrderCapsule.getCount() >= MAX_ACTIVE_ORDER_NUM) {
throw new ContractValidateException(
"Maximum number of orders exceeded" + MAX_ACTIVE_ORDER_NUM);
"Maximum number of orders exceeded, " + MAX_ACTIVE_ORDER_NUM);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,6 +34,7 @@ public class BackupServer implements AutoCloseable {

private final String name = "BackupServer";
private ExecutorService executor;
private Future<?> serverTask;

@Autowired
public BackupServer(final BackupManager backupManager) {
Expand All @@ -42,7 +44,7 @@ public BackupServer(final BackupManager backupManager) {
public void initServer() {
if (port > 0 && commonParameter.getBackupMembers().size() > 0) {
executor = ExecutorServiceManager.newSingleThreadExecutor(name);
executor.submit(() -> {
serverTask = executor.submit(() -> {
try {
start();
} catch (Exception e) {
Expand Down Expand Up @@ -95,14 +97,17 @@ public void initChannel(NioDatagramChannel ch)
public void close() {
logger.info("Closing backup server...");
shutdown = true;
backupManager.stop();
if (channel != null) {
try {
channel.close().await(10, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn("Closing backup server failed.", e);
}
}
if (serverTask != null) {
serverTask.cancel(true);
}
backupManager.stop();
ExecutorServiceManager.shutdownAndAwaitTermination(executor, name);
logger.info("Backup server closed.");
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/test/java/org/tron/common/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Protocol.Block getSignedBlock(ByteString witness, long time, byte[] priva
.build();

ECKey ecKey = ECKey.fromPrivate(privateKey);
assert ecKey != null;
Assert.assertNotNull(ecKey);
ECKey.ECDSASignature signature = ecKey.sign(Sha256Hash.of(CommonParameter
.getInstance().isECKeyCryptoEngine(), raw.toByteArray()).getBytes());
ByteString sign = ByteString.copyFrom(signature.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tron.common.backup;

import static org.mockito.Mockito.mock;

import java.util.ArrayList;
import java.util.List;
import org.junit.After;
Expand All @@ -21,7 +23,7 @@ public class BackupServerTest {
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public Timeout globalTimeout = Timeout.seconds(60);
public Timeout globalTimeout = Timeout.seconds(30);
private BackupServer backupServer;

@Before
Expand All @@ -32,8 +34,7 @@ public void setUp() throws Exception {
List<String> members = new ArrayList<>();
members.add("127.0.0.2");
CommonParameter.getInstance().setBackupMembers(members);
BackupManager backupManager = new BackupManager();
backupManager.init();
BackupManager backupManager = mock(BackupManager.class);
backupServer = new BackupServer(backupManager);
}

Expand All @@ -43,10 +44,8 @@ public void tearDown() {
Args.clearParam();
}

@Test(timeout = 60_000)
public void test() throws InterruptedException {
@Test
public void test() {
backupServer.initServer();
// wait for the server to start so channel is assigned before close() is called
Thread.sleep(1000);
}
}
27 changes: 0 additions & 27 deletions framework/src/test/java/org/tron/common/command/CliTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.tron.common.utils.client.utils.AbiUtil.generateOccupationConstantPrivateKey;

import java.math.BigInteger;
import java.security.SignatureException;
import java.util.Arrays;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Assert;
import org.junit.Test;
import org.tron.common.crypto.sm2.SM2;
import org.tron.common.utils.Sha256Hash;
Expand Down Expand Up @@ -135,19 +135,18 @@ public void testSM2SpongySignature() throws SignatureException {
}

@Test
public void testSignToAddress() {
public void testSignToAddressWrapsInvalidSignature() {
String messageHash = "818e0e76976123b9b78b6076cc2b5d53e61b49ff9cf78304de688a860ce7cb95";
String base64Sign = "G1y76mVO6TRpFwp3qOiLVzHA8uFsrDiOL7hbC2uN9qTHHiLypaW4vnQkfkoUygjo5qBd"
+ "+NlYQ/mAPVWKu6K00co=";
try {
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.TRUE);
} catch (Exception e) {
Assert.assertTrue(e instanceof SignatureException);
}
try {
SignUtils.signatureToAddress(Hex.decode(messageHash), base64Sign, Boolean.FALSE);
} catch (Exception e) {
Assert.assertTrue(e instanceof SignatureException);
}
String truncatedBase64Signature = "AA==";

SignatureException ecException = assertThrows(SignatureException.class,
() -> SignUtils.signatureToAddress(
Hex.decode(messageHash), truncatedBase64Signature, true));
assertEquals(SignatureException.class, ecException.getCause().getClass());

SignatureException sm2Exception = assertThrows(SignatureException.class,
() -> SignUtils.signatureToAddress(
Hex.decode(messageHash), truncatedBase64Signature, false));
assertEquals(SignatureException.class, sm2Exception.getCause().getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.nio.charset.StandardCharsets;
import java.security.SignatureException;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
import org.tron.common.crypto.sm2.SM2;
import org.tron.common.utils.PublicMethod;


@Slf4j
public class SignatureInterfaceTest {

private String SM2_privString = PublicMethod.getSM2RandomPrivateKey();
Expand All @@ -31,14 +31,9 @@ public class SignatureInterfaceTest {


@Test
public void testContructor() {
SignInterface sign = new SM2();
logger.info(Hex.toHexString(sign.getPrivateKey()) + " :SM2 Generated privkey");
logger.info(Hex.toHexString(sign.getPubKey()) + " :SM2 Generated pubkey");

sign = new ECKey();
logger.info(Hex.toHexString(sign.getPrivateKey()) + " :ECDSA Generated privkey");
logger.info(Hex.toHexString(sign.getPubKey()) + " :ECDSA Generated pubkey");
public void testConstructorGeneratesUsableKeys() throws SignatureException {
assertGeneratedKey(new SM2(), false);
assertGeneratedKey(new ECKey(), true);
}

@Test
Expand All @@ -63,10 +58,10 @@ public void testPublicKey() {
@Test
public void testNullKey() {
SignInterface sign = new SM2(SM2_pubKey, false);
assertEquals(null, sign.getPrivateKey());
assertNull(sign.getPrivateKey());

sign = new ECKey(EC_pubKey, false);
assertEquals(null, sign.getPrivateKey());
assertNull(sign.getPrivateKey());
}

@Test
Expand All @@ -75,11 +70,25 @@ public void testAddress() {
byte[] prefix_address = sign.getAddress();
byte[] address = Arrays.copyOfRange(prefix_address, 1, prefix_address.length);
byte[] addressTmp = Arrays.copyOfRange(Hex.decode(SM2_address), 1, prefix_address.length);
assertEquals(Hex.toHexString(addressTmp), Hex.toHexString(address));
assertArrayEquals(addressTmp, address);
sign = new ECKey(EC_pubKey, false);
prefix_address = sign.getAddress();
address = Arrays.copyOfRange(prefix_address, 1, prefix_address.length);
byte[] ecAddressTmp = Arrays.copyOfRange(Hex.decode(EC_address), 1, prefix_address.length);
assertEquals(Hex.toHexString(ecAddressTmp), Hex.toHexString(address));
assertArrayEquals(ecAddressTmp, address);
}

private void assertGeneratedKey(SignInterface sign, boolean ecKeyCryptoEngine)
throws SignatureException {
assertEquals(32, sign.getPrivateKey().length);
assertEquals(65, sign.getPubKey().length);
assertEquals(21, sign.getAddress().length);
assertEquals(64, sign.getNodeId().length);

byte[] hash = Hash.sha3("signature-interface".getBytes(StandardCharsets.UTF_8));
String signature = sign.signHash(hash);
assertEquals(65, sign.Base64toBytes(signature).length);
assertArrayEquals(sign.getAddress(),
SignUtils.signatureToAddress(hash, signature, ecKeyCryptoEngine));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,23 @@ public void testWireBytesMatchCheckBodySizeForAsciiJson() throws Exception {
}

/**
* For UTF-8 JSON with multi-byte characters (CJK), wire bytes and
* {@code body.getBytes().length} must still be identical - UTF-8 round-trips
* through {@code request.getReader()} -> {@code String.getBytes()} losslessly.
* {@code Util.checkBodySize()} uses the platform default charset after the request has been
* decoded into a String. That value is not necessarily the UTF-8 wire size (for example, a
* US-ASCII default charset replaces CJK characters). The primary SizeLimitHandler assertion is
* covered by {@link #testLimitIsBasedOnBytesNotCharacters()}; this test mirrors the deprecated
* servlet-side check without assuming a particular process charset.
*/
@Test
public void testWireBytesMatchCheckBodySizeForUtf8Json() throws Exception {
public void testCheckBodySizeUsesPlatformCharsetForUtf8Json() throws Exception {
String jsonBody = "{\"name\":\"测试地址\",\"amount\":100}";
int wireBytes = jsonBody.getBytes("UTF-8").length;
int expectedServletBytes = jsonBody.getBytes().length;

String respBody = postForBody(httpServerUri, new StringEntity(jsonBody, "UTF-8"));
JSONObject json = JSONObject.parseObject(respBody);
int servletBytes = json.getIntValue("bytes");

Assert.assertEquals("wire bytes should equal checkBodySize for UTF-8 JSON",
wireBytes, servletBytes);
Assert.assertEquals("checkBodySize should use the platform default charset",
expectedServletBytes, servletBytes);
}

/**
Expand Down
Loading
Loading