1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2019 Aleksander Morgado <aleksander@aleksander.es>
*/
#include <config.h>
#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#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) \
{ \
GError *error; \
gint i; \
GEnumClass *enum_class; \
\
enum_class = g_type_class_ref (MM_TYPE_ ## ERROR_CAPS); \
for (i = enum_class->minimum; i <= enum_class->maximum; i++) { \
GEnumValue *enum_value; \
\
enum_value = g_enum_get_value (enum_class, i); \
if (enum_value) { \
error = mm_## ERROR_SMALL ## _for_code ((MM##ERROR_CAMEL)i, NULL); \
g_assert_error (error, MM_ ## ERROR_CAPS, i); \
g_error_free (error); \
} \
} \
g_type_class_unref (enum_class); \
}
TEST_ERROR_HELPER (CONNECTION_ERROR, connection_error, ConnectionError)
TEST_ERROR_HELPER (MOBILE_EQUIPMENT_ERROR, mobile_equipment_error, MobileEquipmentError)
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_WIRELINE_ACCESS_AREA_NOT_ALLOWED, NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_WIRELINE_ACCESS_AREA_NOT_ALLOWED);
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);
}
/*****************************************************************************/
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 (FALSE, 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 (FALSE, 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 (FALSE, 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 (FALSE, 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 (FALSE, 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, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/error-helpers/connection-error", test_error_helpers_connection_error);
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);
g_test_add_func ("/MM/error-helpers/normalize", test_error_helpers_normalize);
return g_test_run ();
}
|