Statistics
| Revision:

root / trunk / tests / test_nntpconnection_backend.c @ 1863

History | View | Annotate | Download (7.8 KB)

1
/* 
2
    Copyright (C) 2005-2011  Erik van Pienbroek
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, write to the Free Software
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
*/
18

                
19
#ifdef HAVE_CONFIG_H
20
#include "config.h"
21
#endif
22

                
23
#include 
24
#include 
25

                
26
#include "nntpconnection_utils.h"
27
#include "nntpconnection_backend_native.h"
28

                
29
#ifdef HAVE_GIO_SOCKET
30
#include "nntpconnection_backend_gio.h"
31
#endif
32

                
33
/* Fake placeholders */
34
typedef void* NGPlugin;
35
void
36
ng_plugin_emit_log_msg(NGPlugin *plugin_data, NGLogLevel log_level, const char *format, ...)
37
{
38
    va_list args;
39
    char msg[1024];
40
    va_start(args, format);
41
    memset(msg, 0, sizeof(msg));
42
    vsnprintf(msg, sizeof(msg) - 1, format, args);
43
    va_end(args);
44

                
45
    g_test_message("%s", msg);
46
}
47

                
48
void
49
nntpgrab_core_emit_traffic_monitor_update(gboolean direct, int bytes_received[10], time_t stamp)
50
{
51
    g_test_message("Signal was emitted: traffic_monitor_update (bytes_received[9] = %i, bytes_received[8] = %i, stamp = %i", bytes_received[9], bytes_received[8], (int) stamp);
52
}
53

                
54
void
55
nntpgrab_core_emit_connecting(gboolean direct, const char *servername, int conn_id)
56
{
57
    g_test_message("Signal was emitted: connecting (servername = %s, conn_id = %i", servername, conn_id);
58
}
59

                
60
void
61
nntpgrab_core_emit_connected(gboolean direct, const char *servername, int conn_id, const char *welcome_msg)
62
{
63
    g_test_message("Signal was emitted: connected (servername = %s, conn_id = %i, welcome_msg = %s", servername, conn_id, welcome_msg);
64
}
65

                
66
void
67
nntpgrab_core_emit_disconnect(gboolean direct, const char *servername, int conn_id, NNTPDisconnectType disconnect_type, const char *errmsg)
68
{
69
    g_test_message("Signal was emitted: disconnect (servername = %s, conn_id = %i, disconnect_type = %i, errmsg = %s", servername, conn_id, disconnect_type, errmsg);
70
}
71

                
72
/* Are the functions below really necessary */
73
void
74
file_unref(NNTPFile *file)
75
{
76
}
77

                
78
void
79
collection_unref(NNTPCollection *collection)
80
{
81
}
82

                
83
void
84
download_queue_update_part_status(const char *servername, int connection_id, NNTPCollection *collection, NNTPFile *file, NNTPPart *part, int server_id, gboolean success, gboolean reset_to_zero, gboolean dont_retry_anymore)
85
{
86
}
87

                
88
NGSchedularState
89
download_thread_get_state (void)
90
{
91
    return SCHEDULAR_STATE_RUNNING;
92
}
93

                
94
void
95
nntpconnection_process_welcome_msg(NntpConnection *conn)
96
{
97
}
98

                
99
void
100
throttle_pause(struct timeval start_time, off_t xferlen, int max_bandwidth)
101
{
102
}
103

                
104
/* Test functions */
105
static void
106
test_backend_internal(NntpConnection *conn)
107
{
108
    char *errmsg = NULL;
109
    gboolean retval = TRUE;
110
    char line[256];
111
    int length;
112
    gboolean more_data_ready;
113

                
114
    /* Try to connect to the server */
115
    conn->poll_fd.fd = -1;
116
    g_test_message("Connecting to usenet server");
117
    retval = nntpconnection_connect_to_server(conn, &errmsg);
118
    if (retval != NNTP_CONNECTION_ERROR_NONE) {
119
        g_assert_cmpint(conn->poll_fd.fd, ==, -1);
120
        g_assert(errmsg != NULL);
121
        g_test_message("Errmsg = %s\n", errmsg);
122
        g_free(errmsg);
123
        g_assert_cmpint(retval, ==, NNTP_CONNECTION_ERROR_NONE);
124
        g_assert_not_reached();
125
    }
126
    g_assert_cmpint(conn->poll_fd.fd, >, -1);
127

                
128
    /* Wait until there's data */
129
    g_test_message("Waiting for data to arrive");
130
    g_assert(nntpconnection_ensure_data_is_ready(conn, G_USEC_PER_SEC * 5));
131

                
132
    /* Read the welcome msg */
133
    g_test_message("Reading welcome message");
134
    memset(&line, 0, sizeof(line));
135
    g_assert(nntpconnection_read_msg(conn, TRUE, sizeof(line) - 1, &line, &length, &more_data_ready));
136

                
137
    /* The welcome msg must start with '200' */
138
    g_test_message("Verifying welcome message");
139
    g_assert_cmpint(nntpconnection_utils_get_status_code(line), ==, 200);
140
    g_assert_cmpint(strlen(line), ==, length);
141

                
142
    /* Send the MODE READER command */
143
    g_test_message("Sending the command MODE READER to server");
144
    memset(&line, 0, sizeof(line));
145
    snprintf(line, sizeof(line) - 1, "MODE READER\r\n");
146
    g_assert(nntpconnection_send_msg(conn, line, strlen(line)));
147

                
148
    /* Wait again for data to arrive */
149
    g_test_message("Waiting for data to arrive");
150
    g_assert(nntpconnection_ensure_data_is_ready(conn, G_USEC_PER_SEC * 5));
151

                
152
    /* Read the response */
153
    g_test_message("Reading response");
154
    memset(&line, 0, sizeof(line));
155
    g_assert(nntpconnection_read_msg(conn, TRUE, sizeof(line) - 1, &line, &length, &more_data_ready));
156

                
157
    /* The response must start with '200' */
158
    g_test_message("Verifying MODE READER response message");
159
    g_assert_cmpint(nntpconnection_utils_get_status_code(line), ==, 200);
160
    g_assert_cmpint(strlen(line), ==, length);
161

                
162
    /* Disconnect from the server */
163
    g_test_message("Disconnecting from server");
164
    nntpconnection_disconnect_from_server(conn, DISCONNECT_NORMAL, NULL, __FILE__, __LINE__);
165
    g_assert_cmpint(conn->poll_fd.fd, ==, -1);
166
}
167

                
168
static void
169
test_backend_normal(NntpConnection *conn)
170
{
171
    /* Configure a fake server */
172
    strncpy(conn->server_info.servername, "GigaNews", sizeof(conn->server_info.servername) - 1);
173
    strncpy(conn->server_info.hostname, "news.giganews.com", sizeof(conn->server_info.hostname) - 1);
174
    conn->server_info.port = 119;
175
    conn->server_info.use_ssl = FALSE;
176
    conn->server_info.enabled = TRUE;
177

                
178
    test_backend_internal(conn);
179
}
180

                
181
static void
182
test_backend_ssl(NntpConnection *conn)
183
{
184
    /* Configure a fake server */
185
    strncpy(conn->server_info.servername, "GigaNews", sizeof(conn->server_info.servername) - 1);
186
    strncpy(conn->server_info.hostname, "news.giganews.com", sizeof(conn->server_info.hostname) - 1);
187
    conn->server_info.port = 563;
188
    conn->server_info.use_ssl = TRUE;
189
    conn->server_info.enabled = TRUE;
190

                
191
    test_backend_internal(conn);
192
}
193

                
194
static
195
void test_backend_native(void)
196
{
197
    NntpConnection *conn = nntpconnection_backend_native_new();
198
    test_backend_normal(conn);
199
    g_object_unref(conn);
200
}
201

                
202
static
203
void test_backend_native_ssl(void)
204
{
205
    NntpConnection *conn = nntpconnection_backend_native_new();
206

                
207
    g_test_message("Testing for the SSL support flag");
208
#ifdef HAVE_SSL
209
    g_assert_cmpint(nntpconnection_get_has_ssl_support(conn), ==, TRUE);
210
#else
211
    g_assert_cmpint(nntpconnection_get_has_ssl_support(conn), ==, FALSE);
212
#endif
213

                
214
    test_backend_ssl(conn);
215

                
216
    g_object_unref(conn);
217
}
218

                
219
#ifdef HAVE_GIO_SOCKET
220
static
221
void test_backend_gio(void)
222
{
223
    NntpConnection *conn = nntpconnection_backend_gio_new();
224
    test_backend_normal(conn);
225
    g_object_unref(conn);
226
}
227
#endif
228

                
229
#ifdef HAVE_GLIB_NETWORKING
230
static
231
void test_backend_gio_ssl(void)
232
{
233
    NntpConnection *conn = nntpconnection_backend_gio_new();
234

                
235
    g_test_message("Testing for the SSL support flag");
236
    g_assert_cmpint(nntpconnection_get_has_ssl_support(conn), ==, TRUE);
237

                
238
    test_backend_ssl(conn);
239
    g_object_unref(conn);
240
}
241
#endif
242

                
243
int main(int argc, char **argv)
244
{
245
    g_type_init();
246
    g_test_init(&argc, &argv, NULL);
247
    g_test_bug_base("https://www.nntpgrab.nl/issues/");
248

                
249
    g_test_add_func("/nntpconnection/backend/native", test_backend_native);
250
    g_test_add_func("/nntpconnection/backend/native/ssl", test_backend_native_ssl);
251
#ifdef HAVE_GIO_SOCKET
252
    g_test_add_func("/nntpconnection/backend/gio", test_backend_gio);
253
#endif
254
#ifdef HAVE_GLIB_NETWORKING
255
    g_test_add_func("/nntpconnection/backend/gio/ssl", test_backend_gio_ssl);
256
#endif
257

                
258
    return g_test_run();
259
}