-
Notifications
You must be signed in to change notification settings - Fork 2
/
OwnableTest.java
117 lines (96 loc) · 4.05 KB
/
OwnableTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.altoros.aion;
import avm.Address;
import org.aion.avm.core.util.ABIUtil;
import org.aion.avm.tooling.AvmRule;
import org.aion.vm.api.interfaces.IExecutionLog;
import org.aion.vm.api.interfaces.ResultCode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class OwnableTest {
private static final byte[] OWNERSHIP_TRANSFERRED = "OwnershipTransferred".getBytes();
@ClassRule
public static AvmRule avmRule = new AvmRule(true);
//default address with balance
private static Address owner = avmRule.getPreminedAccount();
private static Address other = avmRule.getRandomAddress(BigInteger.valueOf(100_000_000));
private Address dappAddr;
@Before
public void deployDapp() {
//deploy Dapp:
// 1- get the Dapp byes to be used for the deploy transaction
// 2- deploy the Dapp and get the address.
byte[] dapp = avmRule.getDappBytes(Ownable.class, null);
AvmRule.ResultWrapper deployed = avmRule.deploy(owner, BigInteger.ZERO, dapp);
Assert.assertNotNull("Failed to deploy app", deployed);
dappAddr = deployed.getDappAddress();
}
private Object doCall(String methodName, Object... arguments) {
AvmRule.ResultWrapper result = doCall(owner, methodName, arguments);
// getReceiptStatus() checks the status of the transaction execution
ResultCode status = result.getReceiptStatus();
System.out.println(status.name());
Assert.assertTrue(status.isSuccess());
List<IExecutionLog> logs = result.getLogs();
for (IExecutionLog l : logs) {
String str = new String(l.getData());
System.out.println(str);
}
return result.getDecodedReturnData();
}
private AvmRule.ResultWrapper doCall(Address from, String methodName, Object... arguments) {
//calling Dapps:
// 1- encode method name and arguments
// 2- make the call;
byte[] txData = ABIUtil.encodeMethodArguments(methodName, arguments);
AvmRule.ResultWrapper result = avmRule.call(from, dappAddr, BigInteger.ZERO, txData);
return result;
}
@Test
public void owner() {
Object address = doCall("getOwner");
Assert.assertEquals(address, owner);
}
@Test
public void renounceOwnership_whenNotOwnerCalling_success() { //onlyOwner
AvmRule.ResultWrapper result = doCall(owner, "renounceOwnership");
Assert.assertTrue(result.getReceiptStatus().isSuccess());
Optional<IExecutionLog> event = findAnyLog(result, OWNERSHIP_TRANSFERRED);
Assert.assertTrue("Event is not emitted", event.isPresent());
}
@Test
public void renounceOwnership_whenNotOwnerCalling_returnError() { //onlyOwner
AvmRule.ResultWrapper result = doCall(other, "renounceOwnership");
Assert.assertTrue(result.getReceiptStatus().isFailed());
Optional<IExecutionLog> event = findAnyLog(result, OWNERSHIP_TRANSFERRED);
Assert.assertFalse("Event is emitted", event.isPresent());
}
@Test
public void transferOwnership_byOwner_succeed() { //onlyOwner
doCall("transferOwnership", other);
Object currentOwner = doCall("getOwner");
Assert.assertEquals(other, currentOwner);
}
@Test
public void transferOwnership_byNotOwner_rejected() { //onlyOwner
doCall(other,"transferOwnership", other);
Object currentOwner = doCall("getOwner");
Assert.assertEquals(owner, currentOwner);
}
private Optional<IExecutionLog> findAnyLog(AvmRule.ResultWrapper result, byte[] event) {
for (IExecutionLog log : result.getLogs()) {
if (!log.getTopics().isEmpty()) {
byte[] topic = new String(log.getTopics().get(0)).trim().getBytes();
if (Arrays.equals(event, topic)) {
return Optional.of(log);
}
}
}
return Optional.empty();
}
}