Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ci/LDG-594-stabilize-fuzzer #10

Merged
merged 8 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: Unit testing with Codecov coverage checking

on:
workflow_dispatch:
push:
branches:
- master
- main
- develop
pull_request:
# push:
# branches:
# - master
# - main
# - develop
# pull_request:
GuilaneDen marked this conversation as resolved.
Show resolved Hide resolved

jobs:
job_unit_test:
Expand Down
7 changes: 6 additions & 1 deletion fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ project(FuzzTxParser

# guard against bad build-type strings
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
endif()

if (NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand Down Expand Up @@ -46,3 +46,8 @@ add_executable(fuzz_tx_parser fuzz_tx_parser.c)
target_compile_options(fuzz_tx_parser PUBLIC ${COMPILATION_FLAGS})
target_link_options(fuzz_tx_parser PUBLIC ${COMPILATION_FLAGS})
target_link_libraries(fuzz_tx_parser PUBLIC txparser)

add_compile_definitions(
APPNAME="Concordium"
FUZZ
)
32 changes: 21 additions & 11 deletions fuzzing/fuzz_tx_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
buffer_t buf = {.ptr = data, .size = size, .offset = 0};
transaction_t tx;
transaction_ctx_t tx;
parser_status_e status;
char nonce[21] = {0};
char address[21] = {0};
char amount[21] = {0};
char tx_memo[466] = {0};

memset(&tx, 0, sizeof(tx));

status = transaction_deserialize(&buf, &tx);
status = simple_transfer_deserialize(&buf, &tx);

if (status == PARSING_OK) {
format_u64(nonce, sizeof(nonce), tx.nonce);
printf("nonce: %s\n", nonce);
format_hex(tx.to, ADDRESS_LEN, address, sizeof(address));
printf("address: %s\n", address);
format_fpu64(amount, sizeof(amount), tx.value, 3); // exponent of smallest unit is 3
// Format recipient address
format_hex(tx.transaction.simple_transfer.recipient,
ADDRESS_LEN,
address,
sizeof(address));
printf("recipient: %s\n", address);

// Format sender address
format_hex(tx.transaction.simple_transfer.sender,
ADDRESS_LEN,
address,
sizeof(address));
printf("sender: %s\n", address);

// Format amount
format_fpu64(amount,
sizeof(amount),
tx.transaction.simple_transfer.value,
3); // exponent of smallest unit is 3
printf("amount: %s\n", amount);
transaction_utils_format_memo(tx.memo, tx.memo_len, tx_memo, sizeof(tx_memo));
printf("memo: %s\n", tx_memo);
}

return 0;
Expand Down
3 changes: 3 additions & 0 deletions fuzzing/mocks/ux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "ux.h"

ux_state_t dummy_ux_state;
10 changes: 10 additions & 0 deletions fuzzing/mocks/ux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

// Mock UX types and globals needed for fuzzing
typedef struct {
unsigned int dummy;
} ux_state_t;

#define G_ux_params (dummy_ux_state)

extern ux_state_t dummy_ux_state;
140 changes: 70 additions & 70 deletions unit-tests/test_tx_parser.c
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
// #include <stdarg.h>
// #include <stddef.h>
// #include <setjmp.h>
// #include <stdint.h>
// #include <stdbool.h>
// #include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

// #include <cmocka.h>
#include <cmocka.h>

// #include "transaction/serialize.h"
// #include "transaction/deserialize.h"
// #include "types.h"
#include "transaction/serialize.h"
#include "transaction/deserialize.h"
#include "types.h"

// static void test_tx_serialization(void **state) {
// (void) state;
static void test_tx_serialization(void **state) {
(void) state;

// transaction_t tx;
// // clang-format off
// uint8_t raw_tx[] = {
// // nonce (8)
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
// // to (20)
// 0x7a, 0xc3, 0x39, 0x97, 0x54, 0x4e, 0x31, 0x75,
// 0xd2, 0x66, 0xbd, 0x02, 0x24, 0x39, 0xb2, 0x2c,
// 0xdb, 0x16, 0x50, 0x8c,
// // value (8)
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x08, 0x07,
// // memo length (varint: 1-9)
// 0xf1,
// // memo (var: 241)
// 0x54, 0x68, 0x65, 0x20, 0x54, 0x68, 0x65, 0x6f,
// 0x72, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x47, 0x72,
// 0x6f, 0x75, 0x70, 0x73, 0x20, 0x69, 0x73, 0x20,
// 0x61, 0x20, 0x62, 0x72, 0x61, 0x6e, 0x63, 0x68,
// 0x20, 0x6f, 0x66, 0x20, 0x6d, 0x61, 0x74, 0x68,
// 0x65, 0x6d, 0x61, 0x74, 0x69, 0x63, 0x73, 0x20,
// 0x69, 0x6e, 0x20, 0x77, 0x68, 0x69, 0x63, 0x68,
// 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x64, 0x6f, 0x65,
// 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68,
// 0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x73,
// 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
// 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65,
// 0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72,
// 0x65, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
// 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x77, 0x69,
// 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
// 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x6f, 0x62,
// 0x74, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x20, 0x66,
// 0x72, 0x6f, 0x6d, 0x20, 0x64, 0x6f, 0x69, 0x6e,
// 0x67, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61,
// 0x6d, 0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67,
// 0x20, 0x74, 0x6f, 0x20, 0x73, 0x6f, 0x6d, 0x65,
// 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c,
// 0x73, 0x65, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x73,
// 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
// 0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x74, 0x6f,
// 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61, 0x6d,
// 0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x2e,
// 0x20, 0x4e, 0x65, 0x77, 0x6d, 0x61, 0x6e, 0x2c,
// 0x20, 0x4a, 0x61, 0x6d, 0x65, 0x73, 0x20, 0x52,
// 0x2e
// };
transaction_t tx;
// clang-format off
uint8_t raw_tx[] = {
// nonce (8)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
// to (20)
0x7a, 0xc3, 0x39, 0x97, 0x54, 0x4e, 0x31, 0x75,
0xd2, 0x66, 0xbd, 0x02, 0x24, 0x39, 0xb2, 0x2c,
0xdb, 0x16, 0x50, 0x8c,
// value (8)
0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x08, 0x07,
// memo length (varint: 1-9)
0xf1,
// memo (var: 241)
0x54, 0x68, 0x65, 0x20, 0x54, 0x68, 0x65, 0x6f,
0x72, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x47, 0x72,
0x6f, 0x75, 0x70, 0x73, 0x20, 0x69, 0x73, 0x20,
0x61, 0x20, 0x62, 0x72, 0x61, 0x6e, 0x63, 0x68,
0x20, 0x6f, 0x66, 0x20, 0x6d, 0x61, 0x74, 0x68,
0x65, 0x6d, 0x61, 0x74, 0x69, 0x63, 0x73, 0x20,
0x69, 0x6e, 0x20, 0x77, 0x68, 0x69, 0x63, 0x68,
0x20, 0x6f, 0x6e, 0x65, 0x20, 0x64, 0x6f, 0x65,
0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68,
0x69, 0x6e, 0x67, 0x20, 0x74, 0x6f, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65,
0x6e, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72,
0x65, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x77, 0x69,
0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72,
0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x6f, 0x62,
0x74, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x20, 0x66,
0x72, 0x6f, 0x6d, 0x20, 0x64, 0x6f, 0x69, 0x6e,
0x67, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61,
0x6d, 0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x74, 0x6f, 0x20, 0x73, 0x6f, 0x6d, 0x65,
0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x65, 0x6c,
0x73, 0x65, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69, 0x6e, 0x67,
0x20, 0x65, 0x6c, 0x73, 0x65, 0x20, 0x74, 0x6f,
0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x61, 0x6d,
0x65, 0x20, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x2e,
0x20, 0x4e, 0x65, 0x77, 0x6d, 0x61, 0x6e, 0x2c,
0x20, 0x4a, 0x61, 0x6d, 0x65, 0x73, 0x20, 0x52,
0x2e
};

// buffer_t buf = {.ptr = raw_tx, .size = sizeof(raw_tx), .offset = 0};
buffer_t buf = {.ptr = raw_tx, .size = sizeof(raw_tx), .offset = 0};

// parser_status_e status = transaction_deserialize(&buf, &tx);
parser_status_e status = transaction_deserialize(&buf, &tx);

// assert_int_equal(status, PARSING_OK);
assert_int_equal(status, PARSING_OK);

// uint8_t output[300];
// int length = transaction_serialize(&tx, output, sizeof(output));
// assert_int_equal(length, sizeof(raw_tx));
// assert_memory_equal(raw_tx, output, sizeof(raw_tx));
// }
uint8_t output[300];
int length = transaction_serialize(&tx, output, sizeof(output));
assert_int_equal(length, sizeof(raw_tx));
assert_memory_equal(raw_tx, output, sizeof(raw_tx));
}

// int main() {
// const struct CMUnitTest tests[] = {cmocka_unit_test(test_tx_serialization)};
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_tx_serialization)};

// return cmocka_run_group_tests(tests, NULL, NULL);
// }
return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading