Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / tests / media / media2_test.cc
1 #include <zypp/media/MediaManager.h>
2 #include <zypp/base/String.h>
3 #include <zypp/base/Logger.h>
4 #include <zypp/Pathname.h>
5
6 #include <string>
7 #include <list>
8 #include <iostream>
9 #include <cstdlib>
10
11 #include <signal.h>
12
13 #include "mymediaverifier.h"
14
15 #include <boost/test/unit_test.hpp>
16 #include <boost/test/unit_test.hpp>
17
18 using boost::unit_test::test_suite;
19 using boost::unit_test::test_case;
20
21 using namespace zypp;
22 using namespace zypp::media;
23
24
25 bool       do_step = false;
26 int        do_quit = 0;
27
28 void quit(int)
29 {
30     do_quit = 1;
31 }
32
33 void goon(int)
34 {
35 }
36
37 #define ONE_STEP(MSG) \
38 do { \
39   DBG << "======================================" << std::endl; \
40   DBG << "==>> " << MSG << std::endl; \
41   DBG << "======================================" << std::endl; \
42   if( do_step) { pause(); if( do_quit) exit(0); } \
43 } while(0);
44
45 BOOST_AUTO_TEST_CASE(strange_test)
46 {
47   {
48       struct sigaction sa;
49       sigemptyset(&sa.sa_mask);
50       sa.sa_flags   = 0;
51       sa.sa_handler = goon;
52       sigaction(SIGINT,  &sa, NULL);
53       sa.sa_handler = quit;
54       sigaction(SIGTERM, &sa, NULL);
55
56       //if( argc > 1 && std::string(argv[1]) == "-i")
57       //  do_step = true;
58   }
59
60   MediaVerifierRef verifier(
61     new MyMediaVerifier(/* "SUSE-Linux-CORE-i386 9" */)
62   );
63   MediaManager     mm;
64   media::MediaId   one;
65   media::MediaId   two;
66   zypp::Url        url;
67
68   url = "cd:/";
69
70   try
71   {
72     ONE_STEP("ONE: open " + url.asString());
73     one = mm.open(url);
74
75     ONE_STEP("TWO: open " + url.asString());
76     two = mm.open(url);
77
78
79     ONE_STEP("ONE: add verifier")
80     mm.addVerifier( one, verifier);
81
82     ONE_STEP("TWO: add verifier")
83     mm.addVerifier( two, verifier);
84
85
86     ONE_STEP("ONE: attach")
87     mm.attach(one);
88
89     ONE_STEP("ONE: provideFile(/INDEX.gz)")
90     mm.provideFile(one, Pathname("/INDEX.gz"));
91
92     ONE_STEP("TWO: attach")
93     mm.attach(two);
94
95
96     ONE_STEP("ONE: provideFile(/content)")
97     mm.provideFile(one, Pathname("/content"));
98
99     ONE_STEP("TWO: provideFile(/INDEX.gz)")
100     mm.provideFile(two, Pathname("/INDEX.gz"));
101
102
103     try
104     {
105       ONE_STEP("ONE: release()")
106       mm.release(one); //! \todo add the device argument once mm.getDevices() is ready
107     }
108     catch(const MediaException &e)
109     {
110       ZYPP_CAUGHT(e);
111       ERR << "ONE: HUH? Eject hasn't worked?!" << std::endl;
112     }
113
114     try {
115       ONE_STEP("ONE: provideFile(/content)")
116       mm.provideFile(one, Pathname("/content"));
117     }
118     catch(const MediaException &e)
119     {
120       ZYPP_CAUGHT(e);
121       DBG << "ONE: OK, EXPECTED IT (released)" << std::endl;
122     }
123
124     try {
125       ONE_STEP("TWO: provideFile(/ls-lR.gz)")
126       mm.provideFile(two, Pathname("/ls-lR.gz"));
127     }
128     catch(const MediaException &e)
129     {
130       ZYPP_CAUGHT(e);
131       DBG << "TWO: OK, EXPECTED IT (released)" << std::endl;
132     }
133
134     ONE_STEP("TWO: (RE)ATTACH IT")
135     mm.attach(two);
136
137     ONE_STEP("TWO: provideFile(/INDEX.gz)")
138     mm.provideFile(two, Pathname("/INDEX.gz"));
139
140     ONE_STEP("CLEANUP")
141   }
142   catch(const MediaException &e)
143   {
144     ZYPP_CAUGHT(e);
145   }
146   catch( ... )
147   {
148     // hmm...
149     ERR << "Catched *unknown* exception" << std::endl;
150   }
151 }
152
153 // vim: set ts=2 sts=2 sw=2 ai et: