ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-ssl.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-sync.c - Test program for synchronous operation of the GNOME
3    Virtual File System.
4
5    Copyright (C) 1999 Free Software Foundation
6
7    The Gnome Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The Gnome Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the Gnome Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21
22    Author: Ian McKellar <yakk@yakk.net> 
23  */
24
25 #include <config.h>
26
27 #include <glib/gmessages.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
29 #include <libgnomevfs/gnome-vfs-socket-buffer.h>
30 #include <libgnomevfs/gnome-vfs-socket.h>
31 #include <libgnomevfs/gnome-vfs-ssl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 enum {
38         SSL, SOCKET, SOCKETBUFFER
39 } abstraction = SOCKETBUFFER;
40
41 static void
42 show_result (GnomeVFSResult result, 
43              const gchar *what, 
44              const gchar *host, 
45              gint port)
46 {
47         fprintf (stderr, "%s `%s:%d': %s\n",
48                  what, host, port, gnome_vfs_result_to_string (result));
49         if (result != GNOME_VFS_OK)
50                 exit (1);
51 }
52
53 #define HTTP_REQUEST "GET / HTTP/1.0\r\n\r\n"
54
55 int
56 main (int argc, char **argv)
57 {
58         GnomeVFSResult        result = GNOME_VFS_OK;
59         gchar                 buffer[1024];
60         gchar                *host;
61         gint                  port;
62         GnomeVFSFileSize      bytes_read;
63         GnomeVFSSSL          *ssl = NULL;
64         GnomeVFSSocket       *socket = NULL;
65         GnomeVFSSocketBuffer *socketbuffer = NULL;
66
67         if (argc != 3) {
68                 printf ("Usage: %s <host> <port>\n", argv[0]);
69                 return 1;
70         }
71
72         host = argv[1];
73         port = atoi (argv[2]);
74
75         if (port <= 0) {
76                 printf ("Invalid port\n");
77                 return 1;
78         }
79
80         if (! gnome_vfs_init ()) {
81                 fprintf (stderr, "Cannot initialize gnome-vfs.\n");
82                 return 1;
83         }
84
85         switch (abstraction) {
86                 case SOCKETBUFFER:
87                         g_print ("Testing GnomeVFSSocketBuffer");
88                 case SOCKET:
89                         g_print (" and GnomeVFSSocket");
90                 case SSL:
91                         g_print (" and GnomeVFSSSL");
92         }
93         g_print (".\n");
94
95         result = gnome_vfs_ssl_create (&ssl, host, port);
96
97         show_result (result, "ssl_create", host, port);
98
99         if (ssl == NULL) {
100                 fprintf (stderr, "couln't connect\n");
101                 return -1;
102         }
103
104         if (abstraction >= SOCKET) {
105                 socket = gnome_vfs_ssl_to_socket (ssl);
106                 if (socket == NULL) {
107                         fprintf (stderr, "couldn't create socket object\n");
108                         return -1;
109                 }
110
111                 if (abstraction == SOCKETBUFFER) {
112                         socketbuffer = gnome_vfs_socket_buffer_new (socket);
113                         if (socketbuffer == NULL) {
114                                 fprintf (stderr, 
115                                        "couldn't create socketbuffer object\n");
116                                 return -1;
117                         }
118                 }
119         }
120
121         switch (abstraction) {
122                 case SSL:
123                         result = gnome_vfs_ssl_write (ssl, HTTP_REQUEST, 
124                                         strlen(HTTP_REQUEST), &bytes_read);
125                         break;
126                 case SOCKET:
127                         result = gnome_vfs_socket_write (socket, HTTP_REQUEST, 
128                                         strlen(HTTP_REQUEST), &bytes_read);
129                         break;
130                 case SOCKETBUFFER:
131                         result = gnome_vfs_socket_buffer_write (socketbuffer,
132                                         HTTP_REQUEST, strlen(HTTP_REQUEST),
133                                         &bytes_read);
134                         gnome_vfs_socket_buffer_flush (socketbuffer);
135                         break;
136         }
137
138         show_result (result, "write", host, port);
139
140         while( result==GNOME_VFS_OK ) {
141                 switch (abstraction) {
142                         case SSL:
143                                 result = gnome_vfs_ssl_read (ssl, buffer, 
144                                                 sizeof buffer - 1, &bytes_read);
145                                 break;
146                         case SOCKET:
147                                 result = gnome_vfs_socket_read (socket, buffer, 
148                                                 sizeof buffer - 1, &bytes_read);
149                                 break;
150                         case SOCKETBUFFER:
151                                 result = gnome_vfs_socket_buffer_read (
152                                                 socketbuffer, buffer, 
153                                                 sizeof buffer - 1, &bytes_read);
154                                 break;
155                 }
156                 show_result (result, "read", host, port);
157         
158                 buffer[bytes_read] = 0;
159                 write (1,buffer,bytes_read);
160                 if(!bytes_read) break;
161         }
162
163         switch (abstraction) {
164                 case SSL:
165                         gnome_vfs_ssl_destroy (ssl);
166                         break;
167                 case SOCKET:
168                         gnome_vfs_socket_close (socket);
169                         break;
170                 case SOCKETBUFFER:
171                         gnome_vfs_socket_buffer_destroy (socketbuffer, TRUE);
172                         break;
173         }
174
175         return 0;
176 }