100_update_alternatives.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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) * 25 # number of check_choices
  79. + 69; # 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 = ("--remove", $main_name, $alt->{path});
  114. call_ua(\@params, %opts);
  115. }
  116. sub remove_all_choices {
  117. my (%opts) = @_;
  118. my @params = ("--remove-all", $main_name);
  119. call_ua(\@params, %opts);
  120. }
  121. sub set_choice {
  122. my ($id, %opts) = @_;
  123. my $alt = $choices[$id];
  124. my @params = ("--set", $main_name, $alt->{path});
  125. call_ua(\@params, %opts);
  126. }
  127. sub config_choice {
  128. my ($id, %opts) = @_;
  129. my ($input, $output) = ("", "");
  130. if ($id >= 0) {
  131. my $alt = $choices[$id];
  132. $input = $alt->{path};
  133. } else {
  134. $input = "0";
  135. }
  136. $input .= "\n";
  137. $opts{from_string} = \$input;
  138. $opts{to_string} = \$output;
  139. my @params = ("--config", $main_name);
  140. call_ua(\@params, %opts);
  141. }
  142. sub get_slaves_status {
  143. my ($id) = @_;
  144. my %slaves;
  145. # None of the slaves are installed
  146. foreach my $alt (@choices) {
  147. for(my $i = 0; $i < @{$alt->{slaves}}; $i++) {
  148. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  149. $slaves{$alt->{slaves}[$i]{name}}{"installed"} = 0;
  150. }
  151. }
  152. # except those of the current alternative (minus optional slaves)
  153. if (defined($id)) {
  154. my $alt = $choices[$id];
  155. for(my $i = 0; $i < @{$alt->{slaves}}; $i++) {
  156. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  157. if (-e $alt->{slaves}[$i]{path}) {
  158. $slaves{$alt->{slaves}[$i]{name}}{"installed"} = 1;
  159. }
  160. }
  161. }
  162. return sort { $a->{name} cmp $b->{name} } values %slaves;
  163. }
  164. sub check_link {
  165. my ($link, $value, $msg) = @_;
  166. ok(-l $link, "$msg: $link disappeared.");
  167. is(readlink($link), $value, "$link doesn't point to $value.");
  168. }
  169. sub check_no_link {
  170. my ($link, $msg) = @_;
  171. lstat($link);
  172. ok(!-e _, "$msg: $link still exists.");
  173. ok(1, "fake test"); # Same number of tests as check_link
  174. }
  175. sub check_slaves {
  176. my ($id, $msg) = @_;
  177. foreach my $slave (get_slaves_status($id)) {
  178. if ($slave->{installed}) {
  179. check_link("$altdir/$slave->{name}", $slave->{path}, $msg);
  180. check_link($slave->{"link"}, "$altdir/$slave->{name}", $msg);
  181. } else {
  182. check_no_link("$altdir/$slave->{name}", $msg);
  183. check_no_link($slave->{"link"}, $msg);
  184. }
  185. }
  186. }
  187. # (4 * (nb_slaves+1) + 2) tests in each check_choice() call
  188. sub check_choice {
  189. my ($id, $mode, $msg) = @_;
  190. my $output;
  191. if (defined $id) {
  192. # Check status
  193. call_ua([ "--query", "$main_name" ], to_string => \$output, test_id => $msg);
  194. $output =~ /^Status: (.*)$/im;
  195. is($1, $mode, "$msg: status is not $mode.");
  196. # Check links
  197. my $alt = $choices[$id];
  198. check_link("$altdir/$main_name", $alt->{path}, $msg);
  199. check_link($main_link, "$altdir/$main_name", $msg);
  200. check_slaves($id, $msg);
  201. } else {
  202. call_ua([ "--query", "$main_name" ], error_to_string => \$output,
  203. expect_failure => 1, test_id => $msg);
  204. ok($output =~ /no alternatives/, "$msg: bad error message for --query.");
  205. # Check that all links have disappeared
  206. check_no_link("$altdir/$main_name", $msg);
  207. check_no_link($main_link, $msg);
  208. check_slaves(undef, $msg);
  209. }
  210. }
  211. ### START OF TESTS
  212. cleanup();
  213. # removal when not installed should not fail
  214. remove_choice(0);
  215. # successive install in auto mode
  216. install_choice(1);
  217. check_choice(1, "auto", "initial install 1");
  218. install_choice(2); # 2 is lower prio, stays at 1
  219. check_choice(1, "auto", "initial install 2");
  220. install_choice(0); # 0 is higher priority
  221. check_choice(0, "auto", "initial install 3");
  222. # verify that the administrative file is sorted properly
  223. {
  224. local $/ = undef;
  225. open(FILE, "<", "$admindir/generic-test") or die $!;
  226. my $content = <FILE>;
  227. close(FILE);
  228. is($content,
  229. "auto
  230. $bindir/generic-test
  231. slave1
  232. $bindir/slave1
  233. slave2
  234. $bindir/slave2
  235. slave3
  236. $bindir/slave3
  237. slave4
  238. $bindir/slave4
  239. /bin/false
  240. 10
  241. /bin/date
  242. /bin/sleep
  243. 5
  244. /bin/true
  245. 20
  246. /usr/bin/yes
  247. /bin/cat
  248. /bin/cat
  249. /bin/cat
  250. ", "administrative file is as expected");
  251. }
  252. # manual change with --set-selections
  253. my $input = "doesntexist auto /bin/date\ngeneric-test manual /bin/false\n";
  254. my $output = "";
  255. call_ua(["--set-selections"], from_string => \$input,
  256. to_string => \$output, test_id => "manual update with --set-selections");
  257. check_choice(1, "manual", "manual update with --set-selections");
  258. $input = "generic-test auto /bin/true\n";
  259. call_ua(["--set-selections"], from_string => \$input,
  260. to_string => \$output, test_id => "auto update with --set-selections");
  261. check_choice(0, "auto", "auto update with --set-selections");
  262. # manual change with set
  263. set_choice(2, test_id => "manual update with --set");
  264. check_choice(2, "manual", "manual update with --set"); # test #388313
  265. remove_choice(2, test_id => "remove manual, back to auto");
  266. check_choice(0, "auto", "remove manual, back to auto");
  267. remove_choice(0, test_id => "remove best");
  268. check_choice(1, "auto", "remove best");
  269. remove_choice(1, test_id => "no alternative left");
  270. check_choice(undef, "", "no alternative left");
  271. # single choice in manual mode, to be removed
  272. install_choice(1);
  273. set_choice(1);
  274. check_choice(1, "manual", "single manual choice");
  275. remove_choice(1);
  276. check_choice(undef, "", "removal single manual");
  277. # test --remove-all
  278. install_choice(0);
  279. install_choice(1);
  280. install_choice(2);
  281. remove_all_choices(test_id => "remove all");
  282. check_choice(undef, "", "no alternative left");
  283. # check auto-recovery of user mistakes (#100135)
  284. install_choice(1);
  285. ok(unlink("$bindir/generic-test"), "failed removal");
  286. ok(unlink("$bindir/slave1"), "failed removal");
  287. install_choice(1);
  288. check_choice(1, "auto", "recreate links in auto mode");
  289. set_choice(1);
  290. ok(unlink("$bindir/generic-test"), "failed removal");
  291. ok(unlink("$bindir/slave1"), "failed removal");
  292. install_choice(1);
  293. check_choice(1, "manual", "recreate links in manual mode");
  294. # check recovery of /etc/alternatives/*
  295. install_choice(0);
  296. ok(unlink("$altdir/generic-test"), "failed removal");
  297. install_choice(1);
  298. check_choice(0, "auto", "<altdir>/generic-test lost, back to auto");
  299. # test --config
  300. config_choice(0);
  301. check_choice(0, "manual", "config to best but manual");
  302. config_choice(1);
  303. check_choice(1, "manual", "config to manual");
  304. config_choice(-1);
  305. check_choice(0, "auto", "config auto");
  306. # test rename of links
  307. install_choice(0);
  308. my $old_slave = $choices[0]{"slaves"}[0]{"link"};
  309. my $old_link = $main_link;
  310. $choices[0]{"slaves"}[0]{"link"} = "$bindir/more/generic-slave";
  311. $main_link = "$bindir/more/mytest";
  312. install_choice(0);
  313. check_choice(0, "auto", "test rename of links");
  314. check_no_link($old_link, "test rename of links");
  315. check_no_link($old_slave, "test rename of links");
  316. # rename with installing other alternatives
  317. $old_link = $main_link;
  318. $main_link = "$bindir/generic-test";
  319. install_choice(1);
  320. check_choice(0, "auto", "rename link");
  321. check_no_link($old_link, "rename link");
  322. # rename with lost file
  323. unlink($old_slave);
  324. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  325. $choices[0]{"slaves"}[0]{"link"} = "$bindir/generic-slave-bis";
  326. install_choice(0);
  327. check_choice(0, "auto", "rename lost file");
  328. check_no_link($old_slave, "rename lost file");
  329. $choices[0]{"slaves"}[0]{"link"} = "$bindir/slave2";
  330. # test install with empty admin file (#457863)
  331. cleanup();
  332. system("touch $admindir/generic-test");
  333. install_choice(0);
  334. # test install with garbage admin file
  335. cleanup();
  336. system("echo garbage > $admindir/generic-test");
  337. install_choice(0, error_to_file => "/dev/null", expect_failure => 1);
  338. # test invalid usages
  339. cleanup();
  340. install_choice(0);
  341. # try to install a slave alternative as new master
  342. call_ua(["--install", "$bindir/testmaster", "slave1", "/bin/date", "10"],
  343. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  344. # try to install a master alternative as slave
  345. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  346. "--slave", "$bindir/testslave", "generic-test", "/bin/true" ],
  347. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  348. # try to reuse master link in slave
  349. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  350. "--slave", "$bindir/testmaster", "testslave", "/bin/true" ],
  351. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  352. # try to reuse links in master alternative
  353. call_ua(["--install", "$bindir/slave1", "testmaster", "/bin/date", "10"],
  354. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  355. # try to reuse links in slave alternative
  356. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  357. "--slave", "$bindir/generic-test", "testslave", "/bin/true" ],
  358. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  359. # lack of absolute filenames in links or file path, non-existing path,
  360. call_ua(["--install", "../testmaster", "testmaster", "/bin/date", "10"],
  361. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  362. call_ua(["--install", "$bindir/testmaster", "testmaster", "./update-alternatives.pl", "10"],
  363. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  364. # non-existing alternative path
  365. call_ua(["--install", "$bindir/testmaster", "testmaster", "$bindir/doesntexist", "10"],
  366. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  367. # invalid alternative name in master
  368. call_ua(["--install", "$bindir/testmaster", "test/master", "/bin/date", "10"],
  369. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  370. # invalid alternative name in slave
  371. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  372. "--slave", "$bindir/testslave", "test slave", "/bin/true" ],
  373. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  374. # install in non-existing dir should fail
  375. call_ua(["--install", "$bindir/doesntexist/testmaster", "testmaster", "/bin/date", "10",
  376. "--slave", "$bindir/testslave", "testslave", "/bin/true" ],
  377. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  378. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  379. "--slave", "$bindir/doesntexist/testslave", "testslave", "/bin/true" ],
  380. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  381. # non-existing alternative path in slave is not a failure
  382. my $old_path = $choices[0]{"slaves"}[0]{"path"};
  383. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  384. $choices[0]{"slaves"}[0]{"path"} = "$bindir/doesntexist";
  385. $choices[0]{"slaves"}[0]{"link"} = "$bindir/baddir/slave2";
  386. # test rename of slave link that existed but that doesnt anymore
  387. # and link is moved into non-existing dir at the same time
  388. install_choice(0);
  389. check_choice(0, "auto", "optional renamed slave2 in non-existing dir");
  390. # same but on fresh install
  391. cleanup();
  392. install_choice(0);
  393. check_choice(0, "auto", "optional slave2 in non-existing dir");
  394. $choices[0]{"slaves"}[0]{"link"} = $old_slave;
  395. # test fresh install with a non-existing slave file
  396. cleanup();
  397. install_choice(0);
  398. check_choice(0, "auto", "optional slave2");
  399. $choices[0]{"slaves"}[0]{"path"} = $old_path;
  400. # test management of pre-existing files
  401. cleanup();
  402. system("touch $main_link $bindir/slave1");
  403. install_choice(0);
  404. ok(!-l $main_link, "install preserves files that should be links");
  405. ok(!-l "$bindir/slave1", "install preserves files that should be slave links");
  406. remove_choice(0);
  407. ok(-f $main_link, "removal keeps real file installed as master link");
  408. ok(-f "$bindir/slave1", "removal keeps real files installed as slave links");
  409. install_choice(0, params => ["--force"]);
  410. check_choice(0, "auto", "install --force replaces files with links");