| 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) 2020 Aleksander Morgado <[email protected]> |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <glib-object.h> |
| 17 |
#include <string.h> |
| 18 |
#include "qmi-utils.h" |
| 19 |
#include "qmi-enums-nas.h" |
| 20 |
|
| 21 |
/******************************************************************************/ |
| 22 |
|
| 23 |
static void |
| 24 |
common_test_read_string_from_plmn_encoded_array (QmiNasPlmnEncodingScheme encoding, |
| 25 |
const guint8 *data, |
| 26 |
gsize data_len, |
| 27 |
const gchar *expected_utf8) |
| 28 |
{ |
| 29 |
g_autofree gchar *utf8 = NULL; |
| 30 |
g_autoptr(GArray) array = NULL; |
| 31 |
|
| 32 |
array = g_array_append_vals (g_array_sized_new (FALSE, FALSE, sizeof (guint8), data_len), |
| 33 |
data, data_len); |
| 34 |
|
| 35 |
utf8 = qmi_nas_read_string_from_plmn_encoded_array (encoding, array); |
| 36 |
|
| 37 |
g_assert_nonnull (utf8); |
| 38 |
g_assert_cmpstr (utf8, ==, expected_utf8); |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
test_read_string_from_plmn_encoded_array_gsm7_default_chars (void) |
| 43 |
{ |
| 44 |
const guint8 gsm[] = { |
| 45 |
0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x88, |
| 46 |
0x84, 0x62, 0xC1, 0x68, 0x38, 0x1E, 0x90, 0x88, |
| 47 |
0x64, 0x42, 0xA9, 0x58, 0x2E, 0x98, 0x8C, 0x86, |
| 48 |
0xD3, 0xF1, 0x7C, 0x40, 0x21, 0xD1, 0x88, 0x54, |
| 49 |
0x32, 0x9D, 0x50, 0x29, 0xD5, 0x8A, 0xD5, 0x72, |
| 50 |
0xBD, 0x60, 0x31, 0xD9, 0x8C, 0x56, 0xB3, 0xDD, |
| 51 |
0x70, 0x39, 0xDD, 0x8E, 0xD7, 0xF3, 0xFD, 0x80, |
| 52 |
0x41, 0xE1, 0x90, 0x58, 0x34, 0x1E, 0x91, 0x49, |
| 53 |
0xE5, 0x92, 0xD9, 0x74, 0x3E, 0xA1, 0x51, 0xE9, |
| 54 |
0x94, 0x5A, 0xB5, 0x5E, 0xB1, 0x59, 0xED, 0x96, |
| 55 |
0xDB, 0xF5, 0x7E, 0xC1, 0x61, 0xF1, 0x98, 0x5C, |
| 56 |
0x36, 0x9F, 0xD1, 0x69, 0xF5, 0x9A, 0xDD, 0x76, |
| 57 |
0xBF, 0xE1, 0x71, 0xF9, 0x9C, 0x5E, 0xB7, 0xDF, |
| 58 |
0xF1, 0x79, 0xFD, 0x9E, 0xDF, 0xF7, 0xFF, 0x01, |
| 59 |
}; |
| 60 |
const gchar *expected_utf8 = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà"; |
| 61 |
|
| 62 |
common_test_read_string_from_plmn_encoded_array (QMI_NAS_PLMN_ENCODING_SCHEME_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
test_read_string_from_plmn_encoded_array_gsm7_extended_chars (void) |
| 67 |
{ |
| 68 |
const guint8 gsm[] = { |
| 69 |
0x1B, 0xC5, 0x86, 0xB2, 0x41, 0x6D, 0x52, 0x9B, |
| 70 |
0xD7, 0x86, 0xB7, 0xE9, 0x6D, 0x7C, 0x1B, 0xE0, |
| 71 |
0xA6, 0x0C |
| 72 |
}; |
| 73 |
const gchar *expected_utf8 = "\f^{}\\[~]|€"; |
| 74 |
|
| 75 |
common_test_read_string_from_plmn_encoded_array (QMI_NAS_PLMN_ENCODING_SCHEME_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
test_read_string_from_plmn_encoded_array_gsm7_mixed_chars (void) |
| 80 |
{ |
| 81 |
const guint8 gsm[] = { |
| 82 |
0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x8C, |
| 83 |
0x8D, 0xA2, 0x62, 0xB9, 0x60, 0x32, 0x1B, 0x94, |
| 84 |
0x86, 0xD3, 0xF1, 0xA0, 0x36, 0xA9, 0xD4, 0x0D, |
| 85 |
0x97, 0xDB, 0xBC, 0x74, 0x3B, 0x5E, 0xCF, 0xB7, |
| 86 |
0xE1, 0xFD, 0x80, 0x51, 0xE9, 0x74, 0xE3, 0xA3, |
| 87 |
0x56, 0xB9, 0x1B, 0x60, 0xD7, 0xFB, 0x05, 0x87, |
| 88 |
0xC5, 0xF0, 0xB8, 0x7C, 0x4E, 0xAF, 0xDB, 0xF9, |
| 89 |
0x7D, 0xFF, 0x7F, 0x53, 0x06 |
| 90 |
}; |
| 91 |
const gchar *expected_utf8 = "@£$¥èéùìø\fΩΠΨΣΘ{ΞÆæß(})789\\:;<=>[?¡QRS]TUÖ|Ñܧ¿abpqrstuvöñüà€"; |
| 92 |
|
| 93 |
common_test_read_string_from_plmn_encoded_array (QMI_NAS_PLMN_ENCODING_SCHEME_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
test_read_string_from_plmn_encoded_array_ucs2le (void) |
| 98 |
{ |
| 99 |
const guint8 ucs2le[] = { |
| 100 |
0x54, 0x00, 0x2d, 0x00, 0x4d, 0x00, 0x6f, 0x00, |
| 101 |
0x62, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00 |
| 102 |
}; |
| 103 |
const gchar *expected_utf8 = "T-Mobile"; |
| 104 |
|
| 105 |
common_test_read_string_from_plmn_encoded_array (QMI_NAS_PLMN_ENCODING_SCHEME_UCS2LE, ucs2le, G_N_ELEMENTS (ucs2le), expected_utf8); |
| 106 |
} |
| 107 |
|
| 108 |
/******************************************************************************/ |
| 109 |
|
| 110 |
static void |
| 111 |
common_test_read_string_from_network_description_encoded_array (QmiNasNetworkDescriptionEncoding encoding, |
| 112 |
const guint8 *data, |
| 113 |
gsize data_len, |
| 114 |
const gchar *expected_utf8) |
| 115 |
{ |
| 116 |
g_autofree gchar *utf8 = NULL; |
| 117 |
g_autoptr(GArray) array = NULL; |
| 118 |
|
| 119 |
array = g_array_append_vals (g_array_sized_new (FALSE, FALSE, sizeof (guint8), data_len), |
| 120 |
data, data_len); |
| 121 |
|
| 122 |
utf8 = qmi_nas_read_string_from_network_description_encoded_array (encoding, array); |
| 123 |
|
| 124 |
if (expected_utf8) { |
| 125 |
g_assert_nonnull (utf8); |
| 126 |
g_assert_cmpstr (utf8, ==, expected_utf8); |
| 127 |
} else |
| 128 |
g_assert_null (utf8); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
test_read_string_from_network_description_encoded_array_gsm7_default_chars (void) |
| 133 |
{ |
| 134 |
const guint8 gsm[] = { |
| 135 |
0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x88, |
| 136 |
0x84, 0x62, 0xC1, 0x68, 0x38, 0x1E, 0x90, 0x88, |
| 137 |
0x64, 0x42, 0xA9, 0x58, 0x2E, 0x98, 0x8C, 0x86, |
| 138 |
0xD3, 0xF1, 0x7C, 0x40, 0x21, 0xD1, 0x88, 0x54, |
| 139 |
0x32, 0x9D, 0x50, 0x29, 0xD5, 0x8A, 0xD5, 0x72, |
| 140 |
0xBD, 0x60, 0x31, 0xD9, 0x8C, 0x56, 0xB3, 0xDD, |
| 141 |
0x70, 0x39, 0xDD, 0x8E, 0xD7, 0xF3, 0xFD, 0x80, |
| 142 |
0x41, 0xE1, 0x90, 0x58, 0x34, 0x1E, 0x91, 0x49, |
| 143 |
0xE5, 0x92, 0xD9, 0x74, 0x3E, 0xA1, 0x51, 0xE9, |
| 144 |
0x94, 0x5A, 0xB5, 0x5E, 0xB1, 0x59, 0xED, 0x96, |
| 145 |
0xDB, 0xF5, 0x7E, 0xC1, 0x61, 0xF1, 0x98, 0x5C, |
| 146 |
0x36, 0x9F, 0xD1, 0x69, 0xF5, 0x9A, 0xDD, 0x76, |
| 147 |
0xBF, 0xE1, 0x71, 0xF9, 0x9C, 0x5E, 0xB7, 0xDF, |
| 148 |
0xF1, 0x79, 0xFD, 0x9E, 0xDF, 0xF7, 0xFF, 0x01, |
| 149 |
}; |
| 150 |
const gchar *expected_utf8 = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà"; |
| 151 |
|
| 152 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
test_read_string_from_network_description_encoded_array_gsm7_extended_chars (void) |
| 157 |
{ |
| 158 |
const guint8 gsm[] = { |
| 159 |
0x1B, 0xC5, 0x86, 0xB2, 0x41, 0x6D, 0x52, 0x9B, |
| 160 |
0xD7, 0x86, 0xB7, 0xE9, 0x6D, 0x7C, 0x1B, 0xE0, |
| 161 |
0xA6, 0x0C |
| 162 |
}; |
| 163 |
const gchar *expected_utf8 = "\f^{}\\[~]|€"; |
| 164 |
|
| 165 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 166 |
} |
| 167 |
|
| 168 |
static void |
| 169 |
test_read_string_from_network_description_encoded_array_gsm7_mixed_chars (void) |
| 170 |
{ |
| 171 |
const guint8 gsm[] = { |
| 172 |
0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x8C, |
| 173 |
0x8D, 0xA2, 0x62, 0xB9, 0x60, 0x32, 0x1B, 0x94, |
| 174 |
0x86, 0xD3, 0xF1, 0xA0, 0x36, 0xA9, 0xD4, 0x0D, |
| 175 |
0x97, 0xDB, 0xBC, 0x74, 0x3B, 0x5E, 0xCF, 0xB7, |
| 176 |
0xE1, 0xFD, 0x80, 0x51, 0xE9, 0x74, 0xE3, 0xA3, |
| 177 |
0x56, 0xB9, 0x1B, 0x60, 0xD7, 0xFB, 0x05, 0x87, |
| 178 |
0xC5, 0xF0, 0xB8, 0x7C, 0x4E, 0xAF, 0xDB, 0xF9, |
| 179 |
0x7D, 0xFF, 0x7F, 0x53, 0x06 |
| 180 |
}; |
| 181 |
const gchar *expected_utf8 = "@£$¥èéùìø\fΩΠΨΣΘ{ΞÆæß(})789\\:;<=>[?¡QRS]TUÖ|Ñܧ¿abpqrstuvöñüà€"; |
| 182 |
|
| 183 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_GSM, gsm, G_N_ELEMENTS (gsm), expected_utf8); |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
test_read_string_from_network_description_encoded_array_ucs2le (void) |
| 188 |
{ |
| 189 |
const guint8 ucs2le[] = { |
| 190 |
0x54, 0x00, 0x2d, 0x00, 0x4d, 0x00, 0x6f, 0x00, |
| 191 |
0x62, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00 |
| 192 |
}; |
| 193 |
const gchar *expected_utf8 = "T-Mobile"; |
| 194 |
|
| 195 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_UNICODE, ucs2le, G_N_ELEMENTS (ucs2le), expected_utf8); |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
test_read_string_from_network_description_encoded_array_ascii (void) |
| 200 |
{ |
| 201 |
const guint8 ascii[] = { 'T', '-', 'M', 'o', 'b', 'i', 'l', 'e' }; |
| 202 |
const gchar *expected_utf8 = "T-Mobile"; |
| 203 |
|
| 204 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_ASCII7, ascii, G_N_ELEMENTS (ascii), expected_utf8); |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
test_read_string_from_network_description_encoded_array_unspecified_ascii (void) |
| 209 |
{ |
| 210 |
const guint8 ascii[] = { 'T', '-', 'M', 'o', 'b', 'i', 'l', 'e' }; |
| 211 |
const gchar *expected_utf8 = "T-Mobile"; |
| 212 |
|
| 213 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_UNSPECIFIED, ascii, G_N_ELEMENTS (ascii), expected_utf8); |
| 214 |
} |
| 215 |
|
| 216 |
static void |
| 217 |
test_read_string_from_network_description_encoded_array_unspecified_other (void) |
| 218 |
{ |
| 219 |
const guint8 other[] = { |
| 220 |
0x54, 0x00, 0x2d, 0x00, 0x4d, 0x00, 0x6f, 0x00, |
| 221 |
0x62, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00 |
| 222 |
}; |
| 223 |
|
| 224 |
common_test_read_string_from_network_description_encoded_array (QMI_NAS_NETWORK_DESCRIPTION_ENCODING_UNSPECIFIED, other, G_N_ELEMENTS (other), NULL); |
| 225 |
} |
| 226 |
|
| 227 |
static void |
| 228 |
test_read_string_from_network_description_encoded_array_unknown (void) |
| 229 |
{ |
| 230 |
const guint8 other[] = { |
| 231 |
0x54, 0x00, 0x2d, 0x00, 0x4d, 0x00, 0x6f, 0x00, |
| 232 |
0x62, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00 |
| 233 |
}; |
| 234 |
|
| 235 |
common_test_read_string_from_network_description_encoded_array (0xFF, other, G_N_ELEMENTS (other), NULL); |
| 236 |
} |
| 237 |
|
| 238 |
/******************************************************************************/ |
| 239 |
|
| 240 |
int main (int argc, char **argv) |
| 241 |
{ |
| 242 |
g_test_init (&argc, &argv, NULL); |
| 243 |
|
| 244 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-plmn-encoded-array/gsm7-default-chars", test_read_string_from_plmn_encoded_array_gsm7_default_chars); |
| 245 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-plmn-encoded-array/gsm7-extended-chars", test_read_string_from_plmn_encoded_array_gsm7_extended_chars); |
| 246 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-plmn-encoded-array/gsm7-mixed-chars", test_read_string_from_plmn_encoded_array_gsm7_mixed_chars); |
| 247 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-plmn-encoded-array/ucs2le", test_read_string_from_plmn_encoded_array_ucs2le); |
| 248 |
|
| 249 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/gsm7-default-chars", test_read_string_from_network_description_encoded_array_gsm7_default_chars); |
| 250 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/gsm7-extended-chars", test_read_string_from_network_description_encoded_array_gsm7_extended_chars); |
| 251 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/gsm7-mixed-chars", test_read_string_from_network_description_encoded_array_gsm7_mixed_chars); |
| 252 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/ucs2le", test_read_string_from_network_description_encoded_array_ucs2le); |
| 253 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/ascii", test_read_string_from_network_description_encoded_array_ascii); |
| 254 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/unspecified-ascii", test_read_string_from_network_description_encoded_array_unspecified_ascii); |
| 255 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/unspecified-other", test_read_string_from_network_description_encoded_array_unspecified_other); |
| 256 |
g_test_add_func ("/libqmi-glib/utils/read-string-from-network-description-encoded-array/unknown", test_read_string_from_network_description_encoded_array_unknown); |
| 257 |
|
| 258 |
return g_test_run (); |
| 259 |
} |