File: avoid_blocking_on_data

package info (click to toggle)
sslh 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,960 kB
  • sloc: ansic: 7,681; perl: 683; sh: 356; makefile: 136
file content (277 lines) | stat: -rw-r--r-- 11,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
--- a/t
+++ b/t
@@ -1,4 +1,4 @@
-#! /usr/bin/perl -w
+#! /usr/bin/perl
 
 # Test script for sslh
 
@@ -6,12 +6,16 @@
 # with:
 # cpan Conf::Libconfig
 
+use warnings;
 use strict;
 use IO::Socket::INET6;
 use IO::Socket::INET;
+use IO::Socket qw(SHUT_WR);
 use Socket qw/MSG_DONTWAIT/;
 use Test::More qw/no_plan/;
 use File::Temp qw(tempdir);
+use Data::Dumper qw(Dumper);
+use Carp qw(confess carp);
 # Because nothing else in Debian uses Conf::Libconfig, and I
 # (don@debian.org) don't want to package it, we have hard coded
 # test.cfg
@@ -71,7 +75,7 @@
 my $user = (getpwuid $<)[0]; # Run under current username
 
 # Which tests do we run
-my $SSH_SHY_CNX =       0;
+my $SSH_SHY_CNX =       1;
 my $PROBES_NOFRAG =     1;
 my $PROBES_AGAIN =      1;
 my $SSL_MIX_SSH =       1;
