100_update_alternatives.t 17 KB

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