Skip to content

Commit

Permalink
FIX #346: Updated test classes to use the Junit rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
aion-shidokht committed Jan 30, 2019
1 parent b3df0c5 commit 14be99e
Show file tree
Hide file tree
Showing 33 changed files with 560 additions and 1,576 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,33 @@
package org.aion.avm.core;

import java.math.BigInteger;
import org.aion.avm.core.dappreading.JarBuilder;
import org.aion.avm.core.util.CodeAndArguments;
import org.aion.avm.core.util.Helpers;
import org.aion.avm.api.ABIEncoder;
import org.aion.avm.api.Address;
import org.aion.avm.core.util.AvmRule;
import org.aion.avm.core.util.TestingHelper;
import org.aion.kernel.AvmAddress;
import org.aion.kernel.AvmTransactionResult;
import org.aion.kernel.Block;
import org.aion.kernel.KernelInterfaceImpl;
import org.aion.kernel.Transaction;
import org.aion.kernel.TransactionContextImpl;
import org.aion.avm.api.ABIEncoder;
import org.aion.avm.api.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.aion.vm.api.interfaces.TransactionContext;
import org.aion.vm.api.interfaces.TransactionResult;
import org.aion.vm.api.interfaces.VirtualMachine;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.math.BigInteger;


/**
* Tests how we handle the "assert" keyword in Java source.
* Currently, we handle this as though assertions were always enabled.
*/
public class AssertKeywordIntegrationTest {
private static final Block BLOCK = new Block(new byte[32], 1L, Helpers.randomAddress(), System.currentTimeMillis(), new byte[0]);
@Rule
public AvmRule avmRule = new AvmRule(false);

private static final long ENERGY_LIMIT = 10_000_000L;
private static final long ENERGY_PRICE = 1L;
// This constant must be whatever is returned by Class#avm_desiredAssertionStatus().
private static final boolean ASSERTIONS_ENABLED = true;

private KernelInterface kernel;
private VirtualMachine avm;

@Before
public void setup() {
this.kernel = new KernelInterfaceImpl();
this.avm = CommonAvmFactory.buildAvmInstance(this.kernel);
}

@After
public void tearDown() {
this.avm.shutdown();
}

@Test
public void testEmptyPass() throws Exception {
Expand Down Expand Up @@ -106,20 +87,17 @@ public void testArgFail() throws Exception {


private Address installTestDApp(Class<?> testClass) {
byte[] jar = JarBuilder.buildJarForMainAndClasses(testClass);
byte[] txData = new CodeAndArguments(jar, new byte[0]).encodeToBytes();
byte[] txData = avmRule.getDappBytes(testClass, new byte[0]);

// Deploy.
Transaction create = Transaction.create(KernelInterfaceImpl.PREMINED_ADDRESS, this.kernel.getNonce(KernelInterfaceImpl.PREMINED_ADDRESS), BigInteger.ZERO, txData, ENERGY_LIMIT, ENERGY_PRICE);
TransactionResult createResult = this.avm.run(new TransactionContext[] {new TransactionContextImpl(create, BLOCK)})[0].get();
TransactionResult createResult = avmRule.deploy(KernelInterfaceImpl.PREMINED_ADDRESS, BigInteger.ZERO, txData, ENERGY_LIMIT, ENERGY_PRICE).getTransactionResult();
Assert.assertEquals(AvmTransactionResult.Code.SUCCESS, createResult.getResultCode());
return TestingHelper.buildAddress(createResult.getReturnData());
}

private Object callStatic(Address dapp, String methodName, Object... arguments) {
byte[] argData = ABIEncoder.encodeMethodArguments(methodName, arguments);
Transaction call = Transaction.call(KernelInterfaceImpl.PREMINED_ADDRESS, AvmAddress.wrap(dapp.unwrap()), this.kernel.getNonce(KernelInterfaceImpl.PREMINED_ADDRESS), BigInteger.ZERO, argData, ENERGY_LIMIT, ENERGY_PRICE);
TransactionResult result = this.avm.run(new TransactionContext[] {new TransactionContextImpl(call, BLOCK)})[0].get();
TransactionResult result = avmRule.call(KernelInterfaceImpl.PREMINED_ADDRESS, AvmAddress.wrap(dapp.unwrap()), BigInteger.ZERO, argData, ENERGY_LIMIT, ENERGY_PRICE).getTransactionResult();
Assert.assertEquals(AvmTransactionResult.Code.SUCCESS, result.getResultCode());
return TestingHelper.decodeResult(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
package org.aion.avm.core;

import java.math.BigInteger;
import org.aion.avm.core.dappreading.JarBuilder;
import org.aion.avm.core.util.CodeAndArguments;
import org.aion.avm.core.util.Helpers;

import org.aion.avm.core.util.AvmRule;
import org.aion.avm.core.util.TestingHelper;
import org.aion.kernel.AvmAddress;
import org.aion.kernel.Block;
import org.aion.kernel.KernelInterfaceImpl;
import org.aion.kernel.Transaction;
import org.aion.kernel.TransactionContextImpl;
import org.aion.kernel.AvmTransactionResult;
import org.aion.avm.api.ABIEncoder;
import org.aion.avm.api.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.aion.vm.api.interfaces.TransactionContext;
import org.aion.vm.api.interfaces.TransactionResult;
import org.aion.vm.api.interfaces.VirtualMachine;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;


/**
* Tests how we handle AssertionError's special constructors.
*/
public class AssertionErrorIntegrationTest {
private static final Block BLOCK = new Block(new byte[32], 1L, Helpers.randomAddress(), System.currentTimeMillis(), new byte[0]);
@Rule
public AvmRule avmRule = new AvmRule(false);

private static final long ENERGY_LIMIT = 10_000_000L;
private static final long ENERGY_PRICE = 1L;

private KernelInterface kernel;
private VirtualMachine avm;

@Before
public void setup() {
this.kernel = new KernelInterfaceImpl();
this.avm = CommonAvmFactory.buildAvmInstance(this.kernel);
}

@After
public void tearDown() {
this.avm.shutdown();
}

@Test
public void testEmpty() throws Exception {
Address dapp = installTestDApp(AssertionErrorIntegrationTestTarget.class);
Expand Down Expand Up @@ -128,20 +106,17 @@ public void testNormal() throws Exception {


private Address installTestDApp(Class<?> testClass) {
byte[] jar = JarBuilder.buildJarForMainAndClasses(testClass);
byte[] txData = new CodeAndArguments(jar, new byte[0]).encodeToBytes();
byte[] txData = avmRule.getDappBytes(testClass, new byte[0]);

// Deploy.
Transaction create = Transaction.create(KernelInterfaceImpl.PREMINED_ADDRESS, this.kernel.getNonce(KernelInterfaceImpl.PREMINED_ADDRESS), BigInteger.ZERO, txData, ENERGY_LIMIT, ENERGY_PRICE);
TransactionResult createResult = this.avm.run(new TransactionContext[] {new TransactionContextImpl(create, BLOCK)})[0].get();
TransactionResult createResult = avmRule.deploy(KernelInterfaceImpl.PREMINED_ADDRESS, BigInteger.ZERO, txData, ENERGY_LIMIT, ENERGY_PRICE).getTransactionResult();
Assert.assertEquals(AvmTransactionResult.Code.SUCCESS, createResult.getResultCode());
return TestingHelper.buildAddress(createResult.getReturnData());
}

private String callStaticString(Address dapp, String methodName, Object... arguments) {
byte[] argData = ABIEncoder.encodeMethodArguments(methodName, arguments);
Transaction call = Transaction.call(KernelInterfaceImpl.PREMINED_ADDRESS, AvmAddress.wrap(dapp.unwrap()), this.kernel.getNonce(KernelInterfaceImpl.PREMINED_ADDRESS), BigInteger.ZERO, argData, ENERGY_LIMIT, ENERGY_PRICE);
TransactionResult result = this.avm.run(new TransactionContext[] {new TransactionContextImpl(call, BLOCK)})[0].get();
TransactionResult result = avmRule.call(KernelInterfaceImpl.PREMINED_ADDRESS, AvmAddress.wrap(dapp.unwrap()), BigInteger.ZERO, argData, ENERGY_LIMIT, ENERGY_PRICE).getTransactionResult();
Assert.assertEquals(AvmTransactionResult.Code.SUCCESS, result.getResultCode());
byte[] utf8 = (byte[])TestingHelper.decodeResult(result);
return (null != utf8)
Expand Down
Loading

0 comments on commit 14be99e

Please sign in to comment.