Use odr_create_Odr_oct
[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(), buf, len);
154 }
155
156 int Zlint::initResponseGetVersion(Z_InitResponse *init)
157 {
158     int no = 0;
159     int i;
160     for (i = 0; i<12; i++)
161         if (ODR_MASK_GET(init->protocolVersion, no))
162         {
163             no = i+1;
164         }
165     return no;
166 }
167
168 void Zlint::add_test(Zlint_test *t)
169 {
170     Zlint_t **d = &m_tests;
171     while (*d)
172         d = &(*d)->m_next;
173     *d = new Zlint_t(t);
174     if (!m_cur_test)
175         m_cur_test = m_tests;
176 }
177
178 void Zlint::msg_check_for(const char *fmt, ...)
179 {
180     m_cur_test->m_test_ok = 0;
181     m_cur_test->m_test_number_sequence++;
182     m_cur_test->m_test_reported = 0;
183
184     va_list ap;
185     va_start(ap, fmt);
186     char buf[1024];
187     vsnprintf(buf, sizeof(buf), fmt, ap);
188     printf ("Checking %s .. ", buf);
189     va_end(ap);
190 }
191
192 void Zlint::msg_check_info(const char *fmt, ...)
193 {
194     va_list ap;
195     va_start(ap, fmt);
196     char buf[1024];
197     vsnprintf(buf, sizeof(buf), fmt, ap);
198     printf (" %s\n", buf);
199     va_end(ap);
200 }
201
202 void Zlint::msg_check_ok()
203 {
204     if (!m_cur_test->m_test_reported)
205     {
206         m_cur_test->m_test_ok = 1;
207         m_cur_test->m_test_reported = 1;
208         printf ("OK\n");
209     }
210 }
211
212 void Zlint::msg_check_fail(const char *fmt, ...)
213 {
214     if (!m_cur_test->m_test_reported)
215     {
216         m_cur_test->m_test_ok = 0;
217         m_cur_test->m_test_reported = 1;
218         printf ("Fail\n");
219     }
220     va_list ap;
221     va_start(ap, fmt);
222     char buf[1024];
223     vsnprintf(buf, sizeof(buf), fmt, ap);
224     printf (" %s\n", buf);
225     va_end(ap);
226 }
227
228 void Zlint::msg_check_notapp()
229 {
230     if (!m_cur_test->m_test_reported)
231     {
232         m_cur_test->m_test_ok = 2;
233         m_cur_test->m_test_reported = 1;
234         printf ("Unsupported\n");
235     }
236 }
237
238 void Zlint::getDatabase(char ***db, int *num)
239 {
240     *db = (char**) odr_malloc(odr_encode(), 2*sizeof(char *));
241     (*db)[0] = m_database;
242     (*db)[1] = 0;
243     *num = 1;
244 }
245
246 Zlint_t::Zlint_t(Zlint_test *t)
247 {
248     m_test_number_sequence = 0;
249     m_test_ok = 0;
250     m_test_reported = 0;
251     m_t = t;
252     m_next = 0;
253 }
254
255 Zlint_t::~Zlint_t()
256 {
257     delete m_t;
258 }
259
260 Zlint_code Zlint_test_simple::recv_fail(Zlint *z, int reason)
261 {
262     z->msg_check_fail("target closed connection");
263     return TEST_FINISHED;
264 }
265 /*
266  * Local variables:
267  * c-basic-offset: 4
268  * c-file-style: "Stroustrup"
269  * indent-tabs-mode: nil
270  * End:
271  * vim: shiftwidth=4 tabstop=8 expandtab
272  */
273