]>
git.saurik.com Git - apt.git/blob - test/libapt/compareversion_test.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Version Test - Simple program to run through a file and comare versions.
7 Each version is compared and the result is checked against an expected
8 result in the file. The format of the file is
10 Where Res is -1, 1, 0. dpkg -D=1 --compare-versions a "<" b can be
11 used to determine what Res should be. # at the start of the line
12 is a comment and blank lines are skipped
14 The runner will also call dpkg --compare-versions to check if APT and
15 dpkg have (still) the same idea.
17 ##################################################################### */
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/debversion.h>
23 #include <apt-pkg/fileutl.h>
33 static bool callDPkg(const char *val
, const char *ref
, const char &op
) {
34 pid_t Process
= ExecFork();
38 args
[0] = "/usr/bin/dpkg";
39 args
[1] = "--compare-versions";
41 args
[3] = (op
== 1) ? ">>" : ( (op
== 0) ? "=" : "<<");
44 execv(args
[0], (char**) args
);
48 waitpid(Process
, &Ret
, 0);
49 return WIFEXITED(Ret
) == true && WEXITSTATUS(Ret
) == 0;
52 static void assertVersion(int const &CurLine
, string
const &A
, string
const &B
, int const &Expected
) {
53 int Res
= debVS
.CmpVersion(A
.c_str(), B
.c_str());
54 bool const dpkg
= callDPkg(A
.c_str(),B
.c_str(), Expected
);
55 Res
= (Res
< 0) ? -1 : ( (Res
> 0) ? 1 : Res
);
58 _error
->Error("Comparison failed on line %u. '%s' '%s' '%s' %i != %i",CurLine
,A
.c_str(),((Expected
== 1) ? "<<" : ( (Expected
== 0) ? "=" : ">>")) ,B
.c_str(),Res
,Expected
);
60 _error
->Error("DPkg differ with line: %u. '%s' '%s' '%s' == false",CurLine
,A
.c_str(),((Expected
== 1) ? "<<" : ( (Expected
== 0) ? "=" : ">>")),B
.c_str());
63 static bool RunTest(const char *File
)
65 if (FileExists(File
) == false)
66 return _error
->Error("Versiontestfile %s doesn't exist!", File
);
68 ifstream
F(File
,ios::in
);
77 F
.getline(Buffer
,sizeof(Buffer
));
82 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
85 if (Buffer
[0] == '#' || Buffer
[0] == 0)
91 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
92 string
A(Start
, I
- Start
);
95 return _error
->Error("Invalid line %u",CurLine
);
100 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
101 string
B(Start
,I
- Start
);
103 if (*I
== 0 || I
[1] == 0)
104 return _error
->Error("Invalid line %u",CurLine
);
108 int const Expected
= atoi(I
);
109 assertVersion(CurLine
, A
, B
, Expected
);
110 // Check the reverse as well
111 assertVersion(CurLine
, B
, A
, Expected
*-1);
115 int main(int argc
, char *argv
[])
122 // Print any errors or warnings found
123 _error
->DumpErrors();