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