blob: 8665ad9642f329ddf031b62cdf696afc6761aa36 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20
21
22#if INCLUDE_XML_TEST
23
24#include <pjlib-util/xml.h>
25#include <pjlib.h>
26
27#define THIS_FILE "xml_test"
28
29static const char *xml_doc[] =
30{
31" <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
32" <p:pidf-full xmlns=\"urn:ietf:params:xml:ns:pidf\"\n"
33" xmlns:p=\"urn:ietf:params:xml:ns:pidf-diff\"\n"
34" xmlns:r=\"urn:ietf:params:xml:ns:pidf:rpid\"\n"
35" xmlns:c=\"urn:ietf:params:xml:ns:pidf:caps\"\n"
36" entity=\"pres:someone@example.com\"\n"
37" version=\"567\">\n"
38"\n"
39" <tuple id=\"sg89ae\">\n"
40" <status>\n"
41" <basic>open</basic>\n"
42" <r:relationship>assistant</r:relationship>\n"
43" </status>\n"
44" <c:servcaps>\n"
45" <c:audio>true</c:audio>\n"
46" <c:video>false</c:video>\n"
47" <c:message>true</c:message>\n"
48" </c:servcaps>\n"
49" <contact priority=\"0.8\">tel:09012345678</contact>\n"
50" </tuple>\n"
51"\n"
52" <tuple id=\"cg231jcr\">\n"
53" <status>\n"
54" <basic>open</basic>\n"
55" </status>\n"
56" <contact priority=\"1.0\">im:pep@example.com</contact>\n"
57" </tuple>\n"
58"\n"
59" <tuple id=\"r1230d\">\n"
60" <status>\n"
61" <basic>closed</basic>\n"
62" <r:activity>meeting</r:activity>\n"
63" </status>\n"
64" <r:homepage>http://example.com/~pep/</r:homepage>\n"
65" <r:icon>http://example.com/~pep/icon.gif</r:icon>\n"
66" <r:card>http://example.com/~pep/card.vcd</r:card>\n"
67" <contact priority=\"0.9\">sip:pep@example.com</contact>\n"
68" </tuple>\n"
69"\n"
70" <note xml:lang=\"en\">Full state presence document</note>\n"
71"\n"
72" <r:person>\n"
73" <r:status>\n"
74" <r:activities>\n"
75" <r:on-the-phone/>\n"
76" <r:busy/>\n"
77" </r:activities>\n"
78" </r:status>\n"
79" </r:person>\n"
80"\n"
81" <r:device id=\"urn:esn:600b40c7\">\n"
82" <r:status>\n"
83" <c:devcaps>\n"
84" <c:mobility>\n"
85" <c:supported>\n"
86" <c:mobile/>\n"
87" </c:supported>\n"
88" </c:mobility>\n"
89" </c:devcaps>\n"
90" </r:status>\n"
91" </r:device>\n"
92"\n"
93" </p:pidf-full>\n"
94}
95;
96
97static int xml_parse_print_test(const char *doc)
98{
99 pj_str_t msg;
100 pj_pool_t *pool;
101 pj_xml_node *root;
102 char *output;
103 int output_len;
104
105 pool = pj_pool_create(mem, "xml", 4096, 1024, NULL);
106 pj_strdup2(pool, &msg, doc);
107 root = pj_xml_parse(pool, msg.ptr, msg.slen);
108 if (!root) {
109 PJ_LOG(1, (THIS_FILE, " Error: unable to parse XML"));
110 return -10;
111 }
112
Benny Prijonoac623b32006-07-03 15:19:31 +0000113 output = (char*)pj_pool_zalloc(pool, msg.slen + 512);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000114 output_len = pj_xml_print(root, output, msg.slen+512, PJ_TRUE);
115 if (output_len < 1) {
116 PJ_LOG(1, (THIS_FILE, " Error: buffer too small to print XML file"));
117 return -20;
118 }
119 output[output_len] = '\0';
120
121
122 pj_pool_release(pool);
123 return 0;
124}
125
126int xml_test()
127{
128 unsigned i;
129 for (i=0; i<sizeof(xml_doc)/sizeof(xml_doc[0]); ++i) {
130 int status;
131 if ((status=xml_parse_print_test(xml_doc[i])) != 0)
132 return status;
133 }
134 return 0;
135}
136
137#else
138/* To prevent warning about "translation unit is empty"
139 * when this test is disabled.
140 */
141int dummy_xml_test;
142#endif /* INCLUDE_XML_TEST */
143
144