modem-helpers: rework AT string quote operation and add unit tests

Rework the AT string quote operation to build the output with the
GString helper, instead of manually calculating how many bytes we'll
need in the output. It just makes it clearer.
This commit is contained in:
Aleksander Morgado
2024-03-12 10:15:59 +00:00
parent 311d6f389e
commit 07f1f58642
14 changed files with 80 additions and 48 deletions
+30
View File
@@ -4545,6 +4545,34 @@ test_bcd_to_string (void *f, gpointer d)
/*****************************************************************************/
typedef struct {
const gchar *input;
const gchar *expected;
} AtQuoteStringTest;
static const AtQuoteStringTest at_quote_string_tests[] = {
{ "", "\"\"" },
{ "internet", "\"internet\"" },
{ "\"internet", "\"\\22internet\"" }, /* double quote is \22 */
{ "\r\ninternet", "\"\\0D\\0Ainternet\"" }, /* CRLF is \0D\0A */
{ "\r\ninternet\r\n", "\"\\0D\\0Ainternet\\0D\\0A\"" }, /* CRLF is \0D\0A */
};
static void
test_at_quote_string (void *f, gpointer d)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (at_quote_string_tests); i++) {
g_autofree gchar *str = NULL;
str = mm_at_quote_string (at_quote_string_tests[i].input);
g_assert_cmpstr (str, ==, at_quote_string_tests[i].expected);
}
}
/*****************************************************************************/
typedef struct {
const gchar *response;
gboolean expected_error;
@@ -4855,6 +4883,8 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_bcd_to_string, NULL));
g_test_suite_add (suite, TESTCASE (test_at_quote_string, NULL));
g_test_suite_add (suite, TESTCASE (test_cpol_response, NULL));
result = g_test_run ();