This file is indexed.

/usr/share/perl5/Net/Daemon/Test.pm is in libnet-daemon-perl 0.48-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- perl -*-
#
#   $Id: Test.pm,v 1.2 1999/08/12 14:28:57 joe Exp $
#
#   Net::Daemon - Base class for implementing TCP/IP daemons
#
#   Copyright (C) 1998, Jochen Wiedmann
#                       Am Eisteich 9
#                       72555 Metzingen
#                       Germany
#
#                       Phone: +49 7123 14887
#                       Email: joe@ispsoft.de
#
#
#   This module is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This module is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this module; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################

package Net::Daemon::Test;

use strict;
require 5.004;

use Net::Daemon ();
use Symbol ();
use File::Basename ();


$Net::Daemon::Test::VERSION = '0.03';
@Net::Daemon::Test::ISA = qw(Net::Daemon);


=head1 NAME

Net::Daemon::Test - support functions for testing Net::Daemon servers


=head1 SYNOPSIS

    # This is the server, stored in the file "servertask".
    #
    # Create a subclass of Net::Daemon::Test, which in turn is
    # a subclass of Net::Daemon
    use Net::Daemon::Test ();
    package MyDaemon;
    @MyDaemon::ISA = qw(Net::Daemon::Test);

    sub Run {
	# Overwrite this and other methods, as you like.
    }

    my $self = Net::Daemon->new(\%attr, \@options);
    eval { $self->Bind() };
    if ($@) {
	die "Server cannot bind: $!";
    }
    eval { $self->Run() };
    if ($@) {
	die "Unexpected server termination: $@";
    }


    # This is the client, the real test script, note we call the
    # "servertask" file below:
    #
    # Call the Child method to spawn a child. Don't forget to use
    # the timeout option.
    use Net::Daemon::Test ();

    my($handle, $port) = eval {
        Net::Daemon::Test->Child(5, # Number of subtests
				 'servertask', '--timeout', '20')
    };
    if ($@) {
	print "not ok 1 $@\n";
	exit 0;
    }
    print "ok 1\n";

    # Real tests following here
    ...

    # Terminate the server
    $handle->Terminate();


=head1 DESCRIPTION

This module is a frame for creating test scripts of Net::Daemon based
server packages, preferrably using Test::Harness, but that's your
choice.

A test consists of two parts: The client part and the server part.
The test is executed by the child part which invokes the server part,
by spawning a child process and invoking an external Perl script.
(Of course we woultn't need this external file with fork(), but that's
the best possibility to make the test scripts portable to Windows
without requiring threads in the test script.)

The server part is a usual Net::Daemon application, for example a script
like dbiproxy. The only difference is that it derives from
Net::Daemon::Test and not from Net::Daemon, the main difference is that
the B<Bind> method attempts to allocate a port automatically. Once a
port is allocated, the number is stored in the file "ndtest.prt".

After spawning the server process, the child will wait ten seconds
(hopefully sufficient) for the creation of ndtest.prt.


=head1 AVAILABLE METHODS

=head2 Server part

=over 8

=item Options

Adds an option B<--timeout> to Net::Daemon: The server's Run method
will die after at most 20 seconds.

=cut

sub Options ($) {
    my $self = shift;
    my $options = $self->SUPER::Options();
    $options->{'timeout'} = {
	'template' => 'timeout=i',
	'description' => '--timeout <secs>        '
	    . "The server will die if the test takes longer\n"
	    . '                        than this number of seconds.'
	};
    $options;
}


=pod

=item Bind

(Instance method) This is mainly the default Bind method, but it attempts
to find and allocate a free port in two ways: First of all, it tries to
call Bind with port 0, most systems will automatically choose a port in
that case. If that seems to fail, ports 30000-30049 are tried. We
hope, one of these will succeed. :-)

=cut

sub Bind ($) {
    # First try: Pass unmodified options to Net::Daemon::Bind
    my $self = shift;
    my($port, $socket);
    $self->{'proto'} ||= $self->{'localpath'} ? 'unix' : 'tcp';
    if ($self->{'proto'} eq 'unix') {
        $port = $self->{'localpath'} || die "Missing option: localpath";
        $socket = eval {
            IO::Socket::UNIX->new('Local' => $port,
                                  'Listen' => $self->{'listen'} || 10);
        }
    } else {
        my @socket_args =
	    ( 'LocalAddr' => $self->{'localaddr'},
	      'LocalPort' => $self->{'localport'},
	      'Proto' => $self->{'proto'} || 'tcp',
	      'Listen' => $self->{'listen'} || 10,
	      'Reuse' => 1
	    );
        $socket = eval { IO::Socket::INET->new(@socket_args) };
        if ($socket) {
	    $port = $socket->sockport();
        } else {
            $port = 30049;
            while (!$socket  &&  $port++ < 30060) {
	        $socket = eval { IO::Socket::INET->new(@socket_args,
	       			                       'LocalPort' => $port) };
            }
        }
    }
    if (!$socket) {
	die "Cannot create socket: " . ($@ || $!);
    }

    # Create the "ndtest.prt" file so that the child knows to what
    # port it may connect.
    my $fh = Symbol::gensym();
    if (!open($fh, ">ndtest.prt")  ||
	!(print $fh $port)  ||
	!close($fh)) {
	die "Error while creating 'ndtest.prt': $!";
    }
    $self->Debug("Created ndtest.prt with port $port\n");
    $self->{'socket'} = $socket;

    if (my $timeout = $self->{'timeout'}) {
	eval { alarm $timeout };
    }

    $self->SUPER::Bind();
}


=pod

=item Run

(Instance method) Overwrites the Net::Daemon's method by adding a timeout.

=back

sub Run ($) {
    my $self = shift;
    $self->Run();
}


=head2 Client part

=over 8

=item Child

(Class method) Attempts to spawn a server process. The server process is
expected to create the file 'ndtest.prt' with the port number.

The method returns a process handle and a port number. The process handle
offers a method B<Terminate> that may later be used to stop the server
process.

=back

=cut

sub Child ($$@) {
    my $self = shift;  my $numTests = shift;
    my($handle, $pid);

    my $args = join(" ", @_);
    print "Starting server: $args\n";

    unlink 'ndtest.prt';

    if ($args =~ /\-\-mode=(?:ithread|thread|single)/  &&  $^O =~ /mswin32/i) {
	require Win32;
	require Win32::Process;
	my $proc = $_[0];

	# Win32::Process seems to require an absolute path; this includes
	# a program extension like ".exe"
	my $path;
	my @pdirs;

	File::Basename::fileparse_set_fstype("MSWin32");
	if (File::Basename::basename($proc) !~ /\./) {
	    $proc .= ".exe";
	}
	if ($proc !~ /^\w\:\\/  &&  $proc !~ /^\\/) {
	    # Doesn't look like an absolute path
	    foreach my $dir (@pdirs = split(/;/, $ENV{'PATH'})) {
		if (-x "$dir/$proc") {
		    $path = "$dir/$proc";
		    last;
		}
	    }
	    if (!$path) {
		print STDERR ("Cannot find $proc in the following"
			      , " directories:\n");
		foreach my $dir (@pdirs) {
		    print STDERR "    $dir\n";
		}
		print STDERR "Terminating.\n";
		exit 1;
	    }
	} else {
	    $path = $proc;
	}

	print "Starting process: proc = $path, args = ", join(" ", @_), "\n";
	if (!&Win32::Process::Create($pid, $path,
 				     join(" ", @_), 0,
                                     Win32::Process::DETACHED_PROCESS(),
 				     ".")) {
 	    die "Cannot create child process: "
 		. Win32::FormatMessage(Win32::GetLastError());
 	}
	$handle = bless(\$pid, "Net::Daemon::Test::Win32");
    } else {
	$pid = eval { fork() };
	if (defined($pid)) {
	    # Aaaah, Unix! :-)
	    if (!$pid) {
		# This is the child process, spawn the server.
		exec @_;
	    }
	    $handle = bless(\$pid, "Net::Daemon::Test::Fork");
	} else {
	    print "1..0\n";
	    exit 0;
	}
    }

    print "1..$numTests\n" if defined($numTests);
    for (my $secs = 20;  $secs  &&  ! -s 'ndtest.prt';  $secs -= sleep 1) {
    }
    if (! -s 'ndtest.prt') {
	die "Server process didn't create a file 'ndtest.prt'.";
    }
    # Sleep another second in case the server is still creating the
    # file with the port number ...
    sleep 1;
    my $fh = Symbol::gensym();
    my $port;
    if (!open($fh, "<ndtest.prt")  ||
	!defined($port = <$fh>)) {
	die "Error while reading 'ndtest.prt': $!";
    }
    ($handle, $port);
}


package Net::Daemon::Test::Fork;

sub Terminate ($) {
    my $self = shift;
    my $pid = $$self;
    kill 'TERM', $pid;
}

package Net::Daemon::Test::Win32;

sub Terminate ($) {
    my $self = shift;
    my $pid = $$self;
    $pid->Kill(0);
}

1;

=head1 AUTHOR AND COPYRIGHT

  Net::Daemon is Copyright (C) 1998, Jochen Wiedmann
                                     Am Eisteich 9
                                     72555 Metzingen
                                     Germany

                                     Phone: +49 7123 14887
                                     Email: joe@ispsoft.de

  All rights reserved.

You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.


=head1 SEE ALSO

L<Net::Daemon(3)>, L<Test::Harness(3)>

=cut