update_alternatives.t 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #!/usr/bin/perl
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. use strict;
  16. use warnings;
  17. use Test::More;
  18. use File::Spec;
  19. use Dpkg::IPC;
  20. use Dpkg::Path qw(find_command);
  21. my $srcdir = $ENV{srcdir} || '.';
  22. my $tmpdir = 't.tmp/update_alternatives';
  23. my $admindir = File::Spec->rel2abs("$tmpdir/admindir"),
  24. my $altdir = File::Spec->rel2abs("$tmpdir/alternatives");
  25. my $bindir = File::Spec->rel2abs("$tmpdir/bin");
  26. my @ua = ("$ENV{builddir}/update-alternatives", '--log', '/dev/null',
  27. '--quiet', '--admindir', "$admindir", '--altdir', "$altdir");
  28. my %paths = (
  29. true => find_command('true'),
  30. false => find_command('false'),
  31. yes => find_command('yes'),
  32. cat => find_command('cat'),
  33. date => find_command('date'),
  34. sleep => find_command('sleep'),
  35. );
  36. if (! -x "$ENV{builddir}/update-alternatives") {
  37. plan skip_all => 'update-alternatives not available';
  38. exit(0);
  39. }
  40. my $main_link = "$bindir/generic-test";
  41. my $main_name = 'generic-test';
  42. my @choices = (
  43. {
  44. path => $paths{true},
  45. priority => 20,
  46. slaves => [
  47. {
  48. link => "$bindir/slave2",
  49. name => 'slave2',
  50. path => $paths{cat},
  51. },
  52. {
  53. link => "$bindir/slave3",
  54. name => 'slave3',
  55. path => $paths{cat},
  56. },
  57. {
  58. link => "$bindir/slave1",
  59. name => 'slave1',
  60. path => $paths{yes},
  61. },
  62. {
  63. link => "$bindir/slave4",
  64. name => 'slave4',
  65. path => $paths{cat},
  66. },
  67. ],
  68. },
  69. {
  70. path => $paths{false},
  71. priority => 10,
  72. slaves => [
  73. {
  74. link => "$bindir/slave1",
  75. name => 'slave1',
  76. path => $paths{date},
  77. },
  78. ],
  79. },
  80. {
  81. path => $paths{sleep},
  82. priority => 5,
  83. slaves => [],
  84. },
  85. );
  86. my $nb_slaves = 4;
  87. plan tests => (4 * ($nb_slaves + 1) + 2) * 26 # number of check_choices
  88. + 106; # rest
  89. sub cleanup {
  90. system("rm -rf $tmpdir && mkdir -p $admindir && mkdir -p $altdir");
  91. system("mkdir -p $bindir/more");
  92. }
  93. sub call_ua {
  94. my ($params, %opts) = @_;
  95. spawn(exec => [ @ua, @$params ], nocheck => 1,
  96. wait_child => 1, env => { LC_ALL => 'C' }, %opts);
  97. my $test_id = '';
  98. $test_id = "$opts{test_id}: " if defined $opts{test_id};
  99. if ($opts{expect_failure}) {
  100. ok($? != 0, "${test_id}update-alternatives @$params should fail.") or
  101. diag("Did not fail as expected: @ua @$params");
  102. } else {
  103. ok($? == 0, "${test_id}update-alternatives @$params should work.") or
  104. diag("Did not succeed as expected: @ua @$params");
  105. }
  106. }
  107. sub install_choice {
  108. my ($id, %opts) = @_;
  109. my $alt = $choices[$id];
  110. my @params;
  111. push @params, @{$opts{params}} if exists $opts{params};
  112. push @params, '--install', "$main_link", "$main_name",
  113. $alt->{path}, $alt->{priority};
  114. foreach my $slave (@{ $alt->{slaves} }) {
  115. push @params, '--slave', $slave->{link}, $slave->{name}, $slave->{path};
  116. }
  117. call_ua(\@params, %opts);
  118. }
  119. sub remove_choice {
  120. my ($id, %opts) = @_;
  121. my $alt = $choices[$id];
  122. my @params;
  123. push @params, @{$opts{params}} if exists $opts{params};
  124. push @params, '--remove', $main_name, $alt->{path};
  125. call_ua(\@params, %opts);
  126. }
  127. sub remove_all_choices {
  128. my (%opts) = @_;
  129. my @params;
  130. push @params, @{$opts{params}} if exists $opts{params};
  131. push @params, '--remove-all', $main_name;
  132. call_ua(\@params, %opts);
  133. }
  134. sub set_choice {
  135. my ($id, %opts) = @_;
  136. my $alt = $choices[$id];
  137. my @params;
  138. push @params, @{$opts{params}} if exists $opts{params};
  139. push @params, '--set', $main_name, $alt->{path};
  140. call_ua(\@params, %opts);
  141. }
  142. sub config_choice {
  143. my ($id, %opts) = @_;
  144. my ($input, $output) = ('', '');
  145. if ($id >= 0) {
  146. my $alt = $choices[$id];
  147. $input = $alt->{path};
  148. } else {
  149. $input = '0';
  150. }
  151. $input .= "\n";
  152. $opts{from_string} = \$input;
  153. $opts{to_string} = \$output;
  154. my @params;
  155. push @params, @{$opts{params}} if exists $opts{params};
  156. push @params, '--config', $main_name;
  157. call_ua(\@params, %opts);
  158. }
  159. sub get_slaves_status {
  160. my ($id) = @_;
  161. my %slaves;
  162. # None of the slaves are installed
  163. foreach my $alt (@choices) {
  164. for my $i (0 .. @{$alt->{slaves}} - 1) {
  165. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  166. $slaves{$alt->{slaves}[$i]{name}}{installed} = 0;
  167. }
  168. }
  169. # except those of the current alternative (minus optional slaves)
  170. if (defined($id)) {
  171. my $alt = $choices[$id];
  172. for my $i (0 .. @{$alt->{slaves}} - 1) {
  173. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  174. if (-e $alt->{slaves}[$i]{path}) {
  175. $slaves{$alt->{slaves}[$i]{name}}{installed} = 1;
  176. }
  177. }
  178. }
  179. my @slaves = sort { $a->{name} cmp $b->{name} } values %slaves;
  180. return @slaves;
  181. }
  182. sub check_link {
  183. my ($link, $value, $msg) = @_;
  184. ok(-l $link, "$msg: $link disappeared.");
  185. is(readlink($link), $value, "$link doesn't point to $value.");
  186. }
  187. sub check_no_link {
  188. my ($link, $msg) = @_;
  189. lstat($link);
  190. ok(!-e _, "$msg: $link still exists.");
  191. ok(1, 'fake test'); # Same number of tests as check_link
  192. }
  193. sub check_slaves {
  194. my ($id, $msg) = @_;
  195. foreach my $slave (get_slaves_status($id)) {
  196. if ($slave->{installed}) {
  197. check_link("$altdir/$slave->{name}", $slave->{path}, $msg);
  198. check_link($slave->{link}, "$altdir/$slave->{name}", $msg);
  199. } else {
  200. check_no_link("$altdir/$slave->{name}", $msg);
  201. check_no_link($slave->{link}, $msg);
  202. }
  203. }
  204. }
  205. # (4 * (nb_slaves+1) + 2) tests in each check_choice() call
  206. sub check_choice {
  207. my ($id, $mode, $msg) = @_;
  208. my $output;
  209. if (defined $id) {
  210. # Check status
  211. call_ua([ '--query', "$main_name" ], to_string => \$output, test_id => $msg);
  212. $output =~ /^Status: (.*)$/im;
  213. is($1, $mode, "$msg: status is not $mode.");
  214. # Check links
  215. my $alt = $choices[$id];
  216. check_link("$altdir/$main_name", $alt->{path}, $msg);
  217. check_link($main_link, "$altdir/$main_name", $msg);
  218. check_slaves($id, $msg);
  219. } else {
  220. call_ua([ '--query', "$main_name" ], error_to_string => \$output,
  221. expect_failure => 1, test_id => $msg);
  222. ok($output =~ /no alternatives/, "$msg: bad error message for --query.");
  223. # Check that all links have disappeared
  224. check_no_link("$altdir/$main_name", $msg);
  225. check_no_link($main_link, $msg);
  226. check_slaves(undef, $msg);
  227. }
  228. }
  229. ### START OF TESTS
  230. cleanup();
  231. # removal when not installed should not fail
  232. remove_choice(0);
  233. # successive install in auto mode
  234. install_choice(1);
  235. check_choice(1, 'auto', 'initial install 1');
  236. install_choice(2); # 2 is lower prio, stays at 1
  237. check_choice(1, 'auto', 'initial install 2');
  238. install_choice(0); # 0 is higher priority
  239. check_choice(0, 'auto', 'initial install 3');
  240. # verify that the administrative file is sorted properly
  241. {
  242. local $/ = undef;
  243. open(my $db_fh, '<', "$admindir/generic-test") or die $!;
  244. my $content = <$db_fh>;
  245. close($db_fh);
  246. my $expected =
  247. "auto
  248. $bindir/generic-test
  249. slave1
  250. $bindir/slave1
  251. slave2
  252. $bindir/slave2
  253. slave3
  254. $bindir/slave3
  255. slave4
  256. $bindir/slave4
  257. ";
  258. my %slaves;
  259. # Store slaves in a hash to easily retrieve present and missing ones.
  260. foreach my $alt (@choices) {
  261. foreach my $slave (@{$alt->{slaves}}) {
  262. $slaves{$slave->{name}}{$alt->{path}} = $slave;
  263. }
  264. }
  265. foreach my $alt (sort { $a->{path} cmp $b->{path} } @choices) {
  266. $expected .= $alt->{path} . "\n";
  267. $expected .= $alt->{priority} . "\n";
  268. foreach my $slave_name (sort keys %slaves) {
  269. $expected .= $slaves{$slave_name}{$alt->{path}}{path} || '';
  270. $expected .= "\n";
  271. }
  272. }
  273. $expected .= "\n";
  274. is($content, $expected, 'administrative file is as expected');
  275. }
  276. # manual change with --set-selections
  277. my $input = "doesntexist auto $paths{date}\ngeneric-test manual $paths{false}\n";
  278. my $output = '';
  279. call_ua(['--set-selections'], from_string => \$input,
  280. to_string => \$output, test_id => 'manual update with --set-selections');
  281. check_choice(1, 'manual', 'manual update with --set-selections');
  282. $input = "generic-test auto $paths{true}\n";
  283. call_ua(['--set-selections'], from_string => \$input,
  284. to_string => \$output, test_id => 'auto update with --set-selections');
  285. check_choice(0, 'auto', 'auto update with --set-selections');
  286. # manual change with set
  287. set_choice(2, test_id => 'manual update with --set');
  288. check_choice(2, 'manual', 'manual update with --set'); # test #388313
  289. remove_choice(2, test_id => 'remove manual, back to auto');
  290. check_choice(0, 'auto', 'remove manual, back to auto');
  291. remove_choice(0, test_id => 'remove best');
  292. check_choice(1, 'auto', 'remove best');
  293. remove_choice(1, test_id => 'no alternative left');
  294. check_choice(undef, '', 'no alternative left');
  295. # single choice in manual mode, to be removed
  296. install_choice(1);
  297. set_choice(1);
  298. check_choice(1, 'manual', 'single manual choice');
  299. remove_choice(1);
  300. check_choice(undef, '', 'removal single manual');
  301. # test --remove-all
  302. install_choice(0);
  303. install_choice(1);
  304. install_choice(2);
  305. remove_all_choices(test_id => 'remove all');
  306. check_choice(undef, '', 'no alternative left');
  307. # check auto-recovery of user mistakes (#100135)
  308. install_choice(1);
  309. ok(unlink("$bindir/generic-test"), 'failed removal');
  310. ok(unlink("$bindir/slave1"), 'failed removal');
  311. install_choice(1);
  312. check_choice(1, 'auto', 'recreate links in auto mode');
  313. set_choice(1);
  314. ok(unlink("$bindir/generic-test"), 'failed removal');
  315. ok(unlink("$bindir/slave1"), 'failed removal');
  316. install_choice(1);
  317. check_choice(1, 'manual', 'recreate links in manual mode');
  318. # check recovery of /etc/alternatives/*
  319. install_choice(0);
  320. ok(unlink("$altdir/generic-test"), 'failed removal');
  321. install_choice(1);
  322. check_choice(0, 'auto', '<altdir>/generic-test lost, back to auto');
  323. # test --config
  324. config_choice(0);
  325. check_choice(0, 'manual', 'config to best but manual');
  326. config_choice(1);
  327. check_choice(1, 'manual', 'config to manual');
  328. config_choice(-1);
  329. check_choice(0, 'auto', 'config auto');
  330. # test rename of links
  331. install_choice(0);
  332. my $old_slave = $choices[0]{slaves}[0]{link};
  333. my $old_link = $main_link;
  334. $choices[0]{slaves}[0]{link} = "$bindir/more/generic-slave";
  335. $main_link = "$bindir/more/mytest";
  336. install_choice(0);
  337. check_choice(0, 'auto', 'test rename of links');
  338. check_no_link($old_link, 'test rename of links');
  339. check_no_link($old_slave, 'test rename of links');
  340. # rename with installing other alternatives
  341. $old_link = $main_link;
  342. $main_link = "$bindir/generic-test";
  343. install_choice(1);
  344. check_choice(0, 'auto', 'rename link');
  345. check_no_link($old_link, 'rename link');
  346. # rename with lost file
  347. unlink($old_slave);
  348. $old_slave = $choices[0]{slaves}[0]{link};
  349. $choices[0]{slaves}[0]{link} = "$bindir/generic-slave-bis";
  350. install_choice(0);
  351. check_choice(0, 'auto', 'rename lost file');
  352. check_no_link($old_slave, 'rename lost file');
  353. # update of alternative with many slaves not currently installed
  354. # and the link of the renamed slave exists while it should not
  355. set_choice(1);
  356. symlink("$paths{cat}", "$bindir/generic-slave-bis");
  357. $choices[0]{slaves}[0]{link} = "$bindir/slave2";
  358. install_choice(0, test_id => 'update with non-installed slaves');
  359. check_no_link("$bindir/generic-slave-bis",
  360. 'drop renamed symlink that should not be installed');
  361. # test install with empty admin file (#457863)
  362. cleanup();
  363. system("touch $admindir/generic-test");
  364. install_choice(0);
  365. # test install with garbage admin file
  366. cleanup();
  367. system("echo garbage > $admindir/generic-test");
  368. install_choice(0, error_to_file => '/dev/null', expect_failure => 1);
  369. # test invalid usages
  370. cleanup();
  371. install_choice(0);
  372. # try to install a slave alternative as new master
  373. call_ua(['--install', "$bindir/testmaster", 'slave1', "$paths{date}", '10'],
  374. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  375. # try to install a master alternative as slave
  376. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$paths{date}", '10',
  377. '--slave', "$bindir/testslave", 'generic-test', "$paths{true}" ],
  378. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  379. # try to reuse master link in slave
  380. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$paths{date}", '10',
  381. '--slave', "$bindir/testmaster", 'testslave', "$paths{true}" ],
  382. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  383. # try to reuse links in master alternative
  384. call_ua(['--install', "$bindir/slave1", 'testmaster', "$paths{date}", '10'],
  385. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  386. # try to reuse links in slave alternative
  387. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$paths{date}", '10',
  388. '--slave', "$bindir/generic-test", 'testslave', "$paths{true}" ],
  389. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  390. # try to reuse slave link in another slave alternative of another choice of
  391. # the same main alternative
  392. call_ua(['--install', $main_link, $main_name, "$paths{date}", '10',
  393. '--slave', "$bindir/slave1", 'testslave', "$paths{true}" ],
  394. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  395. # lack of absolute filenames in links or file path, non-existing path,
  396. call_ua(['--install', '../testmaster', 'testmaster', "$paths{date}", '10'],
  397. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  398. call_ua(['--install', "$bindir/testmaster", 'testmaster', './update-alternatives.pl', '10'],
  399. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  400. # non-existing alternative path
  401. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$bindir/doesntexist", '10'],
  402. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  403. # invalid alternative name in master
  404. call_ua(['--install', "$bindir/testmaster", 'test/master', "$paths{date}", '10'],
  405. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  406. # invalid alternative name in slave
  407. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$paths{date}", '10',
  408. '--slave', "$bindir/testslave", 'test slave', "$paths{true}" ],
  409. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  410. # install in non-existing dir should fail
  411. call_ua(['--install', "$bindir/doesntexist/testmaster", 'testmaster', "$paths{date}", '10',
  412. '--slave', "$bindir/testslave", 'testslave', "$paths{true}" ],
  413. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  414. call_ua(['--install', "$bindir/testmaster", 'testmaster', "$paths{date}", '10',
  415. '--slave', "$bindir/doesntexist/testslave", 'testslave', "$paths{true}" ],
  416. expect_failure => 1, to_file => '/dev/null', error_to_file => '/dev/null');
  417. # non-existing alternative path in slave is not a failure
  418. my $old_path = $choices[0]{slaves}[0]{path};
  419. $old_slave = $choices[0]{slaves}[0]{link};
  420. $choices[0]{slaves}[0]{path} = "$bindir/doesntexist";
  421. $choices[0]{slaves}[0]{link} = "$bindir/baddir/slave2";
  422. # test rename of slave link that existed but that doesn't anymore
  423. # and link is moved into non-existing dir at the same time
  424. install_choice(0);
  425. check_choice(0, 'auto', 'optional renamed slave2 in non-existing dir');
  426. # same but on fresh install
  427. cleanup();
  428. install_choice(0);
  429. check_choice(0, 'auto', 'optional slave2 in non-existing dir');
  430. $choices[0]{slaves}[0]{link} = $old_slave;
  431. # test fresh install with a non-existing slave file
  432. cleanup();
  433. install_choice(0);
  434. check_choice(0, 'auto', 'optional slave2');
  435. $choices[0]{slaves}[0]{path} = $old_path;
  436. # test management of pre-existing files
  437. cleanup();
  438. system("touch $main_link $bindir/slave1");
  439. install_choice(0);
  440. ok(!-l $main_link, 'install preserves files that should be links');
  441. ok(!-l "$bindir/slave1", 'install preserves files that should be slave links');
  442. remove_choice(0);
  443. ok(-f $main_link, 'removal keeps real file installed as master link');
  444. ok(-f "$bindir/slave1", 'removal keeps real files installed as slave links');
  445. install_choice(0, params => ['--force']);
  446. check_choice(0, 'auto', 'install --force replaces files with links');
  447. # test management of pre-existing files #2
  448. cleanup();
  449. system("touch $main_link $bindir/slave2");
  450. install_choice(0);
  451. install_choice(1);
  452. ok(!-l $main_link, 'inactive install preserves files that should be links');
  453. ok(!-l "$bindir/slave2", 'inactive install preserves files that should be slave links');
  454. ok(-f $main_link, 'inactive install keeps real file installed as master link');
  455. ok(-f "$bindir/slave2", 'inactive install keeps real files installed as slave links');
  456. set_choice(1);
  457. ok(!-l $main_link, 'manual switching preserves files that should be links');
  458. ok(!-l "$bindir/slave2", 'manual switching preserves files that should be slave links');
  459. ok(-f $main_link, 'manual switching keeps real file installed as master link');
  460. ok(-f "$bindir/slave2", 'manual switching keeps real files installed as slave links');
  461. remove_choice(1);
  462. ok(!-l $main_link, 'auto switching preserves files that should be links');
  463. ok(!-l "$bindir/slave2", 'auto switching preserves files that should be slave links');
  464. ok(-f $main_link, 'auto switching keeps real file installed as master link');
  465. ok(-f "$bindir/slave2", 'auto switching keeps real files installed as slave links');
  466. remove_all_choices(params => ['--force']);
  467. ok(!-e "$bindir/slave2", 'forced removeall drops real files installed as slave links');
  468. # test management of pre-existing files #3
  469. cleanup();
  470. system("touch $main_link $bindir/slave2");
  471. install_choice(0);
  472. install_choice(1);
  473. remove_choice(0);
  474. ok(!-l $main_link, 'removal + switching preserves files that should be links');
  475. ok(!-l "$bindir/slave2", 'removal + switching preserves files that should be slave links');
  476. ok(-f $main_link, 'removal + switching keeps real file installed as master link');
  477. ok(-f "$bindir/slave2", 'removal + switching keeps real files installed as slave links');
  478. install_choice(0);
  479. ok(!-l $main_link, 'install + switching preserves files that should be links');
  480. ok(!-l "$bindir/slave2", 'install + switching preserves files that should be slave links');
  481. ok(-f $main_link, 'install + switching keeps real file installed as master link');
  482. ok(-f "$bindir/slave2", 'install + switching keeps real files installed as slave links');
  483. set_choice(1, params => ['--force']);
  484. ok(!-e "$bindir/slave2", 'forced switching w/o slave drops real files installed as slave links');
  485. check_choice(1, 'manual', 'set --force replaces files with links');