fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[claws.git] / src / common / tests / utils_get_uri_part_test.c
1 #include "config.h"
2
3 #include <stdio.h>
4 #include <glib.h>
5
6 #include "utils.h"
7
8 #include "mock_prefs_common_get_use_shred.h"
9 #include "mock_prefs_common_get_flush_metadata.h"
10
11 struct td_get_uri_part {
12         gchar *str;
13         gboolean ret;
14         guint uri_length;
15 };
16
17 struct td_get_uri_part td_get_uri_basic = {
18         "http://www.example.com", TRUE, 22
19 };
20 struct td_get_uri_part td_get_uri_slash = {
21         "http://www.example.com/", TRUE, 23
22 };
23 struct td_get_uri_part td_get_uri_question = {
24         "http://www.example.com/foo?", TRUE, 27
25 };
26 struct td_get_uri_part td_get_uri_parenthesis = {
27         "http://www.example.com/f(o)o", TRUE, 28
28 };
29 struct td_get_uri_part td_get_uri_brace = {
30         "http://www.example.com/f[oo", TRUE, 24
31 };
32 struct td_get_uri_part td_get_uri_umlaut = {
33         "http://www.examöple.com", TRUE, 24
34 };
35 struct td_get_uri_part td_get_uri_kanji = {
36         "http://www.漢字.com", TRUE, 21
37 };
38 struct td_get_uri_part td_get_uri_nonprintable = {
39         "http://www.exam\x01ple.com", TRUE, 15
40 };
41
42 #define URI "http://www.example.com"
43 static void
44 test_utils_get_uri_part_nowhitespace()
45 {
46         gboolean ret;
47         gchar *str = g_strdup("Nowhitespace"URI"nowhitespace");
48         const gchar *bp, *ep;
49
50         ret = get_uri_part(str, str + 12, &bp, &ep, FALSE);
51
52         g_assert_true(ret);
53         g_assert_true(ep == str + strlen(str));
54
55         g_free(str);
56 }
57
58 static void
59 test_utils_get_uri_part_whitespace()
60 {
61         gboolean ret;
62         gchar *str = g_strdup("Whitespace "URI" whitespace");
63         const gchar *bp, *ep;
64
65         ret = get_uri_part(str, str + 11, &bp, &ep, FALSE);
66
67         g_assert_true(ret);
68         g_assert_true(ep == bp + strlen(URI));
69
70         g_free(str);
71 }
72 #undef URI
73
74 static void
75 test_utils_get_uri_part(gconstpointer user_data)
76 {
77         const struct td_get_uri_part *data = (struct td_get_uri_part *)user_data;
78         gboolean ret;
79         const gchar *bp, *ep;
80
81         ret = get_uri_part(data->str, data->str, &bp, &ep, FALSE);
82
83         g_assert_nonnull(bp);
84         g_assert_nonnull(ep);
85
86         g_assert_true(ret == data->ret);
87         g_assert_true(ep >= bp);
88         g_assert_true(bp + data->uri_length == ep);
89 }
90
91 int
92 main(int argc, char *argv[])
93 {
94         g_test_init(&argc, &argv, NULL);
95
96         g_test_add_func("/common/utils/get_uri_part/nowhitespace",
97                         test_utils_get_uri_part_nowhitespace);
98         g_test_add_func("/common/utils/get_uri_part/whitespace",
99                         test_utils_get_uri_part_whitespace);
100
101         g_test_add_data_func("/common/utils/get_uri_part/basic",
102                         &td_get_uri_basic,
103                         test_utils_get_uri_part);
104         g_test_add_data_func("/common/utils/get_uri_part/slash",
105                         &td_get_uri_slash,
106                         test_utils_get_uri_part);
107         g_test_add_data_func("/common/utils/get_uri_part/question",
108                         &td_get_uri_question,
109                         test_utils_get_uri_part);
110         g_test_add_data_func("/common/utils/get_uri_part/parenthesis",
111                         &td_get_uri_parenthesis,
112                         test_utils_get_uri_part);
113         g_test_add_data_func("/common/utils/get_uri_part/brace",
114                         &td_get_uri_brace,
115                         test_utils_get_uri_part);
116         g_test_add_data_func("/common/utils/get_uri_part/umlaut",
117                         &td_get_uri_umlaut,
118                         test_utils_get_uri_part);
119         g_test_add_data_func("/common/utils/get_uri_part/kanji",
120                         &td_get_uri_kanji,
121                         test_utils_get_uri_part);
122         g_test_add_data_func("/common/utils/get_uri_part/nonprintable",
123                         &td_get_uri_nonprintable,
124                         test_utils_get_uri_part);
125
126         return g_test_run();
127 }