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