323a3939ede49fd730493cce33f6d5a0c509b69d
[yazpp-moved-to-github.git] / zlint / zlint.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2013 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 #include <yaz/comstack.h>
13 #include <yaz/options.h>
14 #include <yaz/otherinfo.h>
15 #include <yaz/charneg.h>
16 #include <yaz/log.h>
17 #include <yaz/odr.h>
18
19 #include <zlint.h>
20
21 Zlint_test::~Zlint_test()
22 {
23
24 }
25
26 class Zlint_t {
27 public:
28     friend class Zlint;
29     Zlint_t(Zlint_test *t);
30     ~Zlint_t();
31 private:
32     Zlint_test *m_t;
33     Zlint_t *m_next;
34     int m_test_number_sequence;
35     int m_test_ok;
36     int m_test_reported;
37 };
38
39 Zlint::Zlint(IPDU_Observable *the_PDU_Observable) :
40     Z_Assoc(the_PDU_Observable)
41
42 {
43     m_PDU_Observable = the_PDU_Observable;
44     m_host = 0;
45     m_tests = 0;
46     m_cur_test = 0;
47     m_database = 0;
48 }
49
50 Zlint::~Zlint()
51 {
52     while (m_tests)
53     {
54         Zlint_t *t = m_tests;
55         m_tests = t->m_next;
56         delete t;
57     }
58     xfree(m_host);
59     xfree(m_database);
60 }
61
62 void Zlint::set_host(const char *cp)
63 {
64     xfree(m_host);
65     m_host = xstrdup(cp);
66     client(m_host);
67     timeout(30);
68
69     const char *basep;
70     cs_get_host_args(m_host, &basep);
71     if (!basep || !*basep)
72         basep = "Default";
73     xfree(m_database);
74     m_database = xstrdup(basep);
75 }
76
77 void Zlint::timeoutNotify()
78 {
79     if (m_cur_test)
80     {
81         if (m_cur_test->m_t->recv_fail(this, 2) != TEST_FINISHED)
82         {
83             client(m_host);
84             timeout(30);
85             return;
86         }
87         close_goto_next();
88     }
89 }
90
91 void Zlint::failNotify()
92 {
93     if (m_cur_test)
94     {
95         if (m_cur_test->m_t->recv_fail(this, 1) != TEST_FINISHED)
96         {
97             client(m_host);
98             timeout(30);
99             return;
100         }
101         close_goto_next();
102     }
103 }
104
105 void Zlint::connectNotify()
106 {
107     if (m_cur_test)
108     {
109         if (m_cur_test->m_t->init(this) != TEST_FINISHED)
110             return;
111         close_goto_next();
112     }
113 }
114
115 void Zlint::recv_GDU(Z_GDU *gdu, int len)
116 {
117     if (m_cur_test)
118     {
119         int r = m_cur_test->m_t->recv_gdu(this, gdu);
120         if (r == TEST_CONTINUE)
121             return;
122         if (r == TEST_REOPEN)
123         {
124             client(m_host);
125             timeout(30);
126             return;
127         }
128         close_goto_next();
129     }
130 }
131
132 void Zlint::close_goto_next()
133 {
134     if (m_cur_test)
135         m_cur_test = m_cur_test->m_next;
136     if (m_cur_test)
137     {
138         client(m_host);
139         timeout(30);
140     }
141     else
142         close();
143 }
144
145 IPDU_Observer *Zlint::sessionNotify(
146     IPDU_Observable *the_PDU_Observable, int fd)
147 {
148     return 0;
149 }
150
151 Z_ReferenceId *Zlint::mk_refid(const char *buf, int len)
152 {
153     return odr_create_Odr_oct(odr_encode(),
154 #if YAZ_VERSIONL < 0x50000
155                               (unsigned char *)
156 #endif
157                               buf, len);
158 }
159
160 int Zlint::initResponseGetVersion(Z_InitResponse *init)
161 {
162     int no = 0;
163     int i;
164     for (i = 0; i<12; i++)
165         if (ODR_MASK_GET(init->protocolVersion, no))
166         {
167             no = i+1;
168         }
169     return no;
170 }
171
172 void Zlint::add_test(Zlint_test *t)
173 {
174     Zlint_t **d = &m_tests;
175     while (*d)
176         d = &(*d)->m_next;
177     *d = new Zlint_t(t);
178     if (!m_cur_test)
179         m_cur_test = m_tests;
180 }
181
182 void Zlint::msg_check_for(const char *fmt, ...)
183 {
184     m_cur_test->m_test_ok = 0;
185     m_cur_test->m_test_number_sequence++;
186     m_cur_test->m_test_reported = 0;
187
188     va_list ap;
189     va_start(ap, fmt);
190     char buf[1024];
191     vsnprintf(buf, sizeof(buf), fmt, ap);
192     printf ("Checking %s .. ", buf);
193     va_end(ap);
194 }
195
196 void Zlint::msg_check_info(const char *fmt, ...)
197 {
198     va_list ap;
199     va_start(ap, fmt);
200     char buf[1024];
201     vsnprintf(buf, sizeof(buf), fmt, ap);
202     printf (" %s\n", buf);
203     va_end(ap);
204 }
205
206 void Zlint::msg_check_ok()
207 {
208     if (!m_cur_test->m_test_reported)
209     {
210         m_cur_test->m_test_ok = 1;
211         m_cur_test->m_test_reported = 1;
212         printf ("OK\n");
213     }
214 }
215
216 void Zlint::msg_check_fail(const char *fmt, ...)
217 {
218     if (!m_cur_test->m_test_reported)
219     {
220         m_cur_test->m_test_ok = 0;
221         m_cur_test->m_test_reported = 1;
222         printf ("Fail\n");
223     }
224     va_list ap;
225     va_start(ap, fmt);
226     char buf[1024];
227     vsnprintf(buf, sizeof(buf), fmt, ap);
228     printf (" %s\n", buf);
229     va_end(ap);
230 }
231
232 void Zlint::msg_check_notapp()
233 {
234     if (!m_cur_test->m_test_reported)
235     {
236         m_cur_test->m_test_ok = 2;
237         m_cur_test->m_test_reported = 1;
238         printf ("Unsupported\n");
239     }
240 }
241
242 void Zlint::getDatabase(char ***db, int *num)
243 {
244     *db = (char**) odr_malloc(odr_encode(), 2*sizeof(char *));
245     (*db)[0] = m_database;
246     (*db)[1] = 0;
247     *num = 1;
248 }
249
250 Zlint_t::Zlint_t(Zlint_test *t)
251 {
252     m_test_number_sequence = 0;
253     m_test_ok = 0;
254     m_test_reported = 0;
255     m_t = t;
256     m_next = 0;
257 }
258
259 Zlint_t::~Zlint_t()
260 {
261     delete m_t;
262 }
263
264 Zlint_code Zlint_test_simple::recv_fail(Zlint *z, int reason)
265 {
266     z->msg_check_fail("target closed connection");
267     return TEST_FINISHED;
268 }
269 /*
270  * Local variables:
271  * c-basic-offset: 4
272  * c-file-style: "Stroustrup"
273  * indent-tabs-mode: nil
274  * End:
275  * vim: shiftwidth=4 tabstop=8 expandtab
276  */
277