7c57a2ee9bd4dad52be0f6cff96cd0085bc54df5
[platform/upstream/libzypp.git] / tests / parser / yum / PatchesFileReader_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <list>
6 #include <boost/test/auto_unit_test.hpp>
7
8 #include "zypp/OnMediaLocation.h"
9 #include "zypp/parser/yum/PatchesFileReader.h"
10 #include "zypp/Url.h"
11 #include "zypp/PathInfo.h"
12
13 using namespace std;
14 using namespace zypp;
15 using namespace boost::unit_test;
16
17 using namespace zypp::parser::yum;
18
19 #define DATADIR (Pathname(TESTS_SRC_DIR) + "/parser/yum/data")
20
21 class Collector
22 {
23 public:
24   Collector()
25   {}
26   
27   bool callback( const OnMediaLocation &loc, const string &id )
28   {
29     items.push_back( make_pair( id, loc ) );
30     //items.push_back(loc);
31     //cout << items.size() << endl;
32     return true;
33   }
34   
35   vector<pair<string, OnMediaLocation> > items;
36   //vector<OnMediaLocation> items;
37 };
38
39 BOOST_AUTO_TEST_CASE(patches_read_test)
40 {
41   list<Pathname> entries;
42   if ( filesystem::readdir( entries, DATADIR, 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     //cout << file.basename().substr(0, 7) << " " << file.extension() << endl;
49     if ( ( file.basename().substr(0, 7) == "patches" ) && (file.extension() == ".xml" ) )
50     {
51       //cout << *it << endl;
52       
53       Collector collect;
54       PatchesFileReader( file, bind( &Collector::callback, &collect, _1, _2 ));
55       
56       std::ifstream ifs( file.extend(".solution").asString().c_str() );
57       cout << "Comparing to " << file.extend(".solution") << endl;
58       unsigned int count = 0;
59       while ( ifs && ! ifs.eof() && count < collect.items.size() )
60       {
61         string id;
62         string checksum_type;
63         string checksum;
64         string loc;
65         
66         getline(ifs, id);
67         BOOST_CHECK_EQUAL( collect.items[count].first, id);
68         getline(ifs, checksum_type);
69         getline(ifs, checksum);
70         BOOST_CHECK_EQUAL( collect.items[count].second.checksum(), CheckSum(checksum_type, checksum) );
71         getline(ifs, loc);
72         BOOST_CHECK_EQUAL( collect.items[count].second.filename(), Pathname(loc) );
73         
74         count++;
75       }
76       BOOST_CHECK_EQUAL( collect.items.size(), count );
77     }
78   }
79 }
80
81 // vim: set ts=2 sts=2 sw=2 ai et: