]>
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 ##################################################################### */
19 #include <apt-pkg/macros.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/version.h>
22 #include <apt-pkg/debversion.h>
23 #include <apt-pkg/fileutl.h>
29 #include <sys/types.h>
34 bool callDPkg(const char *val
, const char *ref
, const char &op
) {
35 pid_t Process
= ExecFork();
39 args
[0] = "/usr/bin/dpkg";
40 args
[1] = "--compare-versions";
42 args
[3] = (op
== 1) ? ">>" : ( (op
== 0) ? "=" : "<<");
45 execv(args
[0], (char**) args
);
49 waitpid(Process
, &Ret
, 0);
50 return WIFEXITED(Ret
) == true && WEXITSTATUS(Ret
) == 0;
53 void assertVersion(int const &CurLine
, string
const &A
, string
const &B
, int const &Expected
) {
54 int Res
= debVS
.CmpVersion(A
.c_str(), B
.c_str());
55 bool const dpkg
= callDPkg(A
.c_str(),B
.c_str(), Expected
);
56 Res
= (Res
< 0) ? -1 : ( (Res
> 0) ? 1 : Res
);
59 _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
);
61 _error
->Error("DPkg differ with line: %u. '%s' '%s' '%s' == false",CurLine
,A
.c_str(),((Expected
== 1) ? "<<" : ( (Expected
== 0) ? "=" : ">>")),B
.c_str());
64 bool RunTest(const char *File
)
66 if (FileExists(File
) == false)
67 return _error
->Error("Versiontestfile %s doesn't exist!", File
);
69 ifstream
F(File
,ios::in
);
78 F
.getline(Buffer
,sizeof(Buffer
));
83 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
86 if (Buffer
[0] == '#' || Buffer
[0] == 0)
92 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
93 string
A(Start
, I
- Start
);
96 return _error
->Error("Invalid line %u",CurLine
);
101 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
102 string
B(Start
,I
- Start
);
104 if (*I
== 0 || I
[1] == 0)
105 return _error
->Error("Invalid line %u",CurLine
);
109 int const Expected
= atoi(I
);
110 assertVersion(CurLine
, A
, B
, Expected
);
111 // Check the reverse as well
112 assertVersion(CurLine
, B
, A
, Expected
*-1);
116 int main(int argc
, char *argv
[])
123 // Print any errors or warnings found
124 _error
->DumpErrors();