- Prevent fetcher from processing the same index file twice. (bnc #443644)
[platform/upstream/libzypp.git] / tests / zypp / Fetcher_test.cc
1 #include "TestSetup.h"
2
3 #include "zypp/MediaSetAccess.h"
4 #include "zypp/Fetcher.h"
5
6 #include "WebServer.h"
7
8 #define BOOST_TEST_MODULE fetcher_test
9
10 #define DATADIR (Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site")
11
12 BOOST_AUTO_TEST_SUITE( fetcher_test );
13
14 BOOST_AUTO_TEST_CASE(fetcher_enqueuedir_noindex)
15 {
16   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
17   // Now test that without trusting it, it should throw
18   // do the test by trusting the SHA1SUMS file signature key
19   {
20       filesystem::TmpDir dest;
21       Fetcher fetcher;
22       fetcher.enqueueDir(OnMediaLocation("/complexdir"), true);
23       fetcher.start( dest.path(), media );
24       fetcher.reset();
25
26       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2").isExist() );
27       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2/subdir2-file1.txt").isExist() );
28       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file1.txt").isExist() );
29       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file2.txt").isExist() );
30   }
31 }
32
33 BOOST_AUTO_TEST_CASE(fetcher_enqueuedir_autoindex)
34 {
35   base::LogControl::TmpLineWriter shutUp( new zypp::log::FileLineWriter( "/tmp/YLOG" ) );
36   MIL << "GO" << endl;
37   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
38   // do the test by trusting the SHA1SUMS file signature key
39   {
40       filesystem::TmpDir dest;
41
42       // add the key as trusted
43       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
44       Fetcher fetcher;
45       fetcher.setOptions( Fetcher::AutoAddIndexes );
46       fetcher.enqueueDir(OnMediaLocation("/complexdir"), true);
47       fetcher.start( dest.path(), media );
48
49       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2").isExist() );
50       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2/subdir2-file1.txt").isExist() );
51       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file1.txt").isExist() );
52       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file2.txt").isExist() );
53
54       fetcher.reset();
55   }
56 }
57
58 BOOST_AUTO_TEST_CASE(fetcher_enqueue_digested_dir_autoindex)
59 {
60   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
61   // do the test by trusting the SHA1SUMS file signature key but with a broken file
62   {
63       filesystem::TmpDir dest;
64       Fetcher fetcher;
65       // add the key as trusted
66       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir-broken/subdir1/SHA1SUMS.key"), true);
67       fetcher.setOptions( Fetcher::AutoAddIndexes );
68       fetcher.enqueueDigestedDir(OnMediaLocation("/complexdir-broken"), true);
69       BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), FileCheckException);
70       fetcher.reset();
71   }
72 }
73
74 BOOST_AUTO_TEST_CASE(fetcher_enqueuebrokendir_noindex)
75 {
76   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
77   // do the test by trusting the SHA1SUMS file signature key but with a broken file
78   {
79       filesystem::TmpDir dest;
80       Fetcher fetcher;
81       // add the key as trusted
82       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir-broken/subdir1/SHA1SUMS.key"), true);
83       fetcher.enqueueDir(OnMediaLocation("/complexdir-broken"), true);
84       // this should not throw as we provided no indexes and the
85       // enqueue is not digested
86       fetcher.start( dest.path(), media );
87       fetcher.reset();
88
89       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir2").isExist() );
90       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir2/subdir2-file1.txt").isExist() );
91       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir1/subdir1-file1.txt").isExist() );
92       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir1/subdir1-file2.txt").isExist() );
93
94   }
95 }
96
97 BOOST_AUTO_TEST_CASE(fetcher_enqueuebrokendir_index)
98 {
99   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
100   // do the test by trusting the SHA1SUMS file signature key but with a broken file
101   {
102       filesystem::TmpDir dest;
103       Fetcher fetcher;
104       // add the key as trusted
105       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir-broken/subdir1/SHA1SUMS.key"), true);
106       fetcher.setOptions( Fetcher::AutoAddIndexes );
107       fetcher.enqueueDir(OnMediaLocation("/complexdir-broken"), true);
108       BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
109       fetcher.reset();
110   }
111 }
112
113
114 BOOST_AUTO_TEST_CASE(fetcher_enqueue_digesteddir_brokendir_with_index)
115 {
116   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
117   // do the test by trusting the SHA1SUMS file signature key but with a broken file
118   {
119       filesystem::TmpDir dest;
120       Fetcher fetcher;
121       // add the key as trusted
122       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir-broken/subdir1/SHA1SUMS.key"), true);
123       fetcher.setOptions( Fetcher::AutoAddIndexes );
124       fetcher.enqueueDigestedDir(OnMediaLocation("/complexdir-broken"), true);
125       BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
126       fetcher.reset();
127   }
128 }
129
130 BOOST_AUTO_TEST_CASE(fetcher_enqueue_digested_broken_with_autoindex)
131 {
132   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
133   // do the test by trusting the SHA1SUMS file signature key but with a broken file
134   {
135       filesystem::TmpDir dest;
136       Fetcher fetcher;
137       // add the key as trusted
138       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir-broken/subdir1/SHA1SUMS.key"), true);
139       fetcher.setOptions( Fetcher::AutoAddIndexes );
140       fetcher.enqueueDigested(OnMediaLocation("/complexdir-broken/subdir1/subdir1-file1.txt"));
141       BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
142       fetcher.reset();
143   }
144 }
145
146 BOOST_AUTO_TEST_CASE(fetcher_enqueue_digested_with_autoindex)
147 {
148   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
149   // do the test by trusting the SHA1SUMS file signature key with a good file
150   // checksum in auto discovered index
151   {
152       filesystem::TmpDir dest;
153       Fetcher fetcher;
154       // add the key as trusted
155       getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
156       fetcher.setOptions( Fetcher::AutoAddIndexes );
157       fetcher.enqueueDigested(OnMediaLocation("/complexdir/subdir1/subdir1-file1.txt"));
158       fetcher.start( dest.path(), media );
159       fetcher.reset();
160   }
161 }
162
163
164 BOOST_AUTO_TEST_CASE(fetcher_enqueuefile_noindex)
165 {
166   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
167   {
168       filesystem::TmpDir dest;
169       Fetcher fetcher;
170       fetcher.enqueue(OnMediaLocation("/file-1.txt"));
171       fetcher.start( dest.path(), media );
172       BOOST_CHECK( PathInfo(dest.path() + "/file-1.txt").isExist() );
173   }
174
175   //MIL << fetcher;
176 }
177
178 BOOST_AUTO_TEST_CASE(fetcher_simple)
179 {
180     MediaSetAccess media( (DATADIR).asUrl(), "/" );
181     Fetcher fetcher;
182
183     {
184         filesystem::TmpDir dest;
185         OnMediaLocation loc("/complexdir/subdir1/subdir1-file1.txt");
186         loc.setChecksum(CheckSum::sha1("f1d2d2f924e986ac86fdf7b36c94bcdf32beec15"));
187         fetcher.enqueueDigested(loc);
188         fetcher.start(dest.path(), media);
189         fetcher.reset();
190         // now we break the checksum and it should fail
191         loc.setChecksum(CheckSum::sha1("f1d2d2f924e986ac86fdf7b36c94bcdf32beec16"));
192         fetcher.enqueueDigested(loc);
193         BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
194         fetcher.reset();
195
196     }
197 }
198
199 BOOST_AUTO_TEST_CASE(content_index)
200 {
201   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
202   Fetcher fetcher;
203
204   // test transfering one file by setting the index
205   {
206         filesystem::TmpDir dest;
207         OnMediaLocation loc("/contentindex/subdir1/subdir1-file1.txt");
208         // trust the key manually
209         getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/contentindex/content.key"), true);
210         fetcher.addIndex(OnMediaLocation("/contentindex/content", 1));
211         fetcher.enqueue(loc);
212         fetcher.start(dest.path(), media);
213         fetcher.reset();
214         BOOST_CHECK( PathInfo(dest.path() + "/contentindex/subdir1/subdir1-file1.txt").isExist() );
215   }
216
217 }
218
219 BOOST_AUTO_TEST_CASE(enqueue_broken_content_index)
220 {
221   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
222   Fetcher fetcher;
223   filesystem::TmpDir dest;
224   {
225         OnMediaLocation loc("/contentindex-broken-digest/subdir1/subdir1-file1.txt",1);
226         // key was already imported as trusted
227         fetcher.addIndex(OnMediaLocation("/contentindex-broken-digest/content", 1));
228         fetcher.enqueue(loc);
229         fetcher.start(dest.path(), media);
230         fetcher.reset();
231         BOOST_CHECK( PathInfo(dest.path() + "/contentindex-broken-digest/subdir1/subdir1-file1.txt").isExist() );
232
233         // now retrieve a file that is modified, so the checksum has to fail
234         loc = OnMediaLocation("/contentindex-broken-digest/subdir1/subdir1-file2.txt",1);
235         fetcher.addIndex(OnMediaLocation("/contentindex-broken-digest/content", 1));
236         fetcher.enqueue(loc);
237         BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), Exception);
238         fetcher.reset();
239   }
240 }
241
242 BOOST_AUTO_TEST_CASE(enqueue_digested_images_file_content_autoindex)
243 {
244   MediaSetAccess media( ( DATADIR + "/images-file").asUrl(), "/" );
245   Fetcher fetcher;
246   filesystem::TmpDir dest;
247   {
248         OnMediaLocation loc("/images/images.xml",1);
249         fetcher.setOptions( Fetcher::AutoAddIndexes );
250         fetcher.enqueueDigested(loc);
251         fetcher.start(dest.path(), media);
252         fetcher.reset();
253         BOOST_CHECK( PathInfo(dest.path() + "/images/images.xml").isExist() );
254         fetcher.reset();
255   }
256 }
257
258 BOOST_AUTO_TEST_CASE(enqueue_digested_images_file_content_autoindex_unsigned)
259 {
260   MediaSetAccess media( ( DATADIR + "/images-file-unsigned").asUrl(), "/" );
261   Fetcher fetcher;
262   filesystem::TmpDir dest;
263   {
264         OnMediaLocation loc("/images/images.xml",1);
265         fetcher.setOptions( Fetcher::AutoAddIndexes );
266         fetcher.enqueueDigested(loc);
267         // it should throw because unsigned file throws
268         BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), FileCheckException);
269         fetcher.reset();
270         // the target file was NOT transfered
271         BOOST_CHECK( ! PathInfo(dest.path() + "/images/images.xml").isExist() );
272         fetcher.reset();
273   }
274 }
275
276 BOOST_AUTO_TEST_CASE(enqueue_broken_content_noindex)
277 {
278   MediaSetAccess media( ( DATADIR).asUrl(), "/" );
279   Fetcher fetcher;
280
281   {
282         filesystem::TmpDir dest;
283         OnMediaLocation loc("/contentindex-broken-digest/subdir1/subdir1-file1.txt",1);
284         // key was already imported as trusted
285         fetcher.enqueue(loc);
286         fetcher.start(dest.path(), media);
287         fetcher.reset();
288         // now retrieve a file that is modified, so the checksum has to fail
289         loc = OnMediaLocation("/contentindex-broken-digest/subdir1/subdir1-file2.txt",1);
290         fetcher.enqueue(loc);
291         fetcher.start( dest.path(), media );
292         fetcher.reset();
293         BOOST_CHECK( PathInfo(dest.path() + "/contentindex-broken-digest/subdir1/subdir1-file2.txt").isExist() );
294
295   }
296 }
297
298
299 BOOST_AUTO_TEST_CASE(enqueuedir_http)
300 {
301   // at this point the key is already trusted
302   {
303       // add the key as trusted
304       //getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
305
306       WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str() );
307       web.start();
308
309       MediaSetAccess media( Url("http://localhost:9099"), "/" );
310       Fetcher fetcher;
311       filesystem::TmpDir dest;
312
313       // auto add the SHA1SUMS
314       fetcher.setOptions( Fetcher::AutoAddIndexes );
315       fetcher.enqueueDir(OnMediaLocation("/complexdir"), true);
316       fetcher.start( dest.path(), media );
317
318       fetcher.reset();
319
320       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2").isExist() );
321       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2/subdir2-file1.txt").isExist() );
322       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file1.txt").isExist() );
323       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file2.txt").isExist() );
324
325       web.stop();
326   }
327 }
328
329 BOOST_AUTO_TEST_CASE(enqueuedir_http_broken)
330 {
331   // at this point the key is already trusted
332   {
333       // add the key as trusted
334       //getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
335
336       WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str() );
337       web.start();
338
339       MediaSetAccess media( Url("http://localhost:9099"), "/" );
340       Fetcher fetcher;
341       filesystem::TmpDir dest;
342
343       // auto add the SHA1SUMS
344       fetcher.setOptions( Fetcher::AutoAddIndexes );
345       fetcher.enqueueDir(OnMediaLocation("/complexdir-broken"), true);
346       // should throw because wrong checksum
347       BOOST_CHECK_THROW( fetcher.start( dest.path(), media ), FileCheckException);
348       fetcher.reset();
349
350       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir2").isExist() );
351
352       BOOST_CHECK( ! PathInfo(dest.path() + "/complexdir-broken/subdir2/subdir2-file1.txt").isExist() );
353
354       // this one got transfered before the failure, so it is there
355       BOOST_CHECK( PathInfo(dest.path() + "/complexdir-broken/subdir1/subdir1-file1.txt").isExist() );
356       BOOST_CHECK( ! PathInfo(dest.path() + "/complexdir-broken/subdir1/subdir1-file2.txt").isExist() );
357
358       fetcher.reset();
359
360       web.stop();
361   }
362 }
363
364
365 BOOST_AUTO_TEST_SUITE_END();
366
367 // vim: set ts=2 sts=2 sw=2 ai et: