151066ad3d7952e53cd1a80c6b1f8191edc1d5e9
[yazpp-moved-to-github.git] / zlint / test-scan-01.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2013 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 #include <yaz/log.h>
10 #include <yaz/pquery.h>
11
12 #include <zlint.h>
13
14 static const char *try_scan [] = {
15     "@attr 1=4 ab",
16     "@attr 1=1003 ab",
17     "@attr 1=1016 ab",
18     0
19 };
20
21 Zlint_test_scan_01::Zlint_test_scan_01()
22 {
23     m_scan_no = 0;
24 }
25
26 Zlint_test_scan_01::~Zlint_test_scan_01()
27 {
28 }
29
30 Zlint_code Zlint_test_scan_01::init(Zlint *z)
31 {
32     int len;
33     Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
34     Z_InitRequest *init = apdu->u.initRequest;
35
36
37     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
38     ODR_MASK_SET(init->options, Z_Options_namedResultSets);
39     ODR_MASK_SET(init->options, Z_Options_scan);
40
41     int r = z->send_Z_PDU(apdu, &len);
42     if (r < 0)
43     {
44         z->msg_check_fail("unable to send init request");
45         return TEST_FINISHED;
46     }
47     return TEST_CONTINUE;
48 }
49
50 Zlint_code Zlint_test_scan_01::sendTest(Zlint *z)
51 {
52     if (try_scan[m_scan_no])
53     {
54         int len;
55         z->msg_check_for("scan support %s", try_scan[m_scan_no]);
56
57         Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_scanRequest);
58         YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
59         Z_ScanRequest *sr = apdu->u.scanRequest;
60         sr->termListAndStartPoint = yaz_pqf_scan(pqf_parser,
61                                                  z->odr_encode(),
62                                                  &sr->attributeSet,
63                                                  try_scan[m_scan_no]);
64
65         z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
66
67         yaz_pqf_destroy (pqf_parser);
68         z->send_Z_PDU(apdu, &len);
69         return TEST_CONTINUE;
70     }
71     else
72         return TEST_FINISHED;
73 }
74
75 Zlint_code Zlint_test_scan_01::recv_gdu(Zlint *z, Z_GDU *gdu)
76 {
77     if (gdu->which == Z_GDU_Z3950 &&
78         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
79     {
80         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
81         int ver = z->initResponseGetVersion(init);
82         int result = init->result ? *init->result : 0;
83         if (ver > 3 || ver < 2)
84             z->msg_check_fail("got version %d, expected 2 or 3", ver);
85         if (!result)
86         {
87             z->msg_check_fail("init rejected (result false)");
88             return TEST_FINISHED;
89         }
90         else if (!ODR_MASK_GET(init->options, Z_Options_scan))
91         {
92             z->msg_check_notapp();
93             z->msg_check_info("scan unsupported");
94             return TEST_FINISHED;
95         }
96         else
97         {
98             sendTest(z);
99             return TEST_CONTINUE;
100         }
101     }
102     else if (gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_scanResponse)
103     {
104         Z_ScanResponse *sr =  gdu->u.z3950->u.scanResponse;
105         if (sr->entries->nonsurrogateDiagnostics)
106         {
107             z->msg_check_ok();
108             z->msg_check_info("scan NSD for %s", try_scan[m_scan_no]);
109         }
110         else if (sr->entries->entries && sr->entries->num_entries > 0)
111         {
112             z->msg_check_ok();
113         }
114         else
115         {
116             z->msg_check_fail("scan no entries/diagnostics for %s",
117                               try_scan[m_scan_no]);
118         }
119         m_scan_no++;
120         return sendTest(z);
121     }
122     else
123         z->msg_check_fail("did not receive init response as expected");
124     return TEST_FINISHED;
125 }
126
127 Zlint_code Zlint_test_scan_01::recv_fail(Zlint *z, int reason)
128 {
129     m_scan_no++;
130     z->msg_check_fail("target closed connection");
131     return TEST_FINISHED;
132 }
133 /*
134  * Local variables:
135  * c-basic-offset: 4
136  * c-file-style: "Stroustrup"
137  * indent-tabs-mode: nil
138  * End:
139  * vim: shiftwidth=4 tabstop=8 expandtab
140  */
141