diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2021-05-16 22:03:10 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2021-05-22 22:58:37 +0000 |
commit | d1bd0a297103b426e3ec3afe310f1471c6c6dbae (patch) | |
tree | 65110451a05a2187751dad85a9cfc66802318723 /src/tests | |
parent | 9fdbbc6929cb61d4168e1696a94e5b2f07bc578e (diff) |
core: improve error helpers
First, simplify the logic that attempts to find the correct error code
associated to an error text string. This logic is very flaky, as it
relies on knowing what the modems report as text string for the
different error codes, and obviously that is not always the same.
Instead of supporting all error codes in this logic, we now limit them
to a small subset with common error codes and hopefully common error
strings.
Second, in order to quickly get the error description string for a
given error code, we rework the array of supported errors so that
the index of the array is the error code itself. For mobile equipment
errors the logic is straightforward, as they're always in the [0,255]
range, but for the message errors we create an array where the index
of the array is equal to the code plus 300 as they're always in the
[300,500] range. By doing this, we avoid having an array with 300 NULL
items before the actual values we want to support.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test-error-helpers.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/tests/test-error-helpers.c b/src/tests/test-error-helpers.c index ddff9a0d..ef4504ed 100644 --- a/src/tests/test-error-helpers.c +++ b/src/tests/test-error-helpers.c @@ -52,6 +52,85 @@ TEST_ERROR_HELPER (MESSAGE_ERROR, message_error, MessageError) /*****************************************************************************/ +static void +test_error_helpers_mobile_equipment_error_for_code (void) +{ + GError *error = NULL; + + /* first */ + error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_PHONE_FAILURE, NULL); + g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_PHONE_FAILURE); + g_clear_error (&error); + + /* last */ + error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_GPRS_REQUEST_REJECTED_BCM_VIOLATION, NULL); + g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_GPRS_REQUEST_REJECTED_BCM_VIOLATION); + g_clear_error (&error); + + /* other > 255 */ + error = mm_mobile_equipment_error_for_code (256, NULL); + g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_UNKNOWN); + g_clear_error (&error); +} + +static void +test_error_helpers_message_error_for_code (void) +{ + GError *error = NULL; + + /* first */ + error = mm_message_error_for_code (MM_MESSAGE_ERROR_ME_FAILURE, NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_ME_FAILURE); + g_clear_error (&error); + + /* last */ + error = mm_message_error_for_code (MM_MESSAGE_ERROR_UNKNOWN, NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN); + g_clear_error (&error); + + /* other < 300 */ + error = mm_message_error_for_code (299, NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN); + g_clear_error (&error); + + /* other > 500 */ + error = mm_message_error_for_code (501, NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN); + g_clear_error (&error); +} + +/*****************************************************************************/ + +static void +test_error_helpers_mobile_equipment_error_for_string (void) +{ + GError *error = NULL; + + error = mm_mobile_equipment_error_for_string ("operationnotallowed", NULL); + g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_NOT_ALLOWED); + g_clear_error (&error); + + error = mm_mobile_equipment_error_for_string ("arratsaldeon", NULL); + g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_UNKNOWN); + g_clear_error (&error); +} + +static void +test_error_helpers_message_error_for_string (void) +{ + GError *error = NULL; + + error = mm_message_error_for_string ("operationnotallowed", NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_NOT_ALLOWED); + g_clear_error (&error); + + error = mm_message_error_for_string ("arratsaldeon", NULL); + g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN); + g_clear_error (&error); +} + +/*****************************************************************************/ + int main (int argc, char **argv) { setlocale (LC_ALL, ""); @@ -62,5 +141,11 @@ int main (int argc, char **argv) g_test_add_func ("/MM/error-helpers/mobile-equipment-error", test_error_helpers_mobile_equipment_error); g_test_add_func ("/MM/error-helpers/message-error", test_error_helpers_message_error); + g_test_add_func ("/MM/error-helpers/mobile-equipment-error/for-code", test_error_helpers_mobile_equipment_error_for_code); + g_test_add_func ("/MM/error-helpers/message-error/for-code", test_error_helpers_message_error_for_code); + + g_test_add_func ("/MM/error-helpers/mobile-equipment-error/for-string", test_error_helpers_mobile_equipment_error_for_string); + g_test_add_func ("/MM/error-helpers/message-error/for-string", test_error_helpers_message_error_for_string); + return g_test_run (); } |