Make automated FSCommand invocation tests show player-side output.
[gnash.git] / testsuite / network.all / test_ssl.cpp
blob6d1f0a49901ef7504e9741c8e8a6a88104ab8804
1 //
2 // Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
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 3 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #ifdef HAVE_CONFIG_H
19 #include "gnashconfig.h"
20 #endif
22 #ifdef HAVE_DEJAGNU_H
24 //#include <netinet/in.h>
25 #include <string>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <iostream>
33 #include <string>
35 #include "as_object.h"
36 #include "dejagnu.h"
37 #include "http.h"
38 #include "log.h"
39 #include "openssl/ssl.h"
40 #include "sslclient.h"
41 #include "buffer.h"
42 #include "network.h"
43 #include "element.h"
44 #include "sol.h"
45 #include "arg_parser.h"
46 #include "sslclient.h"
47 #include "sslserver.h"
49 using namespace amf;
50 using namespace gnash;
51 using namespace std;
53 static void usage (void);
55 static TestState runtest;
57 static string infile;
59 static void test_client();
60 static void test_server();
62 static SSLClient client;
63 SSLServer server;
64 static Network net;
66 LogFile& dbglogfile = LogFile::getDefaultInstance();
68 int
69 main(int argc, char *argv[])
71 const Arg_parser::Option opts[] =
73 { 'h', "help", Arg_parser::no },
74 { 'v', "verbose", Arg_parser::no },
75 { 's', "hostname", Arg_parser::yes },
76 { 'o', "port", Arg_parser::yes },
77 { 'c', "cert", Arg_parser::yes },
78 { 'p', "pem", Arg_parser::yes },
79 { 'k', "keyfile", Arg_parser::yes },
80 { 'w', "password", Arg_parser::yes },
81 { 'a', "calist", Arg_parser::yes },
82 { 'r', "rootpath", Arg_parser::yes },
83 { 'n', "netdebug", Arg_parser::no },
84 { 'e', "dsemon", Arg_parser::no },
87 Arg_parser parser(argc, argv, opts);
88 if( ! parser.error().empty() ) {
89 cout << parser.error() << endl;
90 exit(EXIT_FAILURE);
93 #ifdef USE_SSL
94 bool servermode = false;
96 for( int i = 0; i < parser.arguments(); ++i ) {
97 const int code = parser.code(i);
98 try {
99 switch( code ) {
100 case 'h':
101 usage ();
102 exit(EXIT_SUCCESS);
103 case 'v':
104 dbglogfile.setVerbosity();
105 log_debug(_("Verbose output turned on"));
106 break;
107 case 's':
108 client.setHostname(parser.argument(i));
109 log_debug(_("Hostname for SSL connection is: %s"),
110 client.getHostname());
111 break;
112 case 'o':
113 net.setPort(parser.argument<short>(i));
114 log_debug(_("Port for SSL connections is: %hd"),
115 net.getPort());
116 break;
117 case 'c':
118 client.setCert(parser.argument(i));
119 log_debug(_("Cert file for SSL connection is: %s"),
120 client.getCert());
121 break;
122 case 'p':
123 client.setPem(parser.argument(i));
124 log_debug(_("Pem file for SSL connection is: %s"),
125 client.getPem());
126 break;
127 case 'k':
128 client.setKeyfile(parser.argument(i));
129 log_debug(_("Keyfile file for SSL connection is: %s"),
130 client.getKeyfile());
131 break;
132 case 'a':
133 client.setCAlist(parser.argument(i));
134 log_debug(_("CA List file for SSL connection is: %s"),
135 client.getCAlist());
136 break;
137 case 'r':
138 client.setRootPath(parser.argument(i));
139 server.setRootPath(parser.argument(i));
140 log_debug(_("Root path for SSL pem files is: %s"),
141 client.getRootPath());
142 break;
143 case 'w':
144 client.setPassword(parser.argument(i));
145 log_debug(_("Password for SSL pem files is: %s"),
146 client.getPassword());
147 break;
148 case 'n':
149 net.toggleDebug(true);
150 break;
151 case 'e':
152 servermode = true;
153 log_debug(_("Enabling SSL server mode"));
154 break;
155 case 0:
156 infile = parser.argument(i);
157 log_debug(_("Input file for testing the SSL connection is: %s"), infile);
158 break;
162 catch (Arg_parser::ArgParserException &e) {
163 cerr << _("Error parsing command line options: ") << e.what() << endl;
164 cerr << _("This is a Gnash bug.") << endl;
168 if (servermode) {
169 test_server();
170 } else {
171 test_client();
173 #endif
174 return 0;
177 #ifdef USE_SSL
178 static void test_client()
180 size_t ret;
181 bool giveup = false;
183 #if 0
184 // Make a tcp/ip connect to the server
185 if (net.createClient(client.getHostname()) == false) {
186 log_error("Can't connect to server %s", client.getHostname());
188 #endif
190 if (client.sslConnect(net.getFileFd(), client.getHostname(), net.getPort())) {
191 runtest.pass("Connected to SSL server");
192 } else {
193 runtest.fail("Couldn't connect to SSL server");
194 giveup = true;
197 // I haven't seen a password with the first character set to
198 // zero ever. so we assume it got set correctly by the callback.
199 if (client.getPassword()[0] != 0) {
200 runtest.pass("Password was set for SSL connection");
201 } else {
202 if (giveup) {
203 runtest.unresolved("Password wasn't set for SSL connection");
204 } else {
205 runtest.fail("Password wasn't set for SSL connection");
209 if (giveup) {
210 runtest.unresolved("Cert didn't match hostfor SSL connection");
211 } else {
212 if (client.checkCert()) {
213 runtest.pass("Cert matched host for SSL connection");
214 } else {
215 runtest.fail("Cert didn't match host for SSL connection");
219 HTTP http;
221 if (giveup) {
222 runtest.unresolved("Couldn't write to SSL connection");
223 } else {
224 amf::Buffer &request = http.formatRequest("/crossdomain.xml", HTTP::HTTP_GET);
226 if ((ret = client.sslWrite(request)) == request.allocated()) {
227 runtest.pass("Wrote bytes to SSL connection");
228 } else {
229 runtest.fail("Couldn't write to SSL connection.");
233 #if 0
234 // This blocks forever unless data is received.
235 if (giveup) {
236 runtest.unresolved("Couldn't read bytes from SSL connection");
237 } else {
238 amf::Buffer buf;
239 if ((ret = client.sslRead(buf)) > 0) {
240 runtest.pass("Read bytes from SSL connection");
241 } else {
242 runtest.fail("Couldn't read bytes to SSL connection.");
245 #endif
247 if (giveup) {
248 runtest.unresolved("Couldn't shutdown SSL connection");
249 } else {
250 if (client.sslShutdown()) {
251 runtest.pass("Shutdown SSL connection");
252 } else {
253 runtest.fail("Couldn't shutdown SSL connection");
259 static void test_server()
261 log_debug("Starting SSL Server");
263 // The por is set by the command line arguments
264 net.createServer();
266 net.newConnection();
268 server.sslAccept(net.getFileFd());
271 #endif
273 static void
274 usage (void)
276 cerr << "This program tests SSL support in the libnet library." << endl;
277 cerr << "Usage: test_ssl [hvsocpkwar]" << endl;
278 cerr << "-h\tHelp" << endl;
279 cerr << "-v\tVerbose" << endl;
280 cerr << "-s\thostname" << endl;
281 cerr << "-o\tPort" << endl;
282 cerr << "-c\tCert File" << endl;
283 cerr << "-p\tPem file" << endl;
284 cerr << "-k\tKeyfile file" << endl;
285 cerr << "-w\tPassword" << endl;
286 cerr << "-a\tCA List" << endl;
287 cerr << "-r\tRoot path" << endl;
288 cerr << "-e\tServer mode" << endl;
289 exit (-1);
292 #else
295 main(int /*argc*/, char /* *argv[]*/)
297 // nop
298 cerr << "This program needs to have DejaGnu installed!" << endl;
299 return 0;
302 #endif