File: command.t

package info (click to toggle)
update-inetd 4.54
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: perl: 1,114; makefile: 26; sh: 7
file content (453 lines) | stat: -rw-r--r-- 13,726 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/perl
#
# basic command-line testing of update-inetd(8)
#
# Copyright © 2009 Serafeim Zanikolas <serzan@hellug.gr>
# Copyright © 2018-2021 Guillem Jover <guillem@debian.org>
#
# This program 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 program 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 program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use File::stat;
use File::Copy;
use File::Temp;
use IPC::Cmd qw(run_forked);
use Test::More tests => 160;

my $GREP_NO_MATCH_EXIT_STATUS = 1;

my $DISABLED_PREFIX = '#<off># ';

# We expect this string to appear in update-inetd's stdout (in verbose mode)
# every time it starts, restarts, or sends SIGHUP to (x)inetd.
my $INETD_WAKEUP_STRING = 'About to ';

my $INETD_CONF = <<"CONF";
# /etc/inetd.conf:  see inetd(8) for further informations.
#
# Internet superserver configuration database
#
#
# Lines starting with "#:LABEL:" or "#<off>#" should not
# be changed unless you know what you are doing!
#
# If you want to disable an entry so it isn't touched during
# package updates just comment it out with a single '#' character.
#
# Packages should modify this file by using update-inetd(8)
#
# <service_name> <sock_type> <proto> <flags> <user> <server_path> <args>
#
#:INTERNAL: Internal services
#discard		stream	tcp	nowait	root	internal
#discard		dgram	udp	wait	root	internal
${DISABLED_PREFIX}daytime		stream	tcp	nowait	root	internal
time		stream	tcp	nowait	root	internal
#time2		stream	tcp	nowait	root	internal

#:STANDARD: These are standard services.

#:BSD: Shell, login, exec and talk are BSD protocols.

#:MAIL: Mail, news and uucp services.

#:INFO: Info services

#:BOOT: TFTP service is provided primarily for booting.  Most sites
#       run this only on machines acting as "boot servers."

#:RPC: RPC based services

#:HAM-RADIO: amateur-radio services

#:OTHER: Other services
CONF

sub run
{
    my ($cmd, $opts) = @_;

    $opts->{ok_exit_status} //= 0;

    note("running '@{$cmd}'");

    my $ret = run_forked($cmd);

    chomp $ret->{merged};
    ok($ret->{exit_code} == $opts->{ok_exit_status},
       "command '@{$cmd}', exit status $ret->{exit_code} (expected $opts->{ok_exit_status}) " .
       "output: <<<$ret->{merged}>>>");

    return $ret->{merged};
}

my $orig_conffile;
my $conffile;

sub fcomparator
{
    my ($f1, $f2) = @_;

    my $tmp = File::Temp->new(UNLINK => 0);
    run([ "sort $f1 >$tmp; mv $tmp $f1" ]);
    run([ "sort $f2 >$tmp; mv $tmp $f2" ]);
    return run([ 'comm', '-3', '--nocheck-order', $f1, $f2 ]);
}

# Serializes a subset of POSIX stat values.
sub stat_serializer
{
    my $filename = shift;

    my $st = stat $filename or die "cannot stat file $filename: $!\n";

    return $st->ctime . " " . $st->mtime . " " . $st->size;
}

# Try 2 test cases per mode: one that is actually effective (eg, adds,
# modifies or removes a line) and one that isn't (because the matching entry
# is commented out with '#'). Test success by matching expected messages in
# verbose mode, and patterns in the supplied inetd.conf.
sub setUp
{
    open my $fh, '>', $conffile or die "cannot create $conffile: $!\n";
    print { $fh } "$INETD_CONF" or die "cannot write to $conffile: $!\n";
    close $fh or die "cannot write to $conffile: $!\n";
    copy($conffile, $orig_conffile)
        or die "cannot copy $conffile to $orig_conffile: $!\n";
}

# The given string must appear n times in output.
sub assertOutputMatches
{
    my ($string, $output, $n) = @_;

    $n //= 1;

    my $nmatches = scalar grep { m/\Q$string\E/ } split /\n/, $output;
    ok($nmatches == $n,
       "string '$string' appears $nmatches time(s) (expected $n) in " .
       "update-inetd's output <<<$output>>>");
}

sub assertConffileMissing
{
    ok(! -e $conffile, "conffile $conffile is missing");
}

