Bump copyright year
[yazpp-moved-to-github.git] / zlint / test-init-07.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2010 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #include <yaz/log.h>
7 #include <yaz/charneg.h>
8 #include <yaz/otherinfo.h>
9
10 #include <zlint.h>
11
12 Zlint_test_init_07::Zlint_test_init_07()
13 {
14 }
15
16 Zlint_test_init_07::~Zlint_test_init_07()
17 {
18 }
19
20 Zlint_code Zlint_test_init_07::init(Zlint *z)
21 {
22     int len;
23     Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
24     Z_InitRequest *init = apdu->u.initRequest;
25     Z_OtherInformation **oi;
26
27     z->msg_check_for("for character set negotiation");
28     
29     /* set all options.. see what target really supports .. */
30     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
31     yaz_oi_APDU(apdu, &oi);
32     if (!oi)
33     {
34         z->msg_check_fail("encoding failure");
35         return TEST_FINISHED;
36     }
37     else
38     {
39         Z_OtherInformationUnit *p0;
40         const char *negotiationCharset[] = {
41             "UTF-8",
42             "UTF-16",
43             "UCS-2",
44             "UCS-4",
45             "ISO-8859-1"
46         };
47         char *yazLang = 0;
48         
49         if ((p0=yaz_oi_update(oi, z->odr_encode(), NULL, 0, 0))) {
50             ODR_MASK_SET(init->options, Z_Options_negotiationModel);
51             
52             p0->which = Z_OtherInfo_externallyDefinedInfo;
53             p0->information.externallyDefinedInfo =
54                 
55                 yaz_set_proposal_charneg(
56                     z->odr_encode(),
57                     negotiationCharset, 5,
58                     (const char**)&yazLang, yazLang ? 1 : 0, 1);
59         }
60     }
61     int r = z->send_Z_PDU(apdu, &len);
62     if (r < 0)
63     {
64         z->msg_check_fail("unable to send init request");
65         return TEST_FINISHED;
66     }
67     return TEST_CONTINUE;
68 }
69
70 Zlint_code Zlint_test_init_07::recv_gdu(Zlint *z, Z_GDU *gdu)
71 {
72     if (gdu->which == Z_GDU_Z3950 &&
73         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
74     {
75         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
76
77         if (ODR_MASK_GET(init->options, Z_Options_negotiationModel))
78         {
79             Z_CharSetandLanguageNegotiation *p =
80                 yaz_get_charneg_record(init->otherInfo);
81             
82             if (p) {
83                 
84                 char *charset=NULL, *lang=NULL;
85                 int selected;
86                 NMEM m = nmem_create();
87                 
88                 yaz_get_response_charneg(m, p, &charset, &lang,
89                                          &selected);
90                 z->msg_check_ok();
91                 z->msg_check_info("Accepted character set : %s", charset);
92                 z->msg_check_info("Accepted code language : %s", lang ? lang : "none");
93                 z->msg_check_info("Accepted records in ...: %d", selected );
94                 nmem_destroy(m);
95                 return TEST_FINISHED;
96             }
97         }
98         z->msg_check_notapp();
99     }
100     else
101         z->msg_check_fail("did not receive init response as expected");
102     return TEST_FINISHED;
103 }
104
105
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * c-file-style: "Stroustrup"
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114