Updated copyright headers. Omit CVS ID.
[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 /* $Id: test_filter_frontend_net.cpp,v 1.20 2008-02-20 15:07:53 adam Exp $
19    Copyright (c) 2005-2007, Index Data.
20
21    See the LICENSE file for details
22  */
23
24 #include "config.hpp"
25 #include <iostream>
26 #include <stdexcept>
27
28 #include "util.hpp"
29 #include "filter_frontend_net.hpp"
30
31 #include "router_chain.hpp"
32 #include "session.hpp"
33 #include "package.hpp"
34
35 #define BOOST_AUTO_TEST_MAIN
36 #define BOOST_TEST_DYN_LINK
37 #include <boost/test/auto_unit_test.hpp>
38
39 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
41
42 class FilterInit: public mp::filter::Base {
43 public:
44     void process(mp::Package & package) const {
45         
46         if (package.session().is_closed())
47         {
48             // std::cout << "Got Close.\n";
49         }
50        
51         Z_GDU *gdu = package.request().get();
52         if (gdu)
53         {
54             // std::cout << "Got PDU. Sending init response\n";
55             mp::odr odr;
56             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initResponse);
57             
58             apdu->u.initResponse->implementationName = "YP2/YAZ";
59             
60             package.response() = apdu;
61         }
62         return package.move();
63     };
64 };
65
66
67 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_1 )
68 {
69     try 
70     {
71         {
72             mp::filter::FrontendNet nf;
73         }
74     }
75     catch ( ... ) {
76         BOOST_CHECK (false);
77     }
78 }
79
80 BOOST_AUTO_TEST_CASE( test_filter_frontend_net_2 )
81 {
82     try 
83     {
84         {
85             mp::RouterChain router;
86
87             FilterInit tf;
88
89             router.append(tf);
90
91             // Create package with Z39.50 init request in it
92             mp::Package pack;
93
94             mp::odr odr;
95             Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
96             
97             pack.request() = apdu;
98             // Done creating query. 
99
100             // Put it in router
101             pack.router(router).move(); 
102
103             // Inspect that we got Z39.50 init response
104             yazpp_1::GDU *gdu = &pack.response();
105
106             Z_GDU *z_gdu = gdu->get();
107             BOOST_CHECK(z_gdu);
108             if (z_gdu) {
109                 BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
110                 BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
111             }
112         }
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 ( ... ) {
146         BOOST_CHECK (false);
147     }
148 }
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * c-file-style: "stroustrup"
155  * End:
156  * vim: shiftwidth=4 tabstop=8 expandtab
157  */