Release 1.6.4
[yazpp-moved-to-github.git] / zlint / test-init-05.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) Index Data 
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
11 #include <zlint.h>
12
13 #define REFID_BUF1 "zlint\000check1"
14 #define REFID_LEN1 12
15 #define REFID_BUF2 "zlint\000check2"
16 #define REFID_LEN2 12
17
18 Zlint_test_init_05::Zlint_test_init_05()
19 {
20     m_init_response_no = 0;
21 }
22
23 Zlint_test_init_05::~Zlint_test_init_05()
24 {
25 }
26
27 Zlint_code Zlint_test_init_05::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     z->msg_check_for("for double init");
34
35     /* send double init with differnet refID's */
36     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
37     ODR_MASK_SET(init->options, Z_Options_concurrentOperations);
38     init->referenceId = z->mk_refid(REFID_BUF1, REFID_LEN1);
39
40     int r = z->send_Z_PDU(apdu, &len);
41     if (r < 0)
42     {
43         z->msg_check_fail("unable to send init request");
44         return TEST_FINISHED;
45     }
46
47     apdu = z->create_Z_PDU(Z_APDU_initRequest);
48     init = apdu->u.initRequest;
49
50     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
51     ODR_MASK_SET(init->options, Z_Options_concurrentOperations);
52
53     init->referenceId = z->mk_refid(REFID_BUF2, REFID_LEN2);
54
55     r = z->send_Z_PDU(apdu, &len);
56     if (r < 0)
57     {
58         z->msg_check_fail("unable to send init request");
59         return TEST_FINISHED;
60     }
61     return TEST_CONTINUE;
62 }
63
64 Zlint_code Zlint_test_init_05::recv_gdu(Zlint *z, Z_GDU *gdu)
65 {
66     if (gdu->which == Z_GDU_Z3950 &&
67         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
68     {
69         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
70         int result = init->result ? *init->result : 0;
71
72         if (m_init_response_no == 0)
73         {
74             if (!init->referenceId)
75             {
76                 z->msg_check_fail("missing referenceID from "
77                                   "first init response");
78                 return TEST_FINISHED;
79             }
80             else if (init->referenceId->len != REFID_LEN1
81                      || memcmp(init->referenceId->buf, REFID_BUF1, REFID_LEN1))
82             {
83                 z->msg_check_fail("reference ID does not match from "
84                                   "first init response");
85                 return TEST_FINISHED;
86             }
87         }
88         else
89         {
90             if (!init->referenceId)
91                 z->msg_check_fail("missing referenceID from "
92                                   "second init response");
93             else if (init->referenceId->len != REFID_LEN2
94                      || memcmp(init->referenceId->buf, REFID_BUF2, REFID_LEN2))
95                 z->msg_check_fail("reference ID does not match from "
96                                   "second init response");
97         }
98
99         if (!result)
100         {
101             z->msg_check_fail("init rejected (result false)");
102             return TEST_FINISHED;
103         }
104         else
105         {
106             if (m_init_response_no == 0)
107             {
108                 m_init_response_no++;
109                 return TEST_CONTINUE;
110             }
111             else
112                 z->msg_check_ok();
113         }
114     }
115     else
116         z->msg_check_fail("did not receive init response as expected");
117     return TEST_FINISHED;
118 }
119
120
121 /*
122  * Local variables:
123  * c-basic-offset: 4
124  * c-file-style: "Stroustrup"
125  * indent-tabs-mode: nil
126  * End:
127  * vim: shiftwidth=4 tabstop=8 expandtab
128  */
129