Dump backtrace in case of SIGABRT, SIGSEGV YAZ-787
[yaz-moved-to-github.git] / ztest / ztest.c
index 896f56e..cc7736d 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /** \file
@@ -33,6 +33,7 @@
 #include <yaz/diagbib1.h>
 #include <yaz/otherinfo.h>
 #include <yaz/facet.h>
+#include <yaz/backtrace.h>
 
 #include "ztest.h"
 
@@ -89,13 +90,14 @@ static void remove_sets(struct session_handle *sh)
 
 /** \brief use term value as hit count
     \param s RPN structure
+    \param hash value for compuation
     \return >= 0: search term number or -1: not found
 
     Traverse RPN tree 'in order' and use term value as hit count.
     Only terms  that looks a numeric is used.. Returns -1 if
     no sub tree has a hit count term
 */
-static Odr_int get_term_hit(Z_RPNStructure *s)
+static Odr_int get_term_hit(Z_RPNStructure *s, unsigned *hash)
 {
     Odr_int h = -1;
     switch(s->which)
@@ -114,13 +116,19 @@ static Odr_int get_term_hit(Z_RPNStructure *s)
                     h = odr_atoi(wrbuf_cstr(hits_str));
                     wrbuf_destroy(hits_str);
                 }
+                else
+                {
+                    int i;
+                    for (i = 0; i < oct->len; i++)
+                        *hash = *hash * 65509 + oct->buf[i];
+                }
             }
         }
         break;
     case Z_RPNStructure_complex:
-        h = get_term_hit(s->u.complex->s1);
+        h = get_term_hit(s->u.complex->s1, hash);
         if (h == -1)
-            h = get_term_hit(s->u.complex->s2);
+            h = get_term_hit(s->u.complex->s2, hash);
         break;
     }
     return h;
@@ -128,7 +136,7 @@ static Odr_int get_term_hit(Z_RPNStructure *s)
 
 /** \brief gets hit count for numeric terms in RPN queries
     \param q RPN Query
-    \return number of hits (random or number for term)
+    \return number of hits
 
     This is just for testing.. A real database of course uses
     the content of a database to establish a value.. In our case, we
@@ -139,12 +147,23 @@ static Odr_int get_hit_count(Z_Query *q)
 {
     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
     {
+        unsigned hash = 0;
         Odr_int h = -1;
-        h = get_term_hit(q->u.type_1->RPNStructure);
+        h = get_term_hit(q->u.type_1->RPNStructure, &hash);
         if (h == -1)
-            h = rand() % 24;
+            h = hash % 24;
         return h;
     }
+    else if (q->which == Z_Query_type_104 &&
+             q->u.type_104->which == Z_External_CQL)
+    {
+        unsigned hash = 0;
+        const char *cql = q->u.type_104->u.cql;
+        int i;
+        for (i = 0; cql[i]; i++)
+            hash = hash * 65509 + cql[i];
+        return hash % 24;
+    }
     else
         return 24;
 }
@@ -259,7 +278,11 @@ Z_OtherInformation *build_facet_response(ODR odr, Z_FacetList *facet_list) {
         attrvalues.limit = 10;
         yaz_facet_attr_get_z_attributes(facet_list->elements[index]->attributes,
                                         &attrvalues);
-        yaz_log(YLOG_LOG, "Attributes: %s %d ", attrvalues.useattr, attrvalues.limit);
+        yaz_log(YLOG_LOG, "Attributes: %s limit=%d start=%d sort=%d",
+                attrvalues.useattr ? attrvalues.useattr : "NONE",
+                attrvalues.limit,
+                attrvalues.start,
+                attrvalues.sortorder);
         if (attrvalues.errstring)
             yaz_log(YLOG_LOG, "Error parsing attributes: %s", attrvalues.errstring);
         if (attrvalues.limit > 0 && attrvalues.useattr) {
@@ -570,13 +593,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
                 rr->taskPackage->retentionTime = 0;
                 rr->taskPackage->permissions = 0;
                 rr->taskPackage->description = 0;
-                rr->taskPackage->targetReference = (Odr_oct *)
-                    odr_malloc(rr->stream, sizeof(Odr_oct));
-                rr->taskPackage->targetReference->buf =
-                    (unsigned char *) odr_strdup(rr->stream, "911");
-                rr->taskPackage->targetReference->len =
-                    rr->taskPackage->targetReference->size =
-                    strlen((char *) (rr->taskPackage->targetReference->buf));
+                rr->taskPackage->targetReference =
+                    odr_create_Odr_oct(rr->stream, "911", 3);
                 rr->taskPackage->creationDateTime = 0;
                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
                 rr->taskPackage->packageDiagnostics = 0;
@@ -671,13 +689,8 @@ int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
                 rr->taskPackage->retentionTime = 0;
                 rr->taskPackage->permissions = 0;
                 rr->taskPackage->description = 0;
-                rr->taskPackage->targetReference = (Odr_oct *)
-                    odr_malloc(rr->stream, sizeof(Odr_oct));
-                rr->taskPackage->targetReference->buf =
-                    (unsigned char *) odr_strdup(rr->stream, "123");
-                rr->taskPackage->targetReference->len =
-                    rr->taskPackage->targetReference->size =
-                    strlen((char *) (rr->taskPackage->targetReference->buf));
+                rr->taskPackage->targetReference =
+                    odr_create_Odr_oct(rr->stream, "123", 3);
                 rr->taskPackage->creationDateTime = 0;
                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
                 rr->taskPackage->packageDiagnostics = 0;
@@ -818,6 +831,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     char *cp;
     const Odr_oid *oid = r->request_format;
     struct result_set *set = get_set(sh, r->setname);
+    const char *esn = yaz_get_esn(r->comp);
 
     if (!set)
     {
@@ -918,10 +932,26 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     }
     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
-        if ((cp = dummy_xml_record(r->number, r->stream)))
+        if ((cp = dummy_xml_record(r->number, r->stream, esn)))
         {
             r->len = strlen(cp);
             r->record = cp;
+            r->schema = "info:srw/schema/1/marcxml-1.1";
+        }
+        else
+        {
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
+            r->surrogate_flag = 1;
+            return 0;
+        }
+    }
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_json))
+    {
+        if ((cp = dummy_json_record(r->number, r->stream, esn)))
+        {
+            r->len = strlen(cp);
+            r->record = cp;
+            r->schema = "info:srw/schema/1/marcxml-1.1";
         }
         else
         {
@@ -1129,6 +1159,8 @@ void bend_close(void *handle)
 
 int main(int argc, char **argv)
 {
+    yaz_enable_panic_backtrace(argv[0]);
+
     return statserv_main(argc, argv, bend_init, bend_close);
 }
 /*