No unsigned char's in public API (except for iconv)
[yaz-moved-to-github.git] / ztest / ztest.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file
6  * \brief yaz-ztest Generic Frontend Server
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12
13 #include <stdio.h>
14 #include <math.h>
15 #include <stdlib.h>
16
17 #if HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20 #if HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #if HAVE_SYS_SELECT_H
24 #include <sys/select.h>
25 #endif
26 #ifdef WIN32
27 #include <windows.h>
28 #endif
29
30 #include <yaz/log.h>
31 #include <yaz/backend.h>
32 #include <yaz/ill.h>
33 #include <yaz/diagbib1.h>
34 #include <yaz/otherinfo.h>
35 #include <yaz/facet.h>
36
37 #include "ztest.h"
38
39 static int log_level=0;
40 static int log_level_set=0;
41
42 struct delay {
43     double d1;
44     double d2;
45 };
46
47 struct result_set {
48     char *name;
49     char *db;
50     Odr_int hits;
51     struct delay search_delay;
52     struct delay present_delay;
53     struct delay fetch_delay;
54     struct result_set *next;
55 };
56
57 struct session_handle {
58     struct result_set *result_sets;
59 };
60
61 int ztest_search(void *handle, bend_search_rr *rr);
62 int ztest_sort(void *handle, bend_sort_rr *rr);
63 int ztest_present(void *handle, bend_present_rr *rr);
64 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
65 int ztest_delete(void *handle, bend_delete_rr *rr);
66
67 static struct result_set *get_set(struct session_handle *sh, const char *name)
68 {
69     struct result_set *set = sh->result_sets;
70     for (; set; set = set->next)
71         if (!strcmp(name, set->name))
72             return set;
73     return 0;
74 }
75
76 static void remove_sets(struct session_handle *sh)
77 {
78     struct result_set *set = sh->result_sets;
79     while (set)
80     {
81         struct result_set *set_next = set->next;
82         xfree(set->name);
83         xfree(set->db);
84         xfree(set);
85         set = set_next;
86     }
87     sh->result_sets = 0;
88 }
89
90 /** \brief use term value as hit count
91     \param s RPN structure
92     \param hash value for compuation
93     \return >= 0: search term number or -1: not found
94
95     Traverse RPN tree 'in order' and use term value as hit count.
96     Only terms  that looks a numeric is used.. Returns -1 if
97     no sub tree has a hit count term
98 */
99 static Odr_int get_term_hit(Z_RPNStructure *s, unsigned *hash)
100 {
101     Odr_int h = -1;
102     switch(s->which)
103     {
104     case Z_RPNStructure_simple:
105         if (s->u.simple->which == Z_Operand_APT)
106         {
107             Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
108             if (apt->term->which == Z_Term_general)
109             {
110                 Odr_oct *oct = apt->term->u.general;
111                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
112                 {
113                     WRBUF hits_str = wrbuf_alloc();
114                     wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
115                     h = odr_atoi(wrbuf_cstr(hits_str));
116                     wrbuf_destroy(hits_str);
117                 }
118                 else
119                 {
120                     int i;
121                     for (i = 0; i < oct->len; i++)
122                         *hash = *hash * 65509 + oct->buf[i];
123                 }
124             }
125         }
126         break;
127     case Z_RPNStructure_complex:
128         h = get_term_hit(s->u.complex->s1, hash);
129         if (h == -1)
130             h = get_term_hit(s->u.complex->s2, hash);
131         break;
132     }
133     return h;
134 }
135
136 /** \brief gets hit count for numeric terms in RPN queries
137     \param q RPN Query
138     \return number of hits
139
140     This is just for testing.. A real database of course uses
141     the content of a database to establish a value.. In our case, we
142     have a way to trigger a certain hit count. Good for testing of
143     client applications etc
144 */
145 static Odr_int get_hit_count(Z_Query *q)
146 {
147     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
148     {
149         unsigned hash = 0;
150         Odr_int h = -1;
151         h = get_term_hit(q->u.type_1->RPNStructure, &hash);
152         if (h == -1)
153             h = hash % 24;
154         return h;
155     }
156     else if (q->which == Z_Query_type_104 &&
157              q->u.type_104->which == Z_External_CQL)
158     {
159         unsigned hash = 0;
160         const char *cql = q->u.type_104->u.cql;
161         int i;
162         for (i = 0; cql[i]; i++)
163             hash = hash * 65509 + cql[i];
164         return hash % 24;
165     }
166     else
167         return 24;
168 }
169
170 /** \brief checks if it's a dummy Slow database
171     \param basename database name to check
172     \param association backend association (or NULL if not available)
173     \retval 1 is slow database
174     \retval 0 is not a slow database
175
176     The Slow database is for testing.. It allows us to simulate
177     a slow server...
178 */
179 static int check_slow(const char *basename, bend_association association)
180 {
181     if (strncmp(basename, "Slow", 4) == 0)
182     {
183 #if HAVE_UNISTD_H
184         int i, w = 3;
185         if (basename[4])
186             sscanf(basename+4, "%d", &w);
187         /* wait up to 3 seconds and check if connection is still alive */
188         for (i = 0; i < w; i++)
189         {
190             if (association && !bend_assoc_is_alive(association))
191             {
192                 yaz_log(YLOG_LOG, "search aborted");
193                 break;
194             }
195             sleep(1);
196         }
197 #endif
198         return 1;
199     }
200     return 0;
201 }
202
203 static int strcmp_prefix(const char *s, const char *p)
204 {
205     size_t l = strlen(p);
206     if (strlen(s) >= l && !memcmp(s, p, l))
207         return 1;
208     return 0;
209 }
210
211 static void init_delay(struct delay *delayp)
212 {
213     delayp->d1 = delayp->d2 = 0.0;
214 }
215
216 static int parse_delay(struct delay *delayp, const char *value)
217 {
218     if (sscanf(value, "%lf:%lf", &delayp->d1, &delayp->d2) == 2)
219         ;
220     else if (sscanf(value, "%lf", &delayp->d1) == 1)
221         delayp->d2 = 0.0;
222     else
223         return -1;
224     return 0;
225 }
226
227 static void ztest_sleep(double d)
228 {
229 #ifdef WIN32
230     Sleep( (DWORD) (d * 1000));
231 #else
232     struct timeval tv;
233     tv.tv_sec = d;
234     tv.tv_usec = (d - (long) d) * 1000000;
235     select(0, 0, 0, 0, &tv);
236 #endif
237 }
238
239 static void do_delay(const struct delay *delayp)
240 {
241     double d = delayp->d1;
242
243     if (d > 0.0)
244     {
245         if (delayp->d2 > d)
246             d += (rand()) * (delayp->d2 - d) / RAND_MAX;
247         ztest_sleep(d);
248     }
249 }
250
251 static void addterms(ODR odr, Z_FacetField *facet_field, const char *facet_name)
252 {
253     int index;
254     int freq = 100;
255     int length = strlen(facet_name) + 10;
256     char *key = odr_malloc(odr, length);
257     key[0] = '\0';
258     for (index = 0; index < facet_field->num_terms; index++)
259     {
260         Z_FacetTerm *facet_term;
261         sprintf(key, "%s%d", facet_name, index);
262         yaz_log(YLOG_DEBUG, "facet add term %s %d %s", facet_name, index, key);
263
264         facet_term = facet_term_create_cstr(odr, key, freq);
265         freq = freq - 10 ;
266         facet_field_term_set(odr, facet_field, facet_term, index);
267     }
268 }
269
270 Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
271     int index, new_index = 0;
272     Z_FacetList *new_list = facet_list_create(odr, facet_list->num);
273
274     for (index = 0; index < facet_list->num; index++) {
275         struct yaz_facet_attr attrvalues;
276         yaz_facet_attr_init(&attrvalues);
277         attrvalues.limit = 10;
278         yaz_facet_attr_get_z_attributes(facet_list->elements[index]->attributes,
279                                         &attrvalues);
280         yaz_log(YLOG_LOG, "Attributes: %s %d ", attrvalues.useattr, attrvalues.limit);
281         if (attrvalues.errstring)
282             yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
283         if (attrvalues.limit > 0 && attrvalues.useattr) {
284             new_list->elements[new_index] = facet_field_create(odr, facet_list->elements[index]->attributes, attrvalues.limit);
285             addterms(odr, new_list->elements[new_index], attrvalues.useattr);
286             new_index++;
287         }
288         else {
289             yaz_log(YLOG_DEBUG, "Facet: skipping %s due to 0 limit.", attrvalues.useattr);
290         }
291
292     }
293     new_list->num = new_index;
294     if (new_index > 0) {
295         Z_OtherInformation *oi = odr_malloc(odr, sizeof(*oi));
296         Z_OtherInformationUnit *oiu = odr_malloc(odr, sizeof(*oiu));
297         oi->num_elements = 1;
298         oi->list = odr_malloc(odr, oi->num_elements * sizeof(*oi->list));
299         oiu->category = 0;
300         oiu->which = Z_OtherInfo_externallyDefinedInfo;
301         oiu->information.externallyDefinedInfo = odr_malloc(odr, sizeof(*oiu->information.externallyDefinedInfo));
302         oiu->information.externallyDefinedInfo->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
303         oiu->information.externallyDefinedInfo->descriptor = 0;
304         oiu->information.externallyDefinedInfo->indirect_reference = 0;
305         oiu->information.externallyDefinedInfo->which = Z_External_userFacets;
306         oiu->information.externallyDefinedInfo->u.facetList = new_list;
307         oi->list[0] = oiu;
308         return oi;
309     }
310     return 0;
311 }
312
313 static void echo_extra_args(ODR stream,
314                             Z_SRW_extra_arg *extra_args, char **extra_response)
315 {
316     if (extra_args)
317     {
318         Z_SRW_extra_arg *a;
319         WRBUF response_xml = wrbuf_alloc();
320         wrbuf_puts(response_xml, "<extra>");
321         for (a = extra_args; a; a = a->next)
322         {
323             wrbuf_puts(response_xml, "<extra name=\"");
324             wrbuf_xmlputs(response_xml, a->name);
325             wrbuf_puts(response_xml, "\"");
326             if (a->value)
327             {
328                 wrbuf_puts(response_xml, " value=\"");
329                 wrbuf_xmlputs(response_xml, a->value);
330                 wrbuf_puts(response_xml, "\"");
331             }
332             wrbuf_puts(response_xml, "/>");
333         }
334         wrbuf_puts(response_xml, "</extra>");
335         *extra_response = odr_strdup(stream, wrbuf_cstr(response_xml));
336         wrbuf_destroy(response_xml);
337     }
338
339 }
340
341 int ztest_search(void *handle, bend_search_rr *rr)
342 {
343     struct session_handle *sh = (struct session_handle*) handle;
344     struct result_set *new_set;
345     const char *db, *db_sep;
346
347     if (rr->num_bases != 1)
348     {
349         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
350         return 0;
351     }
352
353     db = rr->basenames[0];
354
355     /* Allow Default, db.* and Slow */
356     if (strcmp_prefix(db, "Default"))
357         ;  /* Default is OK in our test */
358     else if (strcmp_prefix(db, "db"))
359         ;  /* db.* is OK in our test */
360     else if (check_slow(rr->basenames[0], rr->association))
361     {
362         rr->estimated_hit_count = 1;
363     }
364     else
365     {
366         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
367         rr->errstring = rr->basenames[0];
368         return 0;
369     }
370
371     new_set = get_set(sh, rr->setname);
372     if (new_set)
373     {
374         if (!rr->replace_set)
375         {
376             rr->errcode = YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF;
377             return 0;
378         }
379         xfree(new_set->db);
380     }
381     else
382     {
383         new_set = xmalloc(sizeof(*new_set));
384         new_set->next = sh->result_sets;
385         sh->result_sets = new_set;
386         new_set->name = xstrdup(rr->setname);
387     }
388     new_set->hits = 0;
389     new_set->db = xstrdup(db);
390     init_delay(&new_set->search_delay);
391     init_delay(&new_set->present_delay);
392     init_delay(&new_set->fetch_delay);
393
394     db_sep = strchr(db, '?');
395     if (db_sep)
396     {
397         char **names;
398         char **values;
399         int no_parms = yaz_uri_to_array(db_sep+1, rr->stream, &names, &values);
400         int i;
401         for (i = 0; i < no_parms; i++)
402         {
403             const char *name = names[i];
404             const char *value = values[i];
405             if (!strcmp(name, "seed"))
406                 srand(atoi(value));
407             else if (!strcmp(name, "search-delay"))
408                 parse_delay(&new_set->search_delay, value);
409             else if (!strcmp(name, "present-delay"))
410                 parse_delay(&new_set->present_delay, value);
411             else if (!strcmp(name, "fetch-delay"))
412                 parse_delay(&new_set->fetch_delay, value);
413             else
414             {
415                 rr->errcode = YAZ_BIB1_SERVICE_UNSUPP_FOR_THIS_DATABASE;
416                 rr->errstring = odr_strdup(rr->stream, name);
417             }
418         }
419     }
420
421     echo_extra_args(rr->stream, rr->extra_args, &rr->extra_response_data);
422     rr->hits = get_hit_count(rr->query);
423
424     if (1)
425     {
426         Z_FacetList *facet_list = yaz_oi_get_facetlist(&rr->search_input);
427         if (facet_list) {
428             yaz_log(YLOG_LOG, "%d Facets in search request.", facet_list->num);
429             rr->search_info = build_facet_response(rr->stream, facet_list);
430         }
431         else
432             yaz_log(YLOG_DEBUG, "No facets parsed search request.");
433
434     }
435     do_delay(&new_set->search_delay);
436     new_set->hits = rr->hits;
437
438     return 0;
439 }
440
441
442 /* this huge function handles extended services */
443 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
444 {
445     if (rr->esr->packageName)
446         yaz_log(log_level, "packagename: %s", rr->esr->packageName);
447     yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
448
449
450     yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
451
452     if (!rr->esr->taskSpecificParameters)
453     {
454         yaz_log(log_level, "No task specific parameters");
455     }
456     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
457     {
458         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
459         yaz_log(log_level, "Received ItemOrder");
460         if (it->which == Z_IOItemOrder_esRequest)
461         {
462             Z_IORequest *ir = it->u.esRequest;
463             Z_IOOriginPartToKeep *k = ir->toKeep;
464             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
465             const char *xml_in_response = 0;
466
467             if (k && k->contact)
468             {
469                 if (k->contact->name)
470                     yaz_log(log_level, "contact name %s", k->contact->name);
471                 if (k->contact->phone)
472                     yaz_log(log_level, "contact phone %s", k->contact->phone);
473                 if (k->contact->email)
474                     yaz_log(log_level, "contact email %s", k->contact->email);
475             }
476             if (k->addlBilling)
477             {
478                 yaz_log(log_level, "Billing info (not shown)");
479             }
480
481             if (n->resultSetItem)
482             {
483                 yaz_log(log_level, "resultsetItem");
484                 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
485                 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
486             }
487             if (n->itemRequest)
488             {
489                 Z_External *r = (Z_External*) n->itemRequest;
490                 ILL_ItemRequest *item_req = 0;
491                 ILL_APDU *ill_apdu = 0;
492                 if (r->direct_reference)
493                 {
494                     char oid_name_str[OID_STR_MAX];
495                     oid_class oclass;
496                     const char *oid_name =
497                         yaz_oid_to_string_buf(r->direct_reference,
498                                               &oclass, oid_name_str);
499                     if (oid_name)
500                         yaz_log(log_level, "OID %s", oid_name);
501                     if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
502                     {
503                         yaz_log(log_level, "ILL XML request");
504                         if (r->which == Z_External_octet)
505                             yaz_log(log_level, "%.*s",
506                                     r->u.octet_aligned->len,
507                                     r->u.octet_aligned->buf);
508                         xml_in_response = "<dummy>x</dummy>";
509                     }
510                     if (!oid_oidcmp(r->direct_reference,
511                                     yaz_oid_general_isoill_1))
512                     {
513                         yaz_log(log_level, "Decode ItemRequest begin");
514                         if (r->which == ODR_EXTERNAL_single)
515                         {
516                             odr_setbuf(rr->decode,
517                                        (char *) r->u.single_ASN1_type->buf,
518                                        r->u.single_ASN1_type->len, 0);
519
520                             if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
521                             {
522                                 yaz_log(log_level,
523                                         "Couldn't decode ItemRequest %s near %ld",
524                                         odr_errmsg(odr_geterror(rr->decode)),
525                                         (long) odr_offset(rr->decode));
526                             }
527                             else
528                                 yaz_log(log_level, "Decode ItemRequest OK");
529                             if (rr->print)
530                             {
531                                 ill_ItemRequest(rr->print, &item_req, 0,
532                                                 "ItemRequest");
533                                 odr_reset(rr->print);
534                             }
535                         }
536                         if (!item_req && r->which == ODR_EXTERNAL_single)
537                         {
538                             yaz_log(log_level, "Decode ILL APDU begin");
539                             odr_setbuf(rr->decode,
540                                        (char*) r->u.single_ASN1_type->buf,
541                                        r->u.single_ASN1_type->len, 0);
542
543                             if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
544                             {
545                                 yaz_log(log_level,
546                                         "Couldn't decode ILL APDU %s near %ld",
547                                         odr_errmsg(odr_geterror(rr->decode)),
548                                         (long) odr_offset(rr->decode));
549                                 yaz_log(log_level, "PDU dump:");
550                                 odr_dumpBER(yaz_log_file(),
551                                             (char *) r->u.single_ASN1_type->buf,
552                                             r->u.single_ASN1_type->len);
553                             }
554                             else
555                                 yaz_log(log_level, "Decode ILL APDU OK");
556                             if (rr->print)
557                             {
558                                 ill_APDU(rr->print, &ill_apdu, 0,
559                                          "ILL APDU");
560                                 odr_reset(rr->print);
561                             }
562                         }
563                     }
564                 }
565                 if (item_req)
566                 {
567                     yaz_log(log_level, "ILL protocol version = "
568                             ODR_INT_PRINTF,
569                             *item_req->protocol_version_num);
570                 }
571             }
572             if (k)
573             {
574
575                 Z_External *ext = (Z_External *)
576                     odr_malloc(rr->stream, sizeof(*ext));
577                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
578                     odr_malloc(rr->stream, sizeof(*keep));
579                 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
580                     odr_malloc(rr->stream, sizeof(*targetPart));
581
582                 rr->taskPackage = (Z_TaskPackage *)
583                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
584                 rr->taskPackage->packageType =
585                     odr_oiddup(rr->stream, rr->esr->packageType);
586                 rr->taskPackage->packageName = 0;
587                 rr->taskPackage->userId = 0;
588                 rr->taskPackage->retentionTime = 0;
589                 rr->taskPackage->permissions = 0;
590                 rr->taskPackage->description = 0;
591                 rr->taskPackage->targetReference = (Odr_oct *)
592                     odr_malloc(rr->stream, sizeof(Odr_oct));
593                 rr->taskPackage->targetReference->buf =
594                     odr_strdup(rr->stream, "911");
595                 rr->taskPackage->targetReference->len =
596                     strlen((char *) (rr->taskPackage->targetReference->buf));
597 #if OCT_SIZE
598                 rr->taskPackage->targetReference->size =
599                     strlen((char *) (rr->taskPackage->targetReference->buf));
600 #endif
601                 rr->taskPackage->creationDateTime = 0;
602                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
603                 rr->taskPackage->packageDiagnostics = 0;
604                 rr->taskPackage->taskSpecificParameters = ext;
605
606                 ext->direct_reference =
607                     odr_oiddup(rr->stream, rr->esr->packageType);
608                 ext->indirect_reference = 0;
609                 ext->descriptor = 0;
610                 ext->which = Z_External_itemOrder;
611                 ext->u.itemOrder = (Z_ItemOrder *)
612                     odr_malloc(rr->stream, sizeof(*ext->u.update));
613                 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
614                 ext->u.itemOrder->u.taskPackage =  (Z_IOTaskPackage *)
615                     odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
616                 ext->u.itemOrder->u.taskPackage->originPart = k;
617                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
618
619                 if (xml_in_response)
620                     targetPart->itemRequest =
621                         z_ext_record_xml(rr->stream, xml_in_response,
622                                          strlen(xml_in_response));
623                 else
624                     targetPart->itemRequest = 0;
625
626                 targetPart->statusOrErrorReport = 0;
627                 targetPart->auxiliaryStatus = 0;
628             }
629         }
630     }
631     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
632     {
633         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
634         yaz_log(log_level, "Received DB Update");
635         if (up->which == Z_IUUpdate_esRequest)
636         {
637             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
638             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
639             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
640
641             yaz_log(log_level, "action");
642             if (toKeep->action)
643             {
644                 switch (*toKeep->action)
645                 {
646                 case Z_IUOriginPartToKeep_recordInsert:
647                     yaz_log(log_level, " recordInsert");
648                     break;
649                 case Z_IUOriginPartToKeep_recordReplace:
650                     yaz_log(log_level, " recordReplace");
651                     break;
652                 case Z_IUOriginPartToKeep_recordDelete:
653                     yaz_log(log_level, " recordDelete");
654                     break;
655                 case Z_IUOriginPartToKeep_elementUpdate:
656                     yaz_log(log_level, " elementUpdate");
657                     break;
658                 case Z_IUOriginPartToKeep_specialUpdate:
659                     yaz_log(log_level, " specialUpdate");
660                     break;
661                 default:
662                     yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
663                             *toKeep->action);
664                 }
665             }
666             if (toKeep->databaseName)
667             {
668                 yaz_log(log_level, "database: %s", toKeep->databaseName);
669                 if (!strcmp(toKeep->databaseName, "fault"))
670                 {
671                     rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
672                     rr->errstring = toKeep->databaseName;
673                 }
674                 if (!strcmp(toKeep->databaseName, "accept"))
675                     rr->errcode = -1;
676             }
677             if (toKeep)
678             {
679                 Z_External *ext = (Z_External *)
680                     odr_malloc(rr->stream, sizeof(*ext));
681                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
682                     odr_malloc(rr->stream, sizeof(*keep));
683                 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
684                     odr_malloc(rr->stream, sizeof(*targetPart));
685
686                 rr->taskPackage = (Z_TaskPackage *)
687                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
688                 rr->taskPackage->packageType =
689                     odr_oiddup(rr->stream, rr->esr->packageType);
690                 rr->taskPackage->packageName = 0;
691                 rr->taskPackage->userId = 0;
692                 rr->taskPackage->retentionTime = 0;
693                 rr->taskPackage->permissions = 0;
694                 rr->taskPackage->description = 0;
695                 rr->taskPackage->targetReference = (Odr_oct *)
696                     odr_malloc(rr->stream, sizeof(Odr_oct));
697                 rr->taskPackage->targetReference->buf =
698                     odr_strdup(rr->stream, "123");
699                 rr->taskPackage->targetReference->len =
700                     strlen((char *) (rr->taskPackage->targetReference->buf));
701 #if OCT_SIZE
702                 rr->taskPackage->targetReference->size =
703                     rr->taskPackage->targetReference->len;
704 #endif
705                 rr->taskPackage->creationDateTime = 0;
706                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
707                 rr->taskPackage->packageDiagnostics = 0;
708                 rr->taskPackage->taskSpecificParameters = ext;
709
710                 ext->direct_reference =
711                     odr_oiddup(rr->stream, rr->esr->packageType);
712                 ext->indirect_reference = 0;
713                 ext->descriptor = 0;
714                 ext->which = Z_External_update;
715                 ext->u.update = (Z_IUUpdate *)
716                     odr_malloc(rr->stream, sizeof(*ext->u.update));
717                 ext->u.update->which = Z_IUUpdate_taskPackage;
718                 ext->u.update->u.taskPackage =  (Z_IUUpdateTaskPackage *)
719                     odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
720                 ext->u.update->u.taskPackage->originPart = keep;
721                 ext->u.update->u.taskPackage->targetPart = targetPart;
722
723                 keep->action = odr_intdup(rr->stream, *toKeep->action);
724                 keep->databaseName =
725                     odr_strdup(rr->stream, toKeep->databaseName);
726                 keep->schema = 0;
727                 keep->elementSetName = 0;
728                 keep->actionQualifier = 0;
729
730                 targetPart->updateStatus = odr_intdup(rr->stream, 1);
731                 targetPart->num_globalDiagnostics = 0;
732                 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
733                 targetPart->num_taskPackageRecords = 1;
734                 targetPart->taskPackageRecords =
735                     (Z_IUTaskPackageRecordStructure **)
736                     odr_malloc(rr->stream,
737                                sizeof(Z_IUTaskPackageRecordStructure *));
738                 targetPart->taskPackageRecords[0] =
739                     (Z_IUTaskPackageRecordStructure *)
740                     odr_malloc(rr->stream,
741                                sizeof(Z_IUTaskPackageRecordStructure));
742
743                 targetPart->taskPackageRecords[0]->which =
744                     Z_IUTaskPackageRecordStructure_record;
745                 targetPart->taskPackageRecords[0]->u.record =
746                     z_ext_record_sutrs(rr->stream, "test", 4);
747                 targetPart->taskPackageRecords[0]->correlationInfo = 0;
748                 targetPart->taskPackageRecords[0]->recordStatus =
749                     odr_intdup(rr->stream,
750                                Z_IUTaskPackageRecordStructure_success);
751                 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
752                     = 0;
753
754                 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
755             }
756             if (notToKeep)
757             {
758                 int i;
759                 for (i = 0; i < notToKeep->num; i++)
760                 {
761                     Z_External *rec = notToKeep->elements[i]->record;
762
763                     if (rec->direct_reference)
764                     {
765                         char oid_name_str[OID_STR_MAX];
766                         const char *oid_name
767                             = oid_name = yaz_oid_to_string_buf(
768                                 rec->direct_reference, 0,
769                                 oid_name_str);
770                         if (oid_name)
771                             yaz_log(log_level, "record %d type %s", i,
772                                     oid_name);
773                     }
774                     switch (rec->which)
775                     {
776                     case Z_External_sutrs:
777                         if (rec->u.octet_aligned->len > 170)
778                             yaz_log(log_level, "%d bytes:\n%.168s ...",
779                                     rec->u.sutrs->len,
780                                     rec->u.sutrs->buf);
781                         else
782                             yaz_log(log_level, "%d bytes:\n%s",
783                                     rec->u.sutrs->len,
784                                     rec->u.sutrs->buf);
785                         break;
786                     case Z_External_octet        :
787                         if (rec->u.octet_aligned->len > 170)
788                             yaz_log(log_level, "%d bytes:\n%.168s ...",
789                                     rec->u.octet_aligned->len,
790                                     rec->u.octet_aligned->buf);
791                         else
792                             yaz_log(log_level, "%d bytes\n%s",
793                                     rec->u.octet_aligned->len,
794                                     rec->u.octet_aligned->buf);
795                     }
796                 }
797             }
798         }
799     }
800     return 0;
801 }
802
803 /* result set delete */
804 int ztest_delete(void *handle, bend_delete_rr *rr)
805 {
806     if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
807         rr->delete_status = Z_DeleteStatus_success;
808     else
809         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
810     return 0;
811 }
812
813 /* Our sort handler really doesn't sort... */
814 int ztest_sort(void *handle, bend_sort_rr *rr)
815 {
816     rr->errcode = 0;
817     rr->sort_status = Z_SortResponse_success;
818     return 0;
819 }
820
821
822 /* present request handler */
823 int ztest_present(void *handle, bend_present_rr *rr)
824 {
825     struct session_handle *sh = (struct session_handle*) handle;
826     struct result_set *set = get_set(sh, rr->setname);
827
828     if (!set)
829     {
830         rr->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
831         rr->errstring = odr_strdup(rr->stream, rr->setname);
832         return 0;
833     }
834     do_delay(&set->present_delay);
835     return 0;
836 }
837
838 /* retrieval of a single record (present, and piggy back search) */
839 int ztest_fetch(void *handle, bend_fetch_rr *r)
840 {
841     struct session_handle *sh = (struct session_handle*) handle;
842     char *cp;
843     const Odr_oid *oid = r->request_format;
844     struct result_set *set = get_set(sh, r->setname);
845     const char *esn = yaz_get_esn(r->comp);
846
847     if (!set)
848     {
849         r->errcode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
850         r->errstring = odr_strdup(r->stream, r->setname);
851         return 0;
852     }
853     do_delay(&set->fetch_delay);
854     r->last_in_set = 0;
855     r->basename = set->db;
856     r->output_format = r->request_format;
857
858     if (r->number < 1 || r->number > set->hits)
859     {
860         r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
861         return 0;
862     }
863     if (!oid || yaz_oid_is_iso2709(oid))
864     {
865         cp = dummy_marc_record(r->number, r->stream);
866         if (!cp)
867         {
868             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
869             r->surrogate_flag = 1;
870             return 0;
871         }
872         else
873         {
874             r->len = strlen(cp);
875             r->record = cp;
876             r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
877         }
878     }
879     else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
880     {
881         cp = dummy_marc_record(r->number, r->stream);
882         if (!cp)
883         {
884             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
885             r->surrogate_flag = 1;
886             return 0;
887         }
888         r->record = (char *) dummy_opac(r->number, r->stream, cp);
889         r->len = -1;
890     }
891     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
892     {
893         /* this section returns a small record */
894         char buf[100];
895
896         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
897
898         r->len = strlen(buf);
899         r->record = (char *) odr_malloc(r->stream, r->len+1);
900         strcpy(r->record, buf);
901     }
902     else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
903     {
904         r->len = -1;
905         r->record = (char*) dummy_grs_record(r->number, r->stream);
906         if (!r->record)
907         {
908             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
909             r->surrogate_flag = 1;
910             return 0;
911         }
912     }
913     else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
914     {
915         char fname[20];
916         FILE *f;
917         long size;
918
919         sprintf(fname, "part.%d.ps", r->number);
920         f = fopen(fname, "rb");
921         if (!f)
922         {
923             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
924             r->surrogate_flag = 1;
925             return 0;
926         }
927         fseek(f, 0L, SEEK_END);
928         size = ftell(f);
929         if (size <= 0 || size >= 5000000)
930         {
931             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
932             r->surrogate_flag = 1;
933         }
934         fseek(f, 0L, SEEK_SET);
935         r->record = (char*) odr_malloc(r->stream, size);
936         r->len = size;
937         if (fread(r->record, size, 1, f) != 1)
938         {
939             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
940             r->surrogate_flag = 1;
941         }
942         fclose(f);
943     }
944     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
945     {
946         if ((cp = dummy_xml_record(r->number, r->stream, esn)))
947         {
948             r->len = strlen(cp);
949             r->record = cp;
950         }
951         else
952         {
953             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
954             r->surrogate_flag = 1;
955             return 0;
956         }
957     }
958     else
959     {
960         char buf[OID_STR_MAX];
961         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
962         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
963         return 0;
964     }
965     r->errcode = 0;
966     return 0;
967 }
968
969 /*
970  * silly dummy-scan what reads words from a file.
971  */
972 int ztest_scan(void *handle, bend_scan_rr *q)
973 {
974     static FILE *f = 0;
975     static struct scan_entry list[200];
976     static char entries[200][80];
977     int hits[200];
978     char term[80], *p;
979     int i, pos;
980     int term_position_req = q->term_position;
981     int num_entries_req = q->num_entries;
982
983     /* Throw Database unavailable if other than Default or Slow */
984     if (!yaz_matchstr(q->basenames[0], "Default"))
985         ;  /* Default is OK in our test */
986     else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
987         ;
988     else
989     {
990         q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
991         q->errstring = q->basenames[0];
992         return 0;
993     }
994
995     q->errcode = 0;
996     q->errstring = 0;
997     q->entries = list;
998     q->status = BEND_SCAN_SUCCESS;
999     if (!f && !(f = fopen("dummy-words", "r")))
1000     {
1001         perror("dummy-words");
1002         exit(1);
1003     }
1004     if (q->num_entries > 200)
1005     {
1006         q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
1007         return 0;
1008     }
1009     if (q->term)
1010     {
1011         int len;
1012         if (q->term->term->which != Z_Term_general)
1013         {
1014             q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
1015             return 0;
1016         }
1017         if (*q->step_size != 0)
1018         {
1019             q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
1020             return 0;
1021         }
1022         len = q->term->term->u.general->len;
1023         if (len >= (int ) sizeof(term))
1024             len = sizeof(term)-1;
1025         memcpy(term, q->term->term->u.general->buf, len);
1026         term[len] = '\0';
1027     }
1028     else if (q->scanClause)
1029     {
1030         strncpy(term, q->scanClause, sizeof(term)-1);
1031         term[sizeof(term)-1] = '\0';
1032     }
1033     else
1034         strcpy(term, "0");
1035
1036     for (p = term; *p; p++)
1037         if (yaz_islower(*p))
1038             *p = yaz_toupper(*p);
1039
1040     fseek(f, 0, SEEK_SET);
1041     q->num_entries = 0;
1042
1043     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
1044          i++, pos < 199 ? pos++ : (pos = 0))
1045     {
1046         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
1047         {
1048             if ((q->term_position = term_position_req) > i + 1)
1049             {
1050                 q->term_position = i + 1;
1051                 q->status = BEND_SCAN_PARTIAL;
1052             }
1053             for (; q->num_entries < q->term_position; q->num_entries++)
1054             {
1055                 int po;
1056
1057                 po = pos - q->term_position + q->num_entries+1; /* find pos */
1058                 if (po < 0)
1059                     po += 200;
1060
1061                 if (!strcmp(term, "SD") && q->num_entries == 2)
1062                 {
1063                     list[q->num_entries].term = entries[pos];
1064                     list[q->num_entries].occurrences = -1;
1065                     list[q->num_entries].errcode =
1066                         YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
1067                     list[q->num_entries].errstring = "SD for Scan Term";
1068                 }
1069                 else
1070                 {
1071                     list[q->num_entries].term = entries[po];
1072                     list[q->num_entries].occurrences = hits[po];
1073                 }
1074             }
1075         }
1076         else if (q->num_entries)
1077         {
1078             list[q->num_entries].term = entries[pos];
1079             list[q->num_entries].occurrences = hits[pos];
1080             q->num_entries++;
1081         }
1082         if (q->num_entries >= num_entries_req)
1083             break;
1084     }
1085     echo_extra_args(q->stream, q->extra_args, &q->extra_response_data);
1086     if (feof(f))
1087         q->status = BEND_SCAN_PARTIAL;
1088     return 0;
1089 }
1090
1091 int ztest_explain(void *handle, bend_explain_rr *rr)
1092 {
1093     if (rr->database && !strcmp(rr->database, "Default"))
1094     {
1095         rr->explain_buf = "<explain>\n"
1096             "\t<serverInfo>\n"
1097             "\t\t<host>localhost</host>\n"
1098             "\t\t<port>210</port>\n"
1099             "\t</serverInfo>\n"
1100             "</explain>\n";
1101     }
1102     return 0;
1103 }
1104
1105 int ztest_update(void *handle, bend_update_rr *rr)
1106 {
1107     rr->operation_status = "success";
1108     return 0;
1109 }
1110
1111 bend_initresult *bend_init(bend_initrequest *q)
1112 {
1113     bend_initresult *r = (bend_initresult *)
1114         odr_malloc(q->stream, sizeof(*r));
1115     struct session_handle *sh = xmalloc(sizeof(*sh));
1116
1117     sh->result_sets = 0;
1118
1119     if (!log_level_set)
1120     {
1121         log_level=yaz_log_module_level("ztest");
1122         log_level_set=1;
1123     }
1124
1125     r->errcode = 0;
1126     r->errstring = 0;
1127     r->handle = sh;                         /* tell GFS about our handle */
1128     q->bend_sort = ztest_sort;              /* register sort handler */
1129     q->bend_search = ztest_search;          /* register search handler */
1130     q->bend_present = ztest_present;        /* register present handle */
1131     q->bend_esrequest = ztest_esrequest;
1132     q->bend_delete = ztest_delete;
1133     q->bend_fetch = ztest_fetch;
1134     q->bend_scan = ztest_scan;
1135 #if 0
1136     q->bend_explain = ztest_explain;
1137 #endif
1138     q->bend_srw_scan = ztest_scan;
1139     q->bend_srw_update = ztest_update;
1140
1141     q->query_charset = "ISO-8859-1";
1142     q->records_in_same_charset = 0;
1143
1144     return r;
1145 }
1146
1147 void bend_close(void *handle)
1148 {
1149     struct session_handle *sh = (struct session_handle*) handle;
1150     remove_sets(sh);
1151     xfree(sh);              /* release our session */
1152     return;
1153 }
1154
1155 int main(int argc, char **argv)
1156 {
1157     return statserv_main(argc, argv, bend_init, bend_close);
1158 }
1159 /*
1160  * Local variables:
1161  * c-basic-offset: 4
1162  * c-file-style: "Stroustrup"
1163  * indent-tabs-mode: nil
1164  * End:
1165  * vim: shiftwidth=4 tabstop=8 expandtab
1166  */
1167