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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/* -*- 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) 2012-2016 Aleksander Morgado <aleksander@aleksander.es>
*/
#include <glib.h>
#include "qmicli-helpers.h"
/******************************************************************************/
static void
test_helpers_raw_printable_1 (void)
{
GArray *array;
gchar *printable;
static guint8 buffer[8] = {
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
};
static const gchar *expected =
"0F:\n"
"50:\n"
"EB:\n"
"E2:\n"
"B6:\n"
"00:\n"
"00:\n"
"00\n";
array = g_array_sized_new (FALSE, FALSE, 1, 8);
g_array_insert_vals (array, 0, buffer, 8);
printable = qmicli_get_raw_data_printable (array, 3, "");
g_assert_cmpstr (printable, ==, expected);
g_free (printable);
g_array_unref (array);
}
static void
test_helpers_raw_printable_2 (void)
{
GArray *array;
gchar *printable;
static guint8 buffer[8] = {
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
};
static const gchar *expected =
"\t0F:50:\n"
"\tEB:E2:\n"
"\tB6:00:\n"
"\t00:00\n";
array = g_array_sized_new (FALSE, FALSE, 1, 8);
g_array_insert_vals (array, 0, buffer, 8);
/* When passing 7, we'll be really getting 6 (the closest lower multiple of 3) */
printable = qmicli_get_raw_data_printable (array, 7, "\t");
g_assert_cmpstr (printable, ==, expected);
g_free (printable);
g_array_unref (array);
}
static void
test_helpers_raw_printable_3 (void)
{
GArray *array;
gchar *printable;
static guint8 buffer[8] = {
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
};
static const gchar *expected =
"\t\t\t0F:50:EB:E2:\n"
"\t\t\tB6:00:00:00\n";
array = g_array_sized_new (FALSE, FALSE, 1, 8);
g_array_insert_vals (array, 0, buffer, 8);
printable = qmicli_get_raw_data_printable (array, 12, "\t\t\t");
g_assert_cmpstr (printable, ==, expected);
g_free (printable);
g_array_unref (array);
}
static void
test_helpers_raw_printable_4 (void)
{
GArray *array;
gchar *printable;
static guint8 buffer[8] = {
0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00
};
static const gchar *expected =
"\t0F:50:EB:E2:B6:00:00:00\n";
array = g_array_sized_new (FALSE, FALSE, 1, 8);
g_array_insert_vals (array, 0, buffer, 8);
printable = qmicli_get_raw_data_printable (array, 24, "\t");
g_assert_cmpstr (printable, ==, expected);
g_free (printable);
g_array_unref (array);
}
/******************************************************************************/
static void
test_helpers_supported_messages_list (void)
{
const guint8 bytearray[] = {
0x03, 0x00, 0x00, 0xC0
};
const gchar *expected_str =
"\t0x0000\n" /* 0 dec */
"\t0x0001\n" /* 1 dec */
"\t0x001E\n" /* 30 dec */
"\t0x001F\n"; /* 31 dec */
gchar *str;
str = qmicli_get_supported_messages_list (bytearray, G_N_ELEMENTS (bytearray));
g_assert (str);
g_assert_cmpstr (str, ==, expected_str);
g_free (str);
}
static void
test_helpers_supported_messages_list_none (void)
{
const gchar *expected_str = "\tnone\n";
gchar *str;
str = qmicli_get_supported_messages_list (NULL, 0);
g_assert (str);
g_assert_cmpstr (str, ==, expected_str);
g_free (str);
}
/******************************************************************************/
typedef struct {
const gchar *key;
const gchar *value;
gboolean found;
} KeyValue;
static const KeyValue test_key_values[] = {
{ "key1", "", FALSE },
{ "key2", "value", FALSE },
{ "key3", "1234", FALSE },
{ "key4", "value1234", FALSE },
};
static gboolean
key_value_callback (const gchar *key,
const gchar *value,
GError **error,
gboolean *found)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (test_key_values); i++) {
if (!g_str_equal (test_key_values[i].key, key))
continue;
if (!g_str_equal (test_key_values[i].value, value))
continue;
/* Must not be found multiple times */
g_assert (!found[i]);
found[i] = TRUE;
return TRUE;
}
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Key/value '%s/%s' pair not expected",
key, value);
return FALSE;
}
static void
common_validate_key_value (const gchar *str)
{
gboolean found[G_N_ELEMENTS (test_key_values)] = { FALSE };
gboolean result;
GError *error = NULL;
guint i;
result = qmicli_parse_key_value_string (str,
&error,
(QmiParseKeyValueForeachFn) key_value_callback,
&found[0]);
g_assert_no_error (error);
g_assert (result);
for (i = 0; i < G_N_ELEMENTS (test_key_values); i++)
g_assert (found[i]);
}
static void
test_parse_key_value_string_no_quotes (void)
{
common_validate_key_value ("key1=,key2=value,key3=1234,key4=value1234");
}
static void
test_parse_key_value_string_single_quotes (void)
{
common_validate_key_value ("key1='',key2='value',key3='1234',key4='value1234'");
}
static void
test_parse_key_value_string_double_quotes (void)
{
common_validate_key_value ("key1=\"\",key2=\"value\",key3=\"1234\",key4=\"value1234\"");
}
static void
test_parse_key_value_string_mixed_quotes (void)
{
common_validate_key_value ("key1=\"\",key2='value',key3=1234,key4=\"value1234\"");
}
/******************************************************************************/
int main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/qmicli/helpers/raw-printable/1", test_helpers_raw_printable_1);
g_test_add_func ("/qmicli/helpers/raw-printable/2", test_helpers_raw_printable_2);
g_test_add_func ("/qmicli/helpers/raw-printable/3", test_helpers_raw_printable_3);
g_test_add_func ("/qmicli/helpers/raw-printable/4", test_helpers_raw_printable_4);
g_test_add_func ("/qmicli/helpers/supported-message-list", test_helpers_supported_messages_list);
g_test_add_func ("/qmicli/helpers/supported-message-list/none", test_helpers_supported_messages_list_none);
g_test_add_func ("/qmicli/helpers/key-value/no-quotes", test_parse_key_value_string_no_quotes);
g_test_add_func ("/qmicli/helpers/key-value/single-quotes", test_parse_key_value_string_single_quotes);
g_test_add_func ("/qmicli/helpers/key-value/double-quotes", test_parse_key_value_string_double_quotes);
g_test_add_func ("/qmicli/helpers/key-value/mixed-quotes", test_parse_key_value_string_mixed_quotes);
return g_test_run ();
}
|