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