982cdc4dac18e4c65bc6e6819b68e716a34948f6
[platform/upstream/libzypp.git] / tests / zypp / MediaSetAccess_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #include <boost/test/auto_unit_test.hpp>
4 #include <boost/test/parameterized_test.hpp>
5 #include <boost/test/unit_test_log.hpp>
6
7 #include "zypp/MediaSetAccess.h"
8 #include "zypp/Url.h"
9 #include "zypp/PathInfo.h"
10
11 #include "WebServer.h"
12
13 using std::cout;
14 using std::endl;
15 using std::string;
16 using namespace zypp;
17 using namespace boost::unit_test;
18 using namespace zypp::filesystem;
19
20 class SimpleVerifier : public media::MediaVerifierBase
21 {
22 public:
23
24   SimpleVerifier( const std::string &id )
25   {
26     _media_id = id;
27   }
28
29   virtual bool isDesiredMedia(const media::MediaAccessRef &ref)
30   {
31     return ref->doesFileExist(Pathname("/x." + _media_id ));
32   }
33
34 private:
35   std::string _media_id;
36 };
37
38 bool check_file_exists(const Pathname &path)
39 {
40   FILE *file;
41
42   if ((file = fopen(path.asString().c_str(), "r")) == NULL) return false;
43
44   fclose(file);
45   return true;
46 }
47
48 /*
49  * Check how MediaSetAccess::rewriteUrl() works.
50  */
51 BOOST_AUTO_TEST_CASE(msa_url_rewrite)
52 {
53   BOOST_CHECK_EQUAL(
54     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 1).asString(),
55     Url("iso:/?iso=/path/to/CD1.iso").asString());
56
57   BOOST_CHECK_EQUAL(
58     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 2).asString(),
59     Url("iso:/?iso=/path/to/CD2.iso").asString());
60
61   BOOST_CHECK_EQUAL(
62     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 13).asString(),
63     Url("iso:/?iso=/path/to/CD13.iso").asString());
64
65   BOOST_CHECK_EQUAL(
66     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/cd1.iso"), 2).asString(),
67     Url("iso:/?iso=/path/to/cd2.iso").asString());
68
69   BOOST_CHECK_EQUAL(
70     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/cd2.iso"), 1).asString(),
71     Url("iso:/?iso=/path/to/cd1.iso").asString());
72
73   BOOST_CHECK_EQUAL(
74     MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/dvd1.iso"), 2).asString(),
75     Url("iso:/?iso=/path/to/dvd2.iso").asString());
76
77   BOOST_CHECK_EQUAL(
78     MediaSetAccess::rewriteUrl(Url("dir:/path/to/CD1"), 2).asString(),
79     Url("dir:/path/to/CD2").asString());
80
81   // trailing slash check
82   BOOST_CHECK_EQUAL(
83     MediaSetAccess::rewriteUrl(Url("dir:/path/to/CD1/"), 2).asString(),
84     Url("dir:/path/to/CD2/").asString());
85
86   BOOST_CHECK_EQUAL(
87     MediaSetAccess::rewriteUrl(Url("nfs://nfs-server/exported/path/to/dvd1"), 2).asString(),
88     Url("nfs://nfs-server/exported/path/to/dvd2").asString());
89
90   // single media check  shouldn't this fail somehow??
91   BOOST_CHECK_EQUAL(
92     MediaSetAccess::rewriteUrl(Url("http://ftp.opensuse.org/pub/opensuse/distribution/SL-OSS-factory/inst-source"), 2).asString(),
93     Url("http://ftp.opensuse.org/pub/opensuse/distribution/SL-OSS-factory/inst-source").asString());
94 }
95
96 #define DATADIR (Pathname(TESTS_SRC_DIR) / "/zypp/data/mediasetaccess")
97
98 /*
99  *
100  * test data dir structure:
101  *
102  * .
103  * |-- src1
104  * |   |-- cd1
105  * |   |   |-- dir
106  * |   |   |   |-- file1
107  * |   |   |   |-- file2
108  * |   |   |   `-- subdir
109  * |   |   |       `-- file
110  * |   |   `-- test.txt
111  * |   |-- cd2
112  * |   |   `-- test.txt
113  * |   `-- cd3
114  * |       `-- test.txt
115  * `-- src2
116  *     `-- test.txt
117  *
118  */
119
120 /*
121  * Provide files from set without verifiers.
122  */
123 BOOST_AUTO_TEST_CASE(msa_provide_files_set)
124 {
125   Url url = (DATADIR + "/src1/cd1").asUrl();
126   MediaSetAccess setaccess(url);
127
128   Pathname file1 = setaccess.provideFile("/test.txt", 1);
129   BOOST_CHECK(check_file_exists(file1) == true);
130
131   Pathname file2 = setaccess.provideFile("/test.txt", 2);
132   BOOST_CHECK(check_file_exists(file2) == true);
133
134   Pathname file3 = setaccess.provideFile("/test.txt", 3);
135   BOOST_CHECK(check_file_exists(file3) == true);
136 }
137
138 /*
139  * Provide files from set with verifiers.
140  */
141 BOOST_AUTO_TEST_CASE(msa_provide_files_set_verified)
142 {
143   Url url = (DATADIR + "/src1/cd1").asUrl();
144   MediaSetAccess setaccess(url);
145
146   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media1")));
147   setaccess.setVerifier(2, media::MediaVerifierRef(new SimpleVerifier("media2")));
148   setaccess.setVerifier(3, media::MediaVerifierRef(new SimpleVerifier("media3")));
149
150   // provide file from media1
151   Pathname file1 = setaccess.provideFile("/test.txt", 1);
152   BOOST_CHECK(check_file_exists(file1) == true);
153
154   // provide file from invalid media
155   BOOST_CHECK_THROW(setaccess.provideFile("/test.txt", 2),
156                     media::MediaNotDesiredException);
157
158   // provide file from media3
159   Pathname file3 = setaccess.provideFile("/test.txt", 3);
160   BOOST_CHECK(check_file_exists(file3) == true);
161 }
162
163 /*
164  * Provide file from single media with verifier.
165  */
166 BOOST_AUTO_TEST_CASE(msa_provide_files_single)
167 {
168   Url url = (DATADIR + "/src2").asUrl();
169   MediaSetAccess setaccess(url);
170   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media")));
171
172   // provide file from media
173   Pathname file = setaccess.provideFile("/test.txt", 1);
174   BOOST_CHECK(check_file_exists(file) == true);
175
176   // provide non-existent file
177   // (default answer from callback should be ABORT)
178   BOOST_CHECK_THROW(setaccess.provideFile("/imnothere", 2),
179                     media::MediaFileNotFoundException);
180 }
181
182 /*
183  * Provide directory from src/cd1.
184  */
185 BOOST_AUTO_TEST_CASE(msa_provide_dir)
186 {
187   Url url = (DATADIR + "/src1/cd1").asUrl();
188
189   MediaSetAccess setaccess(url);
190
191   Pathname dir = setaccess.provideDir("/dir", false, 1);
192
193   Pathname file1 = dir + "/file1";
194   BOOST_CHECK(check_file_exists(file1) == true);
195
196   Pathname file2 = dir + "/file2";
197   BOOST_CHECK(check_file_exists(file2) == true);
198
199   // provide non-existent dir
200   // (default answer from callback should be ABORT)
201   BOOST_CHECK_THROW(setaccess.provideDir("/imnothere", 2),
202                     media::MediaFileNotFoundException);
203
204   // This can't be properly tested with 'dir' schema, probably only curl
205   // schemas (http, ftp) where download is actually needed.
206   // Other schemas just get mounted onto a local dir and the whole subtree
207   // is automatically available that way.
208   // BOOST_CHECK(check_file_exists(dir + "/subdir/file") == false);
209   // BOOST_CHECK(check_file_exists(dir + "/subdir") == false);
210 }
211
212
213 /*
214  * Provide directory from src/cd1 (recursively).
215  */
216 BOOST_AUTO_TEST_CASE(msa_provide_dirtree)
217 {
218   Url url = (DATADIR + "/src1/cd1").asUrl();
219   MediaSetAccess setaccess(url);
220
221   Pathname dir = setaccess.provideDir("/dir", true, 1);
222
223   Pathname file1 = dir + "/file1";
224   BOOST_CHECK(check_file_exists(file1) == true);
225
226   Pathname file2 = dir + "/file2";
227   BOOST_CHECK(check_file_exists(file2) == true);
228
229   Pathname file3 = dir + "/subdir/file";
230   BOOST_CHECK(check_file_exists(file3) == true);
231 }
232
233 /*
234  * file exists local
235  */
236 BOOST_AUTO_TEST_CASE(msa_file_exist_local)
237 {
238   Url url = (DATADIR + "/src1/cd1").asUrl();
239   MediaSetAccess setaccess(url);
240
241   BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
242   BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
243 }
244
245 /*
246  * file exists remote
247  */
248 BOOST_AUTO_TEST_CASE(msa_remote_tests)
249 {
250   WebServer web( DATADIR / "/src1/cd1", 10002 );
251   web.start();
252   MediaSetAccess setaccess( web.url(), "/" );
253
254   BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
255   BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
256
257   // check providing a file via http works
258   Pathname local = setaccess.provideFile("/test.txt");
259   BOOST_CHECK(CheckSum::sha1(sha1sum(local)) == CheckSum::sha1("2616e23301d7fcf7ac3324142f8c748cd0b6692b"));
260
261   // providing a file which does not exist should throw
262   BOOST_CHECK_THROW(setaccess.provideFile("/testBADNAME.txt"), media::MediaFileNotFoundException);
263
264   BOOST_CHECK(setaccess.doesFileExist("/test-big.txt"));
265   BOOST_CHECK(setaccess.doesFileExist("dir/test-big.txt"));
266
267   {
268     // providing a file with wrong filesize should throw
269     OnMediaLocation locPlain("dir/test-big.txt");
270     locPlain.setDownloadSize( zypp::ByteCount(500, zypp::ByteCount::B) );
271     BOOST_CHECK_THROW(setaccess.provideFile(locPlain), media::MediaFileSizeExceededException);
272
273     // using the correct file size should NOT throw
274     locPlain.setDownloadSize( zypp::ByteCount(7135, zypp::ByteCount::B) );
275     Pathname file = setaccess.provideFile( locPlain );
276     BOOST_CHECK(check_file_exists(file) == true);
277   }
278
279   {
280     // test the maximum filesize again with metalink downloads
281     // providing a file with wrong filesize should throw
282     OnMediaLocation locMeta("/test-big.txt");
283     locMeta.setDownloadSize( zypp::ByteCount(500, zypp::ByteCount::B) );
284     BOOST_CHECK_THROW(setaccess.provideFile(locMeta), media::MediaFileSizeExceededException);
285
286     // using the correct file size should NOT throw
287     locMeta.setDownloadSize( zypp::ByteCount(7135, zypp::ByteCount::B) );
288     Pathname file = setaccess.provideFile( locMeta );
289     BOOST_CHECK(check_file_exists(file) == true);
290   }
291
292   web.stop();
293 }
294
295
296 // vim: set ts=2 sts=2 sw=2 ai et: