Skip to content

Commit

Permalink
bundle: added test_bundle_finalize. (#9)
Browse files Browse the repository at this point in the history
* bundle: added test_bundle_finalize.

fixed null pointer dereferences in bundle.

* fixed code style.
  • Loading branch information
Sam Chen authored Jan 16, 2020
1 parent 7cce349 commit 9db9dd6
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 17 deletions.
3 changes: 2 additions & 1 deletion common/error_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ extern "C" {
*/
#define STR_OK "OK"
#define STR_ERROR "ERROR"
#define STR_NULL_PARAM "Attempting to access a null pointer"
#define STR_NULL_PARAM "The given parameter is NULL"
#define STR_NULL_POINTER "Accessing to a null pointer"
/** @} */

// Cclient Module
Expand Down
2 changes: 2 additions & 0 deletions common/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const char* error_2_string(retcode_t err) {
return STR_ERROR;
case RC_NULL_PARAM:
return STR_NULL_PARAM;
case RC_NULL_POINTER:
return STR_NULL_POINTER;

// Storage Module
case RC_STORAGE_FAILED_OPEN_DB:
Expand Down
1 change: 1 addition & 0 deletions common/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum retcode_t {
RC_INVALID_PARAM = 0x02 | RC_MODULE_GENERAL | RC_SEVERITY_MAJOR,
RC_OOM = 0x03 | RC_MODULE_GENERAL | RC_SEVERITY_MAJOR,
RC_STILL_RUNNING = 0x04 | RC_MODULE_GENERAL | RC_SEVERITY_MODERATE,
RC_NULL_POINTER = 0x05 | RC_MODULE_GENERAL | RC_SEVERITY_MAJOR,

// Storage Module
RC_STORAGE_FAILED_OPEN_DB = 0x01 | RC_MODULE_STORAGE | RC_SEVERITY_FATAL,
Expand Down
16 changes: 11 additions & 5 deletions common/model/bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,18 @@ iota_transaction_t *bundle_at(bundle_transactions_t *const bundle, size_t index)
return NULL;
}

void bundle_finalize(bundle_transactions_t *bundle, Kerl *const kerl) {
retcode_t bundle_finalize(bundle_transactions_t *bundle, Kerl *const kerl) {
iota_transaction_t *curr_tx = NULL;
bool valid_bundle = false;
iota_transaction_t *head_tx = NULL;
byte_t normalized_hash[HASH_LENGTH_TRYTE];
trit_t increased_tag_trits[NUM_TRITS_TAG];
flex_trit_t bundle_hash[FLEX_TRIT_SIZE_243];

head_tx = (iota_transaction_t *)utarray_front(bundle);
if ((head_tx = (iota_transaction_t *)utarray_front(bundle)) == NULL) {
return RC_NULL_POINTER;
}

flex_trits_to_trits(increased_tag_trits, NUM_TRITS_TAG, transaction_obsolete_tag(head_tx), NUM_TRITS_TAG,
NUM_TRITS_TAG);
while (!valid_bundle) {
Expand All @@ -161,6 +164,7 @@ void bundle_finalize(bundle_transactions_t *bundle, Kerl *const kerl) {
// update bundle_hash
BUNDLE_FOREACH(bundle, curr_tx) { transaction_set_bundle(curr_tx, bundle_hash); }
}
return RC_OK;
}

retcode_t bundle_validate(bundle_transactions_t *const bundle, bundle_status_t *const status) {
Expand All @@ -179,7 +183,11 @@ retcode_t bundle_validate(bundle_transactions_t *const bundle, bundle_status_t *
return RC_NULL_PARAM;
}

curr_tx = (iota_transaction_t *)utarray_eltptr(bundle, 0);
if ((curr_tx = (iota_transaction_t *)utarray_eltptr(bundle, 0)) == NULL) {
*status = BUNDLE_NOT_INITIALIZED;
return RC_NULL_POINTER;
}

last_index = transaction_last_index(curr_tx);

if (utarray_len(bundle) != last_index + 1) {
Expand Down Expand Up @@ -319,9 +327,7 @@ retcode_t bundle_sign(bundle_transactions_t *const bundle, flex_trit_t const *co
return RC_OK;
}

#ifdef DEBUG
void bundle_dump(bundle_transactions_t *bundle) {
iota_transaction_t *curr_tx = NULL;
BUNDLE_FOREACH(bundle, curr_tx) { transaction_obj_dump(curr_tx); }
}
#endif
5 changes: 2 additions & 3 deletions common/model/bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ void bundle_calculate_hash(bundle_transactions_t *bundle, Kerl *const kerl, flex
*
* @param[in] bundle A bundle object.
* @param[in] kerl A Kerl object.
* @return #retcode_t
*/
void bundle_finalize(bundle_transactions_t *bundle, Kerl *const kerl);
retcode_t bundle_finalize(bundle_transactions_t *bundle, Kerl *const kerl);

/**
* @brief Validates a bundle.
Expand Down Expand Up @@ -171,9 +172,7 @@ void bundle_set_messages(bundle_transactions_t *bundle, signature_fragments_t *m
retcode_t bundle_sign(bundle_transactions_t *const bundle, flex_trit_t const *const seed, inputs_t const *const inputs,
Kerl *const kerl);

#ifdef DEBUG
void bundle_dump(bundle_transactions_t *bundle);
#endif

/**
* @brief The bundle iterator.
Expand Down
1 change: 1 addition & 0 deletions common/model/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cc_test(
timeout = "short",
srcs = ["test_bundle.c"],
deps = [
":defs",
"//common/crypto/iss:normalize",
"//common/model:bundle",
"//common/trinary:flex_trit",
Expand Down
4 changes: 4 additions & 0 deletions common/model/tests/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ static tryte_t const* const TEST_BRANCH =
static tryte_t const* const TEST_NONCE = (tryte_t*)"BBBBDNQDJZGUQKOWJ9JZRCKAAAA";
static tryte_t const* const TEST_DATA_ADDRESS =
(tryte_t*)"9XV9RJGFJJZWITDPKSQXRTHCKJAIZZY9BYLBEQUXUNCLITRQDR9CCD99AANMXYEKD9GLJGVB9HIAGRIBQ";

static tryte_t const* const TEST_FINALIZED_BUNDLE_HASH =
(tryte_t*)"SH9PTSGXVZULPFFBNDXFKEDVBBQMY9RTHTDELDEYPWBYBQUEFWBBJRPOBWMYLGJE9YGTDFBEJFKNYRKKD";

static tryte_t const* const TEST_SIG_1 = (tryte_t *)
"99999999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999999999999999999999999999999999999999999999999999999999999"
Expand Down
40 changes: 40 additions & 0 deletions common/model/tests/test_bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* Refer to the LICENSE file for licensing information
*/

#include <inttypes.h>
#include <unity/unity.h>

#include "common/crypto/iss/normalize.h"
#include "common/model/bundle.h"
#include "common/model/tests/defs.h"
#include "common/trinary/flex_trit.h"
#include "common/trinary/tryte_ascii.h"

Expand Down Expand Up @@ -111,12 +113,50 @@ void test_bundle_transactions_message_short(void) { test_bundle_message(short_me

void test_bundle_transactions_message_long(void) { test_bundle_message(long_message); }

void test_bundle_finalize(void) {
bundle_transactions_t *bundle = NULL;
Kerl kerl;
bundle_transactions_new(&bundle);
kerl_init(&kerl);
flex_trit_t flex_bundle_hash[FLEX_TRIT_SIZE_243];
flex_trit_t flex_addr[FLEX_TRIT_SIZE_243];
flex_trit_t flex_tag[FLEX_TRIT_SIZE_81];
TEST_ASSERT(flex_trits_from_trytes(flex_bundle_hash, NUM_TRITS_HASH, TEST_FINALIZED_BUNDLE_HASH, NUM_TRYTES_HASH,
NUM_TRYTES_HASH));
TEST_ASSERT(
flex_trits_from_trytes(flex_addr, NUM_TRITS_HASH, TEST_ADDRESS_0, NUM_TRYTES_ADDRESS, NUM_TRYTES_ADDRESS));
TEST_ASSERT(flex_trits_from_trytes(flex_tag, NUM_TRITS_TAG, TEST_TAG, NUM_TRYTES_TAG, NUM_TRYTES_TAG));
TEST_ASSERT_NOT_NULL(bundle);

// create a transaction in a bundle
iota_transaction_t tx;
transaction_reset(&tx);
transaction_set_address(&tx, flex_addr);
transaction_set_tag(&tx, flex_tag);
transaction_set_timestamp(&tx, TEST_TIMESTAMP);
transaction_set_value(&tx, 0);
transaction_set_obsolete_tag(&tx, flex_tag);
bundle_transactions_add(bundle, &tx);

// clean up and transaction index reordering
bundle_reset_indexes(bundle);
TEST_ASSERT(bundle_finalize(bundle, &kerl) == RC_OK);

// validating the bundel hash
iota_transaction_t *tx_obj = bundle_at(bundle, 0);
TEST_ASSERT_NOT_NULL(tx_obj);
TEST_ASSERT_EQUAL_MEMORY(transaction_bundle(tx_obj), flex_bundle_hash, FLEX_TRIT_SIZE_243);

bundle_transactions_free(&bundle);
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_normalized_bundle);
RUN_TEST(test_bundle_transactions_message_long);
RUN_TEST(test_bundle_transactions_message_short);
RUN_TEST(test_bundle_finalize);

return UNITY_END();
}
8 changes: 2 additions & 6 deletions common/model/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
* Refer to the LICENSE file for licensing information
*/

#include <stdlib.h>
#include <string.h>
#ifdef DEBUG
#include <inttypes.h>
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>

#include "common/crypto/curl-p/digest.h"
#include "common/model/transaction.h"
Expand Down Expand Up @@ -297,7 +295,6 @@ size_t transaction_deserialize_from_trits(iota_transaction_t *const transaction,
// Free an existing transaction - compatible with free()
void transaction_free(iota_transaction_t *const transaction) { free(transaction); }

#ifdef DEBUG
void transaction_obj_dump(iota_transaction_t *tx_obj) {
field_mask_t old_mask = {};
memcpy(&old_mask, &tx_obj->loaded_columns_mask, sizeof(field_mask_t));
Expand Down Expand Up @@ -353,4 +350,3 @@ void transaction_obj_dump(iota_transaction_t *tx_obj) {

memcpy(&tx_obj->loaded_columns_mask, &old_mask, sizeof(field_mask_t));
}
#endif
2 changes: 0 additions & 2 deletions common/model/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,7 @@ static inline void transaction_set_arrival_timestamp(iota_transaction_t *const t
***********************************************************************************************************/

void transaction_reset(iota_transaction_t *const transaction);
#ifdef DEBUG
void transaction_obj_dump(iota_transaction_t *tx_obj);
#endif

uint8_t transaction_weight_magnitude(iota_transaction_t const *const transaction);

Expand Down

0 comments on commit 9db9dd6

Please sign in to comment.