GPL v2.
[metaproxy-moved-to-github.git] / src / test_filter_backend_test.cpp
1 /* $Id: test_filter_backend_test.cpp,v 1.11 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "util.hpp"
27 #include "filter_backend_test.hpp"
28 #include "filter_log.hpp"
29
30 #include "router_chain.hpp"
31 #include "session.hpp"
32 #include "package.hpp"
33
34 #include <yaz/zgdu.h>
35 #include <yaz/pquery.h>
36 #include <yaz/otherinfo.h>
37
38 #define BOOST_AUTO_TEST_MAIN
39 #include <boost/test/auto_unit_test.hpp>
40 using namespace boost::unit_test;
41
42 namespace mp = metaproxy_1;
43
44 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_1 )
45 {
46     try 
47     {
48         mp::filter::BackendTest btest;
49     }
50     catch ( ... ) {
51         BOOST_CHECK (false);
52     }
53 }
54
55 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_2 )
56 {
57     try 
58     {
59         mp::RouterChain router;
60         
61         mp::filter::BackendTest btest;
62         router.append(btest);
63         
64         mp::Package pack;
65         
66         mp::odr odr;
67         Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest);
68         
69         BOOST_CHECK(apdu);
70         
71         pack.request() = apdu;
72         
73         // Put it in router
74         pack.router(router).move(); 
75         
76         // Inspect that we got Z39.50 init Response OK.
77         yazpp_1::GDU *gdu = &pack.response();
78         
79         BOOST_CHECK(!pack.session().is_closed()); 
80         
81         Z_GDU *z_gdu = gdu->get();
82         BOOST_CHECK(z_gdu);
83         if (z_gdu) {
84             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
85             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initResponse);
86         }
87     }
88     catch ( ... ) {
89         BOOST_CHECK (false);
90     }
91 }
92
93 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_3 )
94 {
95     try 
96     {
97         mp::RouterChain router;
98         
99         mp::filter::BackendTest btest;
100         router.append(btest);
101         
102         mp::Package pack;
103         
104         // send search request as first request.. That should fail with
105         // a close from the backend
106         mp::odr odr;
107         Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest);
108
109         mp::util::pqf(odr, apdu, "computer");
110         
111         apdu->u.searchRequest->num_databaseNames = 1;
112         apdu->u.searchRequest->databaseNames = (char**)
113             odr_malloc(odr, sizeof(char *));
114         apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default");
115         
116         BOOST_CHECK(apdu);
117         
118         pack.request() = apdu;
119         
120         // Put it in router
121         pack.router(router).move(); 
122         
123         // Inspect that we got Z39.50 close
124         yazpp_1::GDU *gdu = &pack.response();
125         
126         BOOST_CHECK(pack.session().is_closed()); 
127         
128         Z_GDU *z_gdu = gdu->get();
129         BOOST_CHECK(z_gdu);
130         if (z_gdu) {
131             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
132             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
133         }
134     }
135     catch ( ... ) {
136         BOOST_CHECK (false);
137     }
138 }
139
140 BOOST_AUTO_UNIT_TEST( test_filter_backend_test_4 )
141 {
142     try 
143     {
144         mp::RouterChain router;
145         
146         mp::filter::BackendTest btest;
147         router.append(btest);
148         
149         mp::Package pack;
150         
151         // send present request as first request.. That should fail with
152         // a close from the backend
153         mp::odr odr;
154         Z_APDU *apdu = zget_APDU(odr, Z_APDU_presentRequest);
155
156         BOOST_CHECK(apdu);
157         
158         pack.request() = apdu;
159         
160         // Put it in router
161         pack.router(router).move(); 
162         
163         // Inspect that we got Z39.50 close
164         yazpp_1::GDU *gdu = &pack.response();
165         
166         BOOST_CHECK(pack.session().is_closed()); 
167         
168         Z_GDU *z_gdu = gdu->get();
169         BOOST_CHECK(z_gdu);
170         if (z_gdu) {
171             BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950);
172             BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_close);
173         }
174     }
175     catch ( ... ) {
176         BOOST_CHECK (false);
177     }
178 }
179
180
181 /*
182  * Local variables:
183  * c-basic-offset: 4
184  * indent-tabs-mode: nil
185  * c-file-style: "stroustrup"
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */