100_update_alternatives.t 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. # -*- mode: cperl;-*-
  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 <http://www.gnu.org/licenses/>.
  15. use Dpkg::IPC;
  16. use Dpkg::Path qw(find_command);
  17. use File::Spec;
  18. use Test::More;
  19. use strict;
  20. use warnings;
  21. my $srcdir = $ENV{srcdir} || '.';
  22. my $tmpdir = 't.tmp/900_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; $i < @{$alt->{slaves}}; $i++) {
  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; $i < @{$alt->{slaves}}; $i++) {
  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. return sort { $a->{name} cmp $b->{name} } values %slaves;
  180. }
  181. sub check_link {
  182. my ($link, $value, $msg) = @_;
  183. ok(-l $link, "$msg: $link disappeared.");
  184. is(readlink($link), $value, "$link doesn't point to $value.");
  185. }
  186. sub check_no_link {
  187. my ($link, $msg) = @_;
  188. lstat($link);
  189. ok(!-e _, "$msg: $link still exists.");
  190. ok(1, "fake test"); # Same number of tests as check_link
  191. }
  192. sub check_slaves {
  193. my ($id, $msg) = @_;
  194. foreach my $slave (get_slaves_status($id)) {
  195. if ($slave->{installed}) {
  196. check_link("$altdir/$slave->{name}", $slave->{path}, $msg);
  197. check_link($slave->{"link"}, "$altdir/$slave->{name}", $msg);
  198. } else {
  199. check_no_link("$altdir/$slave->{name}", $msg);
  200. check_no_link($slave->{"link"}, $msg);
  201. }
  202. }
  203. }
  204. # (4 * (nb_slaves+1) + 2) tests in each check_choice() call
  205. sub check_choice {
  206. my ($id, $mode, $msg) = @_;
  207. my $output;
  208. if (defined $id) {
  209. # Check status
  210. call_ua([ "--query", "$main_name" ], to_string => \$output, test_id => $msg);
  211. $output =~ /^Status: (.*)$/im;
  212. is($1, $mode, "$msg: status is not $mode.");
  213. # Check links
  214. my $alt = $choices[$id];
  215. check_link("$altdir/$main_name", $alt->{path}, $msg);
  216. check_link($main_link, "$altdir/$main_name", $msg);
  217. check_slaves($id, $msg);
  218. } else {
  219. call_ua([ "--query", "$main_name" ], error_to_string => \$output,
  220. expect_failure => 1, test_id => $msg);
  221. ok($output =~ /no alternatives/, "$msg: bad error message for --query.");
  222. # Check that all links have disappeared
  223. check_no_link("$altdir/$main_name", $msg);
  224. check_no_link($main_link, $msg);
  225. check_slaves(undef, $msg);
  226. }
  227. }
  228. ### START OF TESTS
  229. cleanup();
  230. # removal when not installed should not fail
  231. remove_choice(0);
  232. # successive install in auto mode
  233. install_choice(1);
  234. check_choice(1, "auto", "initial install 1");
  235. install_choice(2); # 2 is lower prio, stays at 1
  236. check_choice(1, "auto", "initial install 2");
  237. install_choice(0); # 0 is higher priority
  238. check_choice(0, "auto", "initial install 3");
  239. # verify that the administrative file is sorted properly
  240. {
  241. local $/ = undef;
  242. open(FILE, "<", "$admindir/generic-test") or die $!;
  243. my $content = <FILE>;
  244. close(FILE);
  245. is($content,
  246. "auto
  247. $bindir/generic-test
  248. slave1
  249. $bindir/slave1
  250. slave2
  251. $bindir/slave2
  252. slave3
  253. $bindir/slave3
  254. slave4
  255. $bindir/slave4
  256. $paths{false}
  257. 10
  258. $paths{date}
  259. $paths{sleep}
  260. 5
  261. $paths{true}
  262. 20
  263. $paths{yes}
  264. $paths{cat}
  265. $paths{cat}
  266. $paths{cat}
  267. ", "administrative file is as expected");
  268. }
  269. # manual change with --set-selections
  270. my $input = "doesntexist auto $paths{date}\ngeneric-test manual $paths{false}\n";
  271. my $output = "";
  272. call_ua(["--set-selections"], from_string => \$input,
  273. to_string => \$output, test_id => "manual update with --set-selections");
  274. check_choice(1, "manual", "manual update with --set-selections");
  275. $input = "generic-test auto $paths{true}\n";
  276. call_ua(["--set-selections"], from_string => \$input,
  277. to_string => \$output, test_id => "auto update with --set-selections");
  278. check_choice(0, "auto", "auto update with --set-selections");
  279. # manual change with set
  280. set_choice(2, test_id => "manual update with --set");
  281. check_choice(2, "manual", "manual update with --set"); # test #388313
  282. remove_choice(2, test_id => "remove manual, back to auto");
  283. check_choice(0, "auto", "remove manual, back to auto");
  284. remove_choice(0, test_id => "remove best");
  285. check_choice(1, "auto", "remove best");
  286. remove_choice(1, test_id => "no alternative left");
  287. check_choice(undef, "", "no alternative left");
  288. # single choice in manual mode, to be removed
  289. install_choice(1);
  290. set_choice(1);
  291. check_choice(1, "manual", "single manual choice");
  292. remove_choice(1);
  293. check_choice(undef, "", "removal single manual");
  294. # test --remove-all
  295. install_choice(0);
  296. install_choice(1);
  297. install_choice(2);
  298. remove_all_choices(test_id => "remove all");
  299. check_choice(undef, "", "no alternative left");
  300. # check auto-recovery of user mistakes (#100135)
  301. install_choice(1);
  302. ok(unlink("$bindir/generic-test"), "failed removal");
  303. ok(unlink("$bindir/slave1"), "failed removal");
  304. install_choice(1);
  305. check_choice(1, "auto", "recreate links in auto mode");
  306. set_choice(1);
  307. ok(unlink("$bindir/generic-test"), "failed removal");
  308. ok(unlink("$bindir/slave1"), "failed removal");
  309. install_choice(1);
  310. check_choice(1, "manual", "recreate links in manual mode");
  311. # check recovery of /etc/alternatives/*
  312. install_choice(0);
  313. ok(unlink("$altdir/generic-test"), "failed removal");
  314. install_choice(1);
  315. check_choice(0, "auto", "<altdir>/generic-test lost, back to auto");
  316. # test --config
  317. config_choice(0);
  318. check_choice(0, "manual", "config to best but manual");
  319. config_choice(1);
  320. check_choice(1, "manual", "config to manual");
  321. config_choice(-1);
  322. check_choice(0, "auto", "config auto");
  323. # test rename of links
  324. install_choice(0);
  325. my $old_slave = $choices[0]{"slaves"}[0]{"link"};
  326. my $old_link = $main_link;
  327. $choices[0]{"slaves"}[0]{"link"} = "$bindir/more/generic-slave";
  328. $main_link = "$bindir/more/mytest";
  329. install_choice(0);
  330. check_choice(0, "auto", "test rename of links");
  331. check_no_link($old_link, "test rename of links");
  332. check_no_link($old_slave, "test rename of links");
  333. # rename with installing other alternatives
  334. $old_link = $main_link;
  335. $main_link = "$bindir/generic-test";
  336. install_choice(1);
  337. check_choice(0, "auto", "rename link");
  338. check_no_link($old_link, "rename link");
  339. # rename with lost file
  340. unlink($old_slave);
  341. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  342. $choices[0]{"slaves"}[0]{"link"} = "$bindir/generic-slave-bis";
  343. install_choice(0);
  344. check_choice(0, "auto", "rename lost file");
  345. check_no_link($old_slave, "rename lost file");
  346. # update of alternative with many slaves not currently installed
  347. # and the link of the renamed slave exists while it should not
  348. set_choice(1);
  349. symlink("$paths{cat}", "$bindir/generic-slave-bis");
  350. $choices[0]{"slaves"}[0]{"link"} = "$bindir/slave2";
  351. install_choice(0, test_id => "update with non-installed slaves");
  352. check_no_link("$bindir/generic-slave-bis",
  353. "drop renamed symlink that should not be installed");
  354. # test install with empty admin file (#457863)
  355. cleanup();
  356. system("touch $admindir/generic-test");
  357. install_choice(0);
  358. # test install with garbage admin file
  359. cleanup();
  360. system("echo garbage > $admindir/generic-test");
  361. install_choice(0, error_to_file => "/dev/null", expect_failure => 1);
  362. # test invalid usages
  363. cleanup();
  364. install_choice(0);
  365. # try to install a slave alternative as new master
  366. call_ua(["--install", "$bindir/testmaster", "slave1", "$paths{date}", "10"],
  367. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  368. # try to install a master alternative as slave
  369. call_ua(["--install", "$bindir/testmaster", "testmaster", "$paths{date}", "10",
  370. "--slave", "$bindir/testslave", "generic-test", "$paths{true}" ],
  371. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  372. # try to reuse master link in slave
  373. call_ua(["--install", "$bindir/testmaster", "testmaster", "$paths{date}", "10",
  374. "--slave", "$bindir/testmaster", "testslave", "$paths{true}" ],
  375. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  376. # try to reuse links in master alternative
  377. call_ua(["--install", "$bindir/slave1", "testmaster", "$paths{date}", "10"],
  378. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  379. # try to reuse links in slave alternative
  380. call_ua(["--install", "$bindir/testmaster", "testmaster", "$paths{date}", "10",
  381. "--slave", "$bindir/generic-test", "testslave", "$paths{true}" ],
  382. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  383. # try to reuse slave link in another slave alternative of another choice of
  384. # the same main alternative
  385. call_ua(["--install", $main_link, $main_name, "$paths{date}", "10",
  386. "--slave", "$bindir/slave1", "testslave", "$paths{true}" ],
  387. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  388. # lack of absolute filenames in links or file path, non-existing path,
  389. call_ua(["--install", "../testmaster", "testmaster", "$paths{date}", "10"],
  390. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  391. call_ua(["--install", "$bindir/testmaster", "testmaster", "./update-alternatives.pl", "10"],
  392. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  393. # non-existing alternative path
  394. call_ua(["--install", "$bindir/testmaster", "testmaster", "$bindir/doesntexist", "10"],
  395. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  396. # invalid alternative name in master
  397. call_ua(["--install", "$bindir/testmaster", "test/master", "$paths{date}", "10"],
  398. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  399. # invalid alternative name in slave
  400. call_ua(["--install", "$bindir/testmaster", "testmaster", "$paths{date}", "10",
  401. "--slave", "$bindir/testslave", "test slave", "$paths{true}" ],
  402. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  403. # install in non-existing dir should fail
  404. call_ua(["--install", "$bindir/doesntexist/testmaster", "testmaster", "$paths{date}", "10",
  405. "--slave", "$bindir/testslave", "testslave", "$paths{true}" ],
  406. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  407. call_ua(["--install", "$bindir/testmaster", "testmaster", "$paths{date}", "10",
  408. "--slave", "$bindir/doesntexist/testslave", "testslave", "$paths{true}" ],
  409. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  410. # non-existing alternative path in slave is not a failure
  411. my $old_path = $choices[0]{"slaves"}[0]{"path"};
  412. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  413. $choices[0]{"slaves"}[0]{"path"} = "$bindir/doesntexist";
  414. $choices[0]{"slaves"}[0]{"link"} = "$bindir/baddir/slave2";
  415. # test rename of slave link that existed but that doesn't anymore
  416. # and link is moved into non-existing dir at the same time
  417. install_choice(0);
  418. check_choice(0, "auto", "optional renamed slave2 in non-existing dir");
  419. # same but on fresh install
  420. cleanup();
  421. install_choice(0);
  422. check_choice(0, "auto", "optional slave2 in non-existing dir");
  423. $choices[0]{"slaves"}[0]{"link"} = $old_slave;
  424. # test fresh install with a non-existing slave file
  425. cleanup();
  426. install_choice(0);
  427. check_choice(0, "auto", "optional slave2");
  428. $choices[0]{"slaves"}[0]{"path"} = $old_path;
  429. # test management of pre-existing files
  430. cleanup();
  431. system("touch $main_link $bindir/slave1");
  432. install_choice(0);
  433. ok(!-l $main_link, "install preserves files that should be links");
  434. ok(!-l "$bindir/slave1", "install preserves files that should be slave links");
  435. remove_choice(0);
  436. ok(-f $main_link, "removal keeps real file installed as master link");
  437. ok(-f "$bindir/slave1", "removal keeps real files installed as slave links");
  438. install_choice(0, params => ["--force"]);
  439. check_choice(0, "auto", "install --force replaces files with links");
  440. # test management of pre-existing files #2
  441. cleanup();
  442. system("touch $main_link $bindir/slave2");
  443. install_choice(0);
  444. install_choice(1);
  445. ok(!-l $main_link, "inactive install preserves files that should be links");
  446. ok(!-l "$bindir/slave2", "inactive install preserves files that should be slave links");
  447. ok(-f $main_link, "inactive install keeps real file installed as master link");
  448. ok(-f "$bindir/slave2", "inactive install keeps real files installed as slave links");
  449. set_choice(1);
  450. ok(!-l $main_link, "manual switching preserves files that should be links");
  451. ok(!-l "$bindir/slave2", "manual switching preserves files that should be slave links");
  452. ok(-f $main_link, "manual switching keeps real file installed as master link");
  453. ok(-f "$bindir/slave2", "manual switching keeps real files installed as slave links");
  454. remove_choice(1);
  455. ok(!-l $main_link, "auto switching preserves files that should be links");
  456. ok(!-l "$bindir/slave2", "auto switching preserves files that should be slave links");
  457. ok(-f $main_link, "auto switching keeps real file installed as master link");
  458. ok(-f "$bindir/slave2", "auto switching keeps real files installed as slave links");
  459. remove_all_choices(params => ["--force"]);
  460. ok(!-e "$bindir/slave2", "forced removeall drops real files installed as slave links");
  461. # test management of pre-existing files #3
  462. cleanup();
  463. system("touch $main_link $bindir/slave2");
  464. install_choice(0);
  465. install_choice(1);
  466. remove_choice(0);
  467. ok(!-l $main_link, "removal + switching preserves files that should be links");
  468. ok(!-l "$bindir/slave2", "removal + switching preserves files that should be slave links");
  469. ok(-f $main_link, "removal + switching keeps real file installed as master link");
  470. ok(-f "$bindir/slave2", "removal + switching keeps real files installed as slave links");
  471. install_choice(0);
  472. ok(!-l $main_link, "install + switching preserves files that should be links");
  473. ok(!-l "$bindir/slave2", "install + switching preserves files that should be slave links");
  474. ok(-f $main_link, "install + switching keeps real file installed as master link");
  475. ok(-f "$bindir/slave2", "install + switching keeps real files installed as slave links");
  476. set_choice(1, params => ["--force"]);
  477. ok(!-e "$bindir/slave2", "forced switching w/o slave drops real files installed as slave links");
  478. check_choice(1, "manual", "set --force replaces files with links");