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