# The given pattern must appear exactly n times in the conffile.
sub assertConffileMatches
{
    my ($pattern, $n, $ok_exit_status) = @_;

    $n //= 1;
    $ok_exit_status //= 0;

    ok(-e $conffile, "conffile $conffile exists");
    my $output = run([ 'grep', '-c', "'$pattern'", $conffile ],
                     { ok_exit_status => $ok_exit_status });
    is($output, "$n", "conffile $conffile matches pattern $n times");
}

# orig and modified conffiles must differ in exactly nlines.
sub assertConffileDiffs
{
    my $nlines_diff = shift;

    my $output = fcomparator($orig_conffile, $conffile);
    my $actual_nlines_diff = scalar split /\n/, $output;

    if ($nlines_diff == 0 && $output eq '') {
        pass("no conffile diff <<<$output>>>");
        return;
    }
    ok($actual_nlines_diff == $nlines_diff,
       "original and modified conffiles, $actual_nlines_diff actual diff lines, " .
       "$nlines_diff expected diff lines:\n<<<$output>>>");
}

# Test that no stale file is left behind.
sub assertNoTempFile
{
    my $output = shift;

    # Output should have a line: "Using tempfile $new_inetdcf"
    my ($tmpfilename) = $output =~ m/^Using tempfile (.*)$/m;

    ok(length $tmpfilename,
       "extracted temp filename from output; output: <<<$output>>>");
    ok(defined $tmpfilename && ! -e $tmpfilename,
       "no stale temp file '$tmpfilename' left behind; output: <<<$output>>>");
}

sub update_inetd
{
    my ($mode, $srv, $other_opts, $run_opts) = @_;

    $other_opts //= [];

    return run([ 'update-inetd', '--file', "$conffile", '--verbose',
                 "--$mode", $srv, @{$other_opts} ], $run_opts);
}

sub testMissingConfig
{
    my $srv = "pop-3";
    my $srv_entry = "$srv\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t" .
                    "/usr/sbin/in.pop3d";
    my $output;

    # add
    $output = update_inetd("add", "'$srv_entry'");
    assertOutputMatches("warning: cannot add service, $conffile does not exist",
                        $output);
    assertConffileMissing();

    # disable
    $output = update_inetd("disable", $srv);
    assertOutputMatches("No service entries were disabled", $output);
    assertConffileMissing();

    # enable
    $output = update_inetd("enable", $srv);
    assertOutputMatches("warning: cannot enable service, $conffile does not exist",
                        $output);
    assertConffileMissing();

    # remove
    $output = update_inetd("remove", "'$srv'");
    assertOutputMatches("No service entries were removed", $output);
    assertConffileMissing();
}

sub testEffectiveEnable
{
    setUp();

    my $srv = "daytime";
    my $output = update_inetd("enable", $srv);

    assertOutputMatches("Processing service '$srv' ... enabled", $output);
    assertOutputMatches("Number of service entries enabled: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t");
    assertConffileDiffs(2);
    assertNoTempFile($output);
}

sub testIneffectiveEnable
{
    setUp();

    my $srv = "time2";
    my $conffile_stat_before = stat_serializer($conffile);
    my $output = update_inetd("enable", $srv);
    my $conffile_stat_after = stat_serializer($conffile);

    is($conffile_stat_before, $conffile_stat_after, "ineffective enable");
    assertOutputMatches("No service entries were enabled", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output, 0);
    assertConffileMatches("^#$srv\t");
    assertConffileDiffs(0);
    assertNoTempFile($output);
}

sub testEffectiveDisable
{
    setUp();

    my $srv = "time";
    my $output = update_inetd("disable", $srv);

    assertOutputMatches("Processing service '$srv' ... disabled", $output);
    assertOutputMatches("Number of service entries disabled: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$DISABLED_PREFIX$srv\t");
    assertConffileDiffs(2);
    assertNoTempFile($output);
}

sub testIneffectiveDisable
{
    setUp();

    my $srv = "time2";
    my $conffile_stat_before = stat_serializer($conffile);
    my $output = update_inetd("disable", $srv);
    my $conffile_stat_after = stat_serializer($conffile);

    is($conffile_stat_before, $conffile_stat_after, "ineffective disable");
    assertOutputMatches("No service entries were disabled", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output, 0);
    assertConffileMatches("^#$srv\t");
    assertConffileDiffs(0);
    assertNoTempFile($output);
}

