3 #include <boost/test/auto_unit_test.hpp>
4 #include <boost/test/parameterized_test.hpp>
5 #include <boost/test/unit_test_log.hpp>
7 #include "zypp/MediaSetAccess.h"
9 #include "zypp/PathInfo.h"
11 #include "WebServer.h"
17 using namespace boost::unit_test;
18 using namespace zypp::filesystem;
20 class SimpleVerifier : public media::MediaVerifierBase
24 SimpleVerifier( const std::string &id )
29 virtual bool isDesiredMedia(const media::MediaAccessRef &ref)
31 return ref->doesFileExist(Pathname("/x." + _media_id ));
35 std::string _media_id;
38 bool check_file_exists(const Pathname &path)
42 if ((file = fopen(path.asString().c_str(), "r")) == NULL) return false;
49 * Check how MediaSetAccess::rewriteUrl() works.
51 BOOST_AUTO_TEST_CASE(msa_url_rewrite)
54 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 1).asString(),
55 Url("iso:/?iso=/path/to/CD1.iso").asString());
58 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 2).asString(),
59 Url("iso:/?iso=/path/to/CD2.iso").asString());
62 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/CD1.iso"), 13).asString(),
63 Url("iso:/?iso=/path/to/CD13.iso").asString());
66 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/cd1.iso"), 2).asString(),
67 Url("iso:/?iso=/path/to/cd2.iso").asString());
70 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/cd2.iso"), 1).asString(),
71 Url("iso:/?iso=/path/to/cd1.iso").asString());
74 MediaSetAccess::rewriteUrl(Url("iso:/?iso=/path/to/dvd1.iso"), 2).asString(),
75 Url("iso:/?iso=/path/to/dvd2.iso").asString());
78 MediaSetAccess::rewriteUrl(Url("dir:/path/to/CD1"), 2).asString(),
79 Url("dir:/path/to/CD2").asString());
81 // trailing slash check
83 MediaSetAccess::rewriteUrl(Url("dir:/path/to/CD1/"), 2).asString(),
84 Url("dir:/path/to/CD2/").asString());
87 MediaSetAccess::rewriteUrl(Url("nfs://nfs-server/exported/path/to/dvd1"), 2).asString(),
88 Url("nfs://nfs-server/exported/path/to/dvd2").asString());
90 // single media check shouldn't this fail somehow??
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());
96 #define DATADIR (Pathname(TESTS_SRC_DIR) / "/zypp/data/mediasetaccess")
100 * test data dir structure:
121 * Provide files from set without verifiers.
123 BOOST_AUTO_TEST_CASE(msa_provide_files_set)
125 Url url = (DATADIR + "/src1/cd1").asUrl();
126 MediaSetAccess setaccess(url);
128 Pathname file1 = setaccess.provideFile("/test.txt", 1);
129 BOOST_CHECK(check_file_exists(file1) == true);
131 Pathname file2 = setaccess.provideFile("/test.txt", 2);
132 BOOST_CHECK(check_file_exists(file2) == true);
134 Pathname file3 = setaccess.provideFile("/test.txt", 3);
135 BOOST_CHECK(check_file_exists(file3) == true);
139 * Provide files from set with verifiers.
141 BOOST_AUTO_TEST_CASE(msa_provide_files_set_verified)
143 Url url = (DATADIR + "/src1/cd1").asUrl();
144 MediaSetAccess setaccess(url);
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")));
150 // provide file from media1
151 Pathname file1 = setaccess.provideFile("/test.txt", 1);
152 BOOST_CHECK(check_file_exists(file1) == true);
154 // provide file from invalid media
155 BOOST_CHECK_THROW(setaccess.provideFile("/test.txt", 2),
156 media::MediaNotDesiredException);
158 // provide file from media3
159 Pathname file3 = setaccess.provideFile("/test.txt", 3);
160 BOOST_CHECK(check_file_exists(file3) == true);
164 * Provide file from single media with verifier.
166 BOOST_AUTO_TEST_CASE(msa_provide_files_single)
168 Url url = (DATADIR + "/src2").asUrl();
169 MediaSetAccess setaccess(url);
170 setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media")));
172 // provide file from media
173 Pathname file = setaccess.provideFile("/test.txt", 1);
174 BOOST_CHECK(check_file_exists(file) == true);
176 // provide non-existent file
177 // (default answer from callback should be ABORT)
178 BOOST_CHECK_THROW(setaccess.provideFile("/imnothere", 2),
179 media::MediaFileNotFoundException);
183 * Provide directory from src/cd1.
185 BOOST_AUTO_TEST_CASE(msa_provide_dir)
187 Url url = (DATADIR + "/src1/cd1").asUrl();
189 MediaSetAccess setaccess(url);
191 Pathname dir = setaccess.provideDir("/dir", false, 1);
193 Pathname file1 = dir + "/file1";
194 BOOST_CHECK(check_file_exists(file1) == true);
196 Pathname file2 = dir + "/file2";
197 BOOST_CHECK(check_file_exists(file2) == true);
199 // provide non-existent dir
200 // (default answer from callback should be ABORT)
201 BOOST_CHECK_THROW(setaccess.provideDir("/imnothere", 2),
202 media::MediaFileNotFoundException);
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);
214 * Provide directory from src/cd1 (recursively).
216 BOOST_AUTO_TEST_CASE(msa_provide_dirtree)
218 Url url = (DATADIR + "/src1/cd1").asUrl();
219 MediaSetAccess setaccess(url);
221 Pathname dir = setaccess.provideDir("/dir", true, 1);
223 Pathname file1 = dir + "/file1";
224 BOOST_CHECK(check_file_exists(file1) == true);
226 Pathname file2 = dir + "/file2";
227 BOOST_CHECK(check_file_exists(file2) == true);
229 Pathname file3 = dir + "/subdir/file";
230 BOOST_CHECK(check_file_exists(file3) == true);
236 BOOST_AUTO_TEST_CASE(msa_file_exist_local)
238 Url url = (DATADIR + "/src1/cd1").asUrl();
239 MediaSetAccess setaccess(url);
241 BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
242 BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
248 BOOST_AUTO_TEST_CASE(msa_remote_tests)
250 WebServer web( DATADIR / "/src1/cd1", 10002 );
252 MediaSetAccess setaccess( web.url(), "/" );
254 BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
255 BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
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"));
261 // providing a file which does not exist should throw
262 BOOST_CHECK_THROW(setaccess.provideFile("/testBADNAME.txt"), media::MediaFileNotFoundException);
264 BOOST_CHECK(setaccess.doesFileExist("/test-big.txt"));
265 BOOST_CHECK(setaccess.doesFileExist("dir/test-big.txt"));
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);
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);
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);
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);
296 // vim: set ts=2 sts=2 sw=2 ai et: