Make automated FSCommand invocation tests show player-side output.
[gnash.git] / testsuite / network.all / test_ssh.cpp
blobb43b2632e76f2d863c7ad8f4149935c42a605db2
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>
34 #include <cstdio>
36 #include "as_object.h"
37 #include "dejagnu.h"
38 #include "http.h"
39 #include "log.h"
40 #include "buffer.h"
41 #include "network.h"
42 #include "element.h"
43 #include "sol.h"
44 #include "arg_parser.h"
45 #include "sshclient.h"
46 #include "sshserver.h"
48 using namespace amf;
49 using namespace gnash;
50 using namespace std;
52 static void usage (void);
54 static TestState runtest;
56 static string infile;
58 static void test_client();
59 static void test_server();
60 static void test_channels();
61 static SSHClient client;
62 static Network net;
64 LogFile& dbglogfile = LogFile::getDefaultInstance();
66 int
67 main(int argc, char *argv[])
69 bool servermode = false;
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 { 'p', "password", Arg_parser::yes },
78 { 'u', "user", Arg_parser::yes },
79 { 'd', "dss", Arg_parser::no },
80 { 'r', "rsa", Arg_parser::no },
81 { 'f', "sftp", Arg_parser::no },
82 { 'a', "raw", Arg_parser::no },
83 { 'n', "netdebug", Arg_parser::no },
84 { 'c', "daemon", 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 for( int i = 0; i < parser.arguments(); ++i ) {
94 const int code = parser.code(i);
95 try {
96 switch( code ) {
97 case 'h':
98 usage ();
99 exit(EXIT_SUCCESS);
100 case 'v':
101 dbglogfile.setVerbosity();
102 log_debug(_("Verbose output turned on"));
103 break;
104 case 's':
105 client.setHostname(parser.argument(i));
106 log_debug(_("Hostname for SSH connection is: %s"),
107 client.getHostname());
108 break;
109 case 'o':
110 net.setPort(parser.argument<short>(i));
111 log_debug(_("Port for SSH connections is: %hd"),
112 net.getPort());
113 break;
114 case 'p':
115 client.setPassword(parser.argument(i));
116 log_debug(_("SSH password is: %s"),
117 client.getPassword());
118 break;
119 case 'u':
120 client.setUser(parser.argument(i));
121 log_debug(_("SSH user is: %s"),
122 client.getUser());
123 break;
124 case 'd':
125 client.setAuthType(SSHClient::DSS);
126 log_debug(_("SSH Authentication type is: %d"),
127 client.getAuthType());
128 break;
129 case 'r':
130 client.setAuthType(SSHClient::RSA);
131 log_debug(_("SSH Authentication type is: %d"),
132 client.getAuthType());
133 break;
134 case 'a':
135 client.setTransportType(SSHClient::RAW);
136 log_debug(_("SSH Transport type is: %d"),
137 client.getTransportType());
138 break;
139 case 'f':
140 client.setTransportType(SSHClient::SFTP);
141 log_debug(_("SSH Transport type is: %d"),
142 client.getTransportType());
143 break;
144 case 'n':
145 net.toggleDebug(true);
146 break;
147 case 'c':
148 servermode = true;
149 log_debug(_("Enabling SSH server mode"));
150 break;
151 case 0:
152 infile = parser.argument(i);
153 log_debug(_("Input file for testing the SSH connection is: %s"), infile);
154 break;
158 catch (Arg_parser::ArgParserException &e) {
159 cerr << _("Error parsing command line options: ") << e.what() << endl;
160 cerr << _("This is a Gnash bug.") << endl;
164 if (servermode) {
165 test_server();
166 } else {
167 test_client();
170 test_channels();
171 return 0;
174 static void test_client()
176 size_t ret;
177 bool giveup = false;
179 // Make a tcp/ip connect to the server
180 if (net.createClient(client.getHostname(), SSH_PORT) == false) {
181 log_error("Can't connect to server %s", client.getHostname());
184 if (client.sshConnect(net.getFileFd())) {
185 runtest.pass("Connected to SSH server");
186 } else {
187 runtest.fail("Couldn't connect to SSH server");
188 giveup = true;
191 if (client.openChannel()) {
192 runtest.pass("Opened a new channel");
193 } else {
194 runtest.fail("Couldn't open a new channel");
195 giveup = true;
198 client.closeChannel();
199 if (client.getChannel() == 0) {
200 runtest.pass("Closed the current channel");
201 } else {
202 runtest.fail("Couldn't close the current channel");
203 giveup = true;
206 if (giveup) {
207 runtest.unresolved("Couldn't shutdown SSH connection");
208 } else {
209 if (client.sshShutdown()) {
210 runtest.pass("Shutdown SSH connection");
211 } else {
212 runtest.fail("Couldn't shutdown SSH connection");
218 static void test_server()
221 SSHServer server;
226 static void test_channels()
231 static void
232 usage (void)
234 cerr << "This program tests SSH support in the libnet library." << endl;
235 cerr << "Usage: test_ssh [hvsocpkwar]" << endl;
236 cerr << "-h\tHelp" << endl;
237 cerr << "-v\tVerbose" << endl;
238 cerr << "-s\thostname" << endl;
239 cerr << "-o\tPort" << endl;
240 cerr << "-p\tPassword" << endl;
241 cerr << "-u\tUser" << endl;
242 cerr << "-r\tUse RSA" << endl;
243 cerr << "-d\tUse DSS" << endl;
244 cerr << "-f\tUse SFTP for transport" << endl;
245 cerr << "-a\tUse RAW for transport" << endl;
246 cerr << "-n\tEnable network debug" << endl << endl;
247 cerr << "-c\tEnable SSH Server" << endl << endl;
249 cerr << "Libssh version is: " << ssh_version(0) << endl;
250 exit (-1);
253 #else
256 main(int /*argc*/, char /* *argv[]*/)
258 // nop
259 cerr << "This program needs to have DejaGnu installed!" << endl;
260 return 0;
263 #endif