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