Let's make the tests also compile with boost 1.33 (auto_unit_test.hpp is
[platform/upstream/libzypp.git] / tests / zypp / MediaSetAccess_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #ifdef BOOST_AUTO_TEST_MAIN
4 #undef BOOST_AUTO_TEST_MAIN
5 #endif
6 #include <boost/test/auto_unit_test.hpp>
7 #include <boost/test/parameterized_test.hpp>
8 #include <boost/test/unit_test_log.hpp>
9
10 #include "zypp/MediaSetAccess.h"
11 #include "zypp/Url.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
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("/." + _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 void 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 /*
97  * Provide files from set without verifiers.
98  */
99 void msa_provide_files_set(const string &urlstr)
100 {
101   Url url(urlstr);
102   MediaSetAccess setaccess(url);
103
104   Pathname file1 = setaccess.provideFile("/test.txt", 1);
105   BOOST_CHECK(check_file_exists(file1) == true);
106
107   Pathname file2 = setaccess.provideFile("/test.txt", 2);
108   BOOST_CHECK(check_file_exists(file2) == true);
109
110   Pathname file3 = setaccess.provideFile("/test.txt", 3);
111   BOOST_CHECK(check_file_exists(file3) == true);
112 }
113
114 /*
115  * Provide files from set with verifiers.
116  */
117 void msa_provide_files_set_verified(const string &urlstr)
118 {
119   Url url(urlstr);
120   MediaSetAccess setaccess(url);
121
122   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media1")));
123   setaccess.setVerifier(2, media::MediaVerifierRef(new SimpleVerifier("media2")));
124   setaccess.setVerifier(3, media::MediaVerifierRef(new SimpleVerifier("media3")));
125
126   // provide file from media1
127   Pathname file1 = setaccess.provideFile("/test.txt", 1);
128   BOOST_CHECK(check_file_exists(file1) == true);
129
130   // provide file from invalid media
131   BOOST_CHECK_THROW(setaccess.provideFile("/test.txt", 2),
132                     media::MediaNotDesiredException);
133
134   // provide file from media3
135   Pathname file3 = setaccess.provideFile("/test.txt", 3);
136   BOOST_CHECK(check_file_exists(file3) == true);
137 }
138
139 /*
140  * Provide file from single media with verifier.
141  */
142 void msa_provide_files_single(const string &urlstr)
143 {
144   Url url(urlstr);
145   MediaSetAccess setaccess(url);
146   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media")));
147
148   // provide file from media
149   Pathname file = setaccess.provideFile("/test.txt", 1);
150   BOOST_CHECK(check_file_exists(file) == true);
151   
152   // provide non-existent file
153   // (default answer from callback should be ABORT)
154   BOOST_CHECK_THROW(setaccess.provideFile("/imnothere", 2),
155                     media::MediaFileNotFoundException);
156 }
157
158 /*
159  * Provide directory from src/cd1.
160  */
161 void msa_provide_dir(const string &urlstr)
162 {
163   Url url(urlstr);
164   MediaSetAccess setaccess(url);
165
166   Pathname dir = setaccess.provideDir("/dir", false, 1);
167   
168   Pathname file1 = dir + "/file1";
169   BOOST_CHECK(check_file_exists(file1) == true);
170
171   Pathname file2 = dir + "/file2";
172   BOOST_CHECK(check_file_exists(file2) == true);
173
174   // provide non-existent dir
175   // (default answer from callback should be ABORT)
176   BOOST_CHECK_THROW(setaccess.provideDir("/imnothere", 2),
177                     media::MediaFileNotFoundException);
178
179   // This can't be properly tested with 'dir' schema, probably only curl
180   // schemas (http, ftp) where download is actually needed.
181   // Other schemas just get mounted onto a local dir and the whole subtree
182   // is automatically available that way.
183   // BOOST_CHECK(check_file_exists(dir + "/subdir/file") == false);
184   // BOOST_CHECK(check_file_exists(dir + "/subdir") == false);
185 }
186
187
188 /*
189  * Provide directory from src/cd1 (recursively).
190  */
191 void msa_provide_dirtree(const string &urlstr)
192 {
193   Url url(urlstr);
194   MediaSetAccess setaccess(url);
195
196   Pathname dir = setaccess.provideDir("/dir", true, 1);
197
198   Pathname file1 = dir + "/file1";
199   BOOST_CHECK(check_file_exists(file1) == true);
200
201   Pathname file2 = dir + "/file2";
202   BOOST_CHECK(check_file_exists(file2) == true);
203
204   Pathname file3 = dir + "/subdir/file";
205   BOOST_CHECK(check_file_exists(file3) == true);
206 }
207
208
209 /*
210  * 
211  * test data dir structure:
212  * 
213  * .
214  * |-- src1
215  * |   |-- cd1
216  * |   |   |-- dir
217  * |   |   |   |-- file1
218  * |   |   |   |-- file2
219  * |   |   |   `-- subdir
220  * |   |   |       `-- file
221  * |   |   `-- test.txt
222  * |   |-- cd2
223  * |   |   `-- test.txt
224  * |   `-- cd3
225  * |       `-- test.txt
226  * `-- src2
227  *     `-- test.txt
228  * 
229  */
230
231 test_suite*
232 init_unit_test_suite( int argc, char *argv[] )
233 {
234   if (argc < 2)
235   {
236     cout << "mediasetaccesstest:"
237       " absolute path to local directory with test data required as parameter"
238       << endl;
239     return (test_suite *)0;
240   }
241
242   test_suite* test= BOOST_TEST_SUITE("MediaSetAccessTest");
243
244   // urls to test  
245   string datadir = argv[1];
246   std::string const params[] = {"dir:" + datadir + "/src1/cd1"};
247   std::string const params_single[] = {"dir:" + datadir + "/src2"};
248
249   // url rewrite
250   test->add(BOOST_TEST_CASE(&msa_url_rewrite));
251
252   // provide files from media set
253   test->add(BOOST_PARAM_TEST_CASE(&msa_provide_files_set,
254                                   (std::string const*)params, params+1));
255
256   // provide files from media set with verifier
257   test->add(BOOST_PARAM_TEST_CASE(&msa_provide_files_set_verified,
258                                   (std::string const*)params, params+1));
259
260   // provide file from single media
261   test->add(BOOST_PARAM_TEST_CASE(&msa_provide_files_single,
262                                   (std::string const*)params_single, params_single+1));
263
264   // provide directory
265   test->add(BOOST_PARAM_TEST_CASE(&msa_provide_dir,
266                                   (std::string const*)params, params+1));
267
268   // provide directory tree
269   test->add(BOOST_PARAM_TEST_CASE(&msa_provide_dirtree,
270                                   (std::string const*)params, params+1));
271
272   return test;
273 }
274
275 // vim: set ts=2 sts=2 sw=2 ai et: