1 /*----------------------------------------------------------------------------
  2  Name:     xmlBlaster/testsuite/src/c/TestXmlUtil.c
  3  Copyright: "Marcel Ruff" mr@marcelruff.info
  4  Comment:   Test C client library
  5  -----------------------------------------------------------------------------*/
  6 #include <stdio.h>
  7 #include <stdlib.h>
  8 #include <string.h>
  9 #include "util/helper.h"
 10 #include "util/msgUtil.h"
 11 #include "util/base64c.h"
 12 #include "util/XmlUtil.h"
 13 #include "test.h"
 14 
 15 /*
 16 static int argc = 0;
 17 static char** argv = 0;
 18 */
 19 
 20 /*
 21  #define  ERRORSTR_LEN 4096
 22  static char errorString[ERRORSTR_LEN + 1];
 23  */
 24 
 25 static const char * test_strStartsEnds() {
 26         mu_assert("startsWith", !startsWith(0, 0));
 27         mu_assert("startsWith", !startsWith("", "He"));
 28         mu_assert("startsWith", !startsWith("Hello World", "Bla"));
 29         mu_assert("startsWith", startsWith("", ""));
 30         mu_assert("startsWith", startsWith("Hello World", ""));
 31         mu_assert("startsWith", startsWith("Hello World", "He"));
 32         mu_assert("startsWith", startsWith("Hello World", "H"));
 33         mu_assert("startsWith", startsWith("device.joe.nmea", "device."));
 34 
 35         mu_assert("endsWith", !endsWith(0, 0));
 36         mu_assert("endsWith", !endsWith("", "He"));
 37         mu_assert("endsWith", !endsWith("Hello World", "Bla"));
 38         mu_assert("endsWith", endsWith("", ""));
 39         mu_assert("endsWith", endsWith("Hello World", ""));
 40         mu_assert("endsWith", endsWith("Hello World", "ld"));
 41         mu_assert("endsWith", endsWith("Hello World", "d"));
 42         mu_assert("endsWith", endsWith("device.joe.nmea", ".nmea"));
 43 
 44         printf("[client] Good bye.\n");
 45         return 0;
 46 }
 47 
 48 static const char * test_extractAttributeValue() {
 49         {
 50                 const char * const xml = 0;
 51                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 52                 mu_assert("attrval0", val == 0);
 53         }
 54         {
 55                 const char * const xml = "<p/>";
 56                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 57                 mu_assert("attrval0", val == 0);
 58         }
 59         {
 60                 const char * const xml = "<p key='bla'></p>";
 61                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 62                 mu_assert("attrval0", val == 0);
 63         }
 64         {
 65                 const char * const xml = "<p k=''/>";
 66                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 67                 mu_assert("attrval0", val != 0);
 68                 printf("%s\n", val);
 69                 mu_assert("attrval", !strcmp(val, ""));
 70                 xmlBlasterFree(val);
 71         }
 72         {
 73                 const char * const xml = "<p k='bla'>blu</p>";
 74                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 75                 mu_assert("attrval0", val != 0);
 76                 printf("%s\n", val);
 77                 mu_assert("attrval", !strcmp(val, "bla"));
 78                 xmlBlasterFree(val);
 79         }
 80         {
 81                 const char * const xml = "<p k='bla'>blu</p><p k='bla2'>blu2</p>";
 82                 char *val = xmlBlasterExtractAttributeValue(xml, "p", "k");
 83                 mu_assert("attrval0", val != 0);
 84                 printf("%s\n", val);
 85                 mu_assert("attrval", !strcmp(val, "bla"));
 86                 xmlBlasterFree(val);
 87         }
 88 
 89         return 0;
 90 }
 91 
 92 static const char * test_extractTagValue() {
 93         {
 94                 const char * const xml = 0;
 95                 char *val = xmlBlasterExtractTagValue(xml, 0);
 96                 mu_assert("tagval0", val == 0);
 97         }
 98         {
 99                 const char * const xml = "<qos></qos>";
100                 char *val = xmlBlasterExtractTagValue(xml, "sender");
101                 mu_assert("tagval0", val == 0);
102         }
103         {
104                 const char * const xml = "<qos><sender></sender></qos>";
105                 char *val = xmlBlasterExtractTagValue(xml, "sender");
106                 mu_assert("sender check", !strcmp(val, ""));
107                 xmlBlasterFree(val);
108         }
109         {
110                 const char * const xml = "<qos><sender>jack</sender></qos>";
111                 char *val = xmlBlasterExtractTagValue(xml, "sender");
112                 mu_assert("sender check", !strcmp(val, "jack"));
113                 xmlBlasterFree(val);
114         }
115         {
116                 const char * const xml = "<qos><sender>jack&amp;joe</sender></qos>";
117                 char *val = xmlBlasterExtractTagValue(xml, "sender");
118                 mu_assert("sender check", !strcmp(val, "jack&joe"));
119                 xmlBlasterFree(val);
120         }
121         return 0;
122 }
123 
124 static const char * test_extractTagValueWithAttribute() {
125         {
126                 const char * const xml = 0;
127                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", 0);
128                 mu_assert("tagval0", val == 0);
129         }
130         {
131                 const char * const xml = "<p/>";
132                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla");
133                 mu_assert("tagval0", val == 0);
134         }
135         {
136                 const char * const xml = "<p key='bla'></p>";
137                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla");
138                 mu_assert("tagval0", val == 0);
139         }
140         {
141                 const char * const xml = "<p k='bla'>blu</p>";
142                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla");
143                 mu_assert("tagval0", val != 0);
144                 printf("%s\n", val);
145                 mu_assert("tagval", !strcmp(val, "blu"));
146                 xmlBlasterFree(val);
147         }
148         {
149                 const char * const xml = "<p k='bla'/>";
150                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla");
151                 mu_assert("tagval0", val != 0);
152                 printf("%s\n", val);
153                 mu_assert("tagval", !strcmp(val, ""));
154                 xmlBlasterFree(val);
155         }
156         {
157                 const char * const xml = "<p k='bla'></p>";
158                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla");
159                 mu_assert("tagval0", val != 0);
160                 printf("%s\n", val);
161                 mu_assert("tagval", !strcmp(val, ""));
162                 xmlBlasterFree(val);
163         }
164         {
165                 const char * const xml = "<p k='bla1'>blu1</p><p k='bla2'>blu2</p>";
166                 char *val = xmlBlasterExtractTagValueWithAttribute(xml, "p", "k", "bla2");
167                 mu_assert("attrval0", val != 0);
168                 printf("%s\n", val);
169                 mu_assert("attrval", !strcmp(val, "blu2"));
170                 xmlBlasterFree(val);
171         }
172         {
173                 const char * const xml =
174                 "<qos>"
175                 "  <sender>/node/xmlBlaster/client/iphone/session/-562</sender>"
176                 "  <subscribe id='__subId:1224443501105000000'/>"
177                 "  <rcvTimestamp nanos='1224455160443000000'/>"
178                 "  <queue index='0' size='1'/>"
179                 "  <isPublish/>"
180                 "  <clientProperty name='replyTo'>iphone</clientProperty>"
181                 " </qos>";
182                 char *replyTo = xmlBlasterExtractTagValueWithAttribute(xml, "clientProperty", "name", "replyTo");
183                 mu_assert("tagval0", replyTo != 0);
184                 printf("%s\n", replyTo);
185                 mu_assert("tagval0", !strcmp(replyTo, "iphone"));
186                 xmlBlasterFree(replyTo);
187         }
188         return 0;
189 }
190 
191 static const char * test_xmlEscape() {
192         {
193                 char *xml;
194                 int len = 5, newLen;
195                 char *bytes = (char *)malloc(len*sizeof(char));
196                 bytes[0] = '<';
197                 bytes[1] = '<';
198                 bytes[2] = '\0';
199                 bytes[3] = '&';
200                 bytes[4] = '\'';
201                 xml = xmlBlasterEscapeXmlBytes(len, bytes);
202                 printf("%s\n", xml);
203                 mu_assert("xmlEscape", xml != 0);
204                 mu_assert("xmlEscape", !strcmp(xml, "&lt;&lt;&#x0;&amp;&apos;"));
205 
206                 xmlBlasterUnEscapeXml(xml, &newLen);
207                 printf("%s\n", xml);
208                 mu_assert("xmlEscape", newLen == 5);
209                 mu_assert("xmlEscape", xml[0] == '<');
210                 mu_assert("xmlEscape", xml[1] == '<');
211                 mu_assert("xmlEscape", xml[2] == 0);
212                 mu_assert("xmlEscape", xml[3] == '&');
213                 mu_assert("xmlEscape", xml[4] == '\'');
214 
215                 xmlBlasterFree(xml);
216                 free(bytes);
217         }
218         return 0;
219 }
220 
221 static const char * test_xmlUnEscape() {
222         int newLen;
223         {
224                 char *xml;
225                 newLen = 77;
226                 xml = xmlBlasterUnEscapeXml(0, &newLen);
227                 mu_assert("xmlEscape", xml == 0);
228                 mu_assert("xmlEscape", newLen == 0);
229         }
230         {
231                 char *xml = strcpyAlloc("");
232                 xmlBlasterUnEscapeXml(xml, &newLen);
233                 printf("%s\n", xml);
234                 mu_assert("xmlEscape", !strcmp(xml, ""));
235                 mu_assert("xmlEscape", newLen == 0);
236                 xmlBlasterFree(xml);
237         }
238         {
239                 char *xml = strcpyAlloc("&lt;X/&gt;");
240                 xmlBlasterUnEscapeXml(xml, &newLen);
241                 printf("%s\n", xml);
242                 mu_assert("xmlEscape", !strcmp(xml, "<X/>"));
243                 mu_assert("xmlEscape", (size_t)newLen == strlen("<X/>"));
244                 xmlBlasterFree(xml);
245         }
246         {
247                 const char
248                                 * const expected =
249                                                 "<bc ln=\"n6230i\" t=\"all\"><b><ln>dirk</ln><al>dirk</al><st>0</st><pe n=\"gps\" d=\"Show my position\"/><pe n=\"xsms\" d=\"Send mails to me\"/></b></bc>";
250                 char *xml = strcpyAlloc(
251                                 "&lt;bc ln=&quot;n6230i&quot; t=&quot;all&quot;&gt;"
252                                         "&lt;b&gt;"
253                                         "&lt;ln&gt;dirk&lt;/ln&gt;"
254                                         "&lt;al&gt;dirk&lt;/al&gt;"
255                                         "&lt;st&gt;0&lt;/st&gt;"
256                                         "&lt;pe n=&quot;gps&quot; d=&quot;Show my position&quot;/&gt;"
257                                         "&lt;pe n=&quot;xsms&quot; d=&quot;Send mails to me&quot;/&gt;"
258                                         "&lt;/b&gt;"
259                                         "&lt;/bc&gt;");
260                 xmlBlasterUnEscapeXml(xml, &newLen);
261                 printf("%s\n", xml);
262                 mu_assert("xmlEscape", !strcmp(xml, expected));
263                 mu_assert("xmlEscape", (size_t)newLen == strlen(expected));
264                 xmlBlasterFree(xml);
265         }
266         return 0;
267 }
268 
269 
270 #define count 6
271 static const char * test_base64() {
272         int run;
273         const char * binary[count] = { "HelloWorld",       "1",    "22",   "333",  "4444",
274                         "Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, " };
275         const char * base64[count] = { "SGVsbG9Xb3JsZA==", "MQ==", "MjI=", "MzMz", "NDQ0NA==",
276                         "SGVsbG8gV29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8g\r\n"
277             "V29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQs\r\n"
278             "IEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQsIA=="};
279         for (run=0; run<count; run++) {
280                 const char *inP = binary[run];
281                 const char *expectedBase64 = base64[run];
282                 char *encoded = 0;
283                 int lineSize = 60; /* -1; */
284                 char *origP = 0;
285                 int origLen;
286 
287                 encoded = Base64EncodeLen(strlen(inP), inP, lineSize);
288                 mu_assert("base64", encoded != 0);
289                 printf("inP='%s', encoded='%s'\n", inP, encoded);
290                 mu_assert("base64", !strcmp(encoded, expectedBase64));
291 
292 
293                 origP = Base64Decode(encoded, &origLen);
294                 mu_assert("base64", origP != 0);
295                 printf("len=%d, decodedAgain='%s'\n", origLen, origP);
296 
297                 xmlBlasterFree(encoded);
298                 xmlBlasterFree(origP);
299         }
300         return 0;
301 }
302 
303 static const char *all_tests() {
304         mu_run_test(test_base64);
305         mu_run_test(test_strStartsEnds);
306         mu_run_test(test_extractAttributeValue);
307         mu_run_test(test_extractTagValue);
308         mu_run_test(test_extractTagValueWithAttribute);
309         mu_run_test(test_xmlEscape);
310         mu_run_test(test_xmlUnEscape);
311         return 0;
312 }
313 
314 int main(int argc_, char **argv_) {
315         const char *result;
316         /*
317         argc = argc_;
318         argv = argv_;
319         */
320         result = all_tests();
321 
322         if (result != 0) {
323                 printf("%s\n", result);
324         } else {
325                 printf("ALL TESTS PASSED\n");
326         }
327         printf("Tests run: %d\n", tests_run);
328 
329         return result != 0;
330 }


syntax highlighted by Code2HTML, v. 0.9.1