Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / tests / media / media3_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 bool       do_step = false;
25 int        do_quit = 0;
26
27 void quit(int)
28 {
29     do_quit = 1;
30 }
31
32 void goon(int)
33 {
34 }
35
36 #define ONE_STEP(MSG) \
37 do { \
38   DBG << "======================================" << std::endl; \
39   DBG << "==>> " << MSG << std::endl; \
40   DBG << "======================================" << std::endl; \
41   if( do_step) { pause(); if( do_quit) exit(0); } \
42 } while(0);
43
44 BOOST_AUTO_TEST_CASE(strange_test)
45 {
46   bool eject_src = false;
47   bool close_src = false;
48   {
49       struct sigaction sa;
50       sigemptyset(&sa.sa_mask);
51       sa.sa_flags   = 0;
52       sa.sa_handler = goon;
53       sigaction(SIGINT,  &sa, NULL);
54       sa.sa_handler = quit;
55       sigaction(SIGTERM, &sa, NULL);
56
57 //       std::cerr << "ARGS=" << argc << std::endl;
58 //       for(int i=1; i < argc; i++)
59 //       {
60 //         if( std::string(argv[i]) == "-i")
61 //           do_step = true;
62 //         else
63 //         if( std::string(argv[i]) == "-e")
64 //           eject_src = true;
65 //         else
66 //         if( std::string(argv[i]) == "-c")
67 //           close_src = true;
68 //       }
69   }
70
71   MediaVerifierRef verifier(
72     new MyMediaVerifier(/* "SUSE-Linux-CORE-i386 9" */)
73   );
74   MediaManager     mm;
75   media::MediaId   src = 0;
76   media::MediaId   iso;
77   zypp::Url        src_url;
78   zypp::Url        iso_url;
79
80   src_url = "nfs://dist.suse.de/dist/install";
81
82   iso_url = "iso:/";
83   iso_url.setQueryParam("iso", "SUSE-10.1-Beta5/SUSE-Linux-10.1-beta5-i386-CD1.iso");
84   iso_url.setQueryParam("url", src_url.asString());
85
86 /*
87   iso_url = "iso:/";
88   iso_url.setQueryParam("iso", "/space/tmp/iso/SUSE-Linux-10.1-beta7-i386-CD1.iso");
89 */
90
91   try
92   {
93     if( eject_src || close_src)
94     {
95       ONE_STEP("SRC: open " + src_url.asString());
96       src = mm.open(src_url);
97
98       ONE_STEP("SRC: attach")
99       mm.attach(src);
100     }
101
102     ONE_STEP("ISO: open " + iso_url.asString());
103     iso = mm.open(iso_url);
104
105     ONE_STEP("ISO: add verifier")
106     mm.addVerifier(iso, verifier);
107
108     ONE_STEP("ISO: attach")
109     mm.attach(iso);
110
111     ONE_STEP("provideFile(/INDEX.gz)")
112     mm.provideFile(iso, Pathname("/INDEX.gz"));
113
114     if( eject_src)
115     {
116       try
117       {
118         ONE_STEP("SRC: release(ejectDev=\"/dev/device\")")
119         mm.release(src);//! \todo add the device argument once mm.getDevices() is ready
120       }
121       catch(const MediaException &e)
122       {
123         ZYPP_CAUGHT(e);
124         ERR << "ONE: HUH? Eject hasn't worked?!" << std::endl;
125       }
126     }
127     else
128     if( close_src)
129     {
130       try
131       {
132         ONE_STEP("SRC: close()")
133         mm.close(src);
134       }
135       catch(const MediaException &e)
136       {
137         ZYPP_CAUGHT(e);
138         ERR << "SRC: HUH? Close hasn't worked?!" << std::endl;
139       }
140     }
141
142     ONE_STEP("ISO: RELEASE")
143     mm.release(iso);
144
145     ONE_STEP("CLEANUP")
146   }
147   catch(const MediaException &e)
148   {
149     ERR << "Catched media exception..." << std::endl;
150     ZYPP_CAUGHT(e);
151   }
152   catch( ... )
153   {
154     // hmm...
155     ERR << "Catched *unknown* exception" << std::endl;
156   }
157 }
158
159 // vim: set ts=2 sts=2 sw=2 ai et: