00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <fcntl.h>
00023
00024 #include <stdlib.h>
00025 #include <wchar.h>
00026 #include <string.h>
00027 #include <libxml/parser.h>
00028 #include <libxml/tree.h>
00029
00030 #include <sstream>
00031 #include <iostream>
00032 #include <string>
00033
00034 #include "binary_msgs.h"
00035 #include "secstwo_serialize.h"
00036
00037 #include "test_defines.h"
00038
00039
00040
00041 static std::stringstream s3f17_contents_str, s14f9_contents_str;
00042 static std::stringstream s3f17_xml_contents, s14f9_xml_contents;
00043 static std::stringstream s3f17_str, s14f9_str;
00044 static std::stringstream s3f17_xml_str, s14f9_xml_str;
00045
00046 const char unicode_item_xml_str[] = "<unicode name=\"wchar item\">юникодовая строка</unicode>";
00047 const char unicode_item_str[] = "<WCHAR \'wchar item\' [33] юникодовая строка>";
00048
00049 using namespace freesecs;
00050 using namespace freesecs::secstwo;
00051
00052 pdata_item_t pit1, pit2;
00053 pdata_item_t xml_pit1, xml_pit2;
00054
00055 #include <fstream>
00056
00057 void test_binary_deserializer(void)
00058 {
00059 try
00060 {
00061 binary_deserializer_t bs1(s3f17_data, s3f17_data + sizeof(s3f17_data));
00062 pit1 = bs1(NULL);
00063 }
00064 catch(...)
00065 {
00066 TEST_ASSERT(0, "binary deserializer creation");
00067 }
00068
00069 try
00070 {
00071 binary_deserializer_t bs2(s14f9_data, s14f9_data + sizeof(s14f9_data));
00072 pit2 = bs2(NULL);
00073 }
00074 catch(...)
00075 {
00076 TEST_ASSERT(0, "binary deserializer creation");
00077 }
00078
00079 TEST_ASSERT(0 != pit1.get(), "deserialized msg itam1 is not null");
00080 TEST_ASSERT(0 != pit2.get(), "deserialized msg item2 is not null");
00081
00082
00083 pit1->print(s3f17_contents_str);
00084 pit2->print(s14f9_contents_str);
00085
00086 TEST_ASSERT(0 != strlen(s3f17_contents_str.str().c_str()), "s3f17 printout not empty");
00087 TEST_ASSERT(0 != strlen(s14f9_contents_str.str().c_str()), "s14f9 printout not empty");
00088
00089 uint16_t i1 = (*pit1)[NUM(0)];
00090 std::string str1 = (*pit1)[2];
00091 std::string str2 = (*pit1)[4][4][NUM(0)];
00092 std::string str3 = (*pit2)[1];
00093 std::string str4 = (*pit2)[2][1][NUM(0)];
00094 uint8_t i2 = (*pit2)[2][3][1][NUM(0)][NUM(0)][1][NUM(0)];
00095
00096
00097 TEST_ASSERT_STRINGS_EQUAL("CARRIER_00000001", str1.c_str(), "s3f17 data check 1");
00098 TEST_ASSERT(1 == i1, "s3f17 data check 2");
00099 TEST_ASSERT_STRINGS_EQUAL("ContentMap", str2.c_str(), "s3f17 data check 3");
00100
00101 TEST_ASSERT_STRINGS_EQUAL("ControlJob", str3.c_str(), "s14f9 data check 1");
00102 TEST_ASSERT_STRINGS_EQUAL("DataCollectionPlan", str4.c_str(), "s14f9 data check 2");
00103 TEST_ASSERT(1 == i2, "s14f9 data check 3");
00104
00105 }
00107 void test_binary_serializer(void)
00108 {
00109 TEST_ASSERT(pit2.get(), "s14f9 msg item valid");
00110
00111 pit2->print(std::cout);
00112
00113 binary_serializer_t bs3(pit2);
00114
00115 binary_serializer_t::data_container_t raw_data = bs3;
00116
00117 printf("%s<%d>: raw data size %d, expected %d\n",__FUNCTION__,__LINE__,raw_data.size(), sizeof(s14f9_data));
00118
00119 TEST_ASSERT(raw_data.size() == sizeof(s14f9_data), "binary data size check");
00120
00121 TEST_ASSERT(0 == memcmp(&raw_data[0], s14f9_data, sizeof(s14f9_data)), "binary data check");
00122 }
00123
00124
00126 void test_xml_seriaizer(void)
00127 {
00128 pmsg_t s3f17_msg, s14f9_msg;
00129 try
00130 {
00131 xml_serializer_t xmls1(pit1);
00132 xml_serializer_t xmls2(pit2);
00133 s3f17_xml_contents << xmls1.get();
00134 s14f9_xml_contents << xmls2.get();
00135 }
00136 catch(...)
00137 {
00138 TEST_ASSERT(0, "xml serializer creation");
00139 }
00140
00141 TEST_ASSERT(0 != strlen(s3f17_xml_contents.str().c_str()), "s3f17 xml data contents not empty");
00142 TEST_ASSERT(0 != strlen(s14f9_xml_contents.str().c_str()), "s14f9 xml data contents not empty");
00143 }
00145 void test_xml_deserializer()
00146 {
00147 std::stringstream s3f17_out, s14f9_out;
00148
00149 try
00150 {
00151 std::string s3f17 = s3f17_xml_contents.str();
00152 std::string s14f9 = s14f9_xml_contents.str();
00153
00154 xmlDocPtr doc1 = xmlParseMemory(s3f17.c_str(), strlen(s3f17.c_str()));
00155 xmlDocPtr doc2 = xmlParseMemory(s14f9.c_str(), strlen(s14f9.c_str()));
00156
00157 xml_deserializer_t xmlds1(xmlDocGetRootElement(doc1));
00158 xml_deserializer_t xmlds2(xmlDocGetRootElement(doc2));
00159
00160 xml_pit1 = xmlds1;
00161 xml_pit2 = xmlds2;
00162
00163 xmlFreeDoc(doc1);
00164 xmlFreeDoc(doc2);
00165 }
00166 catch(...)
00167 {
00168 TEST_ASSERT(0, "xml deserializer creation");
00169 }
00170
00171 xml_pit1->print(s3f17_out);
00172 xml_pit2->print(s14f9_out);
00173
00174 TEST_ASSERT_STRINGS_EQUAL(s3f17_out.str().c_str(), s3f17_contents_str.str().c_str(), "xml-deserialized s3f17 msg item");
00175 TEST_ASSERT_STRINGS_EQUAL(s14f9_out.str().c_str(), s14f9_contents_str.str().c_str(), "xml-deserialized s14f9 msg item");
00176 }
00178 void test_wchar_to_xml_serialization(void)
00179 {
00180 #if 0
00181 std::stringstream orig, restored, restored_from_xml;
00182 pdata_item_t wit, wit1;
00183 binary_serializer_t::data_container_t wraw_data;
00184 const wchar_t *wstring = L"юникодовая строка";
00185 wit = new wchar_item_t("wchar item", wstring);
00186 try
00187 {
00188 binary_serializer_t wbs(wit);
00189 wraw_data = wbs;
00190 }
00191 catch(...)
00192 {
00193 TEST_ASSERT(0, "wchar serialization to binary");
00194 }
00195
00196 try
00197 {
00198 binary_deserializer_t wbds(&wraw_data[0], &wraw_data[0] + wraw_data.size());
00199 wit1 = wbds(NULL);
00200 }
00201 catch(...)
00202 {
00203 TEST_ASSERT(0, "wchar deserialization from binary");
00204 }
00205 wit->print(orig);
00206 wit1->print(restored);
00207
00208 TEST_ASSERT_STRINGS_EQUAL(orig.str(), restored.str(), "wchar orig vs restored matching");
00209
00210 try
00211 {
00212 xml_serializer_t xmlsw(wit);
00213 TEST_ASSERT_STRINGS_EQUAL(unicode_item_xml_str, xmlsw.get(), "wchar serialization to xml");
00214 }
00215 catch(...)
00216 {
00217 TEST_ASSERT(0, "wchar xml serializer creation");
00218 }
00219
00220 try
00221 {
00222 xmlDocPtr doc3 = xmlParseMemory(xmlsw.get(), strlen(xmlsw.get()));
00223 xml_deserializer_t xmlds3(xmlDocGetRootElement(doc3));
00224 pdata_item_t xml_wit = xmlds3;
00225 xml_wit->print(restored_from_xml);
00226 }
00227 catch(...)
00228 {
00229 TEST_ASSERT(0, "wchar xml deserializer creation");
00230 }
00231
00232 TEST_ASSERT_STRINGS_EQUAL(unicode_item_str, restored_from_xml.str(), "restored whar from xml check");
00233 #endif
00234 }
00236 void test_msg_xml_deserializer(void)
00237 {
00238 std::stringstream msg_str;
00239 pmsg_t pmsg;
00240 xmlDocPtr doc4 = xmlParseMemory(s14f9_xml_str.str().c_str(), strlen(s14f9_xml_str.str().c_str()));
00241 try
00242 {
00243 xml_deserializer_t xmlds4(xmlDocGetRootElement(doc4));
00244 pmsg = xmlds4;
00245 }
00246 catch(...)
00247 {
00248 TEST_ASSERT(0, "s14f9 xml deserializer creation");
00249 }
00250 pmsg->print(msg_str);
00251
00252 TEST_ASSERT_STRINGS_EQUAL(s14f9_str.str().c_str(), msg_str.str().c_str(), "s14f9 xml deserialization");
00253 xmlFreeDoc(doc4);
00255 const char *str = NULL;
00256 try
00257 {
00258 xml_serializer_t xs(pmsg);
00259 str = xs.get();
00260 }
00261 catch(...)
00262 {
00263 TEST_ASSERT(0, "xml serializer creation");
00264 }
00265 TEST_ASSERT_STRINGS_EQUAL(str, s14f9_xml_str.str().c_str(), "s14f9 xml serialization check");
00266 }
00268
00269
00270 void test_deserialization_from_file(void)
00271 {
00272 int fd = open("xsd/test.xml", O_RDONLY);
00273 int schema_fd = open("xsd/secstwo.xsd", O_RDONLY);
00274
00275 TEST_ASSERT(fd > 0 && schema_fd > 0, "xml file and scheme file open");
00276
00277 xml_serializer_t::msg_container_t msgs;
00278
00279 int err = xml_deserializer_t::from_file(fd, schema_fd, msgs);
00280
00281 TEST_ASSERT(!err,"deserialization from file");
00282
00283 int ix = 0;
00284 for(xml_serializer_t::msg_container_t::const_iterator it = msgs.begin(); it != msgs.end(); ++it, ++ix)
00285 {
00286 if(3 == (*it)->stream && 17 == (*it)->function)
00287 {
00288 (*it)->print(s3f17_str);
00289 TEST_ASSERT(0 != strlen(s3f17_str.str().c_str()), "S3F17 deserialization from xml");
00290
00291 xml_serializer_t xs(*it);
00292 s3f17_xml_str << xs.get();
00293 }
00294 else if(14 == (*it)->stream && 9 == (*it)->function)
00295 {
00296 (*it)->print(s14f9_str);
00297 TEST_ASSERT(0 != strlen(s14f9_str.str().c_str()), "S14F9 deserialization from xml");
00298
00299 xml_serializer_t xs(*it);
00300 s14f9_xml_str << xs.get();
00301 }
00302 }
00303
00304 close(fd);
00305 close(schema_fd);
00306
00307 int to_fd = open("serialized.xml", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
00308
00309 TEST_ASSERT(0 < to_fd, "serialized.xml open");
00310
00311 err = xml_serializer_t::to_file(to_fd, (xml_serializer_t::msg_container_t&)msgs);
00312
00313 TEST_ASSERT(!err, "serialized to file");
00314 fsync(to_fd);
00315
00316 const char *shell_compare = "a=`diff -q serialized.xml xsd/test.xml`; if [ -n \"$a\" ]; then exit 0; else exit -1; fi";
00317 TEST_ASSERT(-1 != system(shell_compare), "serialized files comparison");
00318
00319
00320 close(to_fd);
00321 }
00323
00324
00325 void test_deserialization_from_membuf(void)
00326 {
00327
00328 char xml_membuf[20*1024];
00329 char schema_membuf[20*1024];
00330 std::string xml_to_mem;
00331
00332 int fd = open("xsd/test.xml", O_RDONLY);
00333 int schema_fd = open("xsd/secstwo.xsd", O_RDONLY);
00334
00335 ssize_t schema_len, xml_len;
00336
00337 if(0 >= (xml_len = read(fd, xml_membuf, sizeof(xml_membuf))))
00338 {
00339 TEST_ASSERT(0, "reading xsd/test.xml");
00340 }
00341 if(0 >= (schema_len = read(schema_fd, schema_membuf, sizeof(schema_membuf))))
00342 {
00343 TEST_ASSERT(0,"reading xsd/secstwo.xsd");
00344 }
00345
00346 xml_serializer_t::msg_container_t msgs;
00347 int err = xml_deserializer_t::from_mem(xml_membuf, sizeof(xml_membuf),schema_membuf, sizeof(schema_membuf), msgs);
00348
00349 TEST_ASSERT(0 == err, "de-serialization from mem");
00350
00351 int ix = 0;
00352 for(xml_serializer_t::msg_container_t::const_iterator it = msgs.begin(); it != msgs.end(); ++it, ++ix)
00353 {
00354 if(3 == (*it)->stream && 17 == (*it)->function)
00355 {
00356 std::stringstream msg_str;
00357 (*it)->print(msg_str);
00358 TEST_ASSERT_STRINGS_EQUAL(msg_str.str().c_str(), s3f17_str.str().c_str(), "s3f17 deserialized from mem check");
00359 }
00360 else if(14 == (*it)->stream && 9 == (*it)->function)
00361 {
00362 std::stringstream msg_str;
00363 (*it)->print(msg_str);
00364 TEST_ASSERT_STRINGS_EQUAL(msg_str.str().c_str(), s14f9_str.str().c_str(), "s14f9 deserialized from mem check");
00365 }
00366 }
00367
00368 close(fd);
00369 close(schema_fd);
00370
00371 err = xml_serializer_t::to_mem(xml_to_mem, (xml_serializer_t::msg_container_t&)msgs);
00372
00373 TEST_ASSERT(0 == err, "serailised to mem");
00374 TEST_ASSERT(false == xml_to_mem.empty(), "mem not empty");
00375 }
00376
00377
00378 test_t serialize_tests[] = {
00379 test_t( "binary deserializer test", test_binary_deserializer ),
00380 test_t( "binary serializer test", test_binary_serializer ),
00381 test_t("deserialization from xml file", test_deserialization_from_file ),
00382 test_t("xml serializer test", test_xml_seriaizer ),
00383 test_t("xml deserializer test", test_xml_deserializer ),
00384 test_t("wchar serialization to xml", test_wchar_to_xml_serialization ),
00385 test_t("msg from xml deserialization", test_msg_xml_deserializer ),
00386 test_t("deserialization from memory buffer", test_deserialization_from_membuf),
00387 test_t(NULL,0),
00388 };
00389
00390 test_suite_t serialization_suite("Serialization tests", serialize_tests);