Skip to content

Commit

Permalink
Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarev committed Dec 9, 2024
1 parent de5d977 commit f53a6b0
Showing 1 changed file with 27 additions and 277 deletions.
304 changes: 27 additions & 277 deletions rskj-core/src/test/java/co/rsk/vm/opcode/MCopyDslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import org.ethereum.core.util.TransactionReceiptUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.FileNotFoundException;
import java.util.stream.Stream;

public class MCopyDslTest {

Expand Down Expand Up @@ -43,98 +47,6 @@ void testMCOPY_whenNotActivated_behavesAsExpected() throws FileNotFoundException

}

@Test
void testMCOPY_testCase1_behavesAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testCopying32BytesFromOffset32toOffset0.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world,"b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_testCase2_behavesAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testCopying32BytesFromOffset0toOffset0.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_testCase3_behavesAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testCopying8BytesFromOffset1toOffset0.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_testCase4_behavesAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testCopying8BytesFromOffset0toOffset1.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

// Advanced Overwrite Test Cases
// https://github.com/ethereum/execution-spec-tests/blob/c0065176a79f89d93f4c326186fc257ec5b8d5f1/tests/cancun/eip5656_mcopy/test_mcopy.py)

Expand Down Expand Up @@ -172,35 +84,11 @@ void testMCOPY_overwriteCases_behaveAsExpected() throws FileNotFoundException, D

}

// Full Memory Copy/Rewrite/Clean Tests

@Test
void testMCOPY_fullMemoryClean_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testFullMemoryClean.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_fullMemoryCopy_behaveAsExpected() throws FileNotFoundException, DslProcessorException {
@ParameterizedTest
@MethodSource("provideParametersForMCOPYTestCases")
void testMCOPY_OnEachTestCase_ExecutesAsExpected(String dslFile) throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testFullMemoryCopy.txt");
DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/" + dslFile);
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);
Expand All @@ -220,167 +108,29 @@ void testMCOPY_fullMemoryCopy_behaveAsExpected() throws FileNotFoundException, D

}

@Test
void testMCOPY_fullMemoryCopyOffset_behaveAsExpected() throws FileNotFoundException, DslProcessorException {
private static Stream<Arguments> provideParametersForMCOPYTestCases() {
return Stream.of(

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testFullMemoryCopyOffset.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);
// Basic Test Cases from 1 to 4 on EIP
Arguments.of("testCopying32BytesFromOffset32toOffset0.txt"),
Arguments.of("testCopying32BytesFromOffset0toOffset0.txt"),
Arguments.of("testCopying8BytesFromOffset1toOffset0.txt"),
Arguments.of("testCopying8BytesFromOffset0toOffset1.txt"),

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);
// Full Memory Copy/Rewrite/Clean Tests
Arguments.of("testFullMemoryClean.txt"),
Arguments.of("testFullMemoryCopy.txt"),
Arguments.of("testFullMemoryCopyOffset.txt"),
Arguments.of("testFullMemoryRewrite.txt"),

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_fullMemoryRewrite_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testFullMemoryRewrite.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

// Memory Extension Tests

@Test
void testMCOPY_outOfBoundsMemoryExtension_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testOutOfBoundsMemoryExtension.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_singleByteMemoryExtension_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testSingleByteMemoryExtension.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_singleWordMemoryExtension_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testSingleWordMemoryExtension.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_singleWordMinusOneByteMemoryExtension_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testSingleWordMinusOneByteMemoryExtension.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));

}

@Test
void testMCOPY_singleWordPlusOneByteMemoryExtension_behaveAsExpected() throws FileNotFoundException, DslProcessorException {

DslParser parser = DslParser.fromResource("dsl/opcode/mcopy/testSingleWordPlusOneByteMemoryExtension.txt");
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
processor.processCommands(parser);

// Assertions

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b01", 1);
assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopy", true);

assertBlockExistsAndContainsExpectedNumberOfTxs(world, "b02", 1);
TransactionReceipt txTestMCopyOKCallReceipt = assertTxExistsWithExpectedReceiptStatus(world, "txTestMCopyOKCall", true);

// Event Assertions

Assertions.assertEquals(1, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "OK", null));
Assertions.assertEquals(0, TransactionReceiptUtil.getEventCount(txTestMCopyOKCallReceipt, "ERROR", null));
// Memory Extension Tests
Arguments.of("testOutOfBoundsMemoryExtension.txt"),
Arguments.of("testSingleByteMemoryExtension.txt"),
Arguments.of("testSingleWordMemoryExtension.txt"),
Arguments.of("testSingleWordMinusOneByteMemoryExtension.txt"),
Arguments.of("testSingleWordPlusOneByteMemoryExtension.txt")

);
}

private static void assertBlockExistsAndContainsExpectedNumberOfTxs(World world, String blockName, int expectedNumberOfTxs) {
Expand Down

0 comments on commit f53a6b0

Please sign in to comment.