Updated footer comment
[metaproxy-moved-to-github.git] / src / test_filter_backend_test.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_backend_test.hpp"
25 #include "filter_log.hpp"
26
27 #include "router_chain.hpp"
28 #include "session.hpp"
29 #include "package.hpp"
30
31 #include <yaz/zgdu.h>
32 #include <yaz/pquery.h>
33 #include <yaz/otherinfo.h>
34
35 #define BOOST_AUTO_TEST_MAIN
36 #define BOOST_TEST_DYN_LINK
37 #include <boost/test/auto_unit_test.hpp>
38 using namespace boost::unit_test;
39
40 namespace mp = metaproxy_1;
41
42 BOOST_AUTO_TEST_CASE( test_filter_backend_test_1 )
43 {
44     try 
45     {
46         mp::filter::BackendTest btest;
47     }
48     catch ( ... ) {
49         BOOST_CHECK (false);
50     }
51 }
52
53 BOOST_AUTO_TEST_CASE( test_filter_backend_test_2 )
54 {
55     try 
56     {
57         mp::RouterChain router;
58         
59         mp::filter::BackendTest btest;
60         router.append(btest);
61         
62         mp::Package pack;
63         
64         mp::odr odr;
65         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
66         
67         BOOST_CHECK(apdu);
68         
69         pack.request() = apdu;
70         
71         // Put it in router
72         pack.router(router).move(); 
73         
74         // Inspect that we got Z39.50 init Response OK.
75         yazpp_1::GDU *gdu = &pack.response();
76         
77         BOOST_CHECK(!pack.session().is_closed()); 
78         
79         Z_GDU *z_gdu = gdu->get();
80         BOOST_CHECK(z_gdu);
81         if (z_gdu) {
82             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
83             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
84         }
85     }
86     catch ( ... ) {
87         BOOST_CHECK (false);
88     }
89 }
90
91 BOOST_AUTO_TEST_CASE( test_filter_backend_test_3 )
92 {
93     try 
94     {
95         mp::RouterChain router;
96         
97         mp::filter::BackendTest btest;
98         router.append(btest);
99         
100         mp::Package pack;
101         
102         // send search request as first request.. That should fail with
103         // a close from the backend
104         mp::odr odr;
105         Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
106
107         mp::util::pqf(odr, apdu, "computer");
108         
109         apdu->u.searchRequest->num_databaseNames = 1;
110         apdu->u.searchRequest->databaseNames = (char**)
111             odr_malloc(odr, sizeof(char *));
112         apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default");
113         
114         BOOST_CHECK(apdu);
115         
116         pack.request() = apdu;
117         
118         // Put it in router
119         pack.router(router).move(); 
120         
121         // Inspect that we got Z39.50 close
122         yazpp_1::GDU *gdu = &pack.response();
123         
124         BOOST_CHECK(pack.session().is_closed()); 
125         
126         Z_GDU *z_gdu = gdu->get();
127         BOOST_CHECK(z_gdu);
128         if (z_gdu) {
129             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
130             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
131         }
132     }
133     catch ( ... ) {
134         BOOST_CHECK (false);
135     }
136 }
137
138 BOOST_AUTO_TEST_CASE( test_filter_backend_test_4 )
139 {
140     try 
141     {
142         mp::RouterChain router;
143         
144         mp::filter::BackendTest btest;
145         router.append(btest);
146         
147         mp::Package pack;
148         
149         // send present request as first request.. That should fail with
150         // a close from the backend
151         mp::odr odr;
152         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
153
154         BOOST_CHECK(apdu);
155         
156         pack.request() = apdu;
157         
158         // Put it in router
159         pack.router(router).move(); 
160         
161         // Inspect that we got Z39.50 close
162         yazpp_1::GDU *gdu = &pack.response();
163         
164         BOOST_CHECK(pack.session().is_closed()); 
165         
166         Z_GDU *z_gdu = gdu->get();
167         BOOST_CHECK(z_gdu);
168         if (z_gdu) {
169             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
170             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
171         }
172     }
173     catch ( ... ) {
174         BOOST_CHECK (false);
175     }
176 }
177
178
179 /*
180  * Local variables:
181  * c-basic-offset: 4
182  * c-file-style: "Stroustrup"
183  * indent-tabs-mode: nil
184  * End:
185  * vim: shiftwidth=4 tabstop=8 expandtab
186  */
187