Fix error creation and warning
[claws.git] / src / common / tests / utils_get_serverportfp_from_filename_test.c
bloba23bc228bf95416e3e927dab7dd6672864ddcd2c
1 #include "config.h"
3 #include <stdio.h>
4 #include <glib.h>
6 #include "utils.h"
8 #include "mock_prefs_common_get_use_shred.h"
9 #include "mock_prefs_common_get_flush_metadata.h"
11 struct td {
12 gchar *str;
13 gboolean ret;
14 gchar *assertmsg;
15 gchar *host;
16 gchar *port;
17 gchar *fp;
20 #define ASSERTMSG_NULLINPUT "*CRITICAL* 'str != NULL' failed*"
21 #define ASSERTMSG_FAILED "*could not parse filename*"
23 struct td td_null = {
24 NULL,
25 FALSE, ASSERTMSG_NULLINPUT,
26 NULL, NULL, NULL
28 struct td td_empty = {
29 "",
30 FALSE, ASSERTMSG_FAILED,
31 NULL, NULL, NULL
33 struct td td_suffixonly = {
34 ".cert",
35 FALSE, ASSERTMSG_FAILED,
36 NULL, NULL, NULL
38 struct td td_hostonly = {
39 "foo.cert",
40 FALSE, ASSERTMSG_FAILED,
41 "foo", NULL, NULL
43 struct td td_short_nofp = {
44 "shorthost.10.cert",
45 TRUE, NULL,
46 "shorthost", "10", NULL
48 struct td td_short_fp = {
49 "shorthost.10.ab:cd:ef:gh.cert",
50 TRUE, NULL,
51 "shorthost", "10", "ab:cd:ef:gh"
53 struct td td_ip_nofp = {
54 "10.20.30.40.10.cert",
55 TRUE, NULL,
56 "10.20.30.40", "10", NULL
58 struct td td_ip_fp = {
59 "10.20.30.40.10.ab:cd:ef:gh.cert",
60 TRUE, NULL,
61 "10.20.30.40", "10", "ab:cd:ef:gh"
63 struct td td_long_nofp = {
64 "longer.host.name.10.cert",
65 TRUE, NULL,
66 "longer.host.name", "10", NULL
68 struct td td_long_fp = {
69 "longer.host.name.10.ab:cd:ef:gh.cert",
70 TRUE, NULL,
71 "longer.host.name", "10", "ab:cd:ef:gh"
73 struct td td_cert_starts_with_digits = {
74 "longer.host.name.10.20:cd:ef:gh.cert",
75 TRUE, NULL,
76 "longer.host.name", "10", "20:cd:ef:gh"
79 void
80 test_utils_get_serverportfp_from_filename_nulloutput()
82 if (!g_test_undefined())
83 return;
85 if (g_test_subprocess()) {
86 gboolean ret = get_serverportfp_from_filename("valid.host.10.ab:cd:ef:gh.cert",
87 NULL, NULL, NULL);
88 g_assert_true(ret);
89 return;
92 g_test_trap_subprocess(NULL, 0, 0);
93 g_test_trap_assert_failed();
96 void
97 test_utils_get_serverportfp_from_filename(gconstpointer user_data)
99 const struct td *data = (const struct td *)user_data;
101 if (!g_test_undefined())
102 return;
104 if (g_test_subprocess()) {
105 gchar *host, *port, *fp;
106 gboolean ret = get_serverportfp_from_filename(data->str, &host, &port, &fp);
108 g_assert_true(ret == data->ret);
109 g_assert_cmpstr(host, ==, data->host);
110 g_assert_cmpstr(port, ==, data->port);
111 g_assert_cmpstr(fp, ==, data->fp);
112 return;
115 g_test_trap_subprocess(NULL, 0, 0);
117 if (!data->ret && data->assertmsg != NULL) {
118 g_test_trap_assert_stderr(data->assertmsg);
119 g_test_trap_assert_failed();
120 } else {
121 g_test_trap_assert_passed();
126 main(int argc, char *argv[])
128 g_test_init(&argc, &argv, NULL);
130 g_test_add_func("/common/utils/get_serverportfp_from_filename/nulloutput",
131 test_utils_get_serverportfp_from_filename_nulloutput);
133 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/null",
134 &td_null, test_utils_get_serverportfp_from_filename);
135 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/empty",
136 &td_empty, test_utils_get_serverportfp_from_filename);
137 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/suffixonly",
138 &td_suffixonly, test_utils_get_serverportfp_from_filename);
139 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/hostonly",
140 &td_hostonly, test_utils_get_serverportfp_from_filename);
141 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/short_nofp",
142 &td_short_nofp, test_utils_get_serverportfp_from_filename);
143 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/short_fp",
144 &td_short_fp, test_utils_get_serverportfp_from_filename);
145 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/ip_nofp",
146 &td_ip_nofp, test_utils_get_serverportfp_from_filename);
147 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/ip_fp",
148 &td_ip_fp, test_utils_get_serverportfp_from_filename);
149 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/long_nofp",
150 &td_long_nofp, test_utils_get_serverportfp_from_filename);
151 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/long_fp",
152 &td_long_fp, test_utils_get_serverportfp_from_filename);
153 g_test_add_data_func("/common/utils/get_serverportfp_from_filename/cert_starts_with_digits",
154 &td_cert_starts_with_digits, test_utils_get_serverportfp_from_filename);
156 return g_test_run();