8b5a47d86c58e8fd1f397a1e5d460782f3f21d52
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 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)
48         {
49             // std::cout << "Got PDU. Sending init response\n";
50             mp::odr odr;
51             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
52             
53             apdu->u.initResponse->implementationName = "YP2/YAZ";
54             
55             package.response() = apdu;
56         }
57         return package.move();
58     };
59 };
60
61
62 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_1 )
63 {
64     try 
65     {
66         {
67             mp::filter::FrontendNet nf;
68         }
69     }
70     catch ( ... ) {
71         BOOST_CHECK (false);
72     }
73 }
74
75 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
76 {
77     try 
78     {
79         {
80             mp::RouterChain router;
81
82             FilterInit tf;
83
84             router.append(tf);
85
86             // Create package with Z39.50 init request in it
87             mp::Package pack;
88
89             mp::odr odr;
90             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
91             
92             pack.request() = apdu;
93             // Done creating query. 
94
95             // Put it in router
96             pack.router(router).move(); 
97
98             // Inspect that we got Z39.50 init response
99             yazpp_1::GDU *gdu = &pack.response();
100
101             Z_GDU *z_gdu = gdu->get();
102             BOOST_CHECK(z_gdu);
103             if (z_gdu) {
104                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
105                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
106             }
107         }
108     }
109     catch ( ... ) {
110         BOOST_CHECK (false);
111     }
112 }
113
114 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
115 {
116     try 
117     {
118         {
119             mp::RouterChain router;
120
121             // put in frontend first
122             mp::filter::FrontendNet filter_front;
123
124             std::vector <std::string> ports;
125             ports.insert(ports.begin(), "unix:socket");
126             filter_front.set_ports(ports);
127             filter_front.set_listen_duration(1);  // listen a short time only
128             router.append(filter_front);
129
130             // put in a backend
131             FilterInit filter_init;
132             router.append(filter_init);
133
134             mp::Package pack;
135             
136             pack.router(router).move(); 
137         }
138         BOOST_CHECK(true);
139     }
140     catch ( ... ) {
141         BOOST_CHECK (false);
142     }
143 }
144
145 /*
146  * Local variables:
147  * c-basic-offset: 4
148  * c-file-style: "Stroustrup"
149  * indent-tabs-mode: nil
150  * End:
151  * vim: shiftwidth=4 tabstop=8 expandtab
152  */
153