| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details: |
| 12 |
* |
| 13 |
* Copyright (C) 2012-2016 Aleksander Morgado <[email protected]> |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <string.h> |
| 17 |
#include <glib.h> |
| 18 |
#include "qmicli-helpers.h" |
| 19 |
|
| 20 |
/******************************************************************************/ |
| 21 |
|
| 22 |
static void |
| 23 |
test_helpers_raw_printable_1 (void) |
| 24 |
{ |
| 25 |
GArray *array; |
| 26 |
gchar *printable; |
| 27 |
static guint8 buffer[8] = { |
| 28 |
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 |
| 29 |
}; |
| 30 |
static const gchar *expected = |
| 31 |
"0F:\n" |
| 32 |
"50:\n" |
| 33 |
"EB:\n" |
| 34 |
"E2:\n" |
| 35 |
"B6:\n" |
| 36 |
"00:\n" |
| 37 |
"00:\n" |
| 38 |
"00\n"; |
| 39 |
|
| 40 |
array = g_array_sized_new (FALSE, FALSE, 1, 8); |
| 41 |
g_array_insert_vals (array, 0, buffer, 8); |
| 42 |
|
| 43 |
printable = qmicli_get_raw_data_printable (array, 3, ""); |
| 44 |
|
| 45 |
g_assert_cmpstr (printable, ==, expected); |
| 46 |
g_free (printable); |
| 47 |
g_array_unref (array); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
test_helpers_raw_printable_2 (void) |
| 52 |
{ |
| 53 |
GArray *array; |
| 54 |
gchar *printable; |
| 55 |
static guint8 buffer[8] = { |
| 56 |
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 |
| 57 |
}; |
| 58 |
static const gchar *expected = |
| 59 |
"\t0F:50:\n" |
| 60 |
"\tEB:E2:\n" |
| 61 |
"\tB6:00:\n" |
| 62 |
"\t00:00\n"; |
| 63 |
|
| 64 |
array = g_array_sized_new (FALSE, FALSE, 1, 8); |
| 65 |
g_array_insert_vals (array, 0, buffer, 8); |
| 66 |
|
| 67 |
/* When passing 7, we'll be really getting 6 (the closest lower multiple of 3) */ |
| 68 |
printable = qmicli_get_raw_data_printable (array, 7, "\t"); |
| 69 |
|
| 70 |
g_assert_cmpstr (printable, ==, expected); |
| 71 |
g_free (printable); |
| 72 |
g_array_unref (array); |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
test_helpers_raw_printable_3 (void) |
| 77 |
{ |
| 78 |
GArray *array; |
| 79 |
gchar *printable; |
| 80 |
static guint8 buffer[8] = { |
| 81 |
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 |
| 82 |
}; |
| 83 |
static const gchar *expected = |
| 84 |
"\t\t\t0F:50:EB:E2:\n" |
| 85 |
"\t\t\tB6:00:00:00\n"; |
| 86 |
|
| 87 |
array = g_array_sized_new (FALSE, FALSE, 1, 8); |
| 88 |
g_array_insert_vals (array, 0, buffer, 8); |
| 89 |
|
| 90 |
printable = qmicli_get_raw_data_printable (array, 12, "\t\t\t"); |
| 91 |
|
| 92 |
g_assert_cmpstr (printable, ==, expected); |
| 93 |
g_free (printable); |
| 94 |
g_array_unref (array); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
test_helpers_raw_printable_4 (void) |
| 99 |
{ |
| 100 |
GArray *array; |
| 101 |
gchar *printable; |
| 102 |
static guint8 buffer[8] = { |
| 103 |
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 |
| 104 |
}; |
| 105 |
static const gchar *expected = |
| 106 |
"\t0F:50:EB:E2:B6:00:00:00\n"; |
| 107 |
|
| 108 |
array = g_array_sized_new (FALSE, FALSE, 1, 8); |
| 109 |
g_array_insert_vals (array, 0, buffer, 8); |
| 110 |
|
| 111 |
printable = qmicli_get_raw_data_printable (array, 24, "\t"); |
| 112 |
|
| 113 |
g_assert_cmpstr (printable, ==, expected); |
| 114 |
g_free (printable); |
| 115 |
g_array_unref (array); |
| 116 |
} |
| 117 |
|
| 118 |
/******************************************************************************/ |
| 119 |
|
| 120 |
static void |
| 121 |
test_helpers_binary_array_from_string_0 (void) |
| 122 |
{ |
| 123 |
const guint8 expected[] = { |
| 124 |
0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0xAB, 0xCD, 0xEF |
| 125 |
}; |
| 126 |
const gchar *str = "12:34:56:78:9A:BC:DE:F0:ab:cd:ef"; |
| 127 |
GArray *out = NULL; |
| 128 |
|
| 129 |
g_assert (qmicli_read_binary_array_from_string (str, &out)); |
| 130 |
g_assert (out != NULL); |
| 131 |
g_assert_cmpmem ((guint8 *)(out->data), out->len, expected, G_N_ELEMENTS (expected)); |
| 132 |
g_array_unref (out); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
test_helpers_binary_array_from_string_1 (void) |
| 137 |
{ |
| 138 |
const guint8 expected[] = { |
| 139 |
0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0xAB, 0xCD, 0xEF |
| 140 |
}; |
| 141 |
const gchar *str = "123456789ABCDEF0abcdef"; |
| 142 |
GArray *out = NULL; |
| 143 |
|
| 144 |
g_assert (qmicli_read_binary_array_from_string (str, &out)); |
| 145 |
g_assert (out != NULL); |
| 146 |
g_assert_cmpmem ((guint8 *)(out->data), out->len, expected, G_N_ELEMENTS (expected)); |
| 147 |
g_array_unref (out); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
test_helpers_binary_array_from_string_2 (void) |
| 152 |
{ |
| 153 |
const gchar *str = ""; |
| 154 |
GArray *out = NULL; |
| 155 |
|
| 156 |
g_assert (qmicli_read_binary_array_from_string (str, &out)); |
| 157 |
g_assert (out != NULL); |
| 158 |
g_assert_cmpuint (out->len, ==, 0); |
| 159 |
g_array_unref (out); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
test_helpers_binary_array_from_string_3 (void) |
| 164 |
{ |
| 165 |
const gchar *str = "hello"; |
| 166 |
GArray *out = NULL; |
| 167 |
|
| 168 |
g_assert (qmicli_read_binary_array_from_string (str, &out) == FALSE); |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
test_helpers_binary_array_from_string_4 (void) |
| 173 |
{ |
| 174 |
const gchar *str = "a"; |
| 175 |
GArray *out = NULL; |
| 176 |
|
| 177 |
g_assert (qmicli_read_binary_array_from_string (str, &out) == FALSE); |
| 178 |
} |
| 179 |
|
| 180 |
/******************************************************************************/ |
| 181 |
|
| 182 |
#if defined HAVE_QMI_MESSAGE_WMS_GET_BROADCAST_CONFIG |
| 183 |
|
| 184 |
static void |
| 185 |
test_helpers_cbs_channels_from_string (void) |
| 186 |
{ |
| 187 |
g_autoptr (GArray) channels = NULL; |
| 188 |
QmiMessageWmsSetBroadcastConfigInputChannelsElement ch; |
| 189 |
gboolean ret = FALSE; |
| 190 |
|
| 191 |
ret = qmicli_read_cbs_channels_from_string ("asdf", &channels); |
| 192 |
g_assert_false (ret); |
| 193 |
g_assert_null (channels); |
| 194 |
|
| 195 |
ret = qmicli_read_cbs_channels_from_string ("4370", &channels); |
| 196 |
g_assert_true (ret); |
| 197 |
g_assert_cmpuint (channels->len, ==, 1); |
| 198 |
ch = g_array_index (channels, QmiMessageWmsSetBroadcastConfigInputChannelsElement, 0); |
| 199 |
g_assert_cmpuint (ch.start, ==, 4370); |
| 200 |
g_assert_cmpuint (ch.end, ==, 4370); |
| 201 |
g_clear_pointer (&channels, g_array_unref); |
| 202 |
|
| 203 |
ret = qmicli_read_cbs_channels_from_string ("4370,4371-abc", &channels); |
| 204 |
g_assert_false (ret); |
| 205 |
g_assert_null (channels); |
| 206 |
|
| 207 |
ret = qmicli_read_cbs_channels_from_string ("4370,,4371-5000", &channels); |
| 208 |
g_assert_false (ret); |
| 209 |
g_assert_null (channels); |
| 210 |
|
| 211 |
ret = qmicli_read_cbs_channels_from_string ("4370,,-5000", &channels); |
| 212 |
g_assert_false (ret); |
| 213 |
g_assert_null (channels); |
| 214 |
|
| 215 |
ret = qmicli_read_cbs_channels_from_string ("65537", &channels); |
| 216 |
g_assert_false (ret); |
| 217 |
g_assert_null (channels); |
| 218 |
|
| 219 |
ret = qmicli_read_cbs_channels_from_string ("4370,4371-5000", &channels); |
| 220 |
g_assert_true (ret); |
| 221 |
g_assert_cmpuint (channels->len, ==, 2); |
| 222 |
ch = g_array_index (channels, QmiMessageWmsSetBroadcastConfigInputChannelsElement, 0); |
| 223 |
g_assert_cmpuint (ch.start, ==, 4370); |
| 224 |
g_assert_cmpuint (ch.end, ==, 4370); |
| 225 |
ch = g_array_index (channels, QmiMessageWmsSetBroadcastConfigInputChannelsElement, 1); |
| 226 |
g_assert_cmpuint (ch.start, ==, 4371); |
| 227 |
g_assert_cmpuint (ch.end, ==, 5000); |
| 228 |
g_clear_pointer (&channels, g_array_unref); |
| 229 |
} |
| 230 |
|
| 231 |
#endif |
| 232 |
|
| 233 |
/******************************************************************************/ |
| 234 |
|
| 235 |
static void |
| 236 |
test_helpers_supported_messages_list (void) |
| 237 |
{ |
| 238 |
const guint8 bytearray[] = { |
| 239 |
0x03, 0x00, 0x00, 0xC0 |
| 240 |
}; |
| 241 |
const gchar *expected_str = |
| 242 |
"\t0x0000\n" /* 0 dec */ |
| 243 |
"\t0x0001\n" /* 1 dec */ |
| 244 |
"\t0x001E\n" /* 30 dec */ |
| 245 |
"\t0x001F\n"; /* 31 dec */ |
| 246 |
gchar *str; |
| 247 |
|
| 248 |
str = qmicli_get_supported_messages_list (bytearray, G_N_ELEMENTS (bytearray)); |
| 249 |
g_assert (str); |
| 250 |
g_assert_cmpstr (str, ==, expected_str); |
| 251 |
g_free (str); |
| 252 |
} |
| 253 |
|
| 254 |
static void |
| 255 |
test_helpers_supported_messages_list_none (void) |
| 256 |
{ |
| 257 |
const gchar *expected_str = "\tnone\n"; |
| 258 |
gchar *str; |
| 259 |
|
| 260 |
str = qmicli_get_supported_messages_list (NULL, 0); |
| 261 |
g_assert (str); |
| 262 |
g_assert_cmpstr (str, ==, expected_str); |
| 263 |
g_free (str); |
| 264 |
} |
| 265 |
|
| 266 |
/******************************************************************************/ |
| 267 |
|
| 268 |
typedef struct { |
| 269 |
const gchar *key; |
| 270 |
const gchar *value; |
| 271 |
gboolean found; |
| 272 |
} KeyValue; |
| 273 |
|
| 274 |
static const KeyValue test_key_values[] = { |
| 275 |
{ "key1", "", FALSE }, |
| 276 |
{ "key2", "value", FALSE }, |
| 277 |
{ "key3", "1234", FALSE }, |
| 278 |
{ "key4", "value1234", FALSE }, |
| 279 |
}; |
| 280 |
|
| 281 |
static gboolean |
| 282 |
key_value_callback (const gchar *key, |
| 283 |
const gchar *value, |
| 284 |
GError **error, |
| 285 |
gboolean *found) |
| 286 |
{ |
| 287 |
guint i; |
| 288 |
|
| 289 |
for (i = 0; i < G_N_ELEMENTS (test_key_values); i++) { |
| 290 |
if (!g_str_equal (test_key_values[i].key, key)) |
| 291 |
continue; |
| 292 |
if (!g_str_equal (test_key_values[i].value, value)) |
| 293 |
continue; |
| 294 |
|
| 295 |
/* Must not be found multiple times */ |
| 296 |
g_assert (!found[i]); |
| 297 |
found[i] = TRUE; |
| 298 |
return TRUE; |
| 299 |
} |
| 300 |
|
| 301 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 302 |
"Key/value '%s/%s' pair not expected", |
| 303 |
key, value); |
| 304 |
return FALSE; |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
common_validate_key_value (const gchar *str) |
| 309 |
{ |
| 310 |
gboolean found[G_N_ELEMENTS (test_key_values)] = { FALSE }; |
| 311 |
gboolean result; |
| 312 |
GError *error = NULL; |
| 313 |
guint i; |
| 314 |
|
| 315 |
result = qmicli_parse_key_value_string (str, |
| 316 |
&error, |
| 317 |
(QmiParseKeyValueForeachFn) key_value_callback, |
| 318 |
&found[0]); |
| 319 |
g_assert_no_error (error); |
| 320 |
g_assert (result); |
| 321 |
|
| 322 |
for (i = 0; i < G_N_ELEMENTS (test_key_values); i++) |
| 323 |
g_assert (found[i]); |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
test_parse_key_value_string_no_quotes (void) |
| 328 |
{ |
| 329 |
common_validate_key_value ("key1=,key2=value,key3=1234,key4=value1234"); |
| 330 |
} |
| 331 |
|
| 332 |
static void |
| 333 |
test_parse_key_value_string_single_quotes (void) |
| 334 |
{ |
| 335 |
common_validate_key_value ("key1='',key2='value',key3='1234',key4='value1234'"); |
| 336 |
} |
| 337 |
|
| 338 |
static void |
| 339 |
test_parse_key_value_string_double_quotes (void) |
| 340 |
{ |
| 341 |
common_validate_key_value ("key1=\"\",key2=\"value\",key3=\"1234\",key4=\"value1234\""); |
| 342 |
} |
| 343 |
|
| 344 |
static void |
| 345 |
test_parse_key_value_string_mixed_quotes (void) |
| 346 |
{ |
| 347 |
common_validate_key_value ("key1=\"\",key2='value',key3=1234,key4=\"value1234\""); |
| 348 |
} |
| 349 |
|
| 350 |
/******************************************************************************/ |
| 351 |
|
| 352 |
int main (int argc, char **argv) |
| 353 |
{ |
| 354 |
g_test_init (&argc, &argv, NULL); |
| 355 |
|
| 356 |
g_test_add_func ("/qmicli/helpers/raw-printable/1", test_helpers_raw_printable_1); |
| 357 |
g_test_add_func ("/qmicli/helpers/raw-printable/2", test_helpers_raw_printable_2); |
| 358 |
g_test_add_func ("/qmicli/helpers/raw-printable/3", test_helpers_raw_printable_3); |
| 359 |
g_test_add_func ("/qmicli/helpers/raw-printable/4", test_helpers_raw_printable_4); |
| 360 |
|
| 361 |
g_test_add_func ("/qmicli/helpers/binary-array-from-string/0", test_helpers_binary_array_from_string_0); |
| 362 |
g_test_add_func ("/qmicli/helpers/binary-array-from-string/1", test_helpers_binary_array_from_string_1); |
| 363 |
g_test_add_func ("/qmicli/helpers/binary-array-from-string/2", test_helpers_binary_array_from_string_2); |
| 364 |
g_test_add_func ("/qmicli/helpers/binary-array-from-string/3", test_helpers_binary_array_from_string_3); |
| 365 |
g_test_add_func ("/qmicli/helpers/binary-array-from-string/4", test_helpers_binary_array_from_string_4); |
| 366 |
|
| 367 |
#if defined HAVE_QMI_MESSAGE_WMS_GET_BROADCAST_CONFIG |
| 368 |
g_test_add_func ("/qmicli/helpers/cbs-channels-from-string", test_helpers_cbs_channels_from_string); |
| 369 |
#endif |
| 370 |
|
| 371 |
g_test_add_func ("/qmicli/helpers/supported-message-list", test_helpers_supported_messages_list); |
| 372 |
g_test_add_func ("/qmicli/helpers/supported-message-list/none", test_helpers_supported_messages_list_none); |
| 373 |
|
| 374 |
g_test_add_func ("/qmicli/helpers/key-value/no-quotes", test_parse_key_value_string_no_quotes); |
| 375 |
g_test_add_func ("/qmicli/helpers/key-value/single-quotes", test_parse_key_value_string_single_quotes); |
| 376 |
g_test_add_func ("/qmicli/helpers/key-value/double-quotes", test_parse_key_value_string_double_quotes); |
| 377 |
g_test_add_func ("/qmicli/helpers/key-value/mixed-quotes", test_parse_key_value_string_mixed_quotes); |
| 378 |
|
| 379 |
return g_test_run (); |
| 380 |
} |