dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / t / Dpkg_Version.t
CommitLineData
1479465f
GJ
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
16use strict;
17use warnings;
18
19use Test::More;
20
21use Dpkg::ErrorHandling;
22use Dpkg::IPC;
23
24report_options(quiet_warnings => 1);
25
26my @tests = <DATA>;
27my @ops = ('<', '<<', 'lt',
28 '<=', 'le',
29 '=', 'eq',
30 '>=', 'ge',
31 '>', '>>', 'gt');
32
33plan tests => scalar(@tests) * (3 * scalar(@ops) + 4) + 30;
34
35sub dpkg_vercmp {
36 my ($a, $cmp, $b) = @_;
37 my $stderr;
38
39 spawn(exec => [ 'dpkg', '--compare-versions', '--', $a, $cmp, $b ],
40 error_to_string => \$stderr, wait_child => 1, nocheck => 1);
41 diag("dpkg --compare-versions error=$?: $stderr") if $? and $? != 256;
42
43 return $? == 0;
44}
45
46sub obj_vercmp {
47 my ($a, $cmp, $b) = @_;
48 return $a < $b if $cmp eq '<<';
49 return $a lt $b if $cmp eq 'lt';
50 return $a <= $b if $cmp eq '<=' or $cmp eq '<';
51 return $a le $b if $cmp eq 'le';
52 return $a == $b if $cmp eq '=';
53 return $a eq $b if $cmp eq 'eq';
54 return $a >= $b if $cmp eq '>=' or $cmp eq '>';
55 return $a ge $b if $cmp eq 'ge';
56 return $a > $b if $cmp eq '>>';
57 return $a gt $b if $cmp eq 'gt';
58}
59
60use_ok('Dpkg::Version');
61
62my $truth = {
63 '-1' => {
64 '<<' => 1, 'lt' => 1,
65 '<=' => 1, 'le' => 1, '<' => 1,
66 '=' => 0, 'eq' => 0,
67 '>=' => 0, 'ge' => 0, '>' => 0,
68 '>>' => 0, 'gt' => 0,
69 },
70 '0' => {
71 '<<' => 0, 'lt' => 0,
72 '<=' => 1, 'le' => 1, '<' => 1,
73 '=' => 1, 'eq' => 1,
74 '>=' => 1, 'ge' => 1, '>' => 1,
75 '>>' => 0, 'gt' => 0,
76 },
77 '1' => {
78 '<<' => 0, 'lt' => 0,
79 '<=' => 0, 'le' => 0, '<' => 0,
80 '=' => 0, 'eq' => 0,
81 '>=' => 1, 'ge' => 1, '>' => 1,
82 '>>' => 1, 'gt' => 1,
83 },
84};
85
86# Handling of empty/invalid versions
87my $empty = Dpkg::Version->new('');
88ok($empty eq '', "Dpkg::Version->new('') eq ''");
89ok($empty->as_string() eq '', "Dpkg::Version->new('')->as_string() eq ''");
90ok(!$empty->is_valid(), 'empty version is invalid');
91$empty = Dpkg::Version->new('-0');
92ok($empty eq '', "Dpkg::Version->new('-0') eq '-0'");
93ok($empty->as_string() eq '-0', "Dpkg::Version->new('-0')->as_string() eq '-0'");
94ok(!$empty->is_valid(), 'empty upstream version is invalid');
95$empty = Dpkg::Version->new('0:-0');
96ok($empty eq '0:-0', "Dpkg::Version->new('0:-0') eq '0:-0'");
97ok($empty->as_string() eq '0:-0', "Dpkg::Version->new('0:-0')->as_string() eq '0:-0'");
98ok(!$empty->is_valid(), 'empty upstream version with epoch is invalid');
99$empty = Dpkg::Version->new(':1.0');
100ok($empty eq ':1.0', "Dpkg::Version->new(':1.0') eq ':1.0'");
101ok($empty->as_string() eq ':1.0', "Dpkg::Version->new(':1.0')->as_string() eq ':1.0'");
102ok(!$empty->is_valid(), 'empty epoch is invalid');
103$empty = Dpkg::Version->new('1.0-');
104ok($empty eq '1.0-', "Dpkg::Version->new('1.0-') eq '1.0-'");
105ok($empty->as_string() eq '1.0-', "Dpkg::Version->new('1.0-')->as_string() eq '1.0-'");
106ok(!$empty->is_valid(), 'empty revision is invalid');
107my $ver = Dpkg::Version->new('10a:5.2');
108ok(!$ver->is_valid(), 'bad epoch is invalid');
109ok(!$ver, 'bool eval of invalid leads to false');
110ok($ver eq '10a:5.2', 'invalid still same string 1/2');
111$ver = Dpkg::Version->new('5.2@3-2');
112ok($ver eq '5.2@3-2', 'invalid still same string 2/2');
113ok(!$ver->is_valid(), 'illegal character is invalid');
114$ver = Dpkg::Version->new('foo5.2');
115ok(!$ver->is_valid(), 'version does not start with digit 1/2');
116$ver = Dpkg::Version->new('0:foo5.2');
117ok(!$ver->is_valid(), 'version does not start with digit 2/2');
118
119# Native and non-native versions
120$ver = Dpkg::Version->new('1.0');
121ok($ver->is_native(), 'upstream version is native');
122$ver = Dpkg::Version->new('1:1.0');
123ok($ver->is_native(), 'upstream version w/ epoch is native');
124$ver = Dpkg::Version->new('1:1.0:1.0');
125ok($ver->is_native(), 'upstream version w/ epoch and colon is native');
126$ver = Dpkg::Version->new('1.0-1');
127ok(!$ver->is_native(), 'upstream version w/ revision is not native');
128$ver = Dpkg::Version->new('1.0-1.0-1');
129ok(!$ver->is_native(), 'upstream version w/ dash and revision is not native');
130
131# Other tests
132$ver = Dpkg::Version->new('1.2.3-4');
133is($ver || 'default', '1.2.3-4', 'bool eval returns string representation');
134$ver = Dpkg::Version->new('0');
135is($ver || 'default', 'default', 'bool eval of version 0 is still false...');
136
137# Comparisons
138foreach my $case (@tests) {
139 my ($a, $b, $res) = split ' ', $case;
140 my $va = Dpkg::Version->new($a, check => 1);
141 my $vb = Dpkg::Version->new($b, check => 1);
142
143 is("$va", $a, "String representation of Dpkg::Version($a) is $a");
144 is("$vb", $b, "String representation of Dpkg::Version($b) is $b");
145
146 is(version_compare($a, $b), $res, "$a cmp $b => $res");
147 is($va <=> $vb, $res, "Dpkg::Version($a) <=> Dpkg::Version($b) => $res");
148 foreach my $op (@ops) {
149 my $norm_op = version_normalize_relation($op);
150 if ($truth->{$res}{$op}) {
151 ok(version_compare_relation($a, $norm_op, $b), "$a $op $b => true");
152 ok(obj_vercmp($va, $op, $vb), "Dpkg::Version($a) $op Dpkg::Version($b) => true");
153 ok(dpkg_vercmp($a, $op, $b), "dpkg --compare-versions -- $a $op $b => true");
154 } else {
155 ok(!version_compare_relation($a, $norm_op, $b), "$a $op $b => false");
156 ok(!obj_vercmp($va, $op, $vb), "Dpkg::Version($a) $op Dpkg::Version($b) => false");
157 ok(!dpkg_vercmp($a, $op, $b), "dpkg --compare-versions -- $a $op $b => false");
158 }
159 }
160}
161
162__DATA__
1631.0-1 2.0-2 -1
1642.2~rc-4 2.2-1 -1
1652.2-1 2.2~rc-4 1
1661.0000-1 1.0-1 0
1671 0:1 0
1680 0:0-0 0
1692:2.5 1:7.5 1
1701:0foo 0foo 1
1710:0foo 0foo 0
1720foo 0foo 0
1730foo-0 0foo 0
1740foo 0foo-0 0
1750foo 0fo 1
1760foo-0 0foo+ -1
1770foo~1 0foo -1
1780foo~foo+Bar 0foo~foo+bar -1
1790foo~~ 0foo~ -1
1801~ 1 -1
18112345+that-really-is-some-ver-0 12345+that-really-is-some-ver-10 -1
1820foo-0 0foo-01 -1
1830foo.bar 0foobar 1
1840foo.bar 0foo1bar 1
1850foo.bar 0foo0bar 1
1860foo1bar-1 0foobar-1 -1
1870foo2.0 0foo2 1
1880foo2.0.0 0foo2.10.0 -1
1890foo2.0 0foo2.0.0 -1
1900foo2.0 0foo2.10 -1
1910foo2.1 0foo2.10 -1
1921.09 1.9 0
1931.0.8+nmu1 1.0.8 1
1943.11 3.10+nmu1 1
1950.9j-20080306-4 0.9i-20070324-2 1
1961.2.0~b7-1 1.2.0~b6-1 1
1971.011-1 1.06-2 1
1980.0.9+dfsg1-1 0.0.8+dfsg1-3 1
1994.6.99+svn6582-1 4.6.99+svn6496-1 1
20053 52 1
2010.9.9~pre122-1 0.9.9~pre111-1 1
2022:2.3.2-2+lenny2 2:2.3.2-2 1
2031:3.8.1-1 3.8.GA-1 1
2041.0.1+gpl-1 1.0.1-2 1
2051a 1000a -1