@@ -93,12 +97,12 @@
 
 sub verbose_exec
 {
-    my ($cmd) = @_;
-
-    warn "$cmd\n";
-    if (!fork) {
-        exec $cmd;
+    warn "@_\n";
+    my $pid;
+    if (!($pid = fork)) {
+        exec @_;
     }
+    return $pid;
 }
 
 # We want to keep track of tests to print a report at the
@@ -133,6 +137,8 @@
 # no_frag: don't print byte-per-byte
 sub test_probe {
     my (%opts) = @_;
+    print "test_probe called with";
+    print Dumper(\%opts);
 
     my $cnx = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
     warn "Unable to open socket to $sslh_port $!\n" unless $cnx;
@@ -140,26 +146,27 @@
 
     my $pattern = $opts{data};
     if (not defined $pattern) {
-        use Carp qw(confess);
         confess("Pattern not given");
     }
     if ($opts{no_frag}) {
-        syswrite $cnx, $pattern;
+        $cnx->send($pattern);
     } else {
         while (length $pattern) {
-            syswrite $cnx, (substr $pattern, 0, 1, '');
+            $cnx->send(substr $pattern, 0, 1, '');
+            # Timeout for a hundreth of a second
             select undef, undef, undef, .01;
         }
     }
-
+    
     my $data;
-    my $n = sysread $cnx, $data, 1024;
-    if (not defined $data) {
-        use Carp qw(confess);
-        confess("No data received: $opts{binary}:$opts{expected}")
+    my $n = retry_recv($cnx,$data, 1024, MSG_DONTWAIT);
+    if (not defined $data or length($data) == 0) {
+        carp("No data received: $opts{binary}:$opts{expected}");
+        $data = "";
     }
     $data =~ /^(.*?): /;
     my $prefix = $1;
+    $prefix = "" if not defined $prefix;
     $data =~ s/$prefix: //g;
     print "Received $n bytes: protocol $prefix data [$data]\n";
     close $cnx;
@@ -169,6 +176,21 @@
     my_is($data, $opts{data}, "$opts{binary}:$opts{expected}: data shoveled correctly");
 }
 
+sub retry_recv {
+    foreach my $i (0..5) {
+        my $peername = $_[0]->recv(@_[1..$#_]);
+        if (length($_[1]) > 0) {
+            print "Read $_[1] from peer: '$peername'\n";
+            return length($_[1]);
+        }
+        print "Would block, sleeping. Iteration: $i socket: ".\
+            $_[0]->sockhost() . ":" . $_[0]->sockport() ."\n";
+        # some tests need to hit the 10 second timeout.
+        sleep 1 + ($i+1)**2;
+    }
+}
+
+
 # Test all probes, with or without fragmentation
 # options:
 #     no_frag: write test patterns all at once (also
@@ -180,7 +202,7 @@
     my @probes = @{$conf->{"protocols"}};
     foreach my $p (@probes) {
         my %protocols = (
-            'ssh' => { data => "SSH-2.0 tester" },
+            'ssh' => { data => "SSH-2.0 tester\n" },
             'socks5' => { data => "\x05\x04\x01\x02\x03\x04" },
             'http' => { 
                 data => "GET index.html HTTP/1.1",
@@ -263,7 +285,7 @@
         $prefix = make_sni_alpn_name($s);
     }
 
-    verbose_exec "./echosrv --listen $s->{host}:$s->{port} --prefix '$prefix: '";
+    verbose_exec("./echosrv","--listen","$s->{host}:$s->{port}", "--prefix","$prefix: ");
 }
 
 # Write out test.cfg with new unused ports
@@ -338,31 +360,32 @@
 
 # Start sslh with the right plumbing
     my ($sslh_pid, $valgrind);
-    if (!($sslh_pid = fork)) {
+    #if (!($sslh_pid = fork)) {
         my $user = (getpwuid $<)[0]; # Run under current username
-        my $cmd = "./$binary -f -u $user -F test.cfg";
+        my @cmd = ("./$binary", "-f", "-u", $user, "-F", "test.cfg");
         #$valgrind = 1;
         #$cmd = "valgrind --leak-check=full $cmd";
-        verbose_exec $cmd;
-        exit 0;
-    }
+        $sslh_pid = verbose_exec @cmd;
+        #exit 0;
+    #}
     warn "spawned $sslh_pid\n";
     sleep 1; # Give everyone some time to start
     sleep 5 if $valgrind;  # valgrind can be heavy -- wait 5 seconds
 
 
     my $test_data = "hello world\n";
-    my $ssl_test_data =  "\x16\x03\x01\x00\xab\x01\x00\x00\xa7\x03\x03\x89\x22\x33\x95\x43\x7a\xc3\x89\x45\x51\x12\x3c\x28\x24\x1b\x6a\x78\xbf\xbe\x95\xd8\x90\x58\xd7\x65\xf7\xbb\x2d\xb2\x8d\xa0\x75\x00\x00\x38\xc0\x2c\xc0\x30\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0\x2b\xc0\x2f\x00\x9e\xc0\x24\xc0\x28\x00\x6b\xc0\x23\xc0\x27\x00\x67\xc0\x0a\xc0\x14\x00\x39\xc0\x09\xc0\x13\x00\x33\x00\x9d\x00\x9c\x00\x3d\x00\x3c\x00\x35\x00\x2f\x00\xff\x01\x00\x00\x46\x00\x0b\x00\x04\x03\x00\x01\x02\x00\x0a\x00\x0a\x00\x08\x00\x1d\x00\x17\x00\x19\x00\x18\x00\x23\x00\x00\x00\x0d\x00\x20\x00\x1e\x06\x01\x06\x02\x06\x03\x05\x01\x05\x02\x05\x03\x04\x01\x04\x02\x04\x03\x03\x01\x03\x02\x03\x03\x02\x01\x02\x02\x02\x03\x00\x16\x00\x00\x00\x17\x00\x00hello tls alone";
+    my $ssl_test_data =  "\x16\x03\x01\x00\xab\x01\x00\x00\xa7\x03\x03\x89\x22\x33\x95\x43\x7a\xc3\x89\x45\x51\x12\x3c\x28\x24\x1b\x6a\x78\xbf\xbe\x95\xd8\x90\x58\xd7\x65\xf7\xbb\x2d\xb2\x8d\xa0\x75\x00\x00\x38\xc0\x2c\xc0\x30\x00\x9f\xcc\xa9\xcc\xa8\xcc\xaa\xc0\x2b\xc0\x2f\x00\x9e\xc0\x24\xc0\x28\x00\x6b\xc0\x23\xc0\x27\x00\x67\xc0\x0a\xc0\x14\x00\x39\xc0\x09\xc0\x13\x00\x33\x00\x9d\x00\x9c\x00\x3d\x00\x3c\x00\x35\x00\x2f\x00\xff\x01\x00\x00\x46\x00\x0b\x00\x04\x03\x00\x01\x02\x00\x0a\x00\x0a\x00\x08\x00\x1d\x00\x17\x00\x19\x00\x18\x00\x23\x00\x00\x00\x0d\x00\x20\x00\x1e\x06\x01\x06\x02\x06\x03\x05\x01\x05\x02\x05\x03\x04\x01\x04\x02\x04\x03\x03\x01\x03\x02\x03\x03\x02\x01\x02\x02\x02\x03\x00\x16\x00\x00\x00\x17\x00\x00hello tls alone\n";
 
 # Test: Shy SSH connection
     if ($SSH_SHY_CNX) {
         print "***Test: Shy SSH connection\n";
-        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-        warn "Unable to connect to sslh_port:$sslh_port $!\n" unless $cnx_h;
+        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or 
+            die "Unable to connect to sslh_port:$sslh_port $IO::Socket::errstr\n";
         if (defined $cnx_h) {
             sleep 13;
-            print $cnx_h $test_data;
-            my $data = <$cnx_h>;
+            $cnx_h->send($test_data);
+            my $data;
+            retry_recv($cnx_h,$data,1024,MSG_DONTWAIT);
             my_is($data, "ssh: $test_data", "$binary: Shy SSH connection");
         }
     }
@@ -370,60 +393,52 @@
 # Test: One SSL half-started then one SSH
     if ($SSL_MIX_SSH) {
         print "***Test: One SSL half-started then one SSH\n";
-        my $cnx_l = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-        warn "Unable to connect to sslh_port $sslh_port: $!\n" unless $cnx_l;
-        if (defined $cnx_l) {
-            print $cnx_l $ssl_test_data;
-            my $cnx_h= new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-            warn "Unable to connect to sslh_port $sslh_port: $!\n" unless $cnx_h;
-            if (defined $cnx_h) {
-                warn("going to print data to $cnx_h");
-                $cnx_h->send($test_data);
-                warn("printed data to $cnx_h");
-                # sleep 3;
-                my $data_h = <$cnx_h>;
-                warn("waiting for data at $cnx_h");
-                my_is($data_h, "ssh: $test_data", "$binary: SSH during SSL being established");
-            }
-            my $data;
-            my $n = sysread $cnx_l, $data, 1024;
-            warn("reading data from tls");
-            my_is($data, "tls: $ssl_test_data", "$binary: SSL connection interrupted by SSH");
-        }
+        my $cnx_l = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or 
+            die "Unable to connect to sslh_port $sslh_port: $IO::Socket::errstr\n";
+        $cnx_l->send($ssl_test_data);
+        warn("printed data '$ssl_test_data' to ".$cnx_l->sockport());
+        my $cnx_h= new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or
+            die "Unable to connect to sslh_port $sslh_port: $!\n";
+        warn("going to print data to ".$cnx_h->sockport());
+        $cnx_h->send($test_data);
+        warn("printed data '$test_data' to ".$cnx_h->sockport());
+        my $data_h;
+        retry_recv($cnx_h,$data_h,1024,MSG_DONTWAIT);
+        my_is($data_h, "ssh: $test_data", "$binary: SSH during SSL being established");
+        my $data;
+        retry_recv($cnx_l,$data, 1024,MSG_DONTWAIT);
+        warn("reading data from tls");
+        my_is($data, "tls: $ssl_test_data", "$binary: SSL connection interrupted by SSH");
     }
 
 # Test: One SSH half-started then one SSL
     if ($SSH_MIX_SSL) {
         print "***Test: One SSH half-started then one SSL\n";
-        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-        warn "Unable to open sslh_port:$sslh_port $!\n" unless $cnx_h;
-        if (defined $cnx_h) {
-            # sleep 3;
-            my $cnx_l = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-            warn "$!\n" unless $cnx_l;
-            if (defined $cnx_l) {
-                print $cnx_l $ssl_test_data;
-                my $data;
-                my $n = sysread $cnx_l, $data, 1024;
-                my_is($data, "tls: $ssl_test_data", "$binary: SSL during SSH being established");
-            }
-            print $cnx_h $test_data;
-            my $data = <$cnx_h>;
-            my_is($data, "ssh: $test_data", "$binary: SSH connection interrupted by SSL");
-        }
+        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or 
+            die "Unable to open sslh_port:$sslh_port $!\n";
+        my $cnx_l = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or
+            die "Unable to connect to 127.0.0.1:$sslh_port $IO::Socket::errstr";
+        $cnx_l->send($ssl_test_data);
+        my $data;
+        retry_recv($cnx_l,$data, 1024,MSG_DONTWAIT);
+        my_is($data, "tls: $ssl_test_data", "$binary: SSL during SSH being established");
+        $cnx_l->close();
+        $cnx_h->send($test_data);
+        my $data;
+        retry_recv($cnx_h,$data,1024,MSG_DONTWAIT);
+        my_is($data, "ssh: $test_data", "$binary: SSH connection interrupted by SSL");
+        $cnx_h->close();
     }
 
 # Test: Drop connection without writing anything
     if ($DROP_CNX) {
         print "***Test: Connect but don't write anything\n";
-        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port");
-        warn "$!\n" unless $cnx_h;
-        if ($cnx_h) {
-            close $cnx_h;
-            my_is(1, "$binary: Connect and write nothing");
-            # The goal of the test is to check sslh doesn't
-            # crash
-        }
+        my $cnx_h = new IO::Socket::INET(PeerHost => "127.0.0.1:$sslh_port") or
+            die "Unable to connect to 127.0.0.1:$sslh_port $IO::Socket::errstr";
+        close $cnx_h;
+        my_is(1, "$binary: Connect and write nothing");
+        # The goal of the test is to check sslh doesn't
+        # crash
     }