900_update_alternatives.t 12 KB

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