core,error-helpers: new method to normalize GErrors

The ModemManager API should not expose or return error types out of
the error domains it supports.

Default to a generic core error "Failed", while keeping the original
error description.
This commit is contained in:
Aleksander Morgado
2023-10-26 12:35:50 +00:00
parent e0bcf0a3c4
commit 59aae421a1
3 changed files with 264 additions and 26 deletions
+84
View File
@@ -13,6 +13,8 @@
* Copyright (C) 2019 Aleksander Morgado <aleksander@aleksander.es>
*/
#include <config.h>
#include <glib.h>
#include <glib-object.h>
#include <string.h>
@@ -24,6 +26,14 @@
#include "mm-error-helpers.h"
#include "mm-log.h"
#if defined WITH_QMI
# include <libqmi-glib.h>
#endif
#if defined WITH_MBIM
# include <libmbim-glib.h>
#endif
#define TEST_ERROR_HELPER(ERROR_CAPS,ERROR_SMALL,ERROR_CAMEL) \
static void \
test_error_helpers_## ERROR_SMALL (void) \
@@ -131,6 +141,78 @@ test_error_helpers_message_error_for_string (void)
/*****************************************************************************/
static void
normalize_and_assert_mm_error (gboolean expect_fallback,
GQuark domain,
int code,
const gchar *description)
{
g_autoptr(GError) input_error = NULL;
g_autoptr(GError) normalized_error = NULL;
g_debug ("Checking normalized error: %s", description);
input_error = g_error_new_literal (domain, code, description);
normalized_error = mm_normalize_error (input_error);
g_assert (normalized_error);
g_assert ((normalized_error->domain == MM_CORE_ERROR) ||
(normalized_error->domain == MM_MOBILE_EQUIPMENT_ERROR) ||
(normalized_error->domain == MM_CONNECTION_ERROR) ||
(normalized_error->domain == MM_SERIAL_ERROR) ||
(normalized_error->domain == MM_MESSAGE_ERROR) ||
(normalized_error->domain == MM_CDMA_ACTIVATION_ERROR) ||
g_error_matches (normalized_error, G_IO_ERROR, G_IO_ERROR_CANCELLED));
g_assert (expect_fallback == g_error_matches (normalized_error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED));
}
static void
test_error_helpers_normalize (void)
{
normalize_and_assert_mm_error (TRUE, G_IO_ERROR, G_IO_ERROR_FAILED, "GIO error");
normalize_and_assert_mm_error (FALSE, G_IO_ERROR, G_IO_ERROR_CANCELLED, "GIO error cancelled");
normalize_and_assert_mm_error (TRUE, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Known core error"); /* This is exactly the fallback */
normalize_and_assert_mm_error (TRUE, MM_CORE_ERROR, 123456, "Unknown core error");
normalize_and_assert_mm_error (FALSE, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_NO_NETWORK, "Known ME error");
normalize_and_assert_mm_error (TRUE, MM_MOBILE_EQUIPMENT_ERROR, 123456789, "Unknown ME error");
normalize_and_assert_mm_error (FALSE, MM_CONNECTION_ERROR, MM_CONNECTION_ERROR_NO_CARRIER, "Known connection error");
normalize_and_assert_mm_error (TRUE, MM_CONNECTION_ERROR, 123456, "Unknown connection error");
normalize_and_assert_mm_error (FALSE, MM_SERIAL_ERROR, MM_SERIAL_ERROR_OPEN_FAILED, "Known serial error");
normalize_and_assert_mm_error (TRUE, MM_SERIAL_ERROR, 123456, "Unknown serial error");
normalize_and_assert_mm_error (FALSE, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_ME_FAILURE, "Known message error");
normalize_and_assert_mm_error (TRUE, MM_MESSAGE_ERROR, 123456, "Unknown message error");
normalize_and_assert_mm_error (FALSE, MM_CDMA_ACTIVATION_ERROR, MM_CDMA_ACTIVATION_ERROR_ROAMING, "Known CDMA activation error");
normalize_and_assert_mm_error (TRUE, MM_CDMA_ACTIVATION_ERROR, 123456, "Unknown CDMA activation error");
#if defined WITH_QMI
normalize_and_assert_mm_error (TRUE, QMI_CORE_ERROR, QMI_CORE_ERROR_TIMEOUT, "Known QMI core error");
normalize_and_assert_mm_error (TRUE, QMI_CORE_ERROR, 123456, "Unknown QMI core error");
normalize_and_assert_mm_error (TRUE, QMI_PROTOCOL_ERROR, QMI_PROTOCOL_ERROR_ABORTED, "Known QMI protocol error");
normalize_and_assert_mm_error (TRUE, QMI_PROTOCOL_ERROR, 123456, "Unknown QMI protocol error");
#endif
#if defined WITH_MBIM
normalize_and_assert_mm_error (TRUE, MBIM_CORE_ERROR, MBIM_CORE_ERROR_ABORTED, "Known MBIM core error");
normalize_and_assert_mm_error (TRUE, MBIM_CORE_ERROR, 123456, "Unknown MBIM core error");
normalize_and_assert_mm_error (TRUE, MBIM_PROTOCOL_ERROR, MBIM_PROTOCOL_ERROR_CANCEL, "Known MBIM protocol error");
normalize_and_assert_mm_error (TRUE, MBIM_PROTOCOL_ERROR, 123456, "Unknown MBIM protocol error");
normalize_and_assert_mm_error (TRUE, MBIM_STATUS_ERROR, MBIM_STATUS_ERROR_BUSY, "Known MBIM status error");
normalize_and_assert_mm_error (TRUE, MBIM_STATUS_ERROR, 123456, "Unknown MBIM status error");
#endif
}
/*****************************************************************************/
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
@@ -147,5 +229,7 @@ int main (int argc, char **argv)
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);
g_test_add_func ("/MM/error-helpers/normalize", test_error_helpers_normalize);
return g_test_run ();
}