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