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