1 : /*
2 : * Copyright (C) 2007 Enrico Zini <enrico@enricozini.org>
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Lesser General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2.1 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Lesser General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public
15 : * License along with this library; if not, write to the Free Software
16 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 : */
18 :
19 : #include <ept/test.h>
20 : #include <ept/apt/apt.h>
21 : #include <set>
22 : #include <algorithm>
23 :
24 : using namespace std;
25 : using namespace ept;
26 : using namespace ept::apt;
27 :
28 26 : struct TestApt : AptTestEnvironment {
29 : Apt apt;
30 :
31 : // Check that iterations iterates among some packages
32 1 : Test iterators()
33 : {
34 1 : Apt::iterator i = apt.begin();
35 1 : assert(i != apt.end());
36 :
37 1 : size_t count = 0;
38 1789 : for (; i != apt.end(); ++i)
39 1788 : ++count;
40 :
41 1 : assert(count > 100);
42 1 : }
43 :
44 : // Check that iteration gives some well-known packages
45 1 : Test aptExists()
46 : {
47 1 : set<string> packages;
48 :
49 1 : std::copy(apt.begin(), apt.end(), inserter(packages, packages.begin()));
50 :
51 1 : assert(packages.find("libsp1") != packages.end());
52 : // TODO this exposes a bug somewhere... sp definitely is among
53 : // the packages
54 : // assert(packages.find("sp") != packages.end());
55 2 : assert(packages.find("") == packages.end());
56 1 : }
57 :
58 : // Check that timestamp gives some meaningful timestamp
59 1 : Test timestamp()
60 : {
61 1 : time_t ts = apt.timestamp();
62 1 : assert(ts > 1000000);
63 1 : }
64 :
65 : // Check the package validator
66 1 : Test validity()
67 : {
68 1 : assert(apt.isValid("apt"));
69 2 : assert(!apt.isValid("this-package-does-not-really-exists"));
70 1 : }
71 :
72 : // Check the version instantiators
73 1 : Test versions()
74 : {
75 1 : std::string pkg("apt");
76 1 : Version ver = apt.candidateVersion(pkg);
77 1 : assert(ver.isValid());
78 :
79 2 : ver = apt.installedVersion(pkg);
80 1 : assert(ver.isValid());
81 :
82 2 : ver = apt.anyVersion(pkg);
83 1 : assert(ver.isValid());
84 :
85 2 : std::string pkg1("this-package-does-not-really-exists");
86 2 : ver = apt.candidateVersion(pkg1);
87 1 : assert(!ver.isValid());
88 :
89 2 : ver = apt.installedVersion(pkg1);
90 1 : assert(!ver.isValid());
91 :
92 2 : ver = apt.anyVersion(pkg1);
93 1 : assert(!ver.isValid());
94 1 : }
95 :
96 : // Check the version validator
97 1 : Test versionValidity()
98 : {
99 1 : Version ver = apt.candidateVersion("apt");
100 2 : assert(apt.validate(ver) == ver);
101 :
102 1 : ver = Version("this-package-does-not-really-exists", "0.1");
103 2 : assert(!apt.validate(ver).isValid());
104 :
105 1 : ver = Version("apt", "0.31415");
106 2 : assert(!apt.validate(ver).isValid());
107 1 : }
108 :
109 : // Check the raw record accessor
110 1 : Test rawRecord()
111 : {
112 1 : string pkg("sp");
113 1 : Version ver = apt.candidateVersion(pkg);
114 1 : assert(apt.validate(ver) == ver);
115 :
116 1 : string record = apt.rawRecord(ver);
117 1 : assert(record.find("Package: sp") != string::npos);
118 2 : assert(record.find("Section: text") != string::npos);
119 :
120 2 : record = apt.rawRecord(Version("sp", "0.31415"));
121 2 : assert_eq(record, string());
122 :
123 1 : assert_eq(apt.rawRecord(pkg), apt.rawRecord(apt.anyVersion(pkg)));
124 1 : }
125 :
126 : // Check the package state accessor
127 1 : Test state()
128 : {
129 1 : PackageState s = apt.state("kdenetwork");
130 2 : assert(s.isValid());
131 2 : assert(s.isInstalled());
132 :
133 2 : s = apt.state("this-package-does-not-really-exists");
134 2 : assert(!s.isValid());
135 1 : }
136 :
137 : // Check the record iterator (accessing with *)
138 1 : Test recordIteration()
139 : {
140 1 : size_t count = 0;
141 435 : for (Apt::record_iterator i = apt.recordBegin();
142 : i != apt.recordEnd(); ++i)
143 : {
144 434 : assert((*i).size() > 8);
145 434 : assert_eq((*i).substr(0, 8), "Package:");
146 434 : ++count;
147 1 : }
148 2 : assert(count > 200);
149 1 : }
150 :
151 : // Check the record iterator (accessing with ->)
152 1 : Test recordIteration2()
153 : {
154 1 : size_t count = 0;
155 435 : for (Apt::record_iterator i = apt.recordBegin();
156 : i != apt.recordEnd(); ++i)
157 : {
158 434 : assert(i->size() > 8);
159 868 : assert_eq(i->substr(0, 8), "Package:");
160 434 : ++count;
161 1 : }
162 2 : assert(count > 200);
163 1 : }
164 :
165 : // Check that the iterators can be used with the algorithms
166 1 : Test stlIteration()
167 : {
168 1 : vector<string> out;
169 1 : std::copy(apt.begin(), apt.end(), back_inserter(out));
170 1 : }
171 :
172 : // Check that the iterators can be used with the algorithms
173 1 : Test stlRecordIteration()
174 : {
175 1 : vector<string> out;
176 1 : std::copy(apt.recordBegin(), apt.recordEnd(), back_inserter(out));
177 1 : }
178 :
179 : // Check that checkUpdates will keep a working Apt object
180 1 : Test checkUpdates()
181 : {
182 1 : assert(apt.isValid("apt"));
183 1 : apt.checkCacheUpdates();
184 2 : assert(apt.isValid("apt"));
185 1 : apt.invalidateTimestamp();
186 1 : apt.checkCacheUpdates();
187 2 : assert(apt.isValid("apt"));
188 1 : }
189 :
190 : };
191 :
192 : // vim:set ts=4 sw=4:
|