-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* Copyright Cartesi and individual authors (see AUTHORS) | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include <assert.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
#include "rollup.h" | ||
|
||
void test_rollup_init_and_fini(void) | ||
{ | ||
cmt_rollup_t rollup; | ||
|
||
// init twice returns failure, same as when using the kernel driver | ||
assert(cmt_rollup_init(&rollup) == 0); | ||
assert(cmt_rollup_init(&rollup) == -EBUSY); | ||
cmt_rollup_fini(&rollup); | ||
|
||
// and should reset when closed | ||
assert(cmt_rollup_init(&rollup) == 0); | ||
cmt_rollup_fini(&rollup); | ||
|
||
// double free is a bug, but try to avoid crashing | ||
cmt_rollup_fini(&rollup); | ||
|
||
// fail to initialize with NULL | ||
assert(cmt_rollup_init(NULL) == -EINVAL); | ||
cmt_rollup_fini(NULL); | ||
|
||
printf("test_rollup_init_and_fini passed!\n"); | ||
} | ||
|
||
static void check_first_input(cmt_rollup_t *rollup) | ||
{ | ||
cmt_rollup_advance_t advance; | ||
|
||
assert(cmt_rollup_read_advance_state(rollup, &advance) == 0); | ||
assert(advance.chain_id == 1); | ||
// clang-format: off | ||
uint8_t expected_app_contract[CMT_ADDRESS_LENGTH] = { | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x02, | ||
}; | ||
uint8_t expected_msg_sender[CMT_ADDRESS_LENGTH] = { | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x03, | ||
}; | ||
char expected_payload[] = "advance-0"; | ||
// clang-format: on | ||
|
||
// verify the parsed data | ||
assert(memcmp(advance.app_contract, expected_app_contract, CMT_ADDRESS_LENGTH) == 0); | ||
assert(memcmp(advance.msg_sender, expected_msg_sender, CMT_ADDRESS_LENGTH) == 0); | ||
assert(advance.block_number == 4); | ||
assert(advance.block_timestamp == 5); | ||
assert(advance.index == 6); | ||
assert(advance.payload_length == strlen(expected_payload)); | ||
assert(memcmp(advance.payload, expected_payload, strlen(expected_payload)) == 0); | ||
} | ||
|
||
static void check_second_input(cmt_rollup_t *rollup) | ||
{ | ||
cmt_rollup_inspect_t inspect; | ||
|
||
char expected_payload[] = "inspect-0"; | ||
assert(cmt_rollup_read_inspect_state(rollup, &inspect) == 0); | ||
assert(inspect.payload_length == strlen(expected_payload)); | ||
assert(memcmp(inspect.payload, expected_payload, strlen(expected_payload)) == 0); | ||
} | ||
|
||
void test_parse_inputs(void) { | ||
cmt_rollup_t rollup; | ||
cmt_rollup_finish_t finish = { .accept_previous_request = true }; | ||
|
||
assert(cmt_rollup_init(&rollup) == 0); | ||
assert(cmt_rollup_finish(&rollup, &finish) == 0); | ||
check_first_input(&rollup); | ||
|
||
assert(cmt_rollup_finish(&rollup, &finish) == 0); | ||
check_second_input(&rollup); | ||
|
||
assert(cmt_rollup_finish(&rollup, &finish) == -ENODATA); | ||
|
||
cmt_rollup_fini(&rollup); | ||
} | ||
|
||
void emit_outputs_reports_and_exceptions(void) { | ||
cmt_rollup_t rollup; | ||
uint64_t index = 0; | ||
|
||
// clang-format: off | ||
uint8_t address[CMT_ADDRESS_LENGTH] = { | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x01}; | ||
// clang-format: on | ||
|
||
uint8_t value[] = {0xde, 0xad, 0xbe, 0xef}; | ||
char voucher_data[] = "voucher-0"; | ||
assert(cmt_rollup_init(&rollup) == 0); | ||
assert(cmt_rollup_emit_voucher(&rollup, | ||
sizeof address, address, | ||
sizeof value, value, | ||
strlen(voucher_data), voucher_data, &index) == 0); | ||
assert(index == 0); | ||
|
||
char notice_data[] = "notice-0"; | ||
assert(cmt_rollup_emit_notice(&rollup, | ||
strlen(notice_data), notice_data, &index) == 0); | ||
assert(index == 1); | ||
|
||
char report_data[] = "report-0"; | ||
assert(cmt_rollup_emit_report(&rollup, | ||
strlen(report_data), report_data) == 0); | ||
|
||
char exception_data[] = "exception-0"; | ||
assert(cmt_rollup_emit_exception(&rollup, | ||
strlen(exception_data), exception_data) == 0); | ||
cmt_rollup_fini(&rollup); | ||
} | ||
|
||
int main(void) | ||
{ | ||
test_rollup_init_and_fini(); | ||
test_parse_inputs(); | ||
|
||
// binaries must be compared to known good values | ||
emit_outputs_reports_and_exceptions(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
# create test data on the fly (must match the test) | ||
|
||
BIN=$1; shift | ||
|
||
[ ! -d build/data/ ] && mkdir build/data/ | ||
cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes)" \ | ||
0x0000000000000000000000000000000000000001 \ | ||
0x0000000000000000000000000000000000000002 \ | ||
0x0000000000000000000000000000000000000003 \ | ||
0x0000000000000000000000000000000000000004 \ | ||
0x0000000000000000000000000000000000000005 \ | ||
0x0000000000000000000000000000000000000006 \ | ||
0x`echo -en "advance-0" | xxd -p -c0` | xxd -r -p > build/data/valid-advance.bin | ||
|
||
dd if=/dev/zero of=build/data/large-advance.bin bs=1M count=4 | ||
|
||
echo -en "inspect-0" > build/data/valid-inspect.bin | ||
echo -en "report-0" > build/data/valid-report.bin | ||
echo -en "exception-0" > build/data/valid-exception.bin | ||
|
||
cast calldata "Voucher(address,uint256,bytes)" \ | ||
0x0000000000000000000000000000000000000001 \ | ||
0x00000000000000000000000000000000deadbeef \ | ||
0x`echo -en "voucher-0" | xxd -p -c0` | xxd -r -p > build/data/valid-voucher.bin | ||
|
||
cast calldata "Notice(bytes)" \ | ||
0x`echo -en "notice-0" | xxd -p -c0` | xxd -r -p > build/data/valid-notice.bin | ||
|
||
CMT_INPUTS=0:build/data/valid-advance.bin,1:build/data/valid-inspect.bin,0:build/data/large-advance.bin "$BIN" | ||
cmp build/data/valid-voucher.bin none.output-0.bin | ||
cmp build/data/valid-notice.bin none.output-1.bin | ||
cmp build/data/valid-exception.bin none.exception-0.bin | ||
cmp build/data/valid-report.bin none.report-0.bin |