900_update_alternatives.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. # XXX: switch to version without .pl
  26. my @ua = ("$srcdir/update-alternatives.pl", "--log", "/dev/null",
  27. "--quiet", "--admindir", "$admindir", "--altdir", "$altdir");
  28. my $main_link = "$bindir/generic-test";
  29. my $main_name = "generic-test";
  30. my @choices = (
  31. {
  32. path => "/bin/true",
  33. priority => 20,
  34. slaves => [
  35. {
  36. "link" => "$bindir/slave2",
  37. name => "slave2",
  38. path => "/bin/cat",
  39. },
  40. {
  41. "link" => "$bindir/slave1",
  42. name => "slave1",
  43. path => "/usr/bin/yes",
  44. },
  45. ],
  46. },
  47. {
  48. path => "/bin/false",
  49. priority => 10,
  50. slaves => [
  51. {
  52. "link" => "$bindir/slave1",
  53. name => "slave1",
  54. path => "/bin/date",
  55. },
  56. ],
  57. },
  58. {
  59. path => "/bin/sleep",
  60. priority => 5,
  61. slaves => [],
  62. },
  63. );
  64. my $nb_slaves = 2;
  65. plan tests => (4 * ($nb_slaves + 1) + 2) * 24 # number of check_choices
  66. + 63; # rest
  67. sub cleanup {
  68. system("rm -rf $tmpdir && mkdir -p $admindir && mkdir -p $altdir");
  69. system("mkdir -p $bindir/more");
  70. }
  71. sub call_ua {
  72. my ($params, %opts) = @_;
  73. fork_and_exec("exec" => [ @ua, @$params ], nocheck => 1,
  74. wait_child => 1, env => { LC_ALL => "C" }, %opts);
  75. if ($opts{"expect_failure"}) {
  76. ok($? != 0, "update-alternatives @$params did not fail.");
  77. } else {
  78. ok($? == 0, "update-alternatives @$params failed.");
  79. }
  80. }
  81. sub install_choice {
  82. my ($id, %opts) = @_;
  83. my $alt = $choices[$id];
  84. my @params;
  85. push @params, @{$opts{params}} if exists $opts{params};
  86. push @params, "--install", "$main_link", "$main_name",
  87. $alt->{path}, $alt->{priority};
  88. foreach my $slave (@{ $alt->{slaves} }) {
  89. push @params, "--slave", $slave->{"link"}, $slave->{"name"}, $slave->{"path"};
  90. }
  91. call_ua(\@params, %opts);
  92. }
  93. sub remove_choice {
  94. my ($id, %opts) = @_;
  95. my $alt = $choices[$id];
  96. my @params = ("--remove", $main_name, $alt->{path});
  97. call_ua(\@params, %opts);
  98. }
  99. sub set_choice {
  100. my ($id, %opts) = @_;
  101. my $alt = $choices[$id];
  102. my @params = ("--set", $main_name, $alt->{path});
  103. call_ua(\@params, %opts);
  104. }
  105. sub config_choice {
  106. my ($id, %opts) = @_;
  107. my ($input, $output) = ("", "");
  108. if ($id >= 0) {
  109. my $alt = $choices[$id];
  110. $input = $alt->{path};
  111. } else {
  112. $input = "0";
  113. }
  114. $input .= "\n";
  115. $opts{from_string} = \$input;
  116. $opts{to_string} = \$output;
  117. my @params = ("--config", $main_name);
  118. call_ua(\@params, %opts);
  119. }
  120. sub get_slaves_status {
  121. my ($id) = @_;
  122. my %slaves;
  123. # None of the slaves are installed
  124. foreach my $alt (@choices) {
  125. for(my $i = 0; $i < @{$alt->{slaves}}; $i++) {
  126. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  127. $slaves{$alt->{slaves}[$i]{name}}{"installed"} = 0;
  128. }
  129. }
  130. # except those of the current alternative (minus optional slaves)
  131. if (defined($id)) {
  132. my $alt = $choices[$id];
  133. for(my $i = 0; $i < @{$alt->{slaves}}; $i++) {
  134. $slaves{$alt->{slaves}[$i]{name}} = $alt->{slaves}[$i];
  135. if (-e $alt->{slaves}[$i]{path}) {
  136. $slaves{$alt->{slaves}[$i]{name}}{"installed"} = 1;
  137. }
  138. }
  139. }
  140. return sort { $a->{name} cmp $b->{name} } values %slaves;
  141. }
  142. sub check_link {
  143. my ($link, $value, $msg) = @_;
  144. ok(-l $link, "$msg: $link disappeared.");
  145. is(readlink($link), $value, "$link doesn't point to $value.");
  146. }
  147. sub check_no_link {
  148. my ($link, $msg) = @_;
  149. lstat($link);
  150. ok(!-e _, "$msg: $link still exists.");
  151. ok(1, "fake test"); # Same number of tests as check_link
  152. }
  153. sub check_slaves {
  154. my ($id, $msg) = @_;
  155. foreach my $slave (get_slaves_status($id)) {
  156. if ($slave->{installed}) {
  157. check_link("$altdir/$slave->{name}", $slave->{path}, $msg);
  158. check_link($slave->{"link"}, "$altdir/$slave->{name}", $msg);
  159. } else {
  160. check_no_link("$altdir/$slave->{name}", $msg);
  161. check_no_link($slave->{"link"}, $msg);
  162. }
  163. }
  164. }
  165. # (4 * (nb_slaves+1) + 2) tests in each check_choice() call
  166. sub check_choice {
  167. my ($id, $mode, $msg) = @_;
  168. my $output;
  169. if (defined $id) {
  170. # Check status
  171. call_ua([ "--query", "$main_name" ], to_string => \$output);
  172. $output =~ /^Status: (.*)$/im;
  173. is($1, $mode, "$msg: status is not $mode.");
  174. # Check links
  175. my $alt = $choices[$id];
  176. check_link("$altdir/$main_name", $alt->{path}, $msg);
  177. check_link($main_link, "$altdir/$main_name", $msg);
  178. check_slaves($id, $msg);
  179. } else {
  180. call_ua([ "--query", "$main_name" ], error_to_string => \$output,
  181. expect_failure => 1);
  182. ok($output =~ /no alternatives/, "$msg: bad error message for --query.");
  183. # Check that all links have disappeared
  184. check_no_link("$altdir/$main_name", $msg);
  185. check_no_link($main_link, $msg);
  186. check_slaves(undef, $msg);
  187. }
  188. }
  189. ### START OF TESTS
  190. cleanup();
  191. # removal when not installed should not fail
  192. remove_choice(0);
  193. # successive install in auto mode
  194. install_choice(1);
  195. check_choice(1, "auto", "initial install 1");
  196. install_choice(2); # 2 is lower prio, stays at 1
  197. check_choice(1, "auto", "initial install 2");
  198. install_choice(0); # 0 is higher priority
  199. check_choice(0, "auto", "initial install 3");
  200. # manual change with --set-selections
  201. my $input = "doesntexist auto /bin/date\ngeneric-test manual /bin/false\n";
  202. my $output = "";
  203. call_ua(["--set-selections"], from_string => \$input,
  204. to_string => \$output);
  205. check_choice(1, "manual", "manual update with --set-selections");
  206. $input = "generic-test auto /bin/true\n";
  207. call_ua(["--set-selections"], from_string => \$input,
  208. to_string => \$output);
  209. check_choice(0, "auto", "auto update with --set-selections");
  210. # manual change with set
  211. set_choice(2);
  212. check_choice(2, "manual", "manual update with --set"); # test #388313
  213. remove_choice(2);
  214. check_choice(0, "auto", "remove manual, back to auto");
  215. remove_choice(0);
  216. check_choice(1, "auto", "remove best");
  217. remove_choice(1);
  218. check_choice(undef, "", "no alternative left");
  219. # single choice in manual mode, to be removed
  220. install_choice(1);
  221. set_choice(1);
  222. check_choice(1, "manual", "single manual choice");
  223. remove_choice(1);
  224. check_choice(undef, "", "removal single manual");
  225. # check auto-recovery of user mistakes (#100135)
  226. install_choice(1);
  227. ok(unlink("$bindir/generic-test"), "failed removal");
  228. ok(unlink("$bindir/slave1"), "failed removal");
  229. install_choice(1);
  230. check_choice(1, "auto", "recreate links in auto mode");
  231. set_choice(1);
  232. ok(unlink("$bindir/generic-test"), "failed removal");
  233. ok(unlink("$bindir/slave1"), "failed removal");
  234. install_choice(1);
  235. check_choice(1, "manual", "recreate links in manual mode");
  236. # check recovery of /etc/alternatives/*
  237. install_choice(0);
  238. ok(unlink("$altdir/generic-test"), "failed removal");
  239. install_choice(1);
  240. check_choice(0, "auto", "<altdir>/generic-test lost, back to auto");
  241. # test --config
  242. config_choice(0);
  243. check_choice(0, "manual", "config to best but manual");
  244. config_choice(1);
  245. check_choice(1, "manual", "config to manual");
  246. config_choice(-1);
  247. check_choice(0, "auto", "config auto");
  248. # test rename of links
  249. install_choice(0);
  250. my $old_slave = $choices[0]{"slaves"}[0]{"link"};
  251. my $old_link = $main_link;
  252. $choices[0]{"slaves"}[0]{"link"} = "$bindir/more/generic-slave";
  253. $main_link = "$bindir/more/mytest";
  254. install_choice(0);
  255. check_choice(0, "auto", "test rename of links");
  256. check_no_link($old_link, "test rename of links");
  257. check_no_link($old_slave, "test rename of links");
  258. # rename with installing other alternatives
  259. $old_link = $main_link;
  260. $main_link = "$bindir/generic-test";
  261. install_choice(1);
  262. check_choice(0, "auto", "rename link");
  263. check_no_link($old_link, "rename link");
  264. # rename with lost file
  265. unlink($old_slave);
  266. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  267. $choices[0]{"slaves"}[0]{"link"} = "$bindir/generic-slave-bis";
  268. install_choice(0);
  269. check_choice(0, "auto", "rename lost file");
  270. check_no_link($old_slave, "rename lost file");
  271. $choices[0]{"slaves"}[0]{"link"} = "$bindir/slave2";
  272. # test install with empty admin file (#457863)
  273. cleanup();
  274. system("touch $admindir/generic-test");
  275. install_choice(0);
  276. # test install with garbage admin file
  277. cleanup();
  278. system("echo garbage > $admindir/generic-test");
  279. install_choice(0, error_to_file => "/dev/null", expect_failure => 1);
  280. # test invalid usages
  281. cleanup();
  282. install_choice(0);
  283. # try to install a slave alternative as new master
  284. call_ua(["--install", "$bindir/testmaster", "slave1", "/bin/date", "10"],
  285. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  286. # try to install a master alternative as slave
  287. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  288. "--slave", "$bindir/testslave", "generic-test", "/bin/true" ],
  289. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  290. # try to reuse links in master alternative
  291. call_ua(["--install", "$bindir/slave1", "testmaster", "/bin/date", "10"],
  292. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  293. # try to reuse links in slave alternative
  294. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  295. "--slave", "$bindir/generic-test", "testslave", "/bin/true" ],
  296. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  297. # lack of absolute filenames in links or file path, non-existing path,
  298. call_ua(["--install", "../testmaster", "testmaster", "/bin/date", "10"],
  299. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  300. call_ua(["--install", "$bindir/testmaster", "testmaster", "./update-alternatives.pl", "10"],
  301. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  302. # non-existing alternative path
  303. call_ua(["--install", "$bindir/testmaster", "testmaster", "$bindir/doesntexist", "10"],
  304. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  305. # invalid alternative name in master
  306. call_ua(["--install", "$bindir/testmaster", "test/master", "/bin/date", "10"],
  307. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  308. # invalid alternative name in slave
  309. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  310. "--slave", "$bindir/testslave", "test slave", "/bin/true" ],
  311. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  312. # install in non-existing dir should fail
  313. call_ua(["--install", "$bindir/doesntexist/testmaster", "testmaster", "/bin/date", "10",
  314. "--slave", "$bindir/testslave", "testslave", "/bin/true" ],
  315. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  316. call_ua(["--install", "$bindir/testmaster", "testmaster", "/bin/date", "10",
  317. "--slave", "$bindir/doesntexist/testslave", "testslave", "/bin/true" ],
  318. expect_failure => 1, to_file => "/dev/null", error_to_file => "/dev/null");
  319. # non-existing alternative path in slave is not a failure
  320. my $old_path = $choices[0]{"slaves"}[0]{"path"};
  321. $old_slave = $choices[0]{"slaves"}[0]{"link"};
  322. $choices[0]{"slaves"}[0]{"path"} = "$bindir/doesntexist";
  323. $choices[0]{"slaves"}[0]{"link"} = "$bindir/baddir/slave2";
  324. # test rename of slave link that existed but that doesnt anymore
  325. # and link is moved into non-existing dir at the same time
  326. install_choice(0);
  327. check_choice(0, "auto", "optional renamed slave2 in non-existing dir");
  328. # same but on fresh install
  329. cleanup();
  330. install_choice(0);
  331. check_choice(0, "auto", "optional slave2 in non-existing dir");
  332. $choices[0]{"slaves"}[0]{"link"} = $old_slave;
  333. # test fresh install with a non-existing slave file
  334. cleanup();
  335. install_choice(0);
  336. check_choice(0, "auto", "optional slave2");
  337. $choices[0]{"slaves"}[0]{"path"} = $old_path;
  338. # test management of pre-existing files
  339. cleanup();
  340. system("touch $main_link $bindir/slave1");
  341. install_choice(0);
  342. ok(!-l $main_link, "install preserves files that should be links");
  343. ok(!-l "$bindir/slave1", "install preserves files that should be slave links");
  344. remove_choice(0);
  345. ok(-f $main_link, "removal keeps real file installed as master link");
  346. ok(-f "$bindir/slave1", "removal keeps real files installed as slave links");
  347. install_choice(0, params => ["--force"]);
  348. check_choice(0, "auto", "install --force replaces files with links");