900_update_alternatives.t 13 KB

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