Fix GCC warnings. Set imp name/version for init response
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20 #include <iostream>
21 #include <stdexcept>
22
23 #include "util.hpp"
24 #include "filter_frontend_net.hpp"
25
26 #include "router_chain.hpp"
27 #include "session.hpp"
28 #include "package.hpp"
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/auto_unit_test.hpp>
33
34 using namespace boost::unit_test;
35 namespace mp = metaproxy_1;
36
37 class FilterInit: public mp::filter::Base {
38 public:
39     void process(mp::Package & package) const {
40         
41         if (package.session().is_closed())
42         {
43             // std::cout << "Got Close.\n";
44         }
45        
46         Z_GDU *gdu = package.request().get();
47         if (gdu && gdu->which == Z_GDU_Z3950)
48         {
49             // std::cout << "Got PDU. Sending init response\n";
50             mp::odr odr;
51             Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
52             package.response() = apdu;
53         }
54         return package.move();
55     };
56 };
57
58
59 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_1 )
60 {
61     try 
62     {
63         {
64             mp::filter::FrontendNet nf;
65         }
66     }
67     catch ( ... ) {
68         BOOST_CHECK (false);
69     }
70 }
71
72 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
73 {
74     try 
75     {
76         {
77             mp::RouterChain router;
78
79             FilterInit tf;
80
81             router.append(tf);
82
83             // Create package with Z39.50 init request in it
84             mp::Package pack;
85
86             mp::odr odr;
87             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
88             
89             pack.request() = apdu;
90             // Done creating query. 
91
92             // Put it in router
93             pack.router(router).move(); 
94
95             // Inspect that we got Z39.50 init response
96             yazpp_1::GDU *gdu = &pack.response();
97
98             Z_GDU *z_gdu = gdu->get();
99             BOOST_CHECK(z_gdu);
100             if (z_gdu) {
101                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
102                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
103             }
104         }
105     }
106     catch ( ... ) {
107         BOOST_CHECK (false);
108     }
109 }
110
111 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
112 {
113     try 
114     {
115         {
116             mp::RouterChain router;
117
118             // put in frontend first
119             mp::filter::FrontendNet filter_front;
120
121             std::vector <std::string> ports;
122             ports.insert(ports.begin(), "unix:socket");
123             filter_front.set_ports(ports);
124             filter_front.set_listen_duration(1);  // listen a short time only
125             router.append(filter_front);
126
127             // put in a backend
128             FilterInit filter_init;
129             router.append(filter_init);
130
131             mp::Package pack;
132             
133             pack.router(router).move(); 
134         }
135         BOOST_CHECK(true);
136     }
137     catch ( ... ) {
138         BOOST_CHECK (false);
139     }
140 }
141
142 /*
143  * Local variables:
144  * c-basic-offset: 4
145  * c-file-style: "Stroustrup"
146  * indent-tabs-mode: nil
147  * End:
148  * vim: shiftwidth=4 tabstop=8 expandtab
149  */
150