test.cpp

00001 /* 00002 $Id: test_8cpp-source.html,v 1.1 2004/10/05 21:12:03 mentat Exp $ 00003 00004 GNU Messenger - The secure instant messenger 00005 00006 Copyright (C) 2003 Jesse Lovelace <jllovela@eos.ncsu.edu> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #include "gm/gm.h" 00024 #include "gm/xml.h" 00025 #include "gm/xml_manager.h" 00026 #include "gm/contact_manager.h" 00027 #include "gm/settings_manager.h" 00028 #include <sstream> 00029 #include <iostream> 00030 #include <string> 00031 00032 using namespace std; 00033 using namespace GNUMessenger; 00034 00035 int failure; 00036 int success; 00037 00038 #define VERIFY(a,b) { \ 00039 if ((a)==(b)) \ 00040 { cout << "Passed.." << endl; success++; } \ 00041 else \ 00042 { cout << "Failed at " << __FILE__ << ":" << __LINE__ << endl; failure++; } \ 00043 } 00044 00045 00046 #define VERIFY_THROW(a) { bool didThrow = false; \ 00047 try { a; } \ 00048 catch(GNUMessenger::Exception &e) \ 00049 { \ 00050 didThrow = true; \ 00051 cout << e.what() << endl; \ 00052 } \ 00053 if (didThrow) \ 00054 { cout << "Passed.." << endl; success++; } \ 00055 else \ 00056 { cout << "Failed at " << __FILE__ << ":" << __LINE__ << endl; failure++; } \ 00057 } 00058 00059 #define VERIFY_NO_THROW(a) { bool didThrow = false; \ 00060 try { a; } \ 00061 catch(GNUMessenger::Exception &e) \ 00062 { \ 00063 didThrow = true; \ 00064 cout << e.what() << endl; \ 00065 } \ 00066 if (!didThrow) \ 00067 { cout << "Passed.." << endl; success++; } \ 00068 else \ 00069 { cout << "Failed at " << __FILE__ << ":" << __LINE__ << endl; failure++; } \ 00070 } 00071 00072 int testFolder() 00073 { 00074 00075 XMLNode myXML; 00076 myXML << "<folder></folder>"; 00077 00078 Folder myFolder; 00079 Folder myFolder1("<folder><contact name=\"john\"/></folder>"); 00080 Folder myFolder2(myXML); 00081 00082 VERIFY(myFolder2.isOK(), true); 00083 00084 cout << "Successes: " << success << endl; 00085 cout << "Failures: " << failure << endl; 00086 00087 return 0; 00088 } 00089 00090 int testChild() 00091 { 00092 cout << "------begin Testing XML Childs ------\n"; 00093 XMLNode n; 00094 00095 n.setName("hej"); 00096 n.addChild("aloha"); 00097 n.child("aloha").setProperty("hej","hello"); 00098 00099 VERIFY(n.numChildren("aloha"),1); 00100 VERIFY(n.child("aloha").property("hej"),"hello"); 00101 00102 VERIFY(n.child("notvalid").name(), ""); 00103 VERIFY(n.child("doesntexist").child("notthisoneeither").name(), ""); 00104 VERIFY(n.child("doesntexist").property("notthisoneeither"),""); 00105 00106 const XMLNode cn; 00107 00108 VERIFY_THROW(cn.child("notvalid").name()); 00109 00110 return 0; 00111 } 00112 00113 int testProperty() 00114 { 00115 cout << "------begin Testing XML Properties ------\n"; 00116 XMLNode n; 00117 00118 n.setName("hej"); 00119 00120 n.setProperty("test","yes"); 00121 00122 VERIFY(n.property("test"),"yes"); 00123 VERIFY(n["test"],"yes"); 00124 00125 VERIFY(n.property("nonexisting"),""); 00126 00127 return 0; 00128 } 00129 00130 int testParsing() 00131 { 00132 cout << "------begin Testing XML Parsing ------\n"; 00133 00134 XMLParser e(" <messages>" 00135 "<message sender=\"test\" subject=\"-\">" 00136 "foo bla bla" 00137 "</message>" 00138 "</messages> "); 00139 XMLNode bla; 00140 XMLNode n; 00141 XMLNode *blargh= new XMLNode(); 00142 00143 *blargh = e.get_root(); 00144 bla=*blargh; 00145 n=*blargh; 00146 00147 delete blargh; 00148 blargh=0; 00149 00150 n.setProperty("foo","bar"); 00151 00152 VERIFY(bla.property("foo"),"bar"); 00153 VERIFY(n.name(),"messages"); 00154 bla.setName("hej"); 00155 VERIFY(n.name(),"hej"); 00156 00157 //cout << n << endl; 00158 //cout << "------end Testing XML Parsing ------\n"; 00159 00160 return 0; 00161 } 00162 00163 int testParsing2() 00164 { 00165 cout << "------begin Testing invalid XML Parsing ------\n"; 00166 00167 auto_ptr<XMLParser> e; 00168 VERIFY_THROW(e.reset(new XMLParser(" <messages" 00169 "<message sender=\"test\" subject=\"-\">" 00170 "foo bla bla" 00171 "<message>" 00172 "</messages> "))); 00173 00174 XMLNode bla; 00175 XMLNode n; 00176 XMLNode *blargh= new XMLNode(); 00177 00178 // *blargh = e->get_root(); 00179 bla=*blargh; 00180 n=*blargh; 00181 delete blargh; 00182 blargh=0; 00183 00184 n.setProperty("foo","bar"); 00185 VERIFY(bla.property("foo"),"bar"); 00186 00187 //cout << n << endl; 00188 bla.setName("hej"); 00189 VERIFY(n.name(),"hej"); 00190 00191 //cout << "------end Testing invalid XML Parsing ------\n"; 00192 return 0; 00193 } 00194 00195 int testConst() 00196 { 00197 cout << "------begin Testing constant nodes ------\n"; 00198 XMLNode a,c; 00199 a.setName("henrik"); 00200 a.setProperty("foo","var"); 00201 00202 const XMLNode b(a); 00203 c=b; 00204 c.setName("nisse"); 00205 //cout << a << endl; 00206 00207 //VERIFY(a.name(),"henrik"); 00208 // VERIFY(b.name(),"henrik"); 00209 00210 VERIFY(b["foo"],"var"); 00211 00212 VERIFY(c.name(),"nisse"); 00213 VERIFY(c["foo"],"var"); 00214 00215 return 0; 00216 } 00217 00218 00219 int testFoo() 00220 { 00221 // I'm not sure what this test does :) -jll 00222 string str="<foo>\n<bla pro='ba' pro2='ff' >h\n;&amp;e\nj<bar hej='foo' > ni&amp;ise </bar> </bla>\n</foo>\n"; 00223 for (int i=0; i<20; i++) 00224 { 00225 XMLNode n; 00226 n << str; 00227 //cout << "Data: '" << n.child("bla").data() << "'\n"; 00228 str = n; 00229 //cout << "----------------\n" << str << "----------------\n" ; 00230 } 00231 return 0; 00232 } 00233 00234 int jesseTest() 00235 { 00236 // this test checks for a formatting bug that arose when 00237 // the expat code saved whitespaces 00238 cout << "------begin Testing tortcher test \"jesse\" ------\n"; 00239 string teststr("<config>\n<people>\n</people>\n<animals>\n</animals></config>"); 00240 00241 for (int i = 0; i < 30; i++) 00242 { 00243 stringstream name; 00244 name << "jones" << i; 00245 XMLNode n; 00246 n << teststr; 00247 00248 if (i % 5 == 0) 00249 n.child("config").child("animals").addChild(name.str()); 00250 00251 n.child("config").child("people").addChild(name.str()); 00252 teststr = string(n); 00253 } 00254 XMLNode final; 00255 00256 final << teststr; 00257 00258 return 0; 00259 } 00260 00261 int XText1() 00262 { 00263 cout << "------begin Testing XML Parsing 2 ------\n"; 00264 string teststr("<config dudes=\"1\">\n<people>\n</people>\n<animals>\n</animals></config>"); 00265 00266 XMLNode n; 00267 VERIFY_NO_THROW(n << teststr); 00268 00269 return 0; 00270 } 00271 00272 int testMove() 00273 { 00274 cout << "------begin Testing Move ------\n"; 00275 XMLNode node; 00276 VERIFY_NO_THROW(node << "<node><james/><jim/><tom/></node>"); 00277 00278 XMLNode test; 00279 00280 // this should move tom to the front 00281 VERIFY_NO_THROW(test << "<node><tom/><james/><jim/></node>"); 00282 00283 VERIFY_NO_THROW(node.moveChild(2, 0)); 00284 00285 VERIFY(static_cast<string>(node), static_cast<string>(test)); 00286 00287 // this should move james to the back 00288 VERIFY_NO_THROW(test << "<node><tom/><jim/><james/></node>"); 00289 00290 VERIFY_NO_THROW(node.moveChild(1, 3)); 00291 00292 VERIFY(static_cast<string>(node), static_cast<string>(test)); 00293 00294 return 0; 00295 } 00296 00297 int contactTest1() 00298 { 00299 00300 cout << "------begin Testing Contacts ------\n"; 00301 Contact d; 00302 00303 Contact c; 00304 c.addProtocol("kit"); 00305 c.setServerId("kit", "henrik"); 00306 00307 VERIFY (c.getServerId("kit"),"henrik"); 00308 00309 c.setName("nisse"); 00310 00311 VERIFY (c.name(),"nisse"); 00312 VERIFY (c.getStatus("kit"),Contact::Offline); 00313 00314 c.setStatus("kit", Contact::Online); 00315 VERIFY (c.getStatus("kit"),Contact::Online); 00316 00317 VERIFY (c.getProtocol(),"kit"); 00318 00319 d=c; 00320 00321 VERIFY (d.getProtocol(),"kit"); 00322 VERIFY (d.getServerId("kit"),"henrik"); 00323 VERIFY (d.name(),"nisse"); 00324 00325 VERIFY ((c==d),true); 00326 00327 return 0; 00328 } 00329 00330 int contactTest2() 00331 { 00332 cout << "------begin Testing Contacts 2 ------\n"; 00333 Contact c; 00334 00335 //VERIFY (c.getProtocol(),""); 00336 VERIFY_THROW (c.getServerId("toc")); 00337 VERIFY (c.name(),""); 00338 return 0; 00339 } 00340 00341 int contactTest3() 00342 { 00343 cout << "------begin Testing Contacts 3 ------\n"; 00344 // construct 00345 Contact joe; 00346 VERIFY(joe.isOK(), true); 00347 00348 // setName 00349 joe.setName("joe"); 00350 VERIFY(joe.name(), "joe"); 00351 00352 // setStatus 00353 VERIFY_THROW(joe.setStatus("toc", Contact::Online)); 00354 VERIFY_THROW(joe.getStatus("toc")); 00355 00356 // setStatus 00357 joe.addProtocol("toc"); 00358 joe.setStatus("toc", Contact::Online); 00359 VERIFY(joe.getStatus("toc"), Contact::Online); 00360 00361 // Assignment 00362 Contact jim(joe); 00363 00364 VERIFY(joe, jim); 00365 00366 // Assignment 2 00367 jim.setName("tim"); 00368 joe = jim; 00369 VERIFY(joe, jim); 00370 00371 return 0; 00372 00373 } 00374 00375 int contactTest4() 00376 { 00377 cout << "------begin Testing Contacts 4 ------\n"; 00378 XMLNode node; 00379 node << 00380 "<config>" 00381 "<contacts>" 00382 "<folder>" 00383 "<contact name = \"jim\">" 00384 "<info name=\"joe\">" 00385 "<part>" 00386 "<diode/>" 00387 "</part>" 00388 "</info>" 00389 "</contact>" 00390 "</folder>" 00391 "</contacts>" 00392 "</config>"; 00393 00394 XMLNode node2(node.child("contacts").child("folder")); 00395 00396 node2.setProperty("name", "sam"); 00397 00398 ContactManager mng(node); 00399 00400 cout << string(node) << endl; 00401 00402 try { 00403 XMLNode temp2(mng.getContact("jim")); 00404 temp2.child("info").setProperty("name", "tim"); 00405 cout << string(temp2) << endl; 00406 VERIFY(node.child("contacts").child("folder"). 00407 child("contact").child("info").property("name"), "tim"); 00408 } 00409 catch(Exception &e) 00410 { 00411 cout << e.what() << endl; 00412 return 1; 00413 } 00414 00415 cout << string(node) << endl; 00416 00417 00418 00419 return 0; 00420 00421 00422 } 00423 00424 int xml_manager_test1() 00425 { 00426 cout << "------begin Testing XMLManager 1 ------\n"; 00427 00428 XMLManager myManager; 00429 VBuffer passw("dork"); 00430 myManager.createNew("test", passw); 00431 myManager.logout(); 00432 00433 // test to see if the file was created 00434 VERIFY(XMLManager::exists("test"), true); 00435 00436 // test login 00437 VERIFY(myManager.login("test", passw), true); 00438 00439 // test commit 00440 VERIFY(myManager.commit(), true); 00441 00442 // test profile saving 00443 myManager.settings().setProfile("This is me!"); 00444 00445 VERIFY(myManager.settings().getProfile(), "This is me!"); 00446 00447 // test public key saving 00448 myManager.settings().setPublicKey("ABCDEFG"); 00449 00450 VERIFY(myManager.settings().getPublicKey(), "ABCDEFG"); 00451 00452 // test away message saving 00453 myManager.settings().setAwayMessage("out", "not in"); 00454 00455 VERIFY(myManager.settings().getAwayMessage("out"), "not in"); 00456 00457 // test network object storage 00458 XMLNode mynet, myTestNet; 00459 00460 mynet << "<toc><server port=\"99\" host=\"myserv.com\"/></toc>"; 00461 00462 myManager.settings().setNet(mynet); 00463 00464 myManager.settings().getNet("toc", myTestNet); 00465 00466 // I could do a c-style cast here but i'm trying to break myself 00467 // of that habit 00468 VERIFY(static_cast<string>(mynet), static_cast<string>(myTestNet)); 00469 00470 myManager.commit(); 00471 00472 // test logout 00473 VERIFY(myManager.logout(), true); 00474 00475 return 0; 00476 } 00477 00478 int net_conf_test1() 00479 { 00480 cout << "------begin Testing NetConf ------\n"; 00481 XMLNode conf; 00482 conf << "<toc><server port=\"445\" host=\"toc.com\"/>" 00483 "<loginserver host=\"login.toc.com\" port=\"111\"/></toc>"; 00484 00485 NetConf myNet(conf); 00486 00487 VERIFY(myNet.getProtocol(), "toc"); 00488 VERIFY(myNet.getServer(), "toc.com"); 00489 VERIFY(myNet.getLoginServer(), "login.toc.com"); 00490 VERIFY(myNet.getLoginPort(), 111); 00491 VERIFY(myNet.getServerPort(), 445); 00492 00493 VBuffer mypass("mypass"); 00494 myNet.setPassword(mypass); 00495 00496 VERIFY(mypass, myNet.getPassword()); 00497 00498 // oh mad skilz, did you say named constructor? 00499 NetConf myinfo(NetConf(). 00500 setProtocol("toc"). 00501 setServer("toc.aim.com"). 00502 setServerPort(200). 00503 setLogin("m3ntat"). 00504 setPassword(string("dude"))); 00505 00506 return 0; 00507 00508 } 00509 00510 int main() 00511 { 00512 gLogger = new Log(); 00513 00514 success = 0; 00515 failure = 0; 00516 00517 try { 00518 testFolder(); 00519 XText1(); 00520 testProperty(); 00521 testChild(); 00522 testParsing(); 00523 testParsing2(); 00524 testConst(); 00525 testFoo(); 00526 jesseTest(); 00527 testMove(); 00528 00529 contactTest1(); 00530 contactTest2(); 00531 contactTest3(); 00532 contactTest4(); 00533 00534 xml_manager_test1(); 00535 net_conf_test1(); 00536 } 00537 catch(XMLNode::InvalidChild& e) 00538 { 00539 cout << "Exception(InvalidChild): " << e.what() << endl; 00540 return failure; 00541 } 00542 catch(XMLNode::ParseFailure& e) 00543 { 00544 cout << "Exception(ParseFailure): " << e.what() << endl; 00545 return failure; 00546 } 00547 catch(...) 00548 { 00549 cout << "Unkown exception!" << endl; 00550 return failure; 00551 } 00552 00553 delete gLogger; 00554 gLogger = 0; 00555 00556 cout << "Successes: " << success << endl; 00557 cout << "Failures: " << failure << endl; 00558 00559 return failure; 00560 00561 } 00562

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8