summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorFlorian Schlichting <fschlich@zedat.fu-berlin.de>2012-02-22 22:32:16 +0100
committerFlorian Schlichting <fschlich@zedat.fu-berlin.de>2012-02-22 22:32:16 +0100
commit92f4e92ad208c352c84c30fcacfca98128f4e4b7 (patch)
tree31ce5acbe9c781184d47b4f2b418b91f1327f0c3 /t
Import original source of Net-OpenSSH 0.57
Diffstat (limited to 't')
-rw-r--r--t/1_run.t228
-rw-r--r--t/2_pods.t12
-rw-r--r--t/common.pm37
-rw-r--r--t/known_hosts1
-rw-r--r--t/test_server_key12
-rw-r--r--t/test_server_key.pub1
-rw-r--r--t/test_user_key12
-rw-r--r--t/test_user_key.pub1
8 files changed, 304 insertions, 0 deletions
diff --git a/t/1_run.t b/t/1_run.t
new file mode 100644
index 0000000..2b67c38
--- /dev/null
+++ b/t/1_run.t
@@ -0,0 +1,228 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Cwd;
+use File::Spec;
+use Test::More;
+
+use lib "./t";
+use common;
+
+use Net::OpenSSH;
+use Net::OpenSSH::Constants qw(OSSH_ENCODING_ERROR OSSH_MASTER_FAILED);
+
+my $timeout = 15;
+my $fallback;
+
+my $PS = find_cmd 'ps';
+defined $PS or plan skip_all => "ps command not found";
+my $LS = find_cmd('ls');
+defined $LS or plan skip_all => "ls command not found";
+my $CAT = find_cmd('cat');
+defined $CAT or plan skip_all => "cat command not found";
+my $ECHO = find_cmd('echo');
+defined $ECHO or plan skip_all => "echo command not found";
+
+my $PS_P = ($^O =~ /sunos|solaris/i ? "$PS -p" : "$PS p");
+
+# $Net::OpenSSH::debug = -1;
+
+my $V = `ssh -V 2>&1`;
+my ($ver, $num) = $V =~ /^(OpenSSH_(\d+\.\d+).*)$/msi;
+
+plan skip_all => 'OpenSSH 4.1 or later required'
+ unless (defined $num and $num >= 4.1);
+
+chomp $ver;
+diag "\nSSH client found: $ver.\nTrying to connect to localhost, timeout is ${timeout}s.\n";
+
+my %ctor_opts = (host => 'localhost',
+ timeout => $timeout,
+ strict_mode => 0,
+ master_opts => [-o => "StrictHostKeyChecking no"]);
+
+my $ssh = Net::OpenSSH->new(%ctor_opts);
+
+# fallback
+if ($ssh->error and $num > 4.7) {
+ diag "Connection failed... trying fallback aproach";
+ my $sshd_cmd = sshd_cmd;
+ if (defined $sshd_cmd) {
+ my $here = File::Spec->rel2abs("t");
+ diag join("\n", "sshd command found at $sshd_cmd.",
+ "Faking connection, timeout is ${timeout}s.",
+ "Using configuration from '$here'", "");
+ chmod 0600, "$here/test_user_key", "$here/test_server_key";;
+ my @sshd_cmd = ($sshd_cmd, '-i',
+ -h => "$here/test_server_key",
+ -o => "AuthorizedKeysFile $here/test_user_key.pub",
+ -o => "StrictModes no",
+ -o => "PasswordAuthentication no",
+ -o => "PermitRootLogin yes");
+ s/(\W)/\\$1/g for @sshd_cmd;
+
+ $ssh = Net::OpenSSH->new(%ctor_opts,
+ master_opts => [-o => "ProxyCommand @sshd_cmd",
+ -o => "StrictHostKeyChecking no",
+ -o => "NoHostAuthenticationForLocalhost yes",
+ -o => "UserKnownHostsFile $here/known_hosts",
+ -o => "GlobalKnownHostsFile $here/known_hosts"],
+ key_path => "$here/test_user_key");
+ $fallback = 1;
+ }
+ else {
+ diag "sshd command not found!"
+ }
+}
+
+plan skip_all => 'Unable to establish SSH connection to localhost!'
+ if $ssh->error;
+
+plan tests => 46;
+
+sub shell_quote {
+ my $txt = shift;
+ $txt =~ s|([^\w+\-\./])|\\$1|g;
+ $txt
+}
+
+my $muxs = $ssh->get_ctl_path;
+ok(-S $muxs, "mux socket exists");
+is((stat $muxs)[2] & 0777, 0600, "mux socket permissions");
+
+my $cwd = cwd;
+my $sq_cwd = shell_quote $cwd;
+
+my $rshell = $ssh->capture($ECHO => '$SHELL');
+my $rshell_is_csh = ($rshell =~ /\bcsh$/);
+
+my @ls_good= sort `$LS $sq_cwd`;
+my @ls = sort $ssh->capture({stderr_to_stdout => 1}, "$LS $sq_cwd");
+is("@ls", "@ls_good");
+
+my @lines = map "foo $_\n", 1..10;
+my $lines = join('', @lines);
+
+my ($in, $pid) = $ssh->pipe_in("$CAT > $sq_cwd/test.dat");
+ok($ssh->error == 0);
+ok($in);
+ok(defined $pid);
+
+print $in $_ for @lines;
+my @ps = `$PS_P $pid`;
+ok(grep(/ssh/i, @ps));
+ok(close $in);
+@ps = `$PS_P $pid`;
+ok(!grep(/ssh/i, @ps));
+
+ok(-f "$cwd/test.dat");
+
+my ($output, $errput) = $ssh->capture2("$CAT $sq_cwd/test.dat");
+is($errput, '', "errput");
+is($output, $lines, "output") or diag $output;
+
+{
+ my $ssh2 = Net::OpenSSH->new(external_master => 1, ctl_path => $ssh->get_ctl_path);
+ my ($output, $errput) = $ssh2->capture2("$CAT $sq_cwd/test.dat");
+ is($errput, '', "external_master 1");
+ is($output, $lines, "external_master 2") or diag $output;
+ # DESTROY $ssh2
+}
+ok($ssh->check_master, "check_master") or diag "error: ", $ssh->error;
+
+$ssh->system({stdout_file => ['>', "$sq_cwd/test.dat.deleteme"],
+ stderr_discard => 1 }, "$CAT $sq_cwd/test.dat");
+is ($ssh->error, 0, "system ok");
+$output = $ssh->capture("$CAT $sq_cwd/test.dat.deleteme");
+is ($ssh->error, 0, "system ok") or diag "error: ", $ssh->error;
+is ($output, $lines, "redirection works");
+unlink "$sq_cwd/test.dat.deleteme";
+
+$output = $ssh->capture(cd => $sq_cwd, \\'&&', $CAT => 'test.dat');
+is ($output, $lines) or diag "error: ", $ssh->error;
+
+$output = $ssh->capture({stdin_data => \@lines}, $CAT);
+is ($output, $lines);
+
+SKIP: {
+ skip "remote shell is csh", 3 if $rshell_is_csh;
+ $output = $ssh->capture({stdin_data => \@lines, stderr_to_stdout => 1}, "$CAT >&2");
+ is ($output, $lines);
+
+ ($output, $errput) = $ssh->capture2("$CAT $sq_cwd/test.dat 1>&2");
+ is ($errput, $lines);
+ is ($output, '');
+}
+
+my $fh = $ssh->pipe_out("$CAT $sq_cwd/test.dat");
+ok($fh, "pipe_out");
+$output = join('', <$fh>);
+is($output, $lines, "pipe_out lines");
+
+my $string = q(#@$#$%&(@#_)erkljgfd'' 345345' { { / // ///foo);
+
+$output = $ssh->capture(echo => $string);
+chomp $output;
+is ($output, $string, "quote_args");
+
+$string .= "\nline1\nline2";
+
+$output = $ssh->capture(echo => $string);
+chomp $output;
+is ($output, $string, "quote_args with new lines");
+
+eval { $ssh->capture({foo => 1}, 'bar') };
+ok($@ =~ /option/ and $@ =~ /foo/);
+
+is ($ssh->shell_quote('/foo/'), '/foo/');
+is ($ssh->shell_quote('./foo*/bar&biz;'), "'./foo*/bar&biz;'");
+is (Net::OpenSSH->shell_quote('./foo*/bar&biz;'), "'./foo*/bar&biz;'");
+is ($ssh->_quote_args({quote_args => 1, glob_quoting => 1}, './foo*/bar&biz;'), "./foo*/bar'&biz;'");
+is ($ssh->shell_quote_glob('./foo*/bar&biz;'), "./foo*/bar'&biz;'");
+is (Net::OpenSSH->shell_quote_glob('./foo*/bar&biz;'), "./foo*/bar'&biz;'");
+
+$ssh->set_expand_vars(1);
+$ssh->set_var(FOO => 'Bar');
+is ($ssh->shell_quote(\\'foo%FOO%foo%%foo'), 'fooBarfoo%foo');
+is ($ssh->shell_quote('foo%FOO%foo%%foo'), "'fooBarfoo\%foo'");
+$ssh->set_expand_vars(0);
+is ($ssh->shell_quote(\\'foo%FOO%foo%%foo'), 'foo%FOO%foo%%foo');
+is (Net::OpenSSH->shell_quote(\\'foo%FOO%foo%%foo'), 'foo%FOO%foo%%foo');
+
+my $enne = "letra e\xf1e";
+
+$ssh->capture({encoding => 'ascii'}, $ECHO => $enne);
+is ($ssh->error+0, OSSH_ENCODING_ERROR, "bad encoding");
+$ssh->wait_for_master;
+is ($ssh->error, 0, "wait_for_master resets error");
+$ssh->capture({encoding => 'ascii'}, $ECHO => $enne);
+is ($ssh->error+0, OSSH_ENCODING_ERROR, "bad encoding");
+my $captured_enne = $ssh->capture({encoding => 'latin1'}, $ECHO => $enne);
+chomp $captured_enne;
+is ($ssh->error+0, 0, "good encoding");
+is ($captured_enne, $enne, "capture and encoding");
+
+my $rcmd = $ssh->make_remote_command($ECHO => 'hello');
+my $pipe_out = readpipe $rcmd;
+chomp $pipe_out;
+is ($pipe_out, 'hello', 'make_remote_command');
+
+eval {
+ my $ssh3 = $ssh;
+ undef $ssh;
+ die "some text";
+};
+like($@, qr/^some text/, 'DESTROY should not clobber $@');
+
+SKIP: {
+ skip "no login with default key", 1 if $fallback;
+ my $ssh4;
+ for my $passwd (qw(foo bar)) {
+ $ssh4 = eval { Net::OpenSSH->new(%ctor_opts, passwd => $passwd, master_stderr_discard => 1) };
+ $@ and $@ =~ /IO::Pty/ and skip "no IO::Pty", 1;
+ last if $ssh4->error;
+ }
+ is ($ssh4->error+0, OSSH_MASTER_FAILED, "bad password");
+}
+
diff --git a/t/2_pods.t b/t/2_pods.t
new file mode 100644
index 0000000..e8d1f76
--- /dev/null
+++ b/t/2_pods.t
@@ -0,0 +1,12 @@
+#!/usr/bin/perl
+
+use strict;
+use Test::More;
+
+plan skip_all => "Only the author needs to check that POD docs are right"
+ unless eval "no warnings; getlogin eq 'salva'";
+
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+
+all_pod_files_ok( all_pod_files( qw(blib) ) );
diff --git a/t/common.pm b/t/common.pm
new file mode 100644
index 0000000..ea30b3a
--- /dev/null
+++ b/t/common.pm
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+
+use File::Spec;
+
+sub sshd_cmd {
+ my $sc_name = 'sshd';
+
+ my @paths = qw( /usr
+ /usr/local
+ /usr/local/openssh
+ /opt/ssh
+ /opt/openssh );
+
+ @paths = map { ("$_/sbin/sshd", "$_/bin/sshd") } @paths;
+
+ for my $sshd (@paths) {
+ return $sshd if -x $sshd;
+ }
+}
+
+sub find_cmd {
+ my @path = qw(/usr/bin /bin
+ /usr/local/bin
+ /usr/sbin /sbin
+ /opt/bin );
+
+ for my $cmd (@_) {
+ for my $path (@path) {
+ my $r = "$path/$cmd";
+ return $r if -x $r;
+ }
+ }
+ undef;
+}
+
+1;
diff --git a/t/known_hosts b/t/known_hosts
new file mode 100644
index 0000000..53338ff
--- /dev/null
+++ b/t/known_hosts
@@ -0,0 +1 @@
+localhost ssh-dss AAAAB3NzaC1kc3MAAACBAMdjQ+dwZuRXkLQr0jcobWetHX7t6Y1zKjaizEAR37OuK4OsHnsRHJg6APgkmtxcIStKnCu45KQ67Pg9hKr80QXr3UsG8/NaQeaf4uUqoh6iZK4d8PObQXpYzXsbuxF+2l0WYGB+NpkhGz2YmSKaFYZqquMJlv+vP7EcCMj6JFn9AAAAFQCGuBlTM7Xd0gHLrmC8mQpSygywuwAAAIEAm91Rq84l1YbtSDQ6w6lHfDEWAg1sMp1xPvpMd1hLGAIt/M5Oyw8dgGC8aXpchAH6IaeciqvvBvRaOOp+fvq9x25fmLAG8FXYFtb6dCyOre/UdDOLYC121skPPnHuzJEARr1fQR7cFa5+o13o6gfd8lCQFd8xXvIsUCMVOB9lEocAAACAEvIpE9Fce0eecx7BAeY7wwzhiNsgCp9ddb55FpRAYmJtWjJsi9U5eANmagK25lOCKG7yFIkdS0m8oCy9KJ7sN8umdpYwk5c1K60luLY6vP+STQ400OGFce7yka6ern0rWC6uMgWn2KSf+kJzVEyoy+Re5s01bb0Zm5StWxq9o4I=
diff --git a/t/test_server_key b/t/test_server_key
new file mode 100644
index 0000000..7c3bfbf
--- /dev/null
+++ b/t/test_server_key
@@ -0,0 +1,12 @@
+-----BEGIN DSA PRIVATE KEY-----
+MIIBvAIBAAKBgQDHY0PncGbkV5C0K9I3KG1nrR1+7emNcyo2osxAEd+zriuDrB57
+ERyYOgD4JJrcXCErSpwruOSkOuz4PYSq/NEF691LBvPzWkHmn+LlKqIeomSuHfDz
+m0F6WM17G7sRftpdFmBgfjaZIRs9mJkimhWGaqrjCZb/rz+xHAjI+iRZ/QIVAIa4
+GVMztd3SAcuuYLyZClLKDLC7AoGBAJvdUavOJdWG7Ug0OsOpR3wxFgINbDKdcT76
+THdYSxgCLfzOTssPHYBgvGl6XIQB+iGnnIqr7wb0Wjjqfn76vcduX5iwBvBV2BbW
++nQsjq3v1HQzi2AtdtbJDz5x7syRAEa9X0Ee3BWufqNd6OoH3fJQkBXfMV7yLFAj
+FTgfZRKHAoGAEvIpE9Fce0eecx7BAeY7wwzhiNsgCp9ddb55FpRAYmJtWjJsi9U5
+eANmagK25lOCKG7yFIkdS0m8oCy9KJ7sN8umdpYwk5c1K60luLY6vP+STQ400OGF
+ce7yka6ern0rWC6uMgWn2KSf+kJzVEyoy+Re5s01bb0Zm5StWxq9o4ICFQCAmnEu
+JHNcezcg8mLkW9gEfwhISw==
+-----END DSA PRIVATE KEY-----
diff --git a/t/test_server_key.pub b/t/test_server_key.pub
new file mode 100644
index 0000000..598ee75
--- /dev/null
+++ b/t/test_server_key.pub
@@ -0,0 +1 @@
+ssh-dss AAAAB3NzaC1kc3MAAACBAMdjQ+dwZuRXkLQr0jcobWetHX7t6Y1zKjaizEAR37OuK4OsHnsRHJg6APgkmtxcIStKnCu45KQ67Pg9hKr80QXr3UsG8/NaQeaf4uUqoh6iZK4d8PObQXpYzXsbuxF+2l0WYGB+NpkhGz2YmSKaFYZqquMJlv+vP7EcCMj6JFn9AAAAFQCGuBlTM7Xd0gHLrmC8mQpSygywuwAAAIEAm91Rq84l1YbtSDQ6w6lHfDEWAg1sMp1xPvpMd1hLGAIt/M5Oyw8dgGC8aXpchAH6IaeciqvvBvRaOOp+fvq9x25fmLAG8FXYFtb6dCyOre/UdDOLYC121skPPnHuzJEARr1fQR7cFa5+o13o6gfd8lCQFd8xXvIsUCMVOB9lEocAAACAEvIpE9Fce0eecx7BAeY7wwzhiNsgCp9ddb55FpRAYmJtWjJsi9U5eANmagK25lOCKG7yFIkdS0m8oCy9KJ7sN8umdpYwk5c1K60luLY6vP+STQ400OGFce7yka6ern0rWC6uMgWn2KSf+kJzVEyoy+Re5s01bb0Zm5StWxq9o4I= salva@ubuntu
diff --git a/t/test_user_key b/t/test_user_key
new file mode 100644
index 0000000..af33ba0
--- /dev/null
+++ b/t/test_user_key
@@ -0,0 +1,12 @@
+-----BEGIN DSA PRIVATE KEY-----
+MIIBvAIBAAKBgQDc6cTc1LgDorqq9svyYwJXShjgZ84iBCbJz71a5cK6gjwSYxCg
+EvWHzp2NrxVux2XpiPeqF7DCSwW/O74pdVKpYG3Eql/ngohKvJx/YW8QyM+TQKCS
+Bf+QojyBuS3fbT1JmTzo3pYC1Ev6okViAvxinPxNYFbSfzh2lwGbiurc8QIVAJID
+Xn9MHX+/saXqFC4F9WYUcCg1AoGBALTDDqS+qM6hhVWQmdxQSZA+JkP8Xs5KDaT8
+9N1zZtzj9eUT9Hldv2zJsfVZgv3ZGNKhm3W2WhISLRBC+mNiEMJCbUv/Gzdomast
+R6OhSAeGcSYzwijZwudlxgUr/E+BTfbJDPB025jSWhCB9bivjF7oe4MaAWJy6ZDp
+PPpdxMVwAoGAGchZjWis/buTlH7QMyDEgsIjKHEvn2qC49cpW7L0fzgOj9vYMbYG
+LRqMfmraYbvTRLukD1cJmmFbqkSHlQYWcF28ddyf4w8nUQ9T4IX2/aVo6xdlx6l9
+yp5BV+gczfITpNOOLqK01ir2C1SGj8sw/w6FkUWEbupSVd4Wrn5PLc4CFQCAnD/+
+qldMd3HqFCcV4KZwIEBr3Q==
+-----END DSA PRIVATE KEY-----
diff --git a/t/test_user_key.pub b/t/test_user_key.pub
new file mode 100644
index 0000000..dee97ae
--- /dev/null
+++ b/t/test_user_key.pub
@@ -0,0 +1 @@
+ssh-dss AAAAB3NzaC1kc3MAAACBANzpxNzUuAOiuqr2y/JjAldKGOBnziIEJsnPvVrlwrqCPBJjEKAS9YfOnY2vFW7HZemI96oXsMJLBb87vil1UqlgbcSqX+eCiEq8nH9hbxDIz5NAoJIF/5CiPIG5Ld9tPUmZPOjelgLUS/qiRWIC/GKc/E1gVtJ/OHaXAZuK6tzxAAAAFQCSA15/TB1/v7Gl6hQuBfVmFHAoNQAAAIEAtMMOpL6ozqGFVZCZ3FBJkD4mQ/xezkoNpPz03XNm3OP15RP0eV2/bMmx9VmC/dkY0qGbdbZaEhItEEL6Y2IQwkJtS/8bN2iZqy1Ho6FIB4ZxJjPCKNnC52XGBSv8T4FN9skM8HTbmNJaEIH1uK+MXuh7gxoBYnLpkOk8+l3ExXAAAACAGchZjWis/buTlH7QMyDEgsIjKHEvn2qC49cpW7L0fzgOj9vYMbYGLRqMfmraYbvTRLukD1cJmmFbqkSHlQYWcF28ddyf4w8nUQ9T4IX2/aVo6xdlx6l9yp5BV+gczfITpNOOLqK01ir2C1SGj8sw/w6FkUWEbupSVd4Wrn5PLc4= salva@ubuntu