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

at_c: use assertion library in tests and arguments validation #344

Closed
16 tasks
JeremyTubongbanua opened this issue Jul 18, 2024 · 1 comment
Closed
16 tasks
Assignees

Comments

@JeremyTubongbanua
Copy link
Member

JeremyTubongbanua commented Jul 18, 2024

Currently, our unit/functional tests use if statements to check certain conditions after an operation has been completed. For example:

if ((ret = atclient_atkey_from_string(&atkey, atkeystr)) != 0) {
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_from_string failed\n");
  goto exit;
}
if (atkey.metadata.iscached != true) {
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.iscached is not 1\n");
  goto exit;
}
if (atkey.metadata.ispublic != true) {
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey.metadata.ispublic is not 1, it is %d\n",
               atkey.metadata.ispublic);
  goto exit;
}

Proposed Implementation:

// function implementation
static int assert_equals_bool(const char *name, const bool actual, const bool expected) [
    if(actual == expected) {
        atlogger_log(TAG, INFO, "%s is %d as expected.\n", name, actual);
        return 0;
    } else {
        atlogger_log(TAG, ERROR, "%s id %d when it was expected to be %d.\n", name, actual, expected);
        return 1;
    }
} 


// example usage
if((ret = assert_equals_bool(atkey.metadata.iscached, true)) != 0) {
    exit(1);
}

We would need an atassert library to hold our assert functions. The headers would look something like this:

int assert_equals_bool(const bool actual, const bool expected);
int assert_true(const bool actual);
int assert_false(const bool actual);
int assert_equals_char(const char *actual, const char *expected);
int assert_equals_unsigned_char(const char *actual, const char *expected);
int assert_equals_int(const int actual, const int expected);
int assert_equals_double(const double actual, const double expected);
int assert_equals_long(const long actual, const long expected);
int assert_equals_unsigned_long(const unsigned long actual, const unsigned long expected);
int assert_null(void *object);
int assert_non_null(void *object);

Not only would we have to use the assertion library in our unit/functional tests, but we should also use them in the "Arguments Validation" of each function.

For example:

This code currently:

if (atclient == NULL) {
  ret = 1;
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n");
  goto exit;
}
if (!atclient_is_atsign_initialized(atclient)) {
  ret = 1;
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver_connection is not connected\n");
  goto exit;
}

would transform to this:

if ((ret = assert_non_null(atclient)) != 0) {
  ret = 1;
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient is NULL\n");
  goto exit;
}
if ((ret = assert_true(atclient_is_atserver_connection_started(atclient)) != 0) {
  ret = 1;
  atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atserver_connection is not connected\n");
  goto exit;
}

Some refactors:

  • Rename to test_aes_ctr_decrypt.c
  • Rename to test_aes_ctr.c
  • Rename to test_rsa_decrypt.c
  • Rename to test_rsa_encrypt.c
  • Rename to test_rsa_private_key_populate.c
  • Rename to test_rsa_public_key_populate.c
  • Rename to test_rsa_sign.c
  • Rename to test_rsa_verify.c
  • Rename to test_string_utils.c

Todos:

  • Create a atassert package which contains our assertion function
  • atassert.h
  • atassert.c
  • Refactor atclient unit tests to use assertion library
  • Refactor atchops unit tests to use assertion library
  • Refactor functional tests to use assertion library

Todos Part 2:

  • Use this assertion library in our arguments validation
@JeremyTubongbanua JeremyTubongbanua self-assigned this Jul 22, 2024
@JeremyTubongbanua JeremyTubongbanua changed the title at_c: use assertion library instead of if statements in our unit tests at_c: use assertion library instead of if statements in our unit tests and when validating arguments of functions Jul 22, 2024
@JeremyTubongbanua JeremyTubongbanua changed the title at_c: use assertion library instead of if statements in our unit tests and when validating arguments of functions at_c: use assertion library in tests Jul 30, 2024
@JeremyTubongbanua JeremyTubongbanua changed the title at_c: use assertion library in tests at_c: use assertion library in tests and arguments validation Jul 30, 2024
@JeremyTubongbanua
Copy link
Member Author

I am going to close this and write up a new ticket on using Unity as our unit testing framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant