Skip to content

Commit

Permalink
fix: libcmt tests and missing declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
vfusco committed Apr 16, 2024
1 parent 32e60bf commit 07a86a0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
3 changes: 3 additions & 0 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ int cmt_io_init(cmt_io_driver_t *_me) {
}
cmt_io_driver_mock_t *me = &_me->mock;

if (me->tx->begin || me->rx->begin) {
return -EBUSY;
}
size_t tx_length = 2U << 20; // 2MB
size_t rx_length = 2U << 20; // 2MB
cmt_buf_init(me->tx, tx_length, malloc(tx_length));
Expand Down
14 changes: 13 additions & 1 deletion sys-utils/libcmt/src/rollup.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ typedef struct cmt_gio {
* |< 0| failure with a -errno value | */
int cmt_rollup_init(cmt_rollup_t *me);

/** Finalize a @ref cmt_rollup_t statate previously initialized with @ref
/** Finalize a @ref cmt_rollup_t state previously initialized with @ref
* cmt_rollup_init
*
* @param [in] me initialized state
Expand Down Expand Up @@ -154,6 +154,18 @@ int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t data_length, const void *d
* |< 0| failure with a -errno value | */
int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t data_length, const void *data);

/** Report progress
*
* @param [in,out] me initialized cmt_rollup_t instance
* @param [in] progress progress value to be set
*
* @return
* | | |
* |--:|-----------------------------|
* | 0| success |
* |< 0| failure with a -errno value | */
int cmt_rollup_progress(cmt_rollup_t *me, uint32_t progress);

/** Read advance state
*
* @param [in,out] me initialized cmt_rollup_t instance
Expand Down
79 changes: 32 additions & 47 deletions sys-utils/libcmt/tests/keccak.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,48 @@
#include "abi.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

static void inits(void) {
uint8_t md[3][CMT_KECCAK_LENGTH];
uint8_t data[] = {
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0a,
0x0b,
0x0c,
0x0d,
0x0e,
0x0f,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16,
0x17,
0x18,
0x19,
0x1a,
0x1b,
0x1c,
0x1d,
0x1e,
0x1f,
};
void test_cmt_keccak_init(void) {
cmt_keccak_t state;
cmt_keccak_init(&state);
printf("Test cmt_keccak_init: Passed\n");
}

void test_cmt_keccak_hash_operations(void) {
const char *input = "The quick brown fox jumps over the lazy dog";
uint8_t result[CMT_KECCAK_LENGTH] = {0};
uint8_t expected[CMT_KECCAK_LENGTH] = {0x4d, 0x74, 0x1b, 0x6f, 0x1e, 0xb2, 0x9c, 0xb2, 0xa9, 0xb9, 0x91, 0x1c, 0x82,
0xf5, 0x6f, 0xa8, 0xd7, 0x3b, 0x04, 0x95, 0x9d, 0x3d, 0x9d, 0x22, 0x28, 0x95, 0xdf, 0x6c, 0x0b, 0x28, 0xaa,
0x15};

cmt_keccak_t state;
cmt_keccak_init(&state);
cmt_keccak_update(&state, strlen(input), input);
cmt_keccak_final(&state, result);

// Compare result with expected
assert(memcmp(result, expected, CMT_KECCAK_LENGTH) == 0);
printf("Test cmt_keccak_update and cmt_keccak_final: Passed\n");

// from init
cmt_keccak_t t[1];
cmt_keccak_init(t);
cmt_keccak_update(t, sizeof(data), data);
cmt_keccak_final(t, md[0]);
cmt_keccak_data(strlen(input), input, result);

// from data
cmt_keccak_data(sizeof(data), data, md[1]);
assert(memcmp(md[0], md[1], CMT_KECCAK_LENGTH) == 0);
// Compare result with expected
assert(memcmp(result, expected, CMT_KECCAK_LENGTH) == 0);
printf("Test cmt_keccak_data: Passed\n");
}

static void funsel(void) {
void test_cmt_keccak_funsel(void) {
const char s[] = "baz(uint32,bool)";
assert(cmt_keccak_funsel(s) == CMT_ABI_FUNSEL(0xcd, 0xcd, 0x77, 0xc0));
printf("Test cmt_keccak_funsel: Passed\n");
}

int main(void) {
funsel();
inits();
test_cmt_keccak_init();
test_cmt_keccak_hash_operations();
test_cmt_keccak_funsel();
printf("All keccak tests passed!\n");
return 0;
}
2 changes: 1 addition & 1 deletion sys-utils/libcmt/tests/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@ int main(void) {
test_cmt_merkle_push_back();
test_cmt_merkle_push_back_data();
test_cmt_merkle_save_load();
printf("All tests passed!\n");
printf("All merkle tests passed!\n");
return 0;
}

0 comments on commit 07a86a0

Please sign in to comment.