From 1d6d8671262dc6b2a4e02e4a1878d91b86647054 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 06:42:34 +0200 Subject: Adding upstream version 1.7. Signed-off-by: Daniel Baumann --- t/Test/DH.pm | 205 +++++++++ t/dh_installnss/debian/changelog | 5 + t/dh_installnss/debian/compat | 1 + t/dh_installnss/debian/control | 89 ++++ t/dh_installnss/debian/foo-after1.nss | 1 + t/dh_installnss/debian/foo-after2.nss | 1 + t/dh_installnss/debian/foo-before1.nss | 1 + t/dh_installnss/debian/foo-before2.nss | 1 + t/dh_installnss/debian/foo-empty1.nss | 0 t/dh_installnss/debian/foo-empty2.nss | 3 + t/dh_installnss/debian/foo-example.nss | 3 + t/dh_installnss/debian/foo-first1.nss | 2 + t/dh_installnss/debian/foo-first2.nss | 1 + t/dh_installnss/debian/foo-first3.nss | 2 + t/dh_installnss/debian/foo-last1.nss | 1 + t/dh_installnss/debian/foo-last2.nss | 1 + t/dh_installnss/debian/foo-last3.nss | 2 + t/dh_installnss/debian/foo-multidb1.nss | 4 + t/dh_installnss/debian/foo-newdb1.nss | 1 + t/dh_installnss/debian/foo-newdb2.nss | 3 + t/dh_installnss/debian/foo-newdb3.nss | 3 + t/dh_installnss/debian/foo-other.nss | 1 + t/dh_installnss/debian/foo-remove-only1.nss | 2 + t/dh_installnss/debian/foo-skip.nss | 2 + t/dh_installnss/debian/foo-substring.nss | 2 + t/dh_installnss/dh_installnss.t | 652 ++++++++++++++++++++++++++++ t/dh_installnss/nsswitch.conf.template | 17 + 27 files changed, 1006 insertions(+) create mode 100644 t/Test/DH.pm create mode 100644 t/dh_installnss/debian/changelog create mode 100644 t/dh_installnss/debian/compat create mode 100644 t/dh_installnss/debian/control create mode 100644 t/dh_installnss/debian/foo-after1.nss create mode 100644 t/dh_installnss/debian/foo-after2.nss create mode 100644 t/dh_installnss/debian/foo-before1.nss create mode 100644 t/dh_installnss/debian/foo-before2.nss create mode 100644 t/dh_installnss/debian/foo-empty1.nss create mode 100644 t/dh_installnss/debian/foo-empty2.nss create mode 100644 t/dh_installnss/debian/foo-example.nss create mode 100644 t/dh_installnss/debian/foo-first1.nss create mode 100644 t/dh_installnss/debian/foo-first2.nss create mode 100644 t/dh_installnss/debian/foo-first3.nss create mode 100644 t/dh_installnss/debian/foo-last1.nss create mode 100644 t/dh_installnss/debian/foo-last2.nss create mode 100644 t/dh_installnss/debian/foo-last3.nss create mode 100644 t/dh_installnss/debian/foo-multidb1.nss create mode 100644 t/dh_installnss/debian/foo-newdb1.nss create mode 100644 t/dh_installnss/debian/foo-newdb2.nss create mode 100644 t/dh_installnss/debian/foo-newdb3.nss create mode 100644 t/dh_installnss/debian/foo-other.nss create mode 100644 t/dh_installnss/debian/foo-remove-only1.nss create mode 100644 t/dh_installnss/debian/foo-skip.nss create mode 100644 t/dh_installnss/debian/foo-substring.nss create mode 100755 t/dh_installnss/dh_installnss.t create mode 100644 t/dh_installnss/nsswitch.conf.template (limited to 't') diff --git a/t/Test/DH.pm b/t/Test/DH.pm new file mode 100644 index 0000000..7fa30bf --- /dev/null +++ b/t/Test/DH.pm @@ -0,0 +1,205 @@ +package Test::DH; + +use strict; +use warnings; + +use Test::More; + +use Cwd qw(getcwd realpath); +use Errno qw(EEXIST); +use Exporter qw(import); + +use File::Temp qw(tempdir); +use File::Path qw(remove_tree make_path); +use File::Basename qw(dirname); + +our $ROOT_DIR; + +BEGIN { + my $res = realpath(__FILE__) or die('Cannot resolve ' . __FILE__ . ": $!"); + $ROOT_DIR = dirname(dirname(dirname($res))); +}; + +use lib "$ROOT_DIR/lib"; + +# These should be done before Dh_lib is loaded. +BEGIN { + $ENV{PATH} = "$ROOT_DIR:$ENV{PATH}" if $ENV{PATH} !~ m{\Q$ROOT_DIR\E/?:}; + $ENV{PERL5LIB} = join(':', "${ROOT_DIR}/lib", (grep {defined} $ENV{PERL5LIB})) + if not $ENV{PERL5LIB} or $ENV{PERL5LIB} !~ m{\Q$ROOT_DIR\E(?:/lib)?/?:}; + $ENV{DH_DATAFILES} = "${ROOT_DIR}/t/fixtures:${ROOT_DIR}"; + # Nothing in the tests requires root. + $ENV{DEB_RULES_REQUIRES_ROOT} = 'no'; + # Disable colors for good measure + $ENV{DH_COLORS} = 'never'; + $ENV{DPKG_COLORS} = 'never'; + + # Drop DEB_BUILD_PROFILES and DEB_BUILD_OPTIONS so they don't interfere + delete($ENV{DEB_BUILD_PROFILES}); + delete($ENV{DEB_BUILD_OPTIONS}); +}; + +use Debian::Debhelper::Dh_Lib qw(!dirname); + +our @EXPORT = qw( + each_compat_up_to_and_incl_subtest each_compat_subtest + each_compat_from_and_above_subtest run_dh_tool + create_empty_file readlines + error find_script non_deprecated_compat_levels + each_compat_from_x_to_and_incl_y_subtest +); + +our ($TEST_DH_COMPAT); + +my $START_DIR = getcwd(); +my $TEST_DIR; + +sub run_dh_tool { + my (@cmd) = @_; + my $compat = $TEST_DH_COMPAT; + my $options = ref($cmd[0]) ? shift(@cmd) : {}; + my $pid; + + $pid = fork() // BAIL_OUT("fork failed: $!"); + if (not $pid) { + $ENV{DH_COMPAT} = $compat; + $ENV{DH_INTERNAL_TESTSUITE_SILENT_WARNINGS} = 1; + if (defined(my $env = $options->{env})) { + for my $k (sort(keys(%{$env}))) { + if (defined($env->{$k})) { + $ENV{$k} = $env->{$k}; + } else { + delete($ENV{$k}); + } + } + } + if ($options->{quiet}) { + open(STDOUT, '>', '/dev/null') or error("Reopen stdout: $!"); + open(STDERR, '>', '/dev/null') or error("Reopen stderr: $!"); + } else { + # If run under prove/TAP, we don't want to confuse the test runner. + open(STDOUT, '>&', *STDERR) or error("Redirect stdout to stderr: $!"); + } + exec(@cmd); + } + waitpid($pid, 0) == $pid or BAIL_OUT("waitpid($pid) failed: $!"); + return 1 if not $?; + return 0; +} + +sub _prepare_test_root { + my $dir = tempdir(CLEANUP => 1); + if (not mkdir("$dir/debian", 0777)) { + error("mkdir $dir/debian failed: $!") + if $! != EEXIST; + } else { + # auto seed it + my @files = qw( + debian/control + debian/compat + debian/changelog + ); + for my $file (@files) { + install_file($file, "${dir}/${file}"); + } + if (@::TEST_DH_EXTRA_TEMPLATE_FILES) { + my $test_dir = ($TEST_DIR //= dirname($0)); + my $fixture_dir = $::TEST_DH_FIXTURE_DIR // '.'; + my $actual_dir = "$test_dir/$fixture_dir"; + for my $file (@::TEST_DH_EXTRA_TEMPLATE_FILES) { + if (index($file, '/') > -1) { + my $install_dir = dirname($file); + install_dir($install_dir); + } + install_file("${actual_dir}/${file}", "${dir}/${file}"); + } + } + } + return $dir; +} + +sub each_compat_from_x_to_and_incl_y_subtest($$&) { + my ($compat, $high_compat, $code) = @_; + my $lowest = Debian::Debhelper::Dh_Lib::MIN_COMPAT_LEVEL; + my $highest = Debian::Debhelper::Dh_Lib::MAX_COMPAT_LEVEL; + error("compat $compat is no longer support! Min compat $lowest") + if $compat < $lowest; + error("$high_compat is from the future! Max known is $highest") + if $high_compat > $highest; + subtest '' => sub { + # Keep $dir alive until the test is over + my $dir = _prepare_test_root; + chdir($dir) or error("chdir($dir): $!"); + while ($compat <= $high_compat) { + local $TEST_DH_COMPAT = $compat; + $code->($compat); + ++$compat; + } + chdir($START_DIR) or error("chdir($START_DIR): $!"); + }; + return; +} + +sub each_compat_up_to_and_incl_subtest($&) { + unshift(@_, Debian::Debhelper::Dh_Lib::MIN_COMPAT_LEVEL); + goto \&each_compat_from_x_to_and_incl_y_subtest; +} + +sub each_compat_from_and_above_subtest($&) { + splice(@_, 1, 0, Debian::Debhelper::Dh_Lib::MAX_COMPAT_LEVEL); + goto \&each_compat_from_x_to_and_incl_y_subtest; +} + +sub each_compat_subtest(&) { + unshift(@_, + Debian::Debhelper::Dh_Lib::MIN_COMPAT_LEVEL, + Debian::Debhelper::Dh_Lib::MAX_COMPAT_LEVEL); + goto \&each_compat_from_x_to_and_incl_y_subtest; +} + +sub create_empty_file { + my ($file, $chmod) = @_; + open(my $fd, '>', $file) or die("open($file): $!\n"); + close($fd) or die("close($file): $!\n"); + if (defined($chmod)) { + chmod($chmod, $file) + or die(sprintf('chmod(%04o, %s): %s', $chmod, $file, $!)); + } + return 1; +} + +sub readlines { + my ($h) = @_; + my @lines = <$h>; + close $h; + chop @lines; + return \@lines; +} + +# In *inst order (find_script will shuffle them around for *rm order) +my @SNIPPET_FILE_TEMPLATES = ( + 'debian/#PACKAGE#.#SCRIPT#.debhelper', + 'debian/.debhelper/generated/#PACKAGE#/#SCRIPT#.service', +); + +sub find_script { + my ($package, $script) = @_; + my @files; + for my $template (@SNIPPET_FILE_TEMPLATES) { + my $file = ($template =~ s/#PACKAGE#/$package/r); + $file =~ s/#SCRIPT#/$script/; + push(@files, $file) if -f $file; + } + if ($script eq 'postrm' or $script eq 'prerm') { + @files = reverse(@files); + } + return @files; +} + +sub non_deprecated_compat_levels() { + my $start = Debian::Debhelper::Dh_Lib::LOWEST_NON_DEPRECATED_COMPAT_LEVEL; + my $end = Debian::Debhelper::Dh_Lib::MAX_COMPAT_LEVEL; + return ($start..$end); +} + +1; diff --git a/t/dh_installnss/debian/changelog b/t/dh_installnss/debian/changelog new file mode 100644 index 0000000..5850f0e --- /dev/null +++ b/t/dh_installnss/debian/changelog @@ -0,0 +1,5 @@ +foo (1.0-1) unstable; urgency=low + + * Initial release. (Closes: #XXXXXX) + + -- Test Mon, 11 Jul 2016 18:10:59 +0200 diff --git a/t/dh_installnss/debian/compat b/t/dh_installnss/debian/compat new file mode 100644 index 0000000..60d3b2f --- /dev/null +++ b/t/dh_installnss/debian/compat @@ -0,0 +1 @@ +15 diff --git a/t/dh_installnss/debian/control b/t/dh_installnss/debian/control new file mode 100644 index 0000000..8e137f7 --- /dev/null +++ b/t/dh_installnss/debian/control @@ -0,0 +1,89 @@ +Source: foo +Section: misc +Priority: optional +Maintainer: Test +Standards-Version: 3.9.8 + +Package: foo-before1 +Architecture: all +Description: package foo-before1 + +Package: foo-before2 +Architecture: all +Description: package foo-before2 + +Package: foo-after1 +Architecture: all +Description: package foo-after1 + +Package: foo-after2 +Architecture: all +Description: package foo-after2 + +Package: foo-empty1 +Architecture: all +Description: package foo-empty1 + +Package: foo-empty2 +Architecture: all +Description: package foo-empty2 + +Package: foo-first1 +Architecture: all +Description: package foo-first1 + +Package: foo-first2 +Architecture: all +Description: package foo-first2 + +Package: foo-first3 +Architecture: all +Description: package foo-first3 + +Package: foo-last1 +Architecture: all +Description: package foo-last1 + +Package: foo-last2 +Architecture: all +Description: package foo-last2 + +Package: foo-last3 +Architecture: all +Description: package foo-last3 + +Package: foo-multidb1 +Architecture: all +Description: package foo-multidb1 + +Package: foo-newdb1 +Architecture: all +Description: package foo-newdb1 + +Package: foo-newdb2 +Architecture: all +Description: package foo-newdb2 + +Package: foo-newdb3 +Architecture: all +Description: package foo-newdb3 + +Package: foo-remove-only1 +Architecture: all +Description: package foo-remove-only1 + +Package: foo-other +Architecture: all +Description: package foo-other + +Package: foo-example +Architecture: all +Description: package foo-example + +Package: foo-skip +Architecture: all +Description: package foo-skip + +Package: foo-substring +Architecture: all +Description: package foo-substring diff --git a/t/dh_installnss/debian/foo-after1.nss b/t/dh_installnss/debian/foo-after1.nss new file mode 100644 index 0000000..aa65c17 --- /dev/null +++ b/t/dh_installnss/debian/foo-after1.nss @@ -0,0 +1 @@ +hosts after=dns foo diff --git a/t/dh_installnss/debian/foo-after2.nss b/t/dh_installnss/debian/foo-after2.nss new file mode 100644 index 0000000..0c0d053 --- /dev/null +++ b/t/dh_installnss/debian/foo-after2.nss @@ -0,0 +1 @@ +hosts after=dns,files foo diff --git a/t/dh_installnss/debian/foo-before1.nss b/t/dh_installnss/debian/foo-before1.nss new file mode 100644 index 0000000..d0d65a9 --- /dev/null +++ b/t/dh_installnss/debian/foo-before1.nss @@ -0,0 +1 @@ +hosts before=dns foo diff --git a/t/dh_installnss/debian/foo-before2.nss b/t/dh_installnss/debian/foo-before2.nss new file mode 100644 index 0000000..cdfd74d --- /dev/null +++ b/t/dh_installnss/debian/foo-before2.nss @@ -0,0 +1 @@ +hosts before=dns,resolve foo diff --git a/t/dh_installnss/debian/foo-empty1.nss b/t/dh_installnss/debian/foo-empty1.nss new file mode 100644 index 0000000..e69de29 diff --git a/t/dh_installnss/debian/foo-empty2.nss b/t/dh_installnss/debian/foo-empty2.nss new file mode 100644 index 0000000..40afa03 --- /dev/null +++ b/t/dh_installnss/debian/foo-empty2.nss @@ -0,0 +1,3 @@ +# only comments here + +# and empty lines diff --git a/t/dh_installnss/debian/foo-example.nss b/t/dh_installnss/debian/foo-example.nss new file mode 100644 index 0000000..7708ddd --- /dev/null +++ b/t/dh_installnss/debian/foo-example.nss @@ -0,0 +1,3 @@ +hosts before=dns mdns4 +hosts before=mdns4 mdns4_minimal [NOTFOUND=return] +hosts remove-only mdns # In case the user manually added it diff --git a/t/dh_installnss/debian/foo-first1.nss b/t/dh_installnss/debian/foo-first1.nss new file mode 100644 index 0000000..8e29dd7 --- /dev/null +++ b/t/dh_installnss/debian/foo-first1.nss @@ -0,0 +1,2 @@ +#DB POS MODULE (ACTION?) (CONDITION?) +hosts first foo diff --git a/t/dh_installnss/debian/foo-first2.nss b/t/dh_installnss/debian/foo-first2.nss new file mode 100644 index 0000000..10a78de --- /dev/null +++ b/t/dh_installnss/debian/foo-first2.nss @@ -0,0 +1 @@ +hosts first foo [NOTFOUND=return] diff --git a/t/dh_installnss/debian/foo-first3.nss b/t/dh_installnss/debian/foo-first3.nss new file mode 100644 index 0000000..9c80174 --- /dev/null +++ b/t/dh_installnss/debian/foo-first3.nss @@ -0,0 +1,2 @@ +hosts first foo2 +hosts first foo2 [NOTFOUND=return] diff --git a/t/dh_installnss/debian/foo-last1.nss b/t/dh_installnss/debian/foo-last1.nss new file mode 100644 index 0000000..defa945 --- /dev/null +++ b/t/dh_installnss/debian/foo-last1.nss @@ -0,0 +1 @@ +hosts last foo diff --git a/t/dh_installnss/debian/foo-last2.nss b/t/dh_installnss/debian/foo-last2.nss new file mode 100644 index 0000000..e6ad245 --- /dev/null +++ b/t/dh_installnss/debian/foo-last2.nss @@ -0,0 +1 @@ +hosts last foo [NOTFOUND=return] diff --git a/t/dh_installnss/debian/foo-last3.nss b/t/dh_installnss/debian/foo-last3.nss new file mode 100644 index 0000000..cb85368 --- /dev/null +++ b/t/dh_installnss/debian/foo-last3.nss @@ -0,0 +1,2 @@ +hosts last foo2 +hosts last foo2 [NOTFOUND=return] diff --git a/t/dh_installnss/debian/foo-multidb1.nss b/t/dh_installnss/debian/foo-multidb1.nss new file mode 100644 index 0000000..8dc899c --- /dev/null +++ b/t/dh_installnss/debian/foo-multidb1.nss @@ -0,0 +1,4 @@ +shadow first foo +shadow last foo2 +group first foo +passwd last baz diff --git a/t/dh_installnss/debian/foo-newdb1.nss b/t/dh_installnss/debian/foo-newdb1.nss new file mode 100644 index 0000000..606d085 --- /dev/null +++ b/t/dh_installnss/debian/foo-newdb1.nss @@ -0,0 +1 @@ +mydb database-add diff --git a/t/dh_installnss/debian/foo-newdb2.nss b/t/dh_installnss/debian/foo-newdb2.nss new file mode 100644 index 0000000..2d38f54 --- /dev/null +++ b/t/dh_installnss/debian/foo-newdb2.nss @@ -0,0 +1,3 @@ +mydb database-add + +mydb first foo diff --git a/t/dh_installnss/debian/foo-newdb3.nss b/t/dh_installnss/debian/foo-newdb3.nss new file mode 100644 index 0000000..7bd7149 --- /dev/null +++ b/t/dh_installnss/debian/foo-newdb3.nss @@ -0,0 +1,3 @@ +mydb database-require + +mydb first bar diff --git a/t/dh_installnss/debian/foo-other.nss b/t/dh_installnss/debian/foo-other.nss new file mode 100644 index 0000000..6f3c135 --- /dev/null +++ b/t/dh_installnss/debian/foo-other.nss @@ -0,0 +1 @@ +hosts last foo diff --git a/t/dh_installnss/debian/foo-remove-only1.nss b/t/dh_installnss/debian/foo-remove-only1.nss new file mode 100644 index 0000000..816ad1e --- /dev/null +++ b/t/dh_installnss/debian/foo-remove-only1.nss @@ -0,0 +1,2 @@ +hosts first foo +hosts remove-only resolve diff --git a/t/dh_installnss/debian/foo-skip.nss b/t/dh_installnss/debian/foo-skip.nss new file mode 100644 index 0000000..0002201 --- /dev/null +++ b/t/dh_installnss/debian/foo-skip.nss @@ -0,0 +1,2 @@ +hosts first foo skip-if-present=other1,other2 +hosts first foo2 skip-if-present=other3 diff --git a/t/dh_installnss/debian/foo-substring.nss b/t/dh_installnss/debian/foo-substring.nss new file mode 100644 index 0000000..5843e2b --- /dev/null +++ b/t/dh_installnss/debian/foo-substring.nss @@ -0,0 +1,2 @@ +hosts before=dns foo +hosts first foo_extra diff --git a/t/dh_installnss/dh_installnss.t b/t/dh_installnss/dh_installnss.t new file mode 100755 index 0000000..95cfecd --- /dev/null +++ b/t/dh_installnss/dh_installnss.t @@ -0,0 +1,652 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Test::More; +use Cwd qw(abs_path); +use File::Basename qw(dirname); +use File::Path qw(make_path); +use lib dirname(dirname(abs_path(__FILE__))); +use Test::DH; +use Debian::Debhelper::Dh_Lib qw(!dirname); + +plan(tests => 1); + +my $test_dir = abs_path(dirname(__FILE__)); +my $dpkg_root = "debian/foo"; + +our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw( + debian/changelog + debian/control + debian/foo-after1.nss + debian/foo-after2.nss + debian/foo-before1.nss + debian/foo-before2.nss + debian/foo-empty1.nss + debian/foo-empty2.nss + debian/foo-example.nss + debian/foo-first1.nss + debian/foo-first2.nss + debian/foo-first3.nss + debian/foo-last1.nss + debian/foo-last2.nss + debian/foo-last3.nss + debian/foo-multidb1.nss + debian/foo-newdb1.nss + debian/foo-newdb2.nss + debian/foo-newdb3.nss + debian/foo-other.nss + debian/foo-remove-only1.nss + debian/foo-skip.nss + debian/foo-substring.nss +)); + +each_compat_subtest { + make_path(qw(debian/foo debian/foo/etc)); + ok(run_dh_tool("dh_installnss")); + + $ENV{"DPKG_ROOT"} = "$dpkg_root"; + $ENV{"DPKG_MAINTSCRIPT_PACKAGE_INSTCOUNT"} = 0; + + subtest "skips installation if no /etc/nsswitch.conf" => sub { + unlink("$dpkg_root/etc/nsswitch.conf"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + ok(!-e "$dpkg_root/etc/nsswitch.conf"); + }; + + subtest "respects skip-if-present" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-skip", "preinst", "install"); + exec_script_ok("foo-skip", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo2 files other2 dns"); + exec_script_ok("foo-skip", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "position=first" => sub { + subtest "adds in first position" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo files other2 dns"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds in first position (in presence of comments)" => sub { + place_nsswitch_file("hosts", "files other2 dns # comment"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo files other2 dns # comment"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns # comment"); + }; + + subtest "adds in first position (in presence of service in comment)" => sub { + place_nsswitch_file("hosts", "files other2 dns # foo in comment"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo files other2 dns # foo in comment"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns # foo in comment"); + }; + + subtest "adds in first position (with action)" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-first2", "preinst", "install"); + exec_script_ok("foo-first2", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo [NOTFOUND=return] files other2 dns"); + exec_script_ok("foo-first2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds in first position (multiple times)" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-first3", "preinst", "install"); + exec_script_ok("foo-first3", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo2 [NOTFOUND=return] foo2 files other2 dns"); + exec_script_ok("foo-first3", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "does not add if already present in line (service name)" => sub { + place_nsswitch_file("hosts", "files foo dns"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo dns"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "does not add if already present in line (service first)" => sub { + place_nsswitch_file("hosts", "foo files dns"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo files dns"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "does not add if already present in line (service last)" => sub { + place_nsswitch_file("hosts", "files dns foo"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files dns foo"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "does not add if already present in line (service last before comment)" => sub { + place_nsswitch_file("hosts", "files dns foo# comment"); + exec_script_ok("foo-first1", "preinst", "install"); + exec_script_ok("foo-first1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files dns foo# comment"); + exec_script_ok("foo-first1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns# comment"); + }; + + subtest "does not add if already present in line (service name with action)" => sub { + place_nsswitch_file("hosts", "files foo [NOTFOUND=return] dns"); + exec_script_ok("foo-first2", "preinst", "install"); + exec_script_ok("foo-first2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo [NOTFOUND=return] dns"); + exec_script_ok("foo-first2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "does not if already present in line (service name without action)" => sub { + place_nsswitch_file("hosts", "files foo dns"); + exec_script_ok("foo-first2", "preinst", "install"); + exec_script_ok("foo-first2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo dns"); + exec_script_ok("foo-first2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + }; + + subtest "position=last" => sub { + subtest "adds in last position" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-last1", "preinst", "install"); + exec_script_ok("foo-last1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo"); + exec_script_ok("foo-last1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds in last position (with action)" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-last2", "preinst", "install"); + exec_script_ok("foo-last2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo [NOTFOUND=return]"); + exec_script_ok("foo-last2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds in last position (multiple times)" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-last3", "preinst", "install"); + exec_script_ok("foo-last3", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo2 foo2 [NOTFOUND=return]"); + exec_script_ok("foo-last3", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds before a comment" => sub { + place_nsswitch_file("hosts", "files other2 dns #a long comment"); + exec_script_ok("foo-last1", "preinst", "install"); + exec_script_ok("foo-last1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo #a long comment"); + exec_script_ok("foo-last1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns #a long comment"); + }; + + subtest "adds before a comment (no space)" => sub { + place_nsswitch_file("hosts", "files other2 dns#short comment"); + exec_script_ok("foo-last1", "preinst", "install"); + exec_script_ok("foo-last1", "postinst", "configure"); + # The script unconditionally adds a space before the comment. + is(db_line("hosts"), "hosts: files other2 dns foo #short comment"); + exec_script_ok("foo-last1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns #short comment"); + }; + }; + + subtest "position=before" => sub { + subtest "adds before another" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-before1", "preinst", "install"); + exec_script_ok("foo-before1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 foo dns"); + exec_script_ok("foo-before1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds before another (in presence of comments)" => sub { + place_nsswitch_file("hosts", "files other2 dns # comment"); + exec_script_ok("foo-before1", "preinst", "install"); + exec_script_ok("foo-before1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 foo dns # comment"); + exec_script_ok("foo-before1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns # comment"); + }; + + subtest "adds before another with action" => sub { + place_nsswitch_file("hosts", "files other2 dns [NOTFOUND=return]"); + exec_script_ok("foo-before1", "preinst", "install"); + exec_script_ok("foo-before1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 foo dns [NOTFOUND=return]"); + exec_script_ok("foo-before1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns [NOTFOUND=return]"); + }; + + subtest "does not add before another if missing" => sub { + place_nsswitch_file("hosts", "files other2"); + exec_script_ok("foo-before1", "preinst", "install"); + exec_script_ok("foo-before1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2"); + exec_script_ok("foo-before1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2"); + }; + + subtest "does not add if another missing (but in comment)" => sub { + place_nsswitch_file("hosts", "files other2 # dns"); + exec_script_ok("foo-before1", "preinst", "install"); + exec_script_ok("foo-before1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 # dns"); + exec_script_ok("foo-before1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 # dns"); + }; + + subtest "adds before another (multiple possibilites, one found)" => sub { + place_nsswitch_file("hosts", "files resolve other2"); + exec_script_ok("foo-before2", "preinst", "install"); + exec_script_ok("foo-before2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo resolve other2"); + exec_script_ok("foo-before2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files resolve other2"); + }; + + subtest "adds before another (multiple possibilites, same order)" => sub { + place_nsswitch_file("hosts", "files dns resolve other2"); + exec_script_ok("foo-before2", "preinst", "install"); + exec_script_ok("foo-before2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo dns resolve other2"); + exec_script_ok("foo-before2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns resolve other2"); + }; + + subtest "adds before another (multiple possibilites, inverted order)" => sub { + place_nsswitch_file("hosts", "files resolve other2 dns"); + exec_script_ok("foo-before2", "preinst", "install"); + exec_script_ok("foo-before2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo resolve other2 dns"); + exec_script_ok("foo-before2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files resolve other2 dns"); + }; + + subtest "adds before another (multiple possibilites, with action)" => sub { + place_nsswitch_file("hosts", "files resolve [!NOTFOUND=return] other2"); + exec_script_ok("foo-before2", "preinst", "install"); + exec_script_ok("foo-before2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo resolve [!NOTFOUND=return] other2"); + exec_script_ok("foo-before2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files resolve [!NOTFOUND=return] other2"); + }; + }; + + subtest "position=after" => sub { + subtest "adds after another" => sub { + place_nsswitch_file("hosts", "files other2 dns"); + exec_script_ok("foo-after1", "preinst", "install"); + exec_script_ok("foo-after1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo"); + exec_script_ok("foo-after1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns"); + }; + + subtest "adds after another (in presence of comments)" => sub { + place_nsswitch_file("hosts", "files other2 dns # a comment"); + exec_script_ok("foo-after1", "preinst", "install"); + exec_script_ok("foo-after1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 dns foo # a comment"); + exec_script_ok("foo-after1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 dns # a comment"); + }; + + subtest "adds after another with action" => sub { + place_nsswitch_file("hosts", "dns [NOTFOUND=return] other2 files"); + exec_script_ok("foo-after1", "preinst", "install"); + exec_script_ok("foo-after1", "postinst", "configure"); + is(db_line("hosts"), "hosts: dns [NOTFOUND=return] foo other2 files"); + exec_script_ok("foo-after1", "postrm", "remove"); + is(db_line("hosts"), "hosts: dns [NOTFOUND=return] other2 files"); + }; + + subtest "does not add after another if missing (but in comment)" => sub { + place_nsswitch_file("hosts", "files other2 # dns"); + exec_script_ok("foo-after1", "preinst", "install"); + exec_script_ok("foo-after1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2 # dns"); + exec_script_ok("foo-after1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2 # dns"); + }; + + subtest "does not add after another if missing" => sub { + place_nsswitch_file("hosts", "files other2"); + exec_script_ok("foo-after1", "preinst", "install"); + exec_script_ok("foo-after1", "postinst", "configure"); + is(db_line("hosts"), "hosts: files other2"); + exec_script_ok("foo-after1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files other2"); + }; + + subtest "adds after another (multiple possibilites)" => sub { + place_nsswitch_file("hosts", "files resolve other2"); + exec_script_ok("foo-after2", "preinst", "install"); + exec_script_ok("foo-after2", "postinst", "configure"); + is(db_line("hosts"), "hosts: files foo resolve other2"); + exec_script_ok("foo-after2", "postrm", "remove"); + is(db_line("hosts"), "hosts: files resolve other2"); + }; + }; + + subtest "position=remove-only" => sub { + subtest "does not add remove-only services" => sub { + place_nsswitch_file("hosts", "files dns"); + exec_script_ok("foo-remove-only1", "preinst", "install"); + exec_script_ok("foo-remove-only1", "postinst", "configure"); + is(db_line("hosts"), "hosts: foo files dns"); + exec_script_ok("foo-remove-only1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes both added and already present services" => sub { + place_nsswitch_file("hosts", "foo files resolve dns"); + exec_script_ok("foo-remove-only1", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + }; + + subtest "addition" => sub { + subtest "skips if any service is already mentioned in a target database (1)" => sub { + place_nsswitch_file("shadow", "files foo dns"); + exec_script_ok("foo-multidb1", "preinst", "install"); + exec_script_ok("foo-multidb1", "postinst", "configure"); + is(db_line("shadow"), "shadow: files foo dns"); + exec_script_ok("foo-multidb1", "postrm", "remove"); + is(db_line("shadow"), "shadow: files dns"); + }; + + subtest "skips if any service is already mentioned in a target database (2)" => sub { + place_nsswitch_file("passwd", "baz files"); + exec_script_ok("foo-multidb1", "preinst", "install"); + exec_script_ok("foo-multidb1", "postinst", "configure"); + is(db_line("passwd"), "passwd: baz files"); + exec_script_ok("foo-multidb1", "postrm", "remove"); + is(db_line("passwd"), "passwd: files"); + }; + + subtest "adds if the service is mentioned in an unrelated database" => sub { + place_nsswitch_file("passwd", "files foo"); + is(db_line("shadow"), "shadow: files"); + exec_script_ok("foo-multidb1", "preinst", "install"); + exec_script_ok("foo-multidb1", "postinst", "configure"); + is(db_line("shadow"), "shadow: foo files foo2"); + is(db_line("passwd"), "passwd: files foo baz"); + exec_script_ok("foo-multidb1", "postrm", "remove"); + is(db_line("shadow"), "shadow: files"); + is(db_line("passwd"), "passwd: files foo"); + }; + }; + + subtest "removal" => sub { + subtest "succeeds if service has already been removed" => sub { + place_nsswitch_file("hosts", "files dns"); + exec_script_ok("foo-other", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes all instances of service (consecutive 1)" => sub { + place_nsswitch_file("hosts", "files foo foo [NOTFOUND=return] foo dns"); + exec_script_ok("foo-other", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes all instances of service (consecutive 2)" => sub { + place_nsswitch_file("hosts", "files foo [NOTFOUND=return] foo foo dns"); + exec_script_ok("foo-other", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes all instances of service (split 1)" => sub { + place_nsswitch_file("hosts", "files foo [NOTFOUND=return] foo dns foo"); + exec_script_ok("foo-other", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes all instances of service (split 2)" => sub { + place_nsswitch_file("hosts", "foo [NOTFOUND=return] files foo [!UNAVAIL=return] foo dns"); + exec_script_ok("foo-other", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes all instances of service (also during purge)" => sub { + place_nsswitch_file("hosts", "files foo foo [NOTFOUND=return] foo dns"); + exec_script_ok("foo-other", "postrm", "purge"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes services with substring names" => sub { + place_nsswitch_file("hosts", "foo_extra files foo dns"); + exec_script_ok("foo-substring", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + subtest "removes services with substring names (in presence of comments)" => sub { + place_nsswitch_file("hosts", "foo_extra files foo dns# comment: foo is ok"); + exec_script_ok("foo-substring", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns# comment: foo is ok"); + }; + }; + + subtest "position=database-add" => sub { + subtest "adds a database if not already present (without service)" => sub { + place_nsswitch_file(); + is(num_occurrences("mydb", dbs()), 0); + exec_script_ok("foo-newdb1", "preinst", "install"); + exec_script_ok("foo-newdb1", "postinst", "configure"); + is(db_line("mydb"), "mydb: "); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb1", "postrm", "remove"); + is(num_occurrences("mydb", dbs()), 0); + }; + + subtest "does not add the database if already present (without service)" => sub { + place_nsswitch_file(); + is(num_occurrences("mydb", dbs()), 0); + # first install + exec_script_ok("foo-newdb1", "preinst", "install"); + exec_script_ok("foo-newdb1", "postinst", "configure"); + is(db_line("mydb"), "mydb: "); + is(num_occurrences("mydb", dbs()), 1); + # second install + exec_script_ok("foo-newdb1", "preinst", "install"); + exec_script_ok("foo-newdb1", "postinst", "configure"); + is(db_line("mydb"), "mydb: "); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb1", "postrm", "remove"); + is(num_occurrences("mydb", dbs()), 0); + }; + + subtest "adds a database if not already present (with service)" => sub { + place_nsswitch_file(); + is(num_occurrences("mydb", dbs()), 0); + exec_script_ok("foo-newdb2", "preinst", "install"); + exec_script_ok("foo-newdb2", "postinst", "configure"); + is(db_line("mydb"), "mydb: foo "); # TODO: fix trailing space in v2 + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb2", "postrm", "remove"); + is(num_occurrences("mydb", dbs()), 0); + }; + + subtest "does not add the database if already present (with service)" => sub { + place_nsswitch_file(); + is(num_occurrences("mydb", dbs()), 0); + # first install + exec_script_ok("foo-newdb2", "preinst", "install"); + exec_script_ok("foo-newdb2", "postinst", "configure"); + is(db_line("mydb"), "mydb: foo "); # TODO: fix trailing space in v2 + is(num_occurrences("mydb", dbs()), 1); + # second install + exec_script_ok("foo-newdb2", "preinst", "install"); + exec_script_ok("foo-newdb2", "postinst", "configure"); + is(db_line("mydb"), "mydb: foo "); # TODO: fix trailing space in v2 + is(num_occurrences("mydb", dbs()), 1); + # removal + exec_script_ok("foo-newdb2", "postrm", "remove"); + is(num_occurrences("mydb", dbs()), 0); + }; + }; + + subtest "position=detabase-require" => sub { + subtest "adds a service to the the database if already present" => sub { + place_nsswitch_file("mydb", "other"); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb3", "preinst", "install"); + exec_script_ok("foo-newdb3", "postinst", "configure"); + is(db_line("mydb"), "mydb: bar other"); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb3", "postrm", "remove"); + is(db_line("mydb"), "mydb: other"); + is(num_occurrences("mydb", dbs()), 1); + }; + + subtest "does not add a service if the database is missing" => sub { + place_nsswitch_file(); + is(num_occurrences("mydb", dbs()), 0); + exec_script_ok("foo-newdb3", "preinst", "install"); + exec_script_ok("foo-newdb3", "postinst", "configure"); + is(num_occurrences("mydb", dbs()), 0); + exec_script_ok("foo-newdb3", "postrm", "remove"); + is(num_occurrences("mydb", dbs()), 0); + }; + + subtest "does not remove the database during package removal" => sub { + place_nsswitch_file("mydb", " "); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb3", "preinst", "install"); + exec_script_ok("foo-newdb3", "postinst", "configure"); + is(db_line("mydb"), "mydb: bar "); + is(num_occurrences("mydb", dbs()), 1); + exec_script_ok("foo-newdb3", "postrm", "remove"); + is(db_line("mydb"), "mydb: "); + is(num_occurrences("mydb", dbs()), 1); + }; + }; + + subtest "validation" => sub { + subtest "does not add debhelper snippets if .nss is empty" => sub { + my ($postinst_path) = find_script("foo-empty1", "postinst"); + my ($postrm_path) = find_script("foo-empty1", "postrm"); + is($postinst_path, undef); + is($postrm_path, undef); + }; + + subtest "does not add debhelper snippets if .nss is empty (with comments)" => sub { + my ($postinst_path) = find_script("foo-empty2", "postinst"); + my ($postrm_path) = find_script("foo-empty2", "postrm"); + is($postinst_path, undef); + is($postrm_path, undef); + }; + }; + + subtest "example in docs" => sub { + place_nsswitch_file("hosts", "files dns"); + exec_script_ok("foo-example", "preinst", "install"); + exec_script_ok("foo-example", "postinst", "configure"); + is(db_line("hosts"), "hosts: files mdns4_minimal [NOTFOUND=return] mdns4 dns"); + exec_script_ok("foo-example", "postrm", "remove"); + is(db_line("hosts"), "hosts: files dns"); + }; + + ok(run_dh_tool('dh_clean')); +}; + +sub place_nsswitch_file { + my ($db, $line) = @_; + my $template_file = "$test_dir/nsswitch.conf.template"; + my $conf_file = "debian/foo/etc/nsswitch.conf"; + + my $fd; + open($fd, '<', $template_file) or error("open($template_file): $!"); + read($fd, my $conf, -s $fd); + close($fd); + + if ($db && $line) { + my $db_line_re = "^$db:.*\$"; + my $newline = "$db: $line"; + if ($conf =~ /$db_line_re/m) { + $conf =~ s/$db_line_re/$newline/m; + } else { + $conf .= "\n$newline\n"; + } + } + + open($fd, '>', $conf_file) or error("open($conf_file): $!"); + print($fd $conf); + close($fd); +} + +sub exec_script_ok { + is(exec_script(@_), 0); +} + +sub exec_script { + my ($package, $script, @args) = @_; + my ($script_path) = find_script($package, $script); + (defined $script_path && $script_path ne "") or error("No maintscript of type $script found"); + system("sh", "-e", $script_path, @args); + return $? >> 8; +} + +sub db_line { + my ($db) = @_; + + my @lines = nsswitch_file_lines(); + + my $db_line_re = "^$db:"; + my ($line) = grep({ m/$db_line_re/ } @lines); + $line //= ""; + # Normalize spaces to simplify comparison. + $line =~ s/\s+/ /g; + + return $line; +} + +sub dbs { + my @lines = nsswitch_file_lines(); + + my @dbs = (); + foreach my $line (@lines) { + my ($db) = $line =~ m/^([a-zA-Z]+):/g; + push @dbs, $db if $db; + } + + return @dbs; +} + +sub nsswitch_file_lines { + open(my $fd, '<', "$dpkg_root/etc/nsswitch.conf"); + my @lines = @{readlines($fd)}; + close($fd); + + return @lines; +} + +sub num_occurrences { + my ($item, @array) = @_; + + return scalar grep({$_ eq $item} @array); +} diff --git a/t/dh_installnss/nsswitch.conf.template b/t/dh_installnss/nsswitch.conf.template new file mode 100644 index 0000000..8ecf84c --- /dev/null +++ b/t/dh_installnss/nsswitch.conf.template @@ -0,0 +1,17 @@ +# A sample /etc/nsswitch.conf file + +passwd: files systemd +group: files systemd +shadow: files +gshadow: files + +#hosts: files mdns4_minimal [NOTFOUND=return] dns +hosts: files mdns4 [NOTFOUND=return] dns mymachines +networks: files + +protocols: db files +services: db files +ethers: db files +rpc: db files + +netgroup: nis -- cgit v1.2.3