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
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ subprojects {
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.1'
classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
}
}

Expand All @@ -98,7 +97,6 @@ subprojects {
implementation "com.google.code.findbugs:jsr305:3.0.0"
implementation group: 'org.springframework', name: 'spring-context', version: "${springVersion}"
implementation "org.apache.commons:commons-lang3:3.4"
implementation group: 'org.apache.commons', name: 'commons-math', version: '2.2'
implementation "org.apache.commons:commons-collections4:4.1"
implementation group: 'joda-time', name: 'joda-time', version: '2.3'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk18on', version: '1.84'
Expand Down
1 change: 0 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies {
// https://eclipse.dev/aspectj/doc/latest/release/JavaVersionCompatibility.html
api 'org.aspectj:aspectjrt:1.9.8'
api 'org.aspectj:aspectjweaver:1.9.8'
api 'org.aspectj:aspectjtools:1.9.8'
api group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.9',{
exclude group: 'io.grpc', module: 'grpc-context'
exclude group: 'io.grpc', module: 'grpc-core'
Expand Down
2 changes: 0 additions & 2 deletions errorprone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ if (!JavaVersion.current().isJava11Compatible()) {
tasks.withType(Jar).configureEach { enabled = false }
} else {
dependencies {
compileOnly "com.google.errorprone:error_prone_annotations:${errorproneVersion}"
compileOnly "com.google.errorprone:error_prone_check_api:${errorproneVersion}"
compileOnly "com.google.errorprone:error_prone_core:${errorproneVersion}"
compileOnly "com.google.auto.service:auto-service:1.1.1"
annotationProcessor "com.google.auto.service:auto-service:1.1.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public void testCannotWritePropertyFile() throws IOException {
fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true);
fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true);

propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn(null);
strings.when(() -> Strings.isNullOrEmpty(null)).thenReturn(true);
propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY)).thenReturn("");
strings.when(() -> Strings.isNullOrEmpty("")).thenReturn(true);

