GCC Code Coverage Report


Directory: ./
File: tests/common/test-hostname.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 46 63 73.0%
Functions: 3 3 100.0%
Branches: 37 54 68.5%

Line Branch Exec Source
1 #include "config.h"
2
3 #include <glib.h>
4 #include <glib/gi18n.h>
5 #include <locale.h>
6
7 #include "hostname-helper.h"
8
9 static void
10 1 test_hostname (void)
11 {
12
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 g_autofree gchar *contents = NULL;
13 guint i;
14
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 g_auto(GStrv) lines = NULL;
15
16
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (g_file_get_contents (TEST_SRCDIR "/hostnames-test.txt", &contents, NULL, NULL) == FALSE) {
17 g_warning ("Failed to load '%s'", TEST_SRCDIR "/hostnames-test.txt");
18 g_test_fail ();
19 return;
20 }
21
22 1 lines = g_strsplit (contents, "\n", -1);
23
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (lines == NULL) {
24 g_warning ("Test file is empty");
25 g_test_fail ();
26 return;
27 }
28
29
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (i = 0; lines[i] != NULL; i++) {
30
3/3
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
12 g_auto(GStrv) items = NULL;
31
3/3
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
12 g_autofree gchar *utf8 = NULL;
32
3/3
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
12 g_autofree gchar *result1 = NULL;
33
3/3
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
12 g_autofree gchar *result2 = NULL;
34
35
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (*lines[i] == '#')
36 1 continue;
37
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (*lines[i] == '\0')
38 1 break;
39
40 10 items = g_strsplit (lines[i], "\t", -1);
41 10 utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL);
42
43 10 result1 = pretty_hostname_to_static (items[0], FALSE);
44
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (g_strcmp0 (result1, items[2]) != 0) {
45 g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
46 utf8, items[2], result1);
47 g_test_fail ();
48 } else {
49 10 g_debug ("Result for '%s' matches '%s'",
50 utf8, result1);
51 }
52
53 10 result2 = pretty_hostname_to_static (items[0], TRUE);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (g_strcmp0 (result2, items[1]) != 0) {
55 g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
56 utf8, items[1], result2);
57 g_test_fail ();
58 } else {
59 10 g_debug ("Result for '%s' matches '%s'",
60 utf8, result2);
61 }
62 }
63 }
64
65 static void
66 1 test_ssid (void)
67 {
68
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 g_autofree gchar *contents = NULL;
69 guint i;
70
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 g_auto(GStrv) lines = NULL;
71
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (g_file_get_contents (TEST_SRCDIR "/ssids-test.txt", &contents, NULL, NULL) == FALSE) {
73 g_warning ("Failed to load '%s'", TEST_SRCDIR "/ssids-test.txt");
74 g_test_fail ();
75 return;
76 }
77
78 1 lines = g_strsplit (contents, "\n", -1);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (lines == NULL) {
80 g_warning ("Test file is empty");
81 g_test_fail ();
82 return;
83 }
84
85
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (i = 0; lines[i] != NULL; i++) {
86
2/3
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 g_autofree gchar *ssid = NULL;
87
2/3
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 g_auto(GStrv) items = NULL;
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (*lines[i] == '#')
90 continue;
91
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (*lines[i] == '\0')
92 1 break;
93
94 3 items = g_strsplit (lines[i], "\t", -1);
95 3 ssid = pretty_hostname_to_ssid (items[0]);
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 g_assert_cmpstr (ssid, ==, items[1]);
97 }
98 }
99
100 1 int main (int argc, char **argv)
101 {
102 char *locale;
103
104 /* Running in some locales will
105 * break the tests as "ΓΌ" will be transliterated to
106 * "ue" in de_DE, and 'u"' in the C locale.
107 *
108 * Work around that by forcing en_US with UTF-8 in
109 * our tests
110 * https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
111 1 locale = setlocale (LC_ALL, "en_US.UTF-8");
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (locale == NULL) {
113 g_debug("Missing en_US.UTF-8 locale, ignoring test.");
114 return 0;
115 }
116 1 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
117 1 g_test_init (&argc, &argv, NULL);
118
119 1 g_test_add_func ("/common/hostname", test_hostname);
120 1 g_test_add_func ("/common/ssid", test_ssid);
121
122 1 return g_test_run ();
123 }
124