source: yaz/trunk/fuentes/test/test_embed_record.c @ 255

Last change on this file since 255 was 255, checked in by mabarracus, 4 years ago

added trusty code to xenial

File size: 13.6 KB
Line 
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5#if HAVE_CONFIG_H
6#include <config.h>
7#endif
8
9#include <yaz/record_conv.h>
10#include <yaz/test.h>
11#include <yaz/wrbuf.h>
12#include <string.h>
13#include <yaz/log.h>
14#include <yaz/record_render.h>
15
16#include <yaz/base64.h>
17#include <yaz/marcdisp.h>
18#include <yaz/proto.h>
19#include <yaz/prt-ext.h>
20
21#if YAZ_HAVE_XML2
22
23#include <libxml/parser.h>
24#include <libxml/tree.h>
25#endif
26
27void test1(void)
28{
29    int buflen;
30    for (buflen = 0; buflen < 256; buflen++)
31    {
32        char buf[256];
33        char buf_a[1024];
34        char buf_b[1024];
35        int i;
36        for (i = 0; i < buflen; i++)
37            buf[i] = 1 + i;
38        buf[i] = 0;
39
40        yaz_base64encode(buf, buf_a);
41
42        YAZ_CHECK(yaz_base64decode(buf_a, buf_b) == 0);
43
44        YAZ_CHECK(strcmp(buf, buf_b) == 0);
45    }
46}
47
48void test2(void)
49{
50  char base_enc[] =
51    "MDA3NjZuYW0gIDIyMDAyNjU4YSA0NTAwMDAxMDAxMjAwMDAwMDAzMDAwNjAwMDEyMDA1MDAx"
52    "NzAwMDE4MDA4MDA0MTAwMDM1MDEwMDAxNzAwMDc2MDIwMDAxNTAwMDkzMDM1MDAxODAwMTA4"
53    "MDQwMDAxODAwMTI2MDQ5MDAwOTAwMTQ0MDUwMDAyNjAwMTUzMDgyMDAxNzAwMTc5MTAwMDAx"
54    "ODAwMTk2MjQ1MDA2NzAwMjE0MjYwMDA1MjAwMjgxMjYzMDAwOTAwMzMzMzAwMDAyNzAwMzQy"
55    "NTAwMDAyNzAwMzY5NTA0MDA1MTAwMzk2NjUwMDA0NDAwNDQ3OTk5MDAwOTAwNDkxHm9jbTI4"
56    "MzM5ODYzHk9Db0xDHjE5OTQwMTA1MDc0NTIyLjAeOTMwNjA5czE5OTQgICAgY291ICAgICAg"
57    "YiAgICAwMDEgMCBlbmcgIB4gIB9hICAgOTMwMDkwNDcgHiAgH2EwMTMwMzA1NTI5HiAgH2Eo"
58    "b2NtKTI4MzM5ODYzHiAgH2FETEMfY0RMQx9kS0tVHiAgH2FLS1VKHjAwH2FRQTc2LjczLkMy"
59    "OB9iRzczIDE5OTQeMDAfYTAwNS4xMy8zHzIyMB4xIB9hR3JhaGFtLCBQYXVsLh4xMB9hT24g"
60    "TGlzcCA6H2JhZHZhbmNlZCB0ZWNobmlxdWVzIGZvciBjb21tb24gTGlzcCAvH2NQYXVsIEdy"
61    "YWhhbS4eICAfYUVuZ2xld29vZCBDbGlmZnMsIE4uSi4gOh9iUHJlbnRpY2UgSGFsbCwfYzE5"
62    "OTQuHiAgH2E5NDEwHiAgH2F4aWlpLCA0MTMgcC4gOx9jMjMgY20uHiAgH2EiQW4gQWxhbiBS"
63    "LiBBcHQgYm9vay4iHiAgH2FJbmNsdWRlcyBiaWJsaW9ncmFwaGljYWwgcmVmZXJlbmNlcyBh"
64    "bmQgaW5kZXguHiAwH2FDT01NT04gTElTUCAoQ29tcHV0ZXIgcHJvZ3JhbSBsYW5ndWFnZSke"
65    "H2xVQUhJTEweHQ==";
66
67    char bin_marc[] =
68      "00766nam  22002658a 4500001001200000003000600012005001700018008004100035010001700076020001500093035001800108040001800126049000900144050002600153082001700179100001800196245006700214260005200281263000900333300002700342500002700369504005100396650004400447999000900491\036ocm28339863\036OCoLC\03619940105074522.0\036930609s1994    cou      b    001 0 eng  \036  \037a   93009047 \036  \037a0130305529\036  \037a(ocm)28339863\036  \037aDLC\037cDLC\037dKKU\036  \037aKKUJ\03600\037aQA76.73.C28\037bG73 1994\03600\037a005.13/3\037220\0361 \037aGraham, Paul.\03610\037aOn Lisp :\037badvanced techniques for common Lisp /\037cPaul Graham.\036  \037aEnglewood Cliffs, N.J. :\037bPrentice Hall,\037c1994.\036  \037a9410\036  \037axiii, 413 p. ;\037c23 cm.\036  \037a\"An Alan R. Apt book.\"\036  \037aIncludes bibliographical references and index.\036 0\037aCOMMON LISP (Computer program language)\036\037lUAHILL\036\035";
69
70    int marc_size = strlen(bin_marc);
71    char out_rec[1000];
72    yaz_marc_t marc = yaz_marc_create();
73    WRBUF buf = wrbuf_alloc();
74
75    yaz_base64decode(base_enc, out_rec);
76    YAZ_CHECK(strcmp(out_rec, bin_marc) == 0);
77
78    yaz_marc_read_iso2709(marc, out_rec, marc_size);
79
80    yaz_marc_write_marcxml(marc, buf);
81
82    yaz_marc_destroy(marc);
83    wrbuf_destroy(buf);
84
85}
86
87static int test_render(const char *type_spec, int is_marc, const char *input,
88                    const char *expected_output)
89{
90    ODR odr = odr_createmem(ODR_ENCODE);
91    const char *actual_output;
92    int actual_len;
93    int res = 0;
94    WRBUF wrbuf = wrbuf_alloc();
95
96    Z_NamePlusRecord *npr = odr_malloc(odr, sizeof(*npr));
97    npr->which = Z_NamePlusRecord_databaseRecord;
98    if (is_marc)
99        npr->u.databaseRecord = z_ext_record_usmarc(odr, input, strlen(input));
100    else
101        npr->u.databaseRecord = z_ext_record_xml(odr, input, strlen(input));
102
103    actual_output = yaz_record_render(npr, 0, wrbuf, type_spec, &actual_len);
104
105    if (actual_output && expected_output)
106    {
107        if (strlen(expected_output) == actual_len &&
108            !memcmp(expected_output, actual_output, actual_len))
109            res = 1;
110        else
111        {
112            yaz_log(YLOG_LOG, "Got result");
113            yaz_log(YLOG_LOG, "%.*s", actual_len, actual_output);
114            yaz_log(YLOG_LOG, "Expected result");
115            yaz_log(YLOG_LOG, "%s", expected_output);
116        }
117    }
118    else if (!actual_output && !expected_output)
119        res = 1;
120    else if (!actual_output && expected_output)
121    {
122        yaz_log(YLOG_LOG, "Got null result, but expected");
123        yaz_log(YLOG_LOG, "%s", expected_output);
124    }
125    else
126    {
127        yaz_log(YLOG_LOG, "Got result, but expected no result");
128        yaz_log(YLOG_LOG, "%.*s", actual_len, actual_output);
129    }
130    wrbuf_destroy(wrbuf);
131    odr_destroy(odr);
132    return res;
133}
134
135int main(int argc, char **argv)
136{
137    YAZ_CHECK_INIT(argc, argv);
138    YAZ_CHECK_LOG();
139    test1();
140    test2();
141#if YAZ_HAVE_XML2
142    YAZ_CHECK(test_render("xml", 0, "<my/>", "<my/>"));
143
144    YAZ_CHECK(test_render(
145                  "xml", 1,
146                  "\x30\x30\x31\x33\x38\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x30\x37"
147                  "\x33\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
148                  "\x30\x30\x30\x30\x30\x30\x33\x30\x30\x30\x34\x30\x30\x30\x31\x33"
149                  "\x31\x30\x30\x30\x30\x31\x37\x30\x30\x30\x31\x37\x32\x34\x35\x30"
150                  "\x30\x33\x30\x30\x30\x30\x33\x34\x1E\x20\x20\x20\x31\x31\x32\x32"
151                  "\x34\x34\x36\x36\x20\x1E\x44\x4C\x43\x1E\x31\x30\x1F\x61\x4A\x61"
152                  "\x63\x6B\x20\x43\x6F\x6C\x6C\x69\x6E\x73\x1E\x31\x30\x1F\x61\x48"
153                  "\x6F\x77\x20\x74\x6F\x20\x70\x72\x6F\x67\x72\x61\x6D\x20\x61\x20"
154                  "\x63\x6F\x6D\x70\x75\x74\x65\x72\x1E\x1D",
155                  "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
156                  "  <leader>00138nam a22000738a 4500</leader>\n"
157                  "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
158                  "  <controlfield tag=\"003\">DLC</controlfield>\n"
159                  "  <datafield tag=\"100\" ind1=\"1\" ind2=\"0\">\n"
160                  "    <subfield code=\"a\">Jack Collins</subfield>\n"
161                  "  </datafield>\n"
162                  "  <datafield tag=\"245\" ind1=\"1\" ind2=\"0\">\n"
163                  "    <subfield code=\"a\">How to program a computer</subfield>\n"
164                  "  </datafield>\n"
165                  "</record>\n"));
166
167    YAZ_CHECK(test_render("xml", 0, "<my/>", "<my/>"));
168
169    YAZ_CHECK(test_render(
170                  "xml; base64=/my/text()", 0,
171                  "<my>"
172                  "MDAxMzhuYW0gIDIyMDAwNzM4YSA0NTAwMDAxMDAxMzAwMDAwMDAzMDAwNDAwMDEzMTAwMDAxNzAw"
173                  "MDE3MjQ1MDAzMDAwMDM0HiAgIDExMjI0NDY2IB5ETEMeMTAfYUphY2sgQ29sbGlucx4xMB9hSG93"
174                  "IHRvIHByb2dyYW0gYSBjb21wdXRlch4d"
175                  "</my>",
176                  "<?xml version=\"1.0\"?>\n"
177                  "<my><record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
178                  "  <leader>00138nam a22000738a 4500</leader>\n"
179                  "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
180                  "  <controlfield tag=\"003\">DLC</controlfield>\n"
181                  "  <datafield tag=\"100\" ind1=\"1\" ind2=\"0\">\n"
182                  "    <subfield code=\"a\">Jack Collins</subfield>\n"
183                  "  </datafield>\n"
184                  "  <datafield tag=\"245\" ind1=\"1\" ind2=\"0\">\n"
185                  "    <subfield code=\"a\">How to program a computer</subfield>\n"
186                  "  </datafield>\n"
187                  "</record></my>\n"));
188
189    YAZ_CHECK(test_render(
190                  "xml; charset=utf-8; base64=/my/text()", 0,
191                  "<my>"
192                  "MDAxMzhuYW0gIDIyMDAwNzM4YSA0NTAwMDAxMDAxMzAwMDAwMDAzMDAwNDAwMDEzMTAwMDAxNzAw"
193                  "MDE3MjQ1MDAzMDAwMDM0HiAgIDExMjI0NDY2IB5ETEMeMTAfYUphY2sgQ29sbGlucx4xMB9hSG93"
194                  "IHRvIHByb2dyYW0gYSBjb21wdXRlch4d"
195                  "</my>",
196                  "<?xml version=\"1.0\"?>\n"
197                  "<my><record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
198                  "  <leader>00138nam a22000738a 4500</leader>\n"
199                  "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
200                  "  <controlfield tag=\"003\">DLC</controlfield>\n"
201                  "  <datafield tag=\"100\" ind1=\"1\" ind2=\"0\">\n"
202                  "    <subfield code=\"a\">Jack Collins</subfield>\n"
203                  "  </datafield>\n"
204                  "  <datafield tag=\"245\" ind1=\"1\" ind2=\"0\">\n"
205                  "    <subfield code=\"a\">How to program a computer</subfield>\n"
206                  "  </datafield>\n"
207                  "</record></my>\n"));
208
209    YAZ_CHECK(test_render(
210                  "xml; base64=/my/text();charset=utf-8", 0,
211                  "<my>"
212                  "MDAxMzhuYW0gIDIyMDAwNzM4YSA0NTAwMDAxMDAxMzAwMDAwMDAzMDAwNDAwMDEzMTAwMDAxNzAw"
213                  "MDE3MjQ1MDAzMDAwMDM0HiAgIDExMjI0NDY2IB5ETEMeMTAfYUphY2sgQ29sbGlucx4xMB9hSG93"
214                  "IHRvIHByb2dyYW0gYSBjb21wdXRlch4d"
215                  "</my>",
216                  "<?xml version=\"1.0\"?>\n"
217                  "<my><record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
218                  "  <leader>00138nam a22000738a 4500</leader>\n"
219                  "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
220                  "  <controlfield tag=\"003\">DLC</controlfield>\n"
221                  "  <datafield tag=\"100\" ind1=\"1\" ind2=\"0\">\n"
222                  "    <subfield code=\"a\">Jack Collins</subfield>\n"
223                  "  </datafield>\n"
224                  "  <datafield tag=\"245\" ind1=\"1\" ind2=\"0\">\n"
225                  "    <subfield code=\"a\">How to program a computer</subfield>\n"
226                  "  </datafield>\n"
227                  "</record></my>\n"));
228
229    YAZ_CHECK(test_render(
230                  "txml;base64=/my/text();charset=utf-8", 0,
231                  "<my>"
232                  "MDAxMzhuYW0gIDIyMDAwNzM4YSA0NTAwMDAxMDAxMzAwMDAwMDAzMDAwNDAwMDEzMTAwMDAxNzAw"
233                  "MDE3MjQ1MDAzMDAwMDM0HiAgIDExMjI0NDY2IB5ETEMeMTAfYUphY2sgQ29sbGlucx4xMB9hSG93"
234                  "IHRvIHByb2dyYW0gYSBjb21wdXRlch4d"
235                  "</my>",
236                  "<?xml version=\"1.0\"?>\n"
237                  "<my><r xmlns=\"http://www.indexdata.com/turbomarc\">\n"
238                  "  <l>00138nam a22000738a 4500</l>\n"
239                  "  <c001>   11224466 </c001>\n"
240                  "  <c003>DLC</c003>\n"
241                  "  <d100 i1=\"1\" i2=\"0\">\n"
242                  "    <sa>Jack Collins</sa>\n"
243                  "  </d100>\n"
244                  "  <d245 i1=\"1\" i2=\"0\">\n"
245                  "    <sa>How to program a computer</sa>\n"
246                  "  </d245>\n"
247                  "</r></my>\n"));
248
249    YAZ_CHECK(test_render(
250                  "xml; charset=utf-8; base64=/my/text()", 0,
251                  "<my>"
252"MDA0OTJuYW0gYTIyMDAxNDU1YSA0NTAwMDAxMDAxMDAwMDAwMDA1MDAxNzAwMDEwMDA4MDA0MTAw"
253"MDI3MTAwMDA1MjAwMDY4MjQ1MDEwNDAwMTIwMjUwMDAxNDAwMjI0MjYwMDAyMzAwMjM4MzAwMDAx"
254"NjAwMjYxNjUwMDA1MTAwMjc3NjUwMDAxODAwMzI4HjAwMDI3NzQ4NR4yMDA1MTAyNjExMTQzNi4w"
255"HjA1MDQxM3MxODk0ICAgIGdyICAgICAgICAgICAgMDAwIDAgZ3JlIGQeMSAfYc6czrHPgc6/z43O"
256"tM63z4IsIM6az4nOvc+Dz4TOsc69z4TOr869zr/PgiDOmc+JHjEwH2HOlc67zrvOt869zrnOus+M"
257"zr0gzrrPgc+Fz4DPhM6/zrPPgc6xz4bOuc66z4zOvSDOu861zr7Ouc66z4zOvSAvH2POms+Jzr3P"
258"g8+ELiDOmS4gzpzOsc+Bzr/Pjc60zrfPgi4eICAyzrcgzrXOus60Lh4gIB9hzpHOuM6uzr3OsSwf"
259"YzE4OTQuHiAgH2EyNDggz4POtc67Lh4gMB9hR3JlZWsgbGFuZ3VhZ2UsIE1vZGVybh94RGlhbGVj"
260"dHMfdkRpY3Rpb25hcmllcx4gMB9hQ3J5cHRvZ3JhcGh5Lh4d"
261                  "</my>",
262"<?xml version=\"1.0\"?>\n"
263"<my><record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
264"  <leader>00492nam a22001455a 4500</leader>\n"
265"  <controlfield tag=\"001\">000277485</controlfield>\n"
266"  <controlfield tag=\"005\">20051026111436.0</controlfield>\n"
267"  <controlfield tag=\"008\">050413s1894    gr            000 0 gre d</controlfield>\n"
268"  <datafield tag=\"100\" ind1=\"1\" ind2=\" \">\n"
269"    <subfield code=\"a\">&#x39C;&#x3B1;&#x3C1;&#x3BF;&#x3CD;&#x3B4;&#x3B7;&#x3C2;, &#x39A;&#x3C9;&#x3BD;&#x3C3;&#x3C4;&#x3B1;&#x3BD;&#x3C4;&#x3AF;&#x3BD;&#x3BF;&#x3C2; &#x399;&#x3C9;</subfield>\n"
270"  </datafield>\n"
271"  <datafield tag=\"245\" ind1=\"1\" ind2=\"0\">\n"
272"    <subfield code=\"a\">&#x395;&#x3BB;&#x3BB;&#x3B7;&#x3BD;&#x3B9;&#x3BA;&#x3CC;&#x3BD; &#x3BA;&#x3C1;&#x3C5;&#x3C0;&#x3C4;&#x3BF;&#x3B3;&#x3C1;&#x3B1;&#x3C6;&#x3B9;&#x3BA;&#x3CC;&#x3BD; &#x3BB;&#x3B5;&#x3BE;&#x3B9;&#x3BA;&#x3CC;&#x3BD; /</subfield>\n"
273"    <subfield code=\"c\">&#x39A;&#x3C9;&#x3BD;&#x3C3;&#x3C4;. &#x399;. &#x39C;&#x3B1;&#x3C1;&#x3BF;&#x3CD;&#x3B4;&#x3B7;&#x3C2;.</subfield>\n"
274"  </datafield>\n"
275"  <datafield tag=\"250\" ind1=\" \" ind2=\" \">\n"
276"    <subfield code=\"&#x3B7;\"> &#x3B5;&#x3BA;&#x3B4;.</subfield>\n"
277"  </datafield>\n"
278"  <datafield tag=\"260\" ind1=\" \" ind2=\" \">\n"
279"    <subfield code=\"a\">&#x391;&#x3B8;&#x3AE;&#x3BD;&#x3B1;,</subfield>\n"
280"    <subfield code=\"c\">1894.</subfield>\n"
281"  </datafield>\n"
282"  <datafield tag=\"300\" ind1=\" \" ind2=\" \">\n"
283"    <subfield code=\"a\">248 &#x3C3;&#x3B5;&#x3BB;.</subfield>\n"
284"  </datafield>\n"
285"  <datafield tag=\"650\" ind1=\" \" ind2=\"0\">\n"
286"    <subfield code=\"a\">Greek language, Modern</subfield>\n"
287"    <subfield code=\"x\">Dialects</subfield>\n"
288"    <subfield code=\"v\">Dictionaries</subfield>\n"
289"  </datafield>\n"
290"  <datafield tag=\"650\" ind1=\" \" ind2=\"0\">\n"
291"    <subfield code=\"a\">Cryptography.</subfield>\n"
292"  </datafield>\n"
293"</record></my>\n"));
294
295
296#endif
297    YAZ_CHECK_TERM;
298}
299
300
301/*
302 * Local variables:
303 * c-basic-offset: 4
304 * c-file-style: "Stroustrup"
305 * indent-tabs-mode: nil
306 * End:
307 * vim: shiftwidth=4 tabstop=8 expandtab
308 */
309
Note: See TracBrowser for help on using the repository browser.