Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / test_filter_frontend_net.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 <metaproxy/util.hpp>
24 #include "filter_frontend_net.hpp"
25
26 #include <metaproxy/router_chain.hpp>
27 #include <metaproxy/package.hpp>
28
29 #define BOOST_AUTO_TEST_MAIN
30 #define BOOST_TEST_DYN_LINK
31 #include <boost/test/auto_unit_test.hpp>
32
33 using namespace boost::unit_test;
34 namespace mp = metaproxy_1;
35
36 class FilterInit: public mp::filter::Base {
37 public:
38     void process(mp::Package & package) const {
39
40         if (package.session().is_closed())
41         {
42             // std::cout << "Got Close.\n";
43         }
44
45         Z_GDU *gdu = package.request().get();
46         if (gdu && gdu->which == Z_GDU_Z3950)
47         {
48             // std::cout << "Got PDU. Sending init response\n";
49             mp::odr odr;
50             Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
51             package.response() = apdu;
52         }
53         return package.move();
54     };
55     void configure(const xmlNode* ptr, bool test_only, const char *path) {};
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 (std::runtime_error &e) {
68         std::cerr << "std::runtime error: " << e.what() << std::endl;
69         BOOST_CHECK(false);
70     }
71     catch ( ... ) {
72         BOOST_CHECK (false);
73     }
74 }
75
76 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
77 {
78     try
79     {
80         {
81             mp::RouterChain router;
82
83             FilterInit tf;
84
85             router.append(tf);
86
87             // Create package with Z39.50 init request in it
88             mp::Package pack;
89
90             mp::odr odr;
91             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
92
93             pack.request() = apdu;
94             // Done creating query.
95
96             // Put it in router
97             pack.router(router).move();
98
99             // Inspect that we got Z39.50 init response
100             yazpp_1::GDU *gdu = &pack.response();
101
102             Z_GDU *z_gdu = gdu->get();
103             BOOST_CHECK(z_gdu);
104             if (z_gdu) {
105                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
106                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
107             }
108         }
109     }
110     catch (std::runtime_error &e) {
111         std::cerr << "std::runtime error: " << e.what() << std::endl;
112         BOOST_CHECK(false);
113     }
114     catch ( ... ) {
115         BOOST_CHECK (false);
116     }
117 }
118
119 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_3 )
120 {
121     try
122     {
123         {
124             mp::RouterChain router;
125
126             // put in frontend first
127             mp::filter::FrontendNet filter_front;
128
129             std::vector <std::string> ports;
130             ports.insert(ports.begin(), "unix:socket");
131             filter_front.set_ports(ports);
132             filter_front.set_listen_duration(1);  // listen a short time only
133             router.append(filter_front);
134
135             // put in a backend
136             FilterInit filter_init;
137             router.append(filter_init);
138
139             mp::Package pack;
140
141             pack.router(router).move();
142         }
143         BOOST_CHECK(true);
144     }
145     catch (std::runtime_error &e) {
146         std::cerr << "std::runtime error: " << e.what() << std::endl;
147         BOOST_CHECK(false);
148     }
149     catch ( ... ) {
150         BOOST_CHECK (false);
151     }
152 }
153
154 /*
155  * Local variables:
156  * c-basic-offset: 4
157  * c-file-style: "Stroustrup"
158  * indent-tabs-mode: nil
159  * End:
160  * vim: shiftwidth=4 tabstop=8 expandtab
161  */
162