5c44f0624204b7c4da97104d53dfc4390232083b
[metaproxy-moved-to-github.git] / src / test_filter_auth_simple.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 "filter_auth_simple.hpp"
24 #include <metaproxy/util.hpp>
25 #include "router_chain.hpp"
26 #include <metaproxy/package.hpp>
27
28 #define BOOST_AUTO_TEST_MAIN
29 #define BOOST_TEST_DYN_LINK
30 #include <boost/test/auto_unit_test.hpp>
31
32 using namespace boost::unit_test;
33 namespace mp = metaproxy_1;
34
35 class FilterBounceInit: public mp::filter::Base {
36 public:
37     void process(mp::Package & package) const {
38         
39         if (package.session().is_closed())
40         {
41             // std::cout << "Got Close.\n";
42         }
43        
44         Z_GDU *gdu = package.request().get();
45         if (gdu && gdu->which == Z_GDU_Z3950)
46         {
47             // std::cout << "Got PDU. Sending init response\n";
48             mp::odr odr;
49             Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
50             package.response() = apdu;
51         }
52         package.move();
53     };
54 };
55
56
57 BOOST_AUTO_TEST_CASE( test_filter_auth_simple_1 )
58 {
59     try 
60     {
61         mp::filter::AuthSimple lf;
62     }
63     catch ( ... ) {
64         BOOST_CHECK (false);
65     }
66 }
67
68 BOOST_AUTO_TEST_CASE( test_filter_auth_simple2 )
69 {
70     try 
71     {
72         mp::RouterChain router;
73         
74         mp::filter::AuthSimple auth;
75         FilterBounceInit bounce;
76         
77         router.append(auth);
78         router.append(bounce);
79         
80         // Create package with Z39.50 init request in it
81         mp::Package pack;
82         
83         mp::odr odr;
84         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
85         
86         pack.request() = apdu;
87         // Done creating query. 
88         
89         // Put it in router
90         pack.router(router).move(); 
91         
92         // Inspect that we got Z39.50 init response
93         yazpp_1::GDU *gdu = &pack.response();
94         
95         Z_GDU *z_gdu = gdu->get();
96         BOOST_CHECK(z_gdu);
97         if (z_gdu) {
98             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
99             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
100         }
101     }
102     catch ( ... ) {
103         BOOST_CHECK (false);
104     }
105 }
106
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * c-file-style: "Stroustrup"
111  * indent-tabs-mode: nil
112  * End:
113  * vim: shiftwidth=4 tabstop=8 expandtab
114  */
115