propUtil.when(() -> PropUtil.writeProperty(engineFile, ENGINE_KEY, ROCKSDB))
.thenReturn(false);
Expand Down Expand Up @@ -142,27 +142,13 @@ public void testEngineMismatch() throws IOException {

@Test
public void testSuccessfulFirstTimeInit() throws IOException {
try (MockedStatic<FileUtil> fileUtil = mockStatic(FileUtil.class);
MockedStatic<PropUtil> propUtil = mockStatic(PropUtil.class);
MockedStatic<Strings> strings = mockStatic(Strings.class)) {

String dir = temporaryFolder.newFolder(ACCOUNT).toString();
String engineFile = Paths.get(dir, ENGINE_FILE).toString();

fileUtil.when(() -> FileUtil.createDirIfNotExists(dir)).thenReturn(true);
fileUtil.when(() -> FileUtil.createFileIfNotExists(engineFile)).thenReturn(true);

propUtil.when(() -> PropUtil.readProperty(engineFile, ENGINE_KEY))
.thenReturn(null)
.thenReturn(LEVELDB);
strings.when(() -> Strings.isNullOrEmpty(null)).thenReturn(true);
String dir = new File(temporaryFolder.getRoot(), ACCOUNT).toString();
File engineFile = Paths.get(dir, ENGINE_FILE).toFile();

propUtil.when(() -> PropUtil.writeProperty(engineFile, ENGINE_KEY, LEVELDB))
.thenReturn(true);
checkOrInitEngine(LEVELDB, dir, TronError.ErrCode.LEVELDB_INIT);

TronError.ErrCode errCode = TronError.ErrCode.LEVELDB_INIT;
checkOrInitEngine(LEVELDB, dir, errCode);
}
assertTrue(engineFile.isFile());
assertEquals(LEVELDB, PropUtil.readProperty(engineFile.toString(), ENGINE_KEY));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import org.junit.Before;
import org.junit.Test;
import org.tron.core.db.ByteArrayWrapper;

public class ByteArrayMapTest {

Expand Down Expand Up @@ -139,7 +141,10 @@ public void test() {
Map<byte[], String> map = new ByteArrayMap<>();
Map<byte[], String> testMap = createTestMap();
assertNotEquals(map, testMap);
assertTrue(testMap.hashCode() <= 0);
Map<ByteArrayWrapper, String> expected = new HashMap<>();
expected.put(new ByteArrayWrapper("key1".getBytes()), "value1");
expected.put(new ByteArrayWrapper("key2".getBytes()), "value2");
assertEquals(expected.hashCode(), testMap.hashCode());
assertNotNull(testMap.toString());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.tron.common.utils;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -73,14 +72,16 @@ public void testIterator() {
byteArraySet.add(bytes2);

Iterator<byte[]> iterator = byteArraySet.iterator();
Set<String> actual = new HashSet<>();

assertTrue(iterator.hasNext());
assertArrayEquals(bytes1, iterator.next());
actual.add(ByteArray.toHexString(iterator.next()));

assertTrue(iterator.hasNext());
assertArrayEquals(bytes2, iterator.next());
actual.add(ByteArray.toHexString(iterator.next()));

assertFalse(iterator.hasNext());
assertEquals(new HashSet<>(Arrays.asList("010203", "040506")), actual);
}

@Test
Expand All @@ -94,8 +95,11 @@ public void testToArray() {
byte[][] array = byteArraySet.toArray(new byte[0][]);

assertEquals(2, array.length);
assertArrayEquals(bytes1, array[0]);
assertArrayEquals(bytes2, array[1]);
Set<String> actual = new HashSet<>();
for (byte[] bytes : array) {
actual.add(ByteArray.toHexString(bytes));
}
assertEquals(new HashSet<>(Arrays.asList("010203", "040506")), actual);
}

@Test
Expand Down
30 changes: 17 additions & 13 deletions framework/src/test/java/org/tron/common/utils/JsonUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.tron.common.utils.JsonUtil.json2Obj;
import static org.tron.common.utils.JsonUtil.obj2Json;

Expand Down Expand Up @@ -46,17 +46,13 @@ public void test() {

@Test
public void testObj2JsonWithCircularReference() {
Node node1 = new Node("Node1");
Node node2 = new Node("Node2");
node1.setNext(node2);
node2.setNext(node1);

try {
obj2Json(node1);
fail("Expected a RuntimeException to be thrown");
} catch (RuntimeException e) {
assertTrue(e.getCause() instanceof com.fasterxml.jackson.databind.JsonMappingException);
}
Node node = new Node("Node1");
assertTrue(obj2Json(node).contains("\"name\":\"Node1\""));
node.setNext(node);

RuntimeException exception = assertThrows(RuntimeException.class, () -> obj2Json(node));
assertTrue(exception.getCause() instanceof com.fasterxml.jackson.databind.JsonMappingException);
assertTrue(exception.getCause().getMessage().contains("Direct self-reference"));
}

@Test(expected = RuntimeException.class)
Expand All @@ -65,14 +61,22 @@ public void testInvalidJson() {
json2Obj(invalidJson, String.class);
}

class Node {
public static class Node {
private String name;
private org.tron.common.utils.JsonUtilTest.Node next;

public Node(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Node getNext() {
return next;
}

public void setNext(org.tron.common.utils.JsonUtilTest.Node next) {
this.next = next;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,12 @@ public void testConsumeBandwidthTooBigTransactionResultException() {
trx.setInBlock(false);
TransactionTrace trace = new TransactionTrace(trx, StoreFactory
.getInstance(), new RuntimeImpl());
assertThrows(
"Too big transaction result, TxId %s, the result size is %d bytes, maxResultSize %d",
TooBigTransactionResultException exception = assertThrows(
TooBigTransactionResultException.class, () -> dbManager.consumeBandwidth(trx, trace));
Assert.assertEquals(String.format(
"Too big transaction result, TxId %s, the result size is %d bytes, maxResultSize %d",
trx.getTransactionId(), trx.getResultSizeWithMaxContractRet(), Constant.MAX_RESULT_SIZE_IN_TX),
exception.getMessage());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,50 +114,51 @@ public void testNullTransactionResultCapsule() {
} catch (ContractValidateException e) {
fail();
}
assertThrows(ActuatorConstant.TX_RESULT_NULL,
RuntimeException.class, () -> actuator.execute(null));
assertEquals(ActuatorConstant.TX_RESULT_NULL,
assertThrows(RuntimeException.class, () -> actuator.execute(null)).getMessage());
}

@Test
public void testInvalidOwnerAddress() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager())
.setAny(getCancelAllUnfreezeV2ContractInvalidAddress());
assertThrows("Invalid address", ContractValidateException.class, actuator::validate);
assertEquals("Invalid address",
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
public void testInvalidOwnerAccount() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager())
.setAny(getCancelAllUnfreezeV2ContractInvalidAccount());
assertThrows("Account[" + OWNER_ACCOUNT_INVALID + "] does not exist",
ContractValidateException.class, actuator::validate);
assertEquals("Account[" + OWNER_ACCOUNT_INVALID + "] not exists",
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
public void testInvalidOwnerUnfreezeV2List() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager())
.setAny(getCancelAllUnfreezeV2Contract());
assertThrows("no unfreezeV2 list to cancel",
ContractValidateException.class, actuator::validate);
assertEquals("No unfreezeV2 list to cancel",
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
public void testInvalidCancelAllUnfreezeV2Contract() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(null);
assertThrows(ActuatorConstant.CONTRACT_NOT_EXIST,
ContractValidateException.class, actuator::validate);
assertEquals(ActuatorConstant.CONTRACT_NOT_EXIST,
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
public void testInvalidAccountStore() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(null).setAny(getCancelAllUnfreezeV2Contract());
assertThrows(ActuatorConstant.STORE_NOT_EXIST,
ContractValidateException.class, actuator::validate);
assertEquals(ActuatorConstant.STORE_NOT_EXIST,
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
Expand All @@ -166,20 +167,20 @@ public void testSupportAllowCancelAllUnfreezeV2() {
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager())
.setAny(getCancelAllUnfreezeV2Contract());
assertThrows(
assertEquals(
"Not support CancelAllUnfreezeV2 transaction, need to be opened by the committee",
ContractValidateException.class, actuator::validate);
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

@Test
public void testErrorContract() {
dbManager.getDynamicPropertiesStore().saveAllowCancelAllUnfreezeV2(1);
CancelAllUnfreezeV2Actuator actuator = new CancelAllUnfreezeV2Actuator();
actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(getErrorContract());
assertThrows(
assertEquals(
"contract type error, expected type [CancelAllUnfreezeV2Contract], "
+ "real type[WithdrawExpireUnfreezeContract]",
ContractValidateException.class, actuator::validate);
+ "real type[class com.google.protobuf.Any]",
assertThrows(ContractValidateException.class, actuator::validate).getMessage());
}

private Any getCancelAllUnfreezeV2Contract() {
Expand All @@ -204,4 +205,4 @@ private Any getCancelAllUnfreezeV2ContractInvalidAccount() {
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ACCOUNT_INVALID))).build()
);
}
}
}
Loading
Loading