Debian Codegraph @

tests/t/lib/ProFTPD/Tests/SSH2.pm

raw
1 package ProFTPD::Tests::SSH2;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9 use Socket;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 ssh2_session => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 };
25
26 sub new {
27 return shift()->SUPER::new(@_);
28 }
29
30 sub list_tests {
31 return testsuite_get_runnable_tests($TESTS);
32 }
33
34 sub ssh2_session {
35 my $self = shift;
36 my $tmpdir = $self->{tmpdir};
37 my $setup = test_setup($tmpdir, 'ssh2');
38
39 my $config = {
40 PidFile => $setup->{pid_file},
41 ScoreboardFile => $setup->{scoreboard_file},
42 SystemLog => $setup->{log_file},
43
44 AuthUserFile => $setup->{auth_user_file},
45 AuthGroupFile => $setup->{auth_group_file},
46 AuthOrder => 'mod_auth_file.c',
47
48 IfModules => {
49 'mod_delay.c' => {
50 DelayEngine => 'off',
51 },
52 },
53 };
54
55 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
56 $config);
57
58 # Open pipes, for use between the parent and child processes. Specifically,
59 # the child will indicate when it's done with its test by writing a message
60 # to the parent.
61 my ($rfh, $wfh);
62 unless (pipe($rfh, $wfh)) {
63 die("Can't open pipe: $!");
64 }
65
66 my $ex;
67
68 # Fork child
69 $self->handle_sigchld();
70 defined(my $pid = fork()) or die("Can't fork: $!");
71 if ($pid) {
72 eval {
73 sleep(1);
74
75 my $client_opts = {
76 PeerHost => '127.0.0.1',
77 PeerPort => $port,
78 Proto => 'tcp',
79 Type => SOCK_STREAM,
80 Timeout => 10
81 };
82
83 my $client = IO::Socket::INET->new(%$client_opts);
84 unless ($client) {
85 die("Can't connect to 127.0.0.1:$port: $!");
86 }
87
88 my $banner = <$client>;
89 if ($ENV{TEST_VERBOSE}) {
90 print STDERR "# Received banner: $banner";
91 }
92
93 my $req = "SSH-2.0-OpenSSH_5.6p1\r\n";
94 if ($ENV{TEST_VERBOSE}) {
95 print STDERR "# Sending request: $req";
96 }
97 $client->print($req);
98 $client->flush();
99
100 my $resp = <$client>;
101 if ($ENV{TEST_VEROBSE}) {
102 print STDERR "# Received response: $resp";
103 }
104 $client->close();
105
106 chomp($resp);
107 my $expected = '';
108 $self->assert(qr/$expected/, $resp,
109 test_msg("Expected response '$expected', got '$resp'"));
110 };
111
112 if ($@) {
113 $ex = $@;
114 }
115
116 $wfh->print("done\n");
117 $wfh->flush();
118
119 } else {
120 eval { server_wait($setup->{config_file}, $rfh) };
121 if ($@) {
122 warn($@);
123 exit 1;
124 }
125
126 exit 0;
127 }
128
129 # Stop server
130 server_stop($setup->{pid_file});
131 $self->assert_child_ok($pid);
132
133 test_cleanup($setup->{log_file}, $ex);
134 }
135
136 1;