sub testEffectiveAdd
{
    setUp();

    my $srv = "pop-3";
    my $srv_entry = "$srv\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t" .
                    "/usr/sbin/in.pop3d";
    my $output = update_inetd("add", "'$srv_entry'");

    assertOutputMatches("Processing service '$srv' ... added", $output);
    assertOutputMatches("New service(s) added", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t");
    assertConffileDiffs(1);
    assertNoTempFile($output);
    assertOutputMatches("Number of currently enabled services: 1", $output);
}

sub testIneffectiveAdd
{
    setUp();

    my $srv = "time2";
    my $srv_entry = "$srv\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t" .
                    "/usr/sbin/in.pop3d";
    my $conffile_stat_before = stat_serializer($conffile);
    my $output = update_inetd("add", "'$srv_entry'");
    my $conffile_stat_after = stat_serializer($conffile);

    is($conffile_stat_before, $conffile_stat_after, "ineffective add");
    assertOutputMatches("Processing service '$srv' ... not enabled", $output);
    assertOutputMatches("No service(s) added", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output, 0);
    assertConffileMatches("^#$srv\t");
    assertConffileDiffs(0);
    #assertNoTempFile($output) # doesn't create a temp file
}

sub testEffectiveRemove
{
    setUp();

    my $srv = "time";
    my $output = update_inetd("remove", "'$srv'");

    assertOutputMatches("Removing line: '$srv\t", $output);
    assertOutputMatches("Number of service entries removed: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t", 0, $GREP_NO_MATCH_EXIT_STATUS);
    assertConffileDiffs(1);
    assertNoTempFile($output);
}

sub testIneffectiveRemove
{
    setUp();

    my $srv = "time2";
    my $conffile_stat_before = stat_serializer($conffile);
    my $output = update_inetd("remove", "'$srv'");
    my $conffile_stat_after = stat_serializer($conffile);

    is($conffile_stat_before, $conffile_stat_after, "ineffective remove");
    assertOutputMatches("No service entries were removed", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output, 0);
    assertConffileDiffs(0);
    assertNoTempFile($output);
}

sub testAddDisableEnableRemove
{
    setUp();

    my $srv = "pop-3";
    my $srv_entry = "$srv\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t" .
                    "/usr/sbin/in.pop3d";
    my $output;

    # add
    $output = update_inetd("add", "'$srv_entry'");
    assertOutputMatches("Processing service '$srv' ... added", $output);
    assertOutputMatches("New service(s) added", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t");
    assertConffileDiffs(1);
    assertOutputMatches("Number of currently enabled services: 1", $output);

    # disable
    $output = update_inetd("disable", $srv);
    assertOutputMatches("Processing service '$srv' ... disabled", $output);
    assertOutputMatches("Number of service entries disabled: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$DISABLED_PREFIX$srv\t");
    # assertConffileDiff calls compares against the original conffile
    # (before the "add" operation)
    assertConffileDiffs(1);

    # enable
    $output = update_inetd("enable", $srv);
    assertOutputMatches("Processing service '$srv' ... enabled", $output);
    assertOutputMatches("Number of service entries enabled: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t");
    assertConffileDiffs(1);

    # remove
    $output = update_inetd("remove", "'$srv'");
    assertOutputMatches("Removing line: '$srv\t", $output);
    assertOutputMatches("Number of service entries removed: 1", $output);
    assertOutputMatches($INETD_WAKEUP_STRING, $output);
    assertConffileMatches("^$srv\t", 0, $GREP_NO_MATCH_EXIT_STATUS);
    assertConffileDiffs(0);

    #
    assertNoTempFile($output);
}

#
# Main
#

chmod 0755, 'update-inetd';

# Set this envvar so that DebianNet.pm will not actually run invoke-rc.d.
$ENV{UPDATE_INETD_FAKE_IT} = '.';
$ENV{UPDATE_INETD_NOXINETD} = '.';

# If testing the installed code, set PATH to include sbin directories,
# otherwise set current dir first in PERLLIB (last by default) and PATH,
# so that we test the local code.
if (exists $ENV{UPDATE_INETD_INSTALLCHECK}) {
    $ENV{PATH} = "/usr/sbin:/sbin:$ENV{PATH}";
} else {
    $ENV{PERL5LIB} = 'lib';
    $ENV{PATH} = ".:$ENV{PATH}";
}

# Test cases.
$conffile = '/nonexistent';

testMissingConfig();

$orig_conffile = File::Temp->new(SUFFIX => '.orig');
$conffile = File::Temp->new(SUFFIX => '.modified');

testEffectiveEnable();
testIneffectiveEnable();
testEffectiveDisable();
testIneffectiveDisable();
testEffectiveAdd();
testIneffectiveAdd();
testEffectiveRemove();
testIneffectiveRemove();
testAddDisableEnableRemove();