GCC Code Coverage Report


Directory: ./
File: panels/common/hostname-helper.c
Date: 2024-05-03 09:46:52
Exec Total Coverage
Lines: 79 87 90.8%
Functions: 8 8 100.0%
Branches: 46 68 67.6%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2011 Red Hat, Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 #include <glib.h>
20 #include <glib/gi18n.h>
21 #include <string.h>
22
23 #include "hostname-helper.h"
24
25 static char *
26 18 allowed_chars (void)
27 {
28 GString *s;
29 char i;
30
31 18 s = g_string_new (NULL);
32
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 18 times.
486 for (i = 'a'; i <= 'z'; i++)
33
1/2
✓ Branch 0 taken 468 times.
✗ Branch 1 not taken.
468 g_string_append_c (s, i);
34
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 18 times.
486 for (i = 'A'; i <= 'Z'; i++)
35
1/2
✓ Branch 0 taken 468 times.
✗ Branch 1 not taken.
468 g_string_append_c (s, i);
36
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 18 times.
198 for (i = '0'; i <= '9'; i++)
37
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 g_string_append_c (s, i);
38 g_string_append_c (s, '-');
39
40 18 return g_string_free (s, FALSE);
41 }
42
43 static char *
44 18 remove_leading_dashes (char *input)
45 {
46 char *start;
47
48
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 14 times.
38 for (start = input; *start && (*start == '-'); start++)
49 ;
50
51 18 memmove (input, start, strlen (start) + 1);
52
53 18 return input;
54 }
55
56 static gboolean
57 102 is_empty (const char *input)
58 {
59
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (input == NULL ||
60
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 96 times.
102 *input == '\0')
61 6 return TRUE;
62 96 return FALSE;
63 }
64
65 static char *
66 14 remove_trailing_dashes (char *input)
67 {
68 int len;
69
70 14 len = strlen (input);
71
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 while (len--) {
72
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
26 if (input[len] == '-')
73 12 input[len] = '\0';
74 else
75 14 break;
76 }
77 14 return input;
78 }
79
80 static char *
81 18 remove_apostrophes (char *input)
82 {
83 char *apo;
84
85
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
22 while ((apo = strchr (input, '\'')) != NULL)
86 4 memmove (apo, apo + 1, strlen (apo));
87 18 return input;
88 }
89
90 static char *
91 14 remove_duplicate_dashes (char *input)
92 {
93 char *dashes;
94
95
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 while ((dashes = strstr (input, "--")) != NULL)
96 14 memmove (dashes, dashes + 1, strlen (dashes));
97 14 return input;
98 }
99
100 #define CHECK if (is_empty (result)) return g_strdup ("localhost")
101
102 char *
103 20 pretty_hostname_to_static (const char *pretty,
104 gboolean for_display)
105 {
106 20 g_autofree gchar *result = NULL;
107 20 g_autofree gchar *valid_chars = NULL;
108 20 g_autofree gchar *composed = NULL;
109
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 g_return_val_if_fail (pretty != NULL, NULL);
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 g_return_val_if_fail (g_utf8_validate (pretty, -1, NULL), NULL);
112
113 20 g_debug ("Input: '%s'", pretty);
114
115 20 composed = g_utf8_normalize (pretty, -1, G_NORMALIZE_ALL_COMPOSE);
116 20 g_debug ("\tcomposed: '%s'", composed);
117 /* Transform the pretty hostname to ASCII */
118 20 result = g_str_to_ascii (composed, NULL);
119 20 g_debug ("\ttranslit: '%s'", result);
120
121
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
22 CHECK;
122
123 /* Remove apostrophes */
124 18 remove_apostrophes (result);
125 18 g_debug ("\tapostrophes: '%s'", result);
126
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 CHECK;
128
129 /* Remove all the not-allowed chars */
130 18 valid_chars = allowed_chars ();
131 18 g_strcanon (result, valid_chars, '-');
132 18 g_debug ("\tcanon: '%s'", result);
133
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 CHECK;
135
136 /* Remove the leading dashes */
137 18 remove_leading_dashes (result);
138 18 g_debug ("\tleading: '%s'", result);
139
140
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 14 times.
22 CHECK;
141
142 /* Remove trailing dashes */
143 14 remove_trailing_dashes (result);
144 14 g_debug ("\ttrailing: '%s'", result);
145
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 CHECK;
147
148 /* Remove duplicate dashes */
149 14 remove_duplicate_dashes (result);
150 14 g_debug ("\tduplicate: '%s'", result);
151
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 CHECK;
153
154 /* Lower case */
155
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (!for_display)
156 7 return g_ascii_strdown (result, -1);
157
158 7 return g_steal_pointer (&result);
159 }
160 #undef CHECK
161
162 /* Max length of an SSID in bytes */
163 #define SSID_MAX_LEN 32
164 char *
165 3 pretty_hostname_to_ssid (const char *pretty)
166 {
167 const char *p, *prev;
168
169
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (pretty == NULL || *pretty == '\0') {
170 pretty = g_get_host_name ();
171 if (g_strcmp0 (pretty, "localhost") == 0)
172 pretty = NULL;
173 }
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pretty == NULL) {
176 /* translators: This is the default hotspot name, need to be less than 32-bytes */
177 gchar *ret = g_strdup (C_("hotspot", "Hotspot"));
178 g_assert (strlen (ret) <= SSID_MAX_LEN);
179 return ret;
180 }
181
182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 g_return_val_if_fail (g_utf8_validate (pretty, -1, NULL), NULL);
183
184 3 p = pretty;
185 3 prev = NULL;
186
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 while ((p = g_utf8_find_next_char (p, NULL)) != NULL) {
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (p == prev)
188 break;
189
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (p - pretty > SSID_MAX_LEN) {
191 return g_strndup (pretty, prev - pretty);
192 }
193
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 40 times.
41 if (p - pretty == SSID_MAX_LEN) {
194 1 return g_strndup (pretty, p - pretty);
195 }
196
197
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 if (*p == '\0')
198 2 break;
199
200 38 prev = p;
201 }
202
203 2 return g_strdup (pretty);
204 }
205