Dpkg_Deps.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/usr/bin/perl
  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 <https://www.gnu.org/licenses/>.
  15. use strict;
  16. use warnings;
  17. use Test::More tests => 70;
  18. use Dpkg::Arch qw(get_host_arch);
  19. use Dpkg::Version;
  20. use_ok('Dpkg::Deps');
  21. is(deps_concat(), '', 'Concatenate an empty list');
  22. is(deps_concat(undef), '', 'Concatenate list with undef');
  23. is(deps_concat(''), '', 'Concatenate an empty string');
  24. is(deps_concat('', undef), '', 'Concatenate empty string with undef');
  25. is(deps_concat('dep-a', undef, 'dep-b'), 'dep-a, dep-b',
  26. 'Concatenate two strings with intermixed undef');
  27. sub test_dep_parse_option {
  28. my %options = @_;
  29. eval {
  30. my $dep_croak = deps_parse('pkg', %options);
  31. };
  32. my $options = join ' ', map { "$_=$options{$_}" } keys %options;
  33. ok(defined $@, "Parse with bogus arch options $options");
  34. }
  35. test_dep_parse_option(host_arch => 'all');
  36. test_dep_parse_option(host_arch => 'any');
  37. test_dep_parse_option(host_arch => 'linux-any');
  38. test_dep_parse_option(host_arch => 'unknown-arch');
  39. test_dep_parse_option(build_arch => 'all');
  40. test_dep_parse_option(build_arch => 'any');
  41. test_dep_parse_option(build_arch => 'linux-any');
  42. test_dep_parse_option(build_arch => 'unknown-arch');
  43. my $field_multiline = ' , , libgtk2.0-common (= 2.10.13-1) , libatk1.0-0 (>=
  44. 1.13.2), libc6 (>= 2.5-5), libcairo2 (>= 1.4.0), libcupsys2 (>= 1.2.7),
  45. libfontconfig1 (>= 2.4.0), libglib2.0-0 ( >= 2.12.9), libgnutls13 (>=
  46. 1.6.3-0), libjpeg62, python (<< 2.5) , , ';
  47. my $field_multiline_sorted = 'libatk1.0-0 (>= 1.13.2), libc6 (>= 2.5-5), libcairo2 (>= 1.4.0), libcupsys2 (>= 1.2.7), libfontconfig1 (>= 2.4.0), libglib2.0-0 (>= 2.12.9), libgnutls13 (>= 1.6.3-0), libgtk2.0-common (= 2.10.13-1), libjpeg62, python (<< 2.5)';
  48. my $dep_multiline = deps_parse($field_multiline);
  49. $dep_multiline->sort();
  50. is($dep_multiline->output(), $field_multiline_sorted, 'Parse, sort and output');
  51. my $dep_sorted = deps_parse('pkgz, pkgz | pkgb, pkgz | pkga, pkga (>= 1.0), pkgz (<= 2.0)');
  52. $dep_sorted->sort();
  53. is($dep_sorted->output(), 'pkga (>= 1.0), pkgz, pkgz | pkga, pkgz | pkgb, pkgz (<= 2.0)', 'Check sort() algorithm');
  54. my $dep_subset = deps_parse('libatk1.0-0 (>> 1.10), libc6, libcairo2');
  55. is($dep_multiline->implies($dep_subset), 1, 'Dep implies subset of itself');
  56. is($dep_subset->implies($dep_multiline), undef, "Subset doesn't imply superset");
  57. my $dep_opposite = deps_parse('python (>= 2.5)');
  58. is($dep_opposite->implies($dep_multiline), 0, 'Opposite condition implies NOT the depends');
  59. my $dep_or1 = deps_parse('a|b (>=1.0)|c (>= 2.0)');
  60. my $dep_or2 = deps_parse('x|y|a|b|c (<= 0.5)|c (>=1.5)|d|e');
  61. is($dep_or1->implies($dep_or2), 1, 'Implication between OR 1/2');
  62. is($dep_or2->implies($dep_or1), undef, 'Implication between OR 2/2');
  63. my $dep_ma_host = deps_parse('libcairo2');
  64. my $dep_ma_any = deps_parse('libcairo2:any');
  65. my $dep_ma_build = deps_parse('libcairo2:native', build_dep => 1);
  66. my $dep_ma_explicit = deps_parse('libcairo2:amd64');
  67. is($dep_ma_host->implies($dep_ma_any), undef, 'foo !-> foo:any');
  68. is($dep_ma_build->implies($dep_ma_any), undef, 'foo:native !-> foo:any');
  69. is($dep_ma_explicit->implies($dep_ma_any), undef, 'foo:<arch> !-> foo:any');
  70. is($dep_ma_any->implies($dep_ma_host), undef, 'foo:any !-> foo');
  71. is($dep_ma_any->implies($dep_ma_build), undef, 'foo:any !-> foo:native');
  72. is($dep_ma_any->implies($dep_ma_explicit), undef, 'foo:any !-> foo:<arch>');
  73. is($dep_ma_host->implies($dep_ma_host), 1, 'foo -> foo');
  74. is($dep_ma_any->implies($dep_ma_any), 1, 'foo:any -> foo:any');
  75. is($dep_ma_build->implies($dep_ma_build), 1, 'foo:native -> foo:native');
  76. is($dep_ma_explicit->implies($dep_ma_explicit), 1, 'foo:<arch>-> foo:<arch>');
  77. my $field_tests = 'self, @, @builddeps@';
  78. $SIG{__WARN__} = sub {};
  79. my $dep_tests_fail = deps_parse($field_tests);
  80. is($dep_tests_fail, undef, 'normal deps with @ in pkgname');
  81. delete $SIG{__WARN__};
  82. my $dep_tests_pass = deps_parse($field_tests, tests_dep => 1);
  83. is($dep_tests_pass->output(), $field_tests, 'tests deps with @ in pkgname');
  84. my $field_arch = 'libc6 (>= 2.5) [!alpha !hurd-i386], libc6.1 [alpha], libc0.1 [hurd-i386]';
  85. my $dep_i386 = deps_parse($field_arch, reduce_arch => 1, host_arch => 'i386');
  86. my $dep_alpha = deps_parse($field_arch, reduce_arch => 1, host_arch => 'alpha');
  87. my $dep_hurd = deps_parse($field_arch, reduce_arch => 1, host_arch => 'hurd-i386');
  88. is($dep_i386->output(), 'libc6 (>= 2.5)', 'Arch reduce 1/3');
  89. is($dep_alpha->output(), 'libc6.1', 'Arch reduce 2/3');
  90. is($dep_hurd->output(), 'libc0.1', 'Arch reduce 3/3');
  91. my $field_profile = 'dep1 <!stage1 !nocheck>, ' .
  92. 'dep2 <stage1 !nocheck>, ' .
  93. 'dep3 <nocheck !stage1>, ' .
  94. 'dep4 <stage1 nocheck>, ' .
  95. 'dep5 <stage1>, dep6 <!stage1>, ' .
  96. 'dep7 <stage1> | dep8 <nocheck>, ' .
  97. 'dep9 <!stage1> <!nocheck>, ' .
  98. 'dep10 <stage1> <!nocheck>, ' .
  99. 'dep11 <stage1> <nocheck>, '.
  100. 'dep12 <!nocheck> <!stage1>, ' .
  101. 'dep13 <nocheck> <!stage1>, ' .
  102. 'dep14 <nocheck> <stage1>';
  103. my $dep_noprof = deps_parse($field_profile, reduce_profiles => 1, build_profiles => []);
  104. my $dep_stage1 = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['stage1']);
  105. my $dep_nocheck = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['nocheck']);
  106. my $dep_stage1nocheck = deps_parse($field_profile, reduce_profiles => 1, build_profiles => ['stage1', 'nocheck']);
  107. is($dep_noprof->output(), 'dep1, dep6, dep9, dep10, dep12, dep13', 'Profile reduce 1/4');
  108. is($dep_stage1->output(), 'dep2, dep5, dep7, dep9, dep10, dep11, dep12, dep14', 'Profile reduce 2/4');
  109. is($dep_nocheck->output(), 'dep3, dep6, dep8, dep9, dep11, dep12, dep13, dep14', 'Profile reduce 3/4');
  110. is($dep_stage1nocheck->output(), 'dep4, dep5, dep7 | dep8, dep10, dep11, dep13, dep14', 'Profile reduce 4/4');
  111. $dep_noprof = deps_parse($field_profile);
  112. $dep_noprof->reduce_profiles([]);
  113. $dep_stage1 = deps_parse($field_profile);
  114. $dep_stage1->reduce_profiles(['stage1']);
  115. $dep_nocheck = deps_parse($field_profile);
  116. $dep_nocheck->reduce_profiles(['nocheck']);
  117. $dep_stage1nocheck = deps_parse($field_profile);
  118. $dep_stage1nocheck->reduce_profiles(['stage1', 'nocheck']);
  119. is($dep_noprof->output(), 'dep1, dep6, dep9, dep10, dep12, dep13', 'Profile post-reduce 1/4');
  120. is($dep_stage1->output(), 'dep2, dep5, dep7, dep9, dep10, dep11, dep12, dep14', 'Profile post-reduce 2/4');
  121. is($dep_nocheck->output(), 'dep3, dep6, dep8, dep9, dep11, dep12, dep13, dep14', 'Profile post-reduce 3/4');
  122. is($dep_stage1nocheck->output(), 'dep4, dep5, dep7 | dep8, dep10, dep11, dep13, dep14', 'Profile post-reduce 4/4');
  123. my $field_restrict = 'dep1 <!bootstrap !restrict>, ' .
  124. 'dep2 <bootstrap restrict>, ' .
  125. 'dep3 <!restrict>, ' .
  126. 'dep4 <restrict>, ' .
  127. 'dep5 <!bootstrap> <!restrict>, ' .
  128. 'dep6 <bootstrap> <restrict>';
  129. my $dep_restrict = deps_parse($field_restrict, reduce_restrictions => 1, build_profiles => []);
  130. is($dep_restrict->output(), 'dep1, dep3, dep5', 'Unknown restrictions reduce');
  131. $dep_restrict = deps_parse($field_restrict);
  132. $dep_restrict->reduce_profiles([]);
  133. is($dep_restrict->output(), 'dep1, dep3, dep5', 'Unknown restrictions post-reduce');
  134. my $facts = Dpkg::Deps::KnownFacts->new();
  135. $facts->add_installed_package('mypackage', '1.3.4-1', get_host_arch(), 'no');
  136. $facts->add_installed_package('mypackage2', '1.3.4-1', 'somearch', 'no');
  137. $facts->add_installed_package('pkg-ma-foreign', '1.3.4-1', 'somearch', 'foreign');
  138. $facts->add_installed_package('pkg-ma-foreign2', '1.3.4-1', get_host_arch(), 'foreign');
  139. $facts->add_installed_package('pkg-ma-allowed', '1.3.4-1', 'somearch', 'allowed');
  140. $facts->add_installed_package('pkg-ma-allowed2', '1.3.4-1', 'somearch', 'allowed');
  141. $facts->add_installed_package('pkg-ma-allowed3', '1.3.4-1', get_host_arch(), 'allowed');
  142. $facts->add_provided_package('myvirtual', undef, undef, 'mypackage');
  143. $facts->add_provided_package('myvirtual2', REL_EQ, '1.0-1', 'mypackage');
  144. $facts->add_provided_package('myvirtual3', REL_GE, '2.0-1', 'mypackage');
  145. my $field_duplicate = 'libc6 (>= 2.3), libc6 (>= 2.6-1), mypackage (>=
  146. 1.3), myvirtual | something, python (>= 2.5), mypackage2, pkg-ma-foreign,
  147. pkg-ma-foreign2, pkg-ma-allowed:any, pkg-ma-allowed2, pkg-ma-allowed3';
  148. my $dep_dup = deps_parse($field_duplicate);
  149. $dep_dup->simplify_deps($facts, $dep_opposite);
  150. is($dep_dup->output(), 'libc6 (>= 2.6-1), mypackage2, pkg-ma-allowed2', 'Simplify deps');
  151. my $field_virtual = 'myvirtual | other';
  152. my $dep_virtual = deps_parse($field_virtual);
  153. $dep_virtual->simplify_deps($facts);
  154. is($dep_virtual->output(), '',
  155. 'Simplify unversioned depends with unversioned virtual (satisfied)');
  156. $field_virtual = 'myvirtual (>= 1.0) | other';
  157. $dep_virtual = deps_parse($field_virtual);
  158. $dep_virtual->simplify_deps($facts);
  159. is($dep_virtual->output(), 'myvirtual (>= 1.0) | other',
  160. 'Simplify versioned depends on unversioned virtual (unsatisfied)');
  161. $field_virtual = 'myvirtual2 (>= 0.0) | other';
  162. $dep_virtual = deps_parse($field_virtual);
  163. $dep_virtual->simplify_deps($facts);
  164. is($dep_virtual->output(), '',
  165. 'Simplify versioned depends on versioned virtual (satisfied)');
  166. $field_virtual = 'myvirtual2 (>= 2.0) | other';
  167. $dep_virtual = deps_parse($field_virtual);
  168. $dep_virtual->simplify_deps($facts);
  169. is($dep_virtual->output(), 'myvirtual2 (>= 2.0) | other',
  170. 'Simplify versioned depends on versioned virtual (unsatisfied)');
  171. $field_virtual = 'myvirtual3 (= 2.0-1)';
  172. $dep_virtual = deps_parse($field_virtual);
  173. $dep_virtual->simplify_deps($facts);
  174. is($dep_virtual->output(), 'myvirtual3 (= 2.0-1)',
  175. 'Simplify versioned depends on GT versioned virtual (unsatisfied/ignored)');
  176. my $field_dup_union = 'libc6 (>> 2.3), libc6 (>= 2.6-1), fake (<< 2.0),
  177. fake(>> 3.0), fake (= 2.5), python (<< 2.5), python (= 2.4)';
  178. my $dep_dup_union = deps_parse($field_dup_union, union => 1);
  179. $dep_dup_union->simplify_deps($facts);
  180. is($dep_dup_union->output(), 'libc6 (>> 2.3), fake (<< 2.0), fake (>> 3.0), fake (= 2.5), python (<< 2.5)', 'Simplify union deps');
  181. $dep_dup_union = deps_parse('sipsak (<= 0.9.6-2.1), sipsak (<= 0.9.6-2.2)', union => 1);
  182. $dep_dup_union->simplify_deps($facts);
  183. is($dep_dup_union->output(), 'sipsak (<= 0.9.6-2.2)', 'Simplify union deps 2');
  184. my $dep_red = deps_parse('abc | xyz, two, abc');
  185. $dep_red->simplify_deps($facts, $dep_opposite);
  186. is($dep_red->output(), 'abc, two', 'Simplification respect order');
  187. is("$dep_red", $dep_red->output(), 'Stringification == output()');
  188. my $dep_profiles = deps_parse('dupe <stage1 cross>, dupe <stage1 cross>');
  189. $dep_profiles->simplify_deps($facts);
  190. is($dep_profiles->output(), 'dupe <stage1 cross>',
  191. 'Simplification respects duplicated profiles');
  192. $dep_profiles = deps_parse('tool <!cross>, tool <stage1 cross>');
  193. $dep_profiles->simplify_deps($facts);
  194. # XXX: Ideally this would get simplified to "tool <!cross> <stage1 cross>".
  195. is($dep_profiles->output(), 'tool <!cross>, tool <stage1 cross>',
  196. 'Simplification respects profiles');
  197. $dep_profiles = deps_parse('libfoo-dev:native <!stage1>, libfoo-dev <!stage1 cross>', build_dep => 1);
  198. $dep_profiles->simplify_deps($facts);
  199. is($dep_profiles->output(),
  200. 'libfoo-dev:native <!stage1>, libfoo-dev <!stage1 cross>',
  201. 'Simplification respects archqualifiers and profiles');
  202. my $dep_version = deps_parse('pkg, pkg (= 1.0)');
  203. $dep_version->simplify_deps($facts);
  204. is($dep_version->output(), 'pkg (= 1.0)', 'Simplification merges versions');
  205. my $dep_empty1 = deps_parse('');
  206. is($dep_empty1->output(), '', 'Empty dependency');
  207. my $dep_empty2 = deps_parse(' , , ', union => 1);
  208. is($dep_empty2->output(), '', "' , , ' is also an empty dependency");
  209. # Check sloppy but acceptable dependencies
  210. my $dep_sloppy_version = deps_parse('package (= 1.0 )');
  211. is($dep_sloppy_version->output(), 'package (= 1.0)', 'sloppy version restriction');
  212. my $dep_sloppy_arch = deps_parse('package [ alpha ]');
  213. is($dep_sloppy_arch->output(), 'package [alpha]', 'sloppy arch restriction');
  214. my $dep_sloppy_profile = deps_parse('package < !profile > < other >');
  215. is($dep_sloppy_profile->output(), 'package <!profile> <other>',
  216. 'sloppy profile restriction');
  217. $SIG{__WARN__} = sub {};
  218. my $dep_bad_version = deps_parse('package (= 1.0) (>= 2.0)');
  219. is($dep_bad_version, undef, 'Bogus repeated version restriction');
  220. my $dep_bad_arch = deps_parse('package [alpha] [amd64]');
  221. is($dep_bad_arch, undef, 'Bogus repeated arch restriction');
  222. my $dep_bad_multiline = deps_parse("a, foo\nbar, c");
  223. is($dep_bad_multiline, undef, 'invalid dependency split over multiple line');
  224. delete $SIG{__WARN__};
  225. my $dep_iter = deps_parse('a, b:armel, c | d:armhf, d:mips (>> 1.2)');
  226. my %dep_arches;
  227. my %dep_pkgs;
  228. deps_iterate($dep_iter, sub {
  229. my $dep = shift;
  230. $dep_pkgs{$dep->{package}} = 1;
  231. if ($dep->{archqual}) {
  232. $dep_arches{$dep->{archqual}} = 1;
  233. }
  234. return 1;
  235. });
  236. my @dep_arches = sort keys %dep_arches;
  237. my @dep_pkgs = sort keys %dep_pkgs;
  238. is("@dep_arches", 'armel armhf mips', 'Dependency iterator, get arches');
  239. is("@dep_pkgs", 'a b c d', 'Dependency iterator, get packages');