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