1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <embUnit.h> 19 #include <string.h> 20 #include <stdlib.h> 21 #include "xml2wbxml.h" 22 #include "imps_encoder.h" 23 24 #define DEFINE_TEST(test) \ 25 new_TestFixture(#test, test) 26 27 #define ASSERT_EQUAL_INT(expected, actual) \ 28 { \ 29 int tmp = actual; /* avoid duplicated evaluation in TEST_ASSERT_EQUAL_INT */ \ 30 TEST_ASSERT_EQUAL_INT(expected, tmp); \ 31 } 32 33 #define ASSERT_EQUAL_BINARY_CPPSTRING(expected, cpp_string) \ 34 { \ 35 ASSERT_EQUAL_INT(sizeof(expected), cpp_string.size()); \ 36 ASSERT_EQUAL_INT(0, memcmp(expected, cpp_string.c_str(), sizeof(expected))); \ 37 } 38 39 #define ASSERT_EQUAL_CSTRING_CPPSTRING(expected, cpp_string) \ 40 { \ 41 ASSERT_EQUAL_INT(sizeof(expected) - 1, cpp_string.size()); \ 42 ASSERT_EQUAL_INT(0, memcmp(expected, cpp_string.c_str(), sizeof(expected) - 1)); \ 43 } 44 45 static const char simple_csp12_xml[] = { 46 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 47 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\"></WV-CSP-Message>" 48 }; 49 50 static const char simple_csp12_doctype_xml[] = { 51 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 52 "<!DOCTYPE WV-CSP PUBLIC \"-//OMA//DTD WV-CSP 1.2//EN\" " 53 " \"http://www.openmobilealliance.org/DTD/WV-CSP.DTD\">" 54 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\"></WV-CSP-Message>" 55 }; 56 57 static const char simple_csp12_wbxml[] = { 58 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 0x01 59 }; 60 61 static const char simple2_csp11_xml[] = { 62 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 63 "<WV-CSP-Message xmlns=\"http://www.wireless-village.org/CSP1.1\">" 64 "<Session></Session>" 65 "</WV-CSP-Message>" 66 }; 67 68 static const char simple2_csp11_wbxml[] = { 69 0x03, 0x10, 0x6a, 0x00, 0xc9, 0x05, 0x03, 0x31, 0x2e, 0x31, 0x00, 0x01, 70 0x6d, 0x01, 0x01 71 }; 72 73 static const char simple_content_xml[] = { 74 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 75 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\">" 76 " <Session>" 77 " <SessionDescriptor>" 78 " <SessionType>Outband</SessionType>" 79 " </SessionDescriptor>" 80 " </Session>" 81 "</WV-CSP-Message>" 82 }; 83 84 static const char simple_content_wbxml[] = { 85 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 86 0x6d, 0x6e, 0x70, 0x80, 0x19, 0x01, 0x01, 0x01, 0x01 87 }; 88 89 // libwbxml2 generates 0xC3 0x00 for integer value 0, but we take the safer 90 // way 0xC3 0x01 0x00 91 static const char simple_integer_xml[] = { 92 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 93 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\">" 94 "<Session>" 95 " <ClientID>9527</ClientID>" 96 " <Result>" 97 " <Code>401</Code>" 98 " <Description>401</Description>" 99 " </Result>" 100 " <ContentSize>4294967295</ContentSize>" 101 " <MessageCount>0</MessageCount>" 102 "</Session>" 103 "</WV-CSP-Message>" 104 }; 105 static const char simple_integer_wbxml[] = { 106 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 107 0x6d, 0x4a, 0x03, 0x39, 0x35, 0x32, 0x37, 0x00, 0x01, 0x6a, 0x4b, 0xc3, 108 0x02, 0x01, 0x91, 0x01, 0x52, 0x03, 0x34, 0x30, 0x31, 0x00, 0x01, 0x01, 109 0x4f, 0xc3, 0x04, 0xff, 0xff, 0xff, 0xff, 0x01, 0x5a, 0xc3, 0x01, 0x00, 110 0x01, 0x01, 0x01 111 }; 112 113 static const char simple_datetime_xml[] = { 114 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 115 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\">" 116 "<Session>" 117 " <DateTime>20010925T165859Z</DateTime>" 118 " <DeliveryTime>20010925T165859Z</DeliveryTime>" 119 "</Session>" 120 "</WV-CSP-Message>" 121 }; 122 static const char simple_datetime_wbxml[] = { 123 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 124 0x6d, 0x51, 0xc3, 0x06, 0x1f, 0x46, 0x73, 0x0e, 0xbb, 0x5a, 0x01, 0x00, 125 0x06, 0x5a, 0xc3, 0x06, 0x1f, 0x46, 0x73, 0x0e, 0xbb, 0x5a, 0x01, 0x01, 126 0x01 127 }; 128 129 static const char simple_switch_page_xml[] = { 130 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 131 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\">" 132 "<Session>" 133 " <Login-Response>" 134 " <ClientID>Esmertec112233</ClientID>" 135 " <Result>" 136 " <Description>blahblah</Description>" 137 " </Result>" 138 " </Login-Response>" 139 "</Session>" 140 "</WV-CSP-Message>" 141 }; 142 static const char simple_switch_page_wbxml[] = { 143 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 144 0x6d, 0x00, 0x01, 0x5e, 0x00, 0x00, 0x4a, 0x03, 0x45, 0x73, 0x6d, 0x65, 145 0x72, 0x74, 0x65, 0x63, 0x31, 0x31, 0x32, 0x32, 0x33, 0x33, 0x00, 0x01, 146 0x6a, 0x52, 0x03, 0x62, 0x6c, 0x61, 0x68, 0x62, 0x6c, 0x61, 0x68, 0x00, 147 0x01, 0x01, 0x01, 0x01, 0x01 148 }; 149 150 /* 151 <?xml version="1.0" encoding="UTF-8" ?> 152 <WV-CSP-Message xmlns="http://www.wireless-village.org/CSP1.1"> 153 <Session> 154 <SessionDescriptor> 155 <SessionType></SessionType> 156 </SessionDescriptor> 157 </Session> 158 </WV-CSP-Message> 159 */ 160 /*  = U+10ffff = mb_u_int32 C3 ff 7f */ 161 static const char big_entity_wbxml[] = { 162 0x03, 0x10, 0x6a, 0x00, 0xc9, 0x05, 0x03, 0x31, 0x2e, 0x31, 0x00, 0x01, 163 0x6d, 0x6e, 0x70, 0x02, 0xc3, 0xff, 0x7f, 0x01, 0x01, 0x01, 0x01 164 }; 165 166 /* 167 <?xml version="1.0" encoding="UTF-8" ?> 168 <WV-CSP-Message xmlns="http://www.wireless-village.org/CSP1.1"> 169 <Session> 170 <SessionDescriptor> 171 <SessionType>�</SessionType> 172 </SessionDescriptor> 173 </Session> 174 </WV-CSP-Message> 175 */ 176 /* � = 0x110000 = mb_u_int32 C4 80 00 */ 177 static const char invalid_entity_wbxml[] = { 178 0x03, 0x10, 0x6a, 0x00, 0xc9, 0x05, 0x03, 0x31, 0x2e, 0x31, 0x00, 0x01, 179 0x6d, 0x6e, 0x70, 0x02, 0xc4, 0x80, 0x00, 0x01, 0x01, 0x01, 0x01 180 }; 181 182 static const char cap_request_xml[] = { 183 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 184 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/WV-CSP1.2\">" 185 " <Session>" 186 " <SessionDescriptor>" 187 " <SessionType>Inband</SessionType>" 188 " <SessionID>196FE717.00000000.test1</SessionID>" 189 " </SessionDescriptor>" 190 " <Transaction>" 191 " <TransactionDescriptor>" 192 " <TransactionMode>Request</TransactionMode>" 193 " <TransactionID>transId2</TransactionID>" 194 " </TransactionDescriptor>" 195 " <TransactionContent xmlns=\"http://www.openmobilealliance.org/DTD/WV-TRC1.2\">" 196 " <ClientCapability-Request>" 197 " <CapabilityList>" 198 " <ClientType>MOBILE_PHONE</ClientType>" 199 " <ParserSize>32767</ParserSize>" 200 " <MultiTrans>1</MultiTrans>" 201 " <InitialDeliveryMethod>P</InitialDeliveryMethod>" 202 " <ServerPollMin>2</ServerPollMin>" 203 " <SupportedBearer>HTTP</SupportedBearer>" 204 " <SupportedCIRMethod>STCP</SupportedCIRMethod>" 205 " <SupportedCIRMethod>SHTTP</SupportedCIRMethod>" 206 " <SupportedCIRMethod>SSMS</SupportedCIRMethod>" 207 " </CapabilityList>" 208 " </ClientCapability-Request>" 209 " </TransactionContent>" 210 " </Transaction>" 211 " </Session>" 212 "</WV-CSP-Message>" 213 }; 214 static const char cap_request_wbxml[] = { 215 0x03, 0x11, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 216 0x6d, 0x6e, 0x70, 0x80, 0x11, 0x01, 0x6f, 0x03, 0x31, 0x39, 0x36, 0x46, 217 0x45, 0x37, 0x31, 0x37, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 218 0x30, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x31, 0x00, 0x01, 0x01, 0x72, 0x74, 219 0x76, 0x80, 0x20, 0x01, 0x75, 0x03, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x49, 220 0x64, 0x32, 0x00, 0x01, 0x01, 0xf3, 0x0a, 0x03, 0x31, 0x2e, 0x32, 0x00, 221 0x01, 0x00, 0x01, 0x4c, 0x4a, 0x00, 0x05, 0x4f, 0x80, 0x6f, 0x01, 0x00, 222 0x03, 0x4d, 0xc3, 0x02, 0x7f, 0xff, 0x01, 0x4c, 0xc3, 0x01, 0x01, 0x01, 223 0x4b, 0x80, 0x1f, 0x01, 0x4e, 0xc3, 0x01, 0x02, 0x01, 0x4f, 0x80, 0x42, 224 0x01, 0x50, 0x80, 0x44, 0x01, 0x50, 0x80, 0x81, 0x25, 0x01, 0x50, 0x80, 225 0x81, 0x24, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 226 }; 227 228 static const char statusprim_xml[] = { 229 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" 230 "<WV-CSP-Message xmlns=\"http://www.openmobilealliance.org/DTD/IMPS-CSP1.3\">" 231 " <Session>" 232 " <SessionDescriptor>" 233 " <SessionType>Inband</SessionType>" 234 " <SessionID>im.user.com#48815 (at) server.com</SessionID>" 235 " </SessionDescriptor>" 236 " <Transaction>" 237 " <TransactionDescriptor>" 238 " <TransactionMode>Response</TransactionMode>" 239 " <TransactionID>IMApp01#12345@NOK5110</TransactionID>" 240 " </TransactionDescriptor>" 241 " <TransactionContent xmlns=\"http://www.openmobilealliance.org/DTD/IMPS-TRC1.3\">" 242 " <Status>" 243 " <Result>" 244 " <Code>201</Code>" 245 " <Description>Partially successful.</Description>" 246 " <DetailedResult>" 247 " <Code>531</Code>" 248 " <Description>Unknown user.</Description>" 249 " <UserID>wv:bad_user1 (at) im.com</UserID>" 250 " <UserID>wv:bad_user2 (at) im.com</UserID>" 251 " </DetailedResult>" 252 " <DetailedResult>" 253 " <Code>532</Code>" 254 " <Description>Blocked.</Description>" 255 " <UserID>wv:bad_user3 (at) im.com</UserID>" 256 " <UserID>wv:bad_user4 (at) im.com</UserID>" 257 " </DetailedResult>" 258 " </Result>" 259 " </Status>" 260 " </TransactionContent>" 261 " </Transaction>" 262 " <Poll>F</Poll>" 263 " </Session>" 264 "</WV-CSP-Message>" 265 }; 266 267 static const char statusprim_wbxml[] = { 268 0x03, 0x12, 0x6a, 0x00, 0xc9, 0x0b, 0x03, 0x31, 0x2e, 0x33, 0x00, 0x01, 269 0x6d, 0x6e, 0x70, 0x80, 0x11, 0x01, 0x6f, 0x03, 0x69, 0x6d, 0x2e, 0x75, 270 0x73, 0x65, 0x72, 0x2e, 0x63, 0x6f, 0x6d, 0x23, 0x34, 0x38, 0x38, 0x31, 271 0x35, 0x40, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e, 0x63, 0x6f, 0x6d, 272 0x00, 0x01, 0x01, 0x72, 0x74, 0x76, 0x80, 0x21, 0x01, 0x75, 0x03, 0x49, 273 0x4d, 0x41, 0x70, 0x70, 0x30, 0x31, 0x23, 0x31, 0x32, 0x33, 0x34, 0x35, 274 0x40, 0x4e, 0x4f, 0x4b, 0x35, 0x31, 0x31, 0x30, 0x00, 0x01, 0x01, 0xf3, 275 0x0d, 0x03, 0x31, 0x2e, 0x33, 0x00, 0x01, 0x71, 0x6a, 0x4b, 0xc3, 0x01, 276 0xc9, 0x01, 0x52, 0x03, 0x50, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x6c, 277 0x79, 0x20, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x66, 0x75, 0x6c, 278 0x2e, 0x00, 0x01, 0x53, 0x4b, 0xc3, 0x02, 0x02, 0x13, 0x01, 0x52, 0x03, 279 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x75, 0x73, 0x65, 0x72, 280 0x2e, 0x00, 0x01, 0x7a, 0x03, 0x77, 0x76, 0x3a, 0x62, 0x61, 0x64, 0x5f, 281 0x75, 0x73, 0x65, 0x72, 0x31, 0x40, 0x69, 0x6d, 0x2e, 0x63, 0x6f, 0x6d, 282 0x00, 0x01, 0x7a, 0x03, 0x77, 0x76, 0x3a, 0x62, 0x61, 0x64, 0x5f, 0x75, 283 0x73, 0x65, 0x72, 0x32, 0x40, 0x69, 0x6d, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 284 0x01, 0x01, 0x53, 0x4b, 0xc3, 0x02, 0x02, 0x14, 0x01, 0x52, 0x03, 0x42, 285 0x6c, 0x6f, 0x63, 0x6b, 0x65, 0x64, 0x2e, 0x00, 0x01, 0x7a, 0x03, 0x77, 286 0x76, 0x3a, 0x62, 0x61, 0x64, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x33, 0x40, 287 0x69, 0x6d, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x01, 0x7a, 0x03, 0x77, 0x76, 288 0x3a, 0x62, 0x61, 0x64, 0x5f, 0x75, 0x73, 0x65, 0x72, 0x34, 0x40, 0x69, 289 0x6d, 0x2e, 0x63, 0x6f, 0x6d, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 290 0x61, 0x80, 0x0b, 0x01, 0x01, 0x01 291 }; 292 293 /* 294 <?xml version="1.0" encoding="UTF-8" ?> 295 <WV-CSP-Message xmlns="http://www.openmobilealliance.org/DTD/WV-CSP1.2"> 296 <Session> 297 <SessionDescriptor> 298 <SessionType>Inband</SessionType> 299 <SessionID>AA0BD762.00000000.test1</SessionID> 300 </SessionDescriptor> 301 <Transaction> 302 <TransactionDescriptor> 303 <TransactionMode>Request</TransactionMode> 304 <TransactionID>transId8</TransactionID> 305 </TransactionDescriptor> 306 <TransactionContent xmlns="http://www.openmobilealliance.org/DTD/WV-TRC1.2"> 307 <SendMessage-Request> 308 <DeliveryReport>T</DeliveryReport> 309 <MessageInfo> 310 <Recipient> 311 <User> 312 <UserID>wv:test2</UserID> 313 </User> 314 </Recipient> 315 <Sender> 316 <User> 317 <UserID>wv:test1</UserID> 318 </User> 319 </Sender> 320 <DateTime>20070625T055652Z</DateTime> 321 <ContentSize>5</ContentSize> 322 </MessageInfo> 323 <ContentData>Today  Today</ContentData> 324 </SendMessage-Request> 325 </TransactionContent> 326 </Transaction> 327 </Session> 328 </WV-CSP-Message> 329 */ 330 static const char sendmsgreq1_wbxml[] = { 331 0x03, 0x10, 0x6a, 0x00, 0xc9, 0x08, 0x03, 0x31, 0x2e, 0x32, 0x00, 0x01, 332 0x6d, 0x6e, 0x70, 0x80, 0x11, 0x01, 0x6f, 0x03, 0x41, 0x41, 0x30, 0x42, 333 0x44, 0x37, 0x36, 0x32, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 334 0x30, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x31, 0x00, 0x01, 0x01, 0x72, 0x74, 335 0x76, 0x80, 0x20, 0x01, 0x75, 0x03, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x49, 336 0x64, 0x38, 0x00, 0x01, 0x01, 0xf3, 0x0a, 0x03, 0x31, 0x2e, 0x32, 0x00, 337 0x01, 0x00, 0x06, 0x57, 0x48, 0x80, 0x2c, 0x01, 0x53, 0x00, 0x00, 0x67, 338 0x79, 0x7a, 0x03, 0x77, 0x76, 0x3a, 0x74, 0x65, 0x73, 0x74, 0x32, 0x00, 339 0x01, 0x01, 0x01, 0x6c, 0x79, 0x7a, 0x03, 0x77, 0x76, 0x3a, 0x74, 0x65, 340 0x73, 0x74, 0x31, 0x00, 0x01, 0x01, 0x01, 0x51, 0x03, 0x32, 0x30, 0x30, 341 0x37, 0x30, 0x36, 0x32, 0x35, 0x54, 0x30, 0x35, 0x35, 0x36, 0x35, 0x32, 342 0x5a, 0x00, 0x01, 0x4f, 0xc3, 0x01, 0x05, 0x01, 0x01, 0x4d, 0x03, 0x54, 343 0x6f, 0x64, 0x61, 0x79, 0x20, 0x00, 0x02, 0x81, 0x20, 0x03, 0x54, 0x6f, 344 0x64, 0x61, 0x79, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 345 }; 346 347 class DataHandler : public WbxmlHandler 348 { 349 public: 350 void wbxmlData(const char *data, uint32_t len) 351 { 352 wbxml.append(data, len); 353 #if 0 354 for (uint32_t i = 0; i < len; i++) { 355 if (!(i % 12)) 356 printf ("\n"); 357 printf ("0x%02x, ", (unsigned char)data[i]); 358 } 359 printf ("\n"); 360 #endif 361 } 362 string wbxml; 363 }; 364 365 static void setUp(void) 366 { 367 } 368 369 static void tearDown(void) 370 { 371 } 372 373 static void testSmokeTest(void) 374 { 375 Xml2WbxmlEncoder encoder; 376 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_csp12_xml, 377 strlen(simple_csp12_xml), true)); 378 379 Xml2WbxmlEncoder encoder1; 380 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder1.encode(simple_csp12_doctype_xml, 381 strlen(simple_csp12_doctype_xml), true)); 382 } 383 384 static void testSimpleCsp12(void) 385 { 386 Xml2WbxmlEncoder encoder; 387 DataHandler handler; 388 encoder.setWbxmlHandler(&handler); 389 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_csp12_doctype_xml, 390 strlen(simple_csp12_doctype_xml), true)); 391 ASSERT_EQUAL_BINARY_CPPSTRING(simple_csp12_wbxml, handler.wbxml); 392 393 Xml2WbxmlEncoder encoder1; 394 DataHandler handler1; 395 encoder1.setWbxmlHandler(&handler1); 396 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder1.encode(simple_csp12_xml, 397 strlen(simple_csp12_xml), true)); 398 ASSERT_EQUAL_BINARY_CPPSTRING(simple_csp12_wbxml, handler.wbxml); 399 } 400 401 static void testSimpleCsp11Level2(void) 402 { 403 Xml2WbxmlEncoder encoder; 404 DataHandler handler; 405 encoder.setWbxmlHandler(&handler); 406 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple2_csp11_xml, 407 strlen(simple2_csp11_xml), true)); 408 ASSERT_EQUAL_BINARY_CPPSTRING(simple2_csp11_wbxml, handler.wbxml); 409 } 410 411 static void testSimpleContent(void) 412 { 413 Xml2WbxmlEncoder encoder; 414 DataHandler handler; 415 encoder.setWbxmlHandler(&handler); 416 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_content_xml, 417 strlen(simple_content_xml), true)); 418 ASSERT_EQUAL_BINARY_CPPSTRING(simple_content_wbxml, handler.wbxml); 419 } 420 421 static void testSimpleOpaqueInteger(void) 422 { 423 Xml2WbxmlEncoder encoder; 424 DataHandler handler; 425 encoder.setWbxmlHandler(&handler); 426 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_integer_xml, 427 strlen(simple_integer_xml), true)); 428 ASSERT_EQUAL_BINARY_CPPSTRING(simple_integer_wbxml, handler.wbxml); 429 } 430 431 static void testSimpleOpaqueDatetime(void) 432 { 433 Xml2WbxmlEncoder encoder; 434 DataHandler handler; 435 encoder.setWbxmlHandler(&handler); 436 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_datetime_xml, 437 strlen(simple_datetime_xml), true)); 438 ASSERT_EQUAL_BINARY_CPPSTRING(simple_datetime_wbxml, handler.wbxml); 439 } 440 441 static void testSimpleSwitchPage(void) 442 { 443 Xml2WbxmlEncoder encoder; 444 DataHandler handler; 445 encoder.setWbxmlHandler(&handler); 446 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(simple_switch_page_xml, 447 strlen(simple_switch_page_xml), true)); 448 ASSERT_EQUAL_BINARY_CPPSTRING(simple_switch_page_wbxml, handler.wbxml); 449 } 450 451 /* various token values, EXT_0 + mbuint32 */ 452 static void testCapRequest(void) 453 { 454 Xml2WbxmlEncoder encoder; 455 DataHandler handler; 456 encoder.setWbxmlHandler(&handler); 457 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(cap_request_xml, 458 strlen(cap_request_xml), true)); 459 ASSERT_EQUAL_BINARY_CPPSTRING(cap_request_wbxml, handler.wbxml); 460 } 461 462 static void testEncodeInChunk(void) 463 { 464 Xml2WbxmlEncoder encoder; 465 DataHandler handler; 466 encoder.setWbxmlHandler(&handler); 467 468 int chunkSize = strlen(statusprim_xml) / 4; 469 470 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(statusprim_xml, 471 chunkSize, false)); 472 473 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(statusprim_xml + chunkSize, 474 chunkSize, false)); 475 476 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(statusprim_xml + chunkSize * 2, 477 chunkSize, false)); 478 479 ASSERT_EQUAL_INT(WBXML_STATUS_OK, encoder.encode(statusprim_xml + chunkSize * 3, 480 strlen(statusprim_xml) - chunkSize * 3, true)); 481 482 ASSERT_EQUAL_BINARY_CPPSTRING(statusprim_wbxml, handler.wbxml); 483 } 484 485 static void testImpsEncoderSmokeTest(void) 486 { 487 ImpsWbxmlEncoder encoder(PUBLICID_IMPS_1_3); 488 DataHandler handler; 489 encoder.setWbxmlHandler(&handler); 490 const char * atts[] = {NULL}; 491 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("WV-CSP-Message", atts)); 492 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 493 ASSERT_EQUAL_INT(6, handler.wbxml.size()); 494 ASSERT_EQUAL_CSTRING_CPPSTRING("\x03\x12\x6a\x00\x49\x01", handler.wbxml); 495 } 496 497 static void testImpsEncoderReset(void) 498 { 499 ImpsWbxmlEncoder encoder(PUBLICID_IMPS_1_3); 500 DataHandler handler; 501 encoder.setWbxmlHandler(&handler); 502 const char * atts[] = {NULL}; 503 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("WV-CSP-Message", atts)); 504 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 505 ASSERT_EQUAL_INT(6, handler.wbxml.size()); 506 ASSERT_EQUAL_CSTRING_CPPSTRING("\x03\x12\x6a\x00\x49\x01", handler.wbxml); 507 508 encoder.reset(); 509 handler.wbxml.clear(); 510 511 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("WV-CSP-Message", atts)); 512 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("Session", atts)); 513 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 514 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 515 ASSERT_EQUAL_INT(8, handler.wbxml.size()); 516 ASSERT_EQUAL_CSTRING_CPPSTRING("\x03\x12\x6a\x00\x49\x6d\x01\x01", handler.wbxml); 517 } 518 519 static void testImpsStringContentData(void) 520 { 521 ImpsWbxmlEncoder encoder(PUBLICID_IMPS_1_3); 522 DataHandler handler; 523 encoder.setWbxmlHandler(&handler); 524 const char * atts[] = {NULL}; 525 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("ContentData", atts)); 526 ASSERT_EQUAL_INT(NO_ERROR, encoder.characters("Hello", 5)); 527 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 528 ASSERT_EQUAL_INT(13, handler.wbxml.size()); 529 ASSERT_EQUAL_CSTRING_CPPSTRING("\x03\x12\x6a\x00\x4d\x03Hello\x00\x01", handler.wbxml); 530 } 531 532 static void testImpsOpaqueContentData(void) 533 { 534 ImpsWbxmlEncoder encoder(PUBLICID_IMPS_1_3); 535 DataHandler handler; 536 encoder.setWbxmlHandler(&handler); 537 const char * atts[] = {NULL}; 538 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("ContentData", atts)); 539 ASSERT_EQUAL_INT(NO_ERROR, encoder.opaque("Hello", 5)); 540 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 541 ASSERT_EQUAL_INT(13, handler.wbxml.size()); 542 ASSERT_EQUAL_CSTRING_CPPSTRING("\x03\x12\x6a\x00\x4d\xc3\x05Hello\x01", handler.wbxml); 543 544 encoder.reset(); 545 handler.wbxml.clear(); 546 547 const int dataSize = 500000; // 7a120, MB uint32 "\x9e\xc2\x20" 548 char * data = new char[dataSize]; 549 memset(data, 7, dataSize); 550 551 ASSERT_EQUAL_INT(NO_ERROR, encoder.startElement("ContentData", atts)); 552 ASSERT_EQUAL_INT(NO_ERROR, encoder.opaque(data,dataSize)); 553 ASSERT_EQUAL_INT(NO_ERROR, encoder.endElement()); 554 555 ASSERT_EQUAL_INT(dataSize + 9 + 1, handler.wbxml.size()); 556 ASSERT_EQUAL_INT(0, memcmp("\x03\x12\x6a\x00\x4d\xc3\x9e\xc2\x20", handler.wbxml.c_str(), 9)); 557 ASSERT_EQUAL_INT(0, memcmp(data, handler.wbxml.c_str() + 9, dataSize)); 558 ASSERT_EQUAL_INT(0x01, handler.wbxml[9 + dataSize]); 559 delete [] data; 560 } 561 562 #if 0 563 564 /* OPAQUE integer */ 565 static void testCspStatusPrim(void) 566 { 567 WbxmlParser parser(0); 568 DomTestHandler handler; 569 parser.setContentHandler(&handler); 570 ASSERT_EQUAL_INT(WBXML_STATUS_OK, parser.parse(statusprim_wbxml, 571 sizeof(statusprim_wbxml), true)); 572 573 Element &root = handler.root; 574 TEST_ASSERT_EQUAL_STRING("WV-CSP-Message", root.name.c_str()); 575 TEST_ASSERT_EQUAL_INT(1, root.attribs.size()); 576 TEST_ASSERT_EQUAL_STRING("xmlns", root.attribs[0].name.c_str()); 577 TEST_ASSERT_EQUAL_STRING("http://www.openmobilealliance.org/DTD/IMPS-CSP1.3", 578 root.attribs[0].value.c_str()); 579 580 Element &transacContent = root.children[0].children[1].children[1]; 581 TEST_ASSERT_EQUAL_STRING("TransactionContent", transacContent.name.c_str()); 582 TEST_ASSERT_EQUAL_INT(1, transacContent.attribs.size()); 583 TEST_ASSERT_EQUAL_STRING("xmlns", transacContent.attribs[0].name.c_str()); 584 TEST_ASSERT_EQUAL_STRING("http://www.openmobilealliance.org/DTD/IMPS-TRC1.3", 585 transacContent.attribs[0].value.c_str()); 586 587 Element &resultCode = transacContent.children[0].children[0].children[0]; 588 TEST_ASSERT_EQUAL_STRING("Code", resultCode.name.c_str()); 589 TEST_ASSERT_EQUAL_INT(0, resultCode.attribs.size()); 590 TEST_ASSERT_EQUAL_INT(201, resultCode.intVal); 591 592 Element &detailedResultCode = transacContent.children[0].children[0].children[2].children[0]; 593 TEST_ASSERT_EQUAL_STRING("Code", detailedResultCode.name.c_str()); 594 TEST_ASSERT_EQUAL_INT(0, detailedResultCode.attribs.size()); 595 TEST_ASSERT_EQUAL_INT(531, detailedResultCode.intVal); 596 } 597 598 /* SWITCH_PAGE, OPAQUE integer, ENTITY */ 599 static void testCspSendMsg(void) 600 { 601 WbxmlParser parser(0); 602 DomTestHandler handler; 603 parser.setContentHandler(&handler); 604 ASSERT_EQUAL_INT(WBXML_STATUS_OK, parser.parse(sendmsgreq1_wbxml, 605 sizeof(sendmsgreq1_wbxml), true)); 606 607 Element &root = handler.root; 608 Element &transacContent = root.children[0].children[1].children[1]; 609 Element &contentData = transacContent.children[0].children[2]; 610 611 TEST_ASSERT_EQUAL_STRING("ContentData", contentData.name.c_str()); 612 TEST_ASSERT_EQUAL_INT(0, contentData.attribs.size()); 613 //   => U+00A0, UTF-8 0xC2 0xA0 614 TEST_ASSERT_EQUAL_STRING("Today \xc2\xa0Today", contentData.characters.c_str()); 615 } 616 617 /* more ENTITY tests */ 618 static void testBigEntity(void) 619 { 620 WbxmlParser parser(0); 621 DomTestHandler handler; 622 parser.setContentHandler(&handler); 623 ASSERT_EQUAL_INT(WBXML_STATUS_OK, parser.parse(big_entity_wbxml, 624 sizeof(big_entity_wbxml), true)); 625 626 Element &sessionType = handler.root.children[0].children[0].children[0]; 627 TEST_ASSERT_EQUAL_INT(0, sessionType.children.size()); 628 TEST_ASSERT_EQUAL_STRING("SessionType", sessionType.name.c_str()); 629 TEST_ASSERT_EQUAL_INT(0, sessionType.attribs.size()); 630 TEST_ASSERT_EQUAL_STRING("\xf4\x8f\xbf\xbf", sessionType.characters.c_str()); 631 } 632 633 static void testInvalidEntity(void) 634 { 635 WbxmlParser parser(0); 636 ASSERT_EQUAL_INT(WBXML_STATUS_ERROR, parser.parse(invalid_entity_wbxml, 637 sizeof(invalid_entity_wbxml), true)); 638 } 639 640 static void testIncompleteData(void) 641 { 642 WbxmlParser parser(0); 643 ASSERT_EQUAL_INT(WBXML_STATUS_ERROR, parser.parse(sendmsgreq1_wbxml, 644 1, true)); 645 646 WbxmlParser parser1(0); 647 ASSERT_EQUAL_INT(WBXML_STATUS_ERROR, parser1.parse(sendmsgreq1_wbxml, 648 3, true)); 649 650 WbxmlParser parser2(0); 651 ASSERT_EQUAL_INT(WBXML_STATUS_ERROR, parser2.parse(sendmsgreq1_wbxml, 652 4, true)); 653 654 WbxmlParser parser3(0); 655 ASSERT_EQUAL_INT(WBXML_STATUS_ERROR, parser3.parse(sendmsgreq1_wbxml, 656 5, true)); 657 } 658 #endif 659 660 extern "C" TestRef ImpsEncoderTest_tests(void) 661 { 662 EMB_UNIT_TESTFIXTURES(fixtures) { 663 DEFINE_TEST(testSmokeTest), 664 DEFINE_TEST(testSimpleCsp12), 665 DEFINE_TEST(testSimpleCsp11Level2), 666 DEFINE_TEST(testSimpleContent), 667 DEFINE_TEST(testSimpleOpaqueInteger), 668 DEFINE_TEST(testSimpleOpaqueDatetime), 669 DEFINE_TEST(testSimpleSwitchPage), 670 DEFINE_TEST(testCapRequest), 671 DEFINE_TEST(testEncodeInChunk), 672 DEFINE_TEST(testImpsEncoderSmokeTest), 673 DEFINE_TEST(testImpsEncoderReset), 674 DEFINE_TEST(testImpsStringContentData), 675 DEFINE_TEST(testImpsOpaqueContentData), 676 }; 677 EMB_UNIT_TESTCALLER(ImpsEncoderTest, "ImpsEncoderTest", setUp, tearDown, fixtures); 678 679 // temporary work around for the linker/loader problem of the sooner build 680 static TestCaller t = ImpsEncoderTest; 681 t.isa = (TestImplement *)&TestCallerImplement; 682 return (TestRef)&t; 683 } 684 685