- move source -> repository tests
[platform/upstream/libzypp.git] / tests / parser / yum / RepomdFileReader_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <list>
6 #include <boost/test/unit_test.hpp>
7 #include <boost/test/parameterized_test.hpp>
8 #include <boost/test/unit_test_log.hpp>
9
10 #include "zypp/parser/yum/RepomdFileReader.h"
11 #include "zypp/Url.h"
12 #include "zypp/PathInfo.h"
13
14 using namespace std;
15 using namespace zypp;
16 using namespace boost::unit_test;
17
18 using namespace zypp::parser::yum;
19 using source::yum::YUMResourceType;
20
21 class Collector
22 {
23 public:
24   Collector()
25   {}
26   
27   bool callback( const OnMediaLocation &loc, const YUMResourceType &t )
28   {
29     items.push_back( make_pair( t, loc ) );
30     //items.push_back(loc);
31     //cout << items.size() << endl;
32     return true;
33   }
34   
35   vector<pair<YUMResourceType, OnMediaLocation> > items;
36   //vector<OnMediaLocation> items;
37 };
38
39 void repomd_read_test(const string &dir)
40 {
41   list<Pathname> entries;
42   if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
43     ZYPP_THROW(Exception("failed to read directory"));
44     
45   for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
46   {
47     Pathname file = *it;
48     if ( ( file.basename().substr(0, 6) == "repomd" ) && (file.extension() == ".xml" ) )
49     {
50       cout << *it << endl;
51       
52       Collector collect;
53       RepomdFileReader( file, bind( &Collector::callback, &collect, _1, _2 ));
54       
55       std::ifstream ifs( file.extend(".solution").asString().c_str() );
56       
57       int count = 0;
58       while ( ifs && ! ifs.eof() && count < collect.items.size() )
59       {
60         string dtype;
61         string checksum_type;
62         string checksum;
63         string loc;
64         
65         getline(ifs, dtype);
66         BOOST_CHECK_EQUAL( collect.items[count].first, YUMResourceType(dtype));
67         getline(ifs, checksum_type);
68         getline(ifs, checksum);
69         BOOST_CHECK_EQUAL( collect.items[count].second.checksum(), CheckSum(checksum_type, checksum) );
70         getline(ifs, loc);
71         BOOST_CHECK_EQUAL( collect.items[count].second.filename(), Pathname(loc) );
72         
73         count++;
74       }
75       BOOST_CHECK_EQUAL( collect.items.size(), count );
76     }
77   }
78 }
79
80 test_suite*
81 init_unit_test_suite( int argc, char *argv[] )
82 {
83   if (argc < 2)
84   {
85     cout << "RepomdFileReader_test:"
86       " path to directory with test data required as parameter" << endl;
87     return (test_suite *)0;
88   }
89   
90   test_suite* test= BOOST_TEST_SUITE("RepomdFileReader");
91   string datadir = argv[1];
92   std::string const params[] = { datadir };
93   test->add(BOOST_PARAM_TEST_CASE(&repomd_read_test,
94                                  (std::string const*)params, params+1));
95   return test;
96 }
97
98 // vim: set ts=2 sts=2 sw=2 ai et: