Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ippSimpleServerTestCase.cpp

Go to the documentation of this file.
00001 // 
00002 // DISCLAIMER: 
00003 //  This software was produced by the National Institute of Standards 
00004 //  and Technology (NIST), an agency of the U.S. government, and by statute is 
00005 //  not subject to copyright in the United States.  Recipients of this 
00006 //  software assume all responsibility associated with its operation,
00007 //  modification,maintenance, and subsequent redistribution. 
00008 //
00009 //  See NIST Administration Manual 4.09.07 b and Appendix I. 
00010 //
00011 #define _CRT_SECURE_NO_DEPRECATE
00012 
00013 #include "ippdme/winsock.h"
00014 #include "ippSimpleServerTestCase.h"
00015 #include "ippMemoryLeakDetector.h"
00016 
00017 #include "ippdme/Server/ippSimpleServer.h"
00018 #include "ippdme/Server/ippSimpleClient.h"
00019 #include "ippdme/Server/ippLogger.h"
00020 #include "ippdme/assert.h"
00021 
00022 int PortForTesting = 12940;
00023 
00024 
00025 
00026 
00027 
00028 CXXTEST_ENUM_TRAITS( ippSimpleServer::STATE,
00029                      CXXTEST_ENUM_MEMBER( ippSimpleServer::uninitialized )
00030                      CXXTEST_ENUM_MEMBER( ippSimpleServer::listening )
00031                      CXXTEST_ENUM_MEMBER( ippSimpleServer::connected ));
00032 
00033 
00034 
00035 CXXTEST_ENUM_TRAITS( ippSimpleClient::STATE,
00036                      CXXTEST_ENUM_MEMBER( ippSimpleClient::disconnected )
00037                      CXXTEST_ENUM_MEMBER( ippSimpleClient::trying_to_connect )
00038                      CXXTEST_ENUM_MEMBER( ippSimpleClient::connected ));
00039 
00040 class ippTraceLogger : public ippLogger
00041 {
00042 private:
00043   std::string _prefix;
00044 public:
00045   bool _ouput_on_cout;
00046 
00047   ippTraceLogger(const char* prefix)
00048   :_prefix(prefix),_ouput_on_cout(false)
00049   {
00050   
00051   }
00052   virtual void WriteLine(const char* str)
00053   {
00054     if(_ouput_on_cout) { 
00055       std::cout << _prefix << " " << str <<std::endl;
00056     }
00057   }
00058 
00059 
00060 };
00061 class ippCommand;
00062 //-----------------------------------------------------------------------------
00063 
00064 void ippSimpleServerTestCase::testSimpleServer1()
00065 {
00066   //PortForTesting++;
00067   // try 3 connections/disconnection
00068   ippSimpleServer server;  
00069   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::uninitialized );
00070   
00071   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));  
00072   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::listening );
00073   server.PowerOff();
00074   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::uninitialized );
00075 
00076   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00077   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::listening );
00078 
00079   server.PowerOff();
00080   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::uninitialized );
00081 
00082   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00083   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::listening );
00084   server.PowerOff();
00085   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::uninitialized );
00086 
00087   // --- now  try to have a client connecting
00088   //     several type
00089   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00090   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::listening );
00091 
00092   ippSimpleClient client;
00093 
00094   for (int i=0 ;i < 10 ; i++) 
00095   {
00096      
00097     std::cout << " testSimpleServer1 iteration " << i << std::endl;
00098     client.Connect("localhost",PortForTesting);
00099     client.HeartBeat();server.HeartBeat(); 
00100     client.HeartBeat();server.HeartBeat();
00101     client.HeartBeat();server.HeartBeat();
00102     IPP_UTEST_ASSERT_EQUALS(server.GetState(),ippSimpleServer::connected );
00103     IPP_UTEST_ASSERT_EQUALS(client.GetState(),ippSimpleClient::connected );
00104     IPP_UTEST_ASSERT(client.ReadyToSend() );
00105 
00106     client.Disconnect();
00107     server.ProbeConnection();
00108     client.HeartBeat();server.HeartBeat();
00109     client.HeartBeat();server.HeartBeat();
00110     client.HeartBeat();server.HeartBeat();
00111     IPP_UTEST_ASSERT_EQUALS(server.GetState(),ippSimpleServer::listening );
00112     IPP_UTEST_ASSERT_EQUALS(client.GetState(),ippSimpleClient::disconnected );
00113     IPP_UTEST_ASSERT(!client.ReadyToSend() );
00114     ippSleep(20);
00115   }
00116     
00117   server.PowerOff();
00118 }
00119 
00120 
00121 void ippSimpleServerTestCase::testSimpleServer2()
00122 {
00123 
00124   //PortForTesting++;
00125   ippTraceLogger clientLogger("client");
00126   ippTraceLogger serverLogger("server");
00127   clientLogger._ouput_on_cout = true;
00128   serverLogger._ouput_on_cout = true;
00129 
00130   ippSimpleServer server;
00131   server.setLogger(&serverLogger);
00132   
00133   // start the server
00134   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00135 
00136   ippSimpleClient client;
00137 
00138 
00139   client.setLogger(&clientLogger);
00140   client.Connect("localhost",PortForTesting);
00141 
00142 
00143 
00144   server.HeartBeat();client.HeartBeat();
00145   server.HeartBeat();client.HeartBeat();
00146 
00147   
00148   // send a valid command 
00149   client.SendCommand("00001 StartSession()");
00150   server.HeartBeat();client.HeartBeat();
00151   server.HeartBeat();client.HeartBeat();
00152   IPP_UTEST_ASSERT(!client.NeedToSendClearAllErrors());
00153   IPP_UTEST_ASSERT(client.ReadyToSend());
00154 
00155   // send a command with a \r or \n in the middle of the string
00156   client.SendCommand("00001 Start\rSe\nssion()");
00157   server.HeartBeat();client.HeartBeat();
00158   IPP_UTEST_ASSERT(client.NeedToSendClearAllErrors());
00159   client.ClearAllErrors(); server.HeartBeat();client.HeartBeat();
00160   IPP_UTEST_ASSERT(!client.NeedToSendClearAllErrors());
00161   IPP_UTEST_ASSERT(client.ReadyToSend());
00162  
00163   // send a command with a invalid character the middle of the string
00164   client.SendCommand("00001 LoadTool(\"\t\")");
00165   server.HeartBeat();client.HeartBeat();
00166   IPP_UTEST_ASSERT(client.NeedToSendClearAllErrors());
00167   client.ClearAllErrors(); server.HeartBeat();client.HeartBeat();
00168   IPP_UTEST_ASSERT(!client.NeedToSendClearAllErrors());
00169   IPP_UTEST_ASSERT(client.ReadyToSend());
00170 
00171   // send a command with a bag tag
00172   client.SendCommand("0000%1 StartSession()");
00173   server.HeartBeat();client.HeartBeat();
00174   IPP_UTEST_ASSERT(client.ReadyToSend());
00175   IPP_UTEST_ASSERT(client.NeedToSendClearAllErrors());
00176   client.ClearAllErrors(); server.HeartBeat();client.HeartBeat();
00177   IPP_UTEST_ASSERT(!client.NeedToSendClearAllErrors());
00178   IPP_UTEST_ASSERT(client.ReadyToSend());
00179 
00180   // send a command with no space at position 6
00181   client.SendCommand("00001StartSession()");
00182   server.HeartBeat();client.HeartBeat();
00183   IPP_UTEST_ASSERT(client.ReadyToSend());
00184   IPP_UTEST_ASSERT(client.NeedToSendClearAllErrors());
00185   client.ClearAllErrors(); server.HeartBeat();client.HeartBeat();
00186   IPP_UTEST_ASSERT(!client.NeedToSendClearAllErrors());
00187   IPP_UTEST_ASSERT(client.ReadyToSend());
00188 
00189   int n=100;
00190   while(n--) {
00191      server.HeartBeat();
00192      client.HeartBeat();
00193   }
00194   
00195 }
00196 
00197 void ippSimpleServerTestCase::testSimpleServer3()
00198 {
00199 
00200   //PortForTesting++;
00201   // test what is going on with the client
00202   //  when server disconnect unexepectidally
00203   
00204   ippTraceLogger clientLogger("client");
00205   ippTraceLogger serverLogger("server");
00206   clientLogger._ouput_on_cout = true;
00207   serverLogger._ouput_on_cout = true;
00208 
00209   ippSimpleServer server;
00210   server.setLogger(&serverLogger);
00211   
00212   // start the server
00213   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00214 
00215   ippSimpleClient client;
00216   client.setLogger(&clientLogger);
00217 
00218   client.Connect("localhost",PortForTesting);
00219   server.HeartBeat();client.HeartBeat();
00220   IPP_UTEST_ASSERT_EQUALS(client.GetState(), ippSimpleClient::connected );
00221 
00222   server.HeartBeat();client.HeartBeat();
00223 
00224   
00225   // send a valid command 
00226   client.SendCommand("00001 StartSession()");
00227   server.HeartBeat();  client.HeartBeat();
00228   IPP_UTEST_ASSERT_EQUALS(client.GetState() , ippSimpleClient::connected );
00229 
00230   server.PowerOff();
00231   client.SendCommand("0001 StartSession()");
00232   client.HeartBeat();
00233   client.HeartBeat();
00234   IPP_UTEST_ASSERT_EQUALS(client.GetState() , ippSimpleClient::disconnected );
00235 
00236 }
00237 void ippSimpleServerTestCase::testSimpleServer4()
00238 {
00239   //PortForTesting++;
00240   // check that only client can connect to a 
00241   // server at a particular time.
00242 
00243   ippTraceLogger clientLogger("client");
00244   ippTraceLogger serverLogger("server");
00245 
00246   ippSimpleServer server;
00247   server.setLogger(&serverLogger);
00248   
00249   // start the server
00250   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00251 
00252   ippSimpleClient client1;
00253   client1.setLogger(&clientLogger);
00254   client1.Connect("localhost",PortForTesting);
00255   server.HeartBeat();client1.HeartBeat();
00256   IPP_UTEST_ASSERT_EQUALS(client1.GetState() , ippSimpleClient::connected );
00257 
00258   ippSimpleClient client2;
00259   client2.setLogger(&clientLogger);
00260   client2.Connect("localhost",PortForTesting);
00261   server.HeartBeat();client2.HeartBeat();
00262   IPP_UTEST_ASSERT_EQUALS(client2.GetState() , ippSimpleClient::disconnected );
00263 
00264   ippSimpleClient client3;
00265   client3.setLogger(&clientLogger);
00266   client3.Connect("localhost",PortForTesting);
00267   server.HeartBeat();client3.HeartBeat();
00268   IPP_UTEST_ASSERT_EQUALS(client3.GetState() , ippSimpleClient::disconnected );
00269  
00270 }
00271 #include "ippTestFolder.h"
00272 #include "ippdme/Parser/ippCommandParser.h"
00273 #include <fstream>
00274 void ippSimpleServerTestCase::SendCommands(
00275  ippSimpleClient& client,
00276  ippSimpleServer& server,
00277  const char* filename
00278 )
00279 {
00280 
00281   int counter = 0;
00282   
00283   const int IPP_MAXSTRINGSIZE = 65535;
00284   char buf[IPP_MAXSTRINGSIZE];
00285  
00286   //xx std::cout << "parsing file : " << filename  << std::endl;
00287 
00288   std::ifstream input;
00289   input.open(filename);
00290   IPP_UTEST_ASSERT(!input.bad());
00291   if (input.bad()) return;
00292   int line =0;
00293   while(!input.eof()) {
00294     std::string str;
00295     input.getline(buf,IPP_MAXSTRINGSIZE);
00296 
00297     line++;
00298     size_t l = strlen(buf);
00299     buf[l] =13;
00300     buf[l+1] =10;
00301     buf[l+2] =0;
00302 
00303     str = buf;
00304     if (str.find("\\")==0 || str.find(":")==0 || str.length()==2) {
00305     } else {
00306 
00307 
00308       ippCommandParser cmdParser;
00309 
00310       cmdParser.setInput(str.c_str());
00311       ippCommandPtr command = cmdParser.parseCommand();
00312       
00313       IPP_UTEST_ASSERT(command);
00314       if (command ) {
00315         client.SendCommand(command);
00316 
00317         while( (client.GetState() == ippSimpleClient::connected) && !client.ReadyToSend()) {
00318           client.HeartBeat();server.HeartBeat();
00319         }
00320       }
00321 
00322     }
00323   }
00324 }
00325 
00326 
00327 #include "ippTestFolder.h"
00328 
00329 void ippSimpleServerTestCase::testSimpleServer5()
00330 {
00331   //PortForTesting++;
00332   // this test applies the "AllCmdOK commands to 
00333   // a server, through a simple client.
00334  
00335   ippTraceLogger clientLogger("client");
00336   ippTraceLogger serverLogger("server");
00337 
00338   ippSimpleServer server;
00339   server.setLogger(&serverLogger);
00340   
00341   // start the server
00342   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00343 
00344   ippSimpleClient client;
00345   client.setLogger(&clientLogger);
00346 
00347   client.Connect("localhost",PortForTesting);
00348   server.HeartBeat();client.HeartBeat();
00349 
00350   IPP_UTEST_ASSERT(client.GetState() == ippSimpleClient::connected );
00351 
00352   server.HeartBeat();client.HeartBeat();
00353 
00354   
00355   ippTestFolder folder;
00356   bool bSuccess = folder.find_first_file(test_file_folder().c_str(),"AllCmdOK.prg");
00357   IPP_UTEST_ASSERT(bSuccess);
00358 
00359   while(bSuccess) {
00360     
00361     std::string filename = folder.current_filename();
00362     SendCommands(client,server,filename.c_str());
00363     bSuccess = folder.next();
00364 
00365   }
00366 }
00367 
00368 
00369 void ippSimpleServerTestCase::testSimpleServerStartSessionEndSession()
00370 {
00371    //PortForTesting++;
00372   ippTraceLogger clientLogger("client");
00373   ippTraceLogger serverLogger("server");
00374   
00375 
00376   clientLogger._ouput_on_cout = true;
00377   serverLogger._ouput_on_cout = true;
00378 
00379   ippSimpleServer server;
00380   server.setLogger(&serverLogger);
00381   // start the server
00382   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00383   IPP_UTEST_ASSERT(!server.InSession());
00384 
00385   ippSimpleClient client;
00386 
00387   client.setLogger(&clientLogger);
00388   client.Connect("localhost",PortForTesting);
00389   server.HeartBeat();client.HeartBeat();
00390   server.HeartBeat();client.HeartBeat();
00391   server.HeartBeat();client.HeartBeat();
00392   IPP_UTEST_ASSERT_EQUALS(client.GetState() , ippSimpleClient::connected );
00393   IPP_UTEST_ASSERT(!server.InSession());
00394   IPP_UTEST_ASSERT(!client.InErrorState());
00395   IPP_UTEST_ASSERT(client.ReadyToSend());
00396 
00397   
00398   // verify that calling EndSession doesn't arm
00399   client.SendCommand(new ippCommand(1,CommandTag,EndSession));
00400   server.HeartBeat();client.HeartBeat();
00401   IPP_UTEST_ASSERT(!server.InSession());
00402   IPP_UTEST_ASSERT(!client.InErrorState());
00403   IPP_UTEST_ASSERT(client.ReadyToSend());
00404 
00405   // now try a StartSession/EndSession sequence
00406   client.SendCommand(new ippCommand(1,CommandTag,StartSession));
00407   server.HeartBeat();client.HeartBeat();
00408   IPP_UTEST_ASSERT(server.InSession());
00409   IPP_UTEST_ASSERT(!client.InErrorState());
00410   IPP_UTEST_ASSERT(client.ReadyToSend());
00411   
00412   client.SendCommand(new ippCommand(1,CommandTag,EndSession));
00413   server.HeartBeat();client.HeartBeat();
00414   IPP_UTEST_ASSERT(!server.InSession());
00415   IPP_UTEST_ASSERT(!client.InErrorState());
00416   IPP_UTEST_ASSERT(client.ReadyToSend());
00417 
00418   // now try a StartSession/StartSession 
00419   client.SendCommand(new ippCommand(1,CommandTag,StartSession));
00420   server.HeartBeat();client.HeartBeat();
00421   IPP_UTEST_ASSERT(server.InSession());
00422   IPP_UTEST_ASSERT(!client.InErrorState());
00423   IPP_UTEST_ASSERT(client.ReadyToSend());
00424 
00425   client.SendCommand(new ippCommand(1,CommandTag,StartSession));
00426   server.HeartBeat();client.HeartBeat();
00427   IPP_UTEST_ASSERT(server.InSession());
00428   IPP_UTEST_ASSERT(client.InErrorState());
00429   client.ClearAllErrors();
00430   server.HeartBeat();client.HeartBeat();
00431   IPP_UTEST_ASSERT(server.InSession());
00432   IPP_UTEST_ASSERT(!client.InErrorState());
00433   IPP_UTEST_ASSERT(client.ReadyToSend());
00434 
00435   
00436 
00437 
00438 }
00439 
00440 
00441 
00442 void ippSimpleServerTestCase::testSimpleServerReceivingInvalidCommand()
00443 {
00444   ippTraceLogger clientLogger("client");
00445   ippTraceLogger serverLogger("server");
00446 
00447   clientLogger._ouput_on_cout = true;
00448   serverLogger._ouput_on_cout = true;
00449 
00450   ippSimpleServer server;
00451   server.setLogger(&serverLogger);
00452   // start the server
00453   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00454   IPP_UTEST_ASSERT(!server.InSession());
00455 
00456   ippSimpleClient client;
00457 
00458   client.setLogger(&clientLogger);
00459   
00460   client.Connect("localhost",PortForTesting);
00461   server.HeartBeat();client.HeartBeat();
00462   server.HeartBeat();client.HeartBeat();
00463   IPP_UTEST_ASSERT_EQUALS(client.GetState() , ippSimpleClient::connected );
00464   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::connected );
00465   IPP_UTEST_ASSERT(!server.InSession());
00466   IPP_UTEST_ASSERT(!client.InErrorState());
00467 
00468   // now try a StartSession/EndSession sequence
00469   client.SendCommand(new ippCommand(1,CommandTag,StartSession));
00470   server.HeartBeat();client.HeartBeat();
00471   IPP_UTEST_ASSERT(server.InSession());
00472   IPP_UTEST_ASSERT(!client.InErrorState());
00473 
00474   // now send an invalid command
00475   IPP_UTEST_ASSERT(client.ReadyToSend());
00476   client.SendCommand("00001 ChangeTool(\"SomeTool\",badparameter)");
00477   server.HeartBeat();client.HeartBeat();
00478   server.HeartBeat();client.HeartBeat();
00479   IPP_UTEST_ASSERT(server.InSession());
00480   IPP_UTEST_ASSERT(client.InErrorState());
00481   IPP_UTEST_ASSERT(client.ReadyToSend());
00482 
00483 
00484 }
00485 
00486 #include "ippMockExecutor.h"
00487 
00488 
00489 
00490 void ippSimpleServerTestCase::testSimpleServerReceivingAbortE()
00491 {
00492   ippTraceLogger clientLogger("client");
00493   ippTraceLogger serverLogger("server");
00494 
00495   clientLogger._ouput_on_cout = true;
00496   serverLogger._ouput_on_cout = true;
00497 
00498   ippMockExecutor fake_exectutor;
00499   ippSimpleServer server(&fake_exectutor);
00500   server.setLogger(&serverLogger);
00501 
00502 
00503 
00504   // start the server
00505   IPP_UTEST_ASSERT(server.PowerOn(PortForTesting));
00506   IPP_UTEST_ASSERT(!server.InSession());
00507 
00508   ippSimpleClient client;
00509 
00510   client.setLogger(&clientLogger);
00511   
00512   client.Connect("localhost",PortForTesting);
00513   server.HeartBeat();client.HeartBeat();
00514   server.HeartBeat();client.HeartBeat();
00515   IPP_UTEST_ASSERT_EQUALS(client.GetState() , ippSimpleClient::connected );
00516   IPP_UTEST_ASSERT_EQUALS(server.GetState() , ippSimpleServer::connected );
00517   IPP_UTEST_ASSERT(!server.InSession());
00518   IPP_UTEST_ASSERT(!client.InErrorState());
00519 
00520   // now try a StartSession/EndSession sequence
00521   client.SendCommand(new ippCommand(1,CommandTag,StartSession));
00522   server.HeartBeat();client.HeartBeat();
00523   IPP_UTEST_ASSERT(server.InSession());
00524   IPP_UTEST_ASSERT(!client.InErrorState());
00525 
00526   // now send an invalid command
00527   client.SendCommand(new ippScanOnLineCommand(10  , 1.,1.,1.,10.,10.,10.,0.,0.,1.0,1.));
00528   server.HeartBeat();client.HeartBeat();
00529   client.SendCommand(new ippScanOnLineCommand(11  , 1.,1.,1.,10.,10.,10.,0.,0.,1.0,1.));
00530   server.HeartBeat();client.HeartBeat();
00531   client.SendCommand(new ippScanOnLineCommand(12  , 1.,1.,1.,10.,10.,10.,0.,0.,1.0,1.));
00532   server.HeartBeat();client.HeartBeat();
00533   client.SendCommand(new ippCommand(14,AbortE));
00534   server.HeartBeat();client.HeartBeat();
00535   client.SendCommand(new ippScanOnLineCommand(16  , 1.,1.,1.,10.,10.,10.,0.,0.,1.0,1.));
00536   server.HeartBeat();client.HeartBeat();
00537 
00538   int watch_dog =1000;
00539   while(watch_dog-->0 && client.NumberOfUncompletedCommandsInQueue() > 0) {
00540     server.HeartBeat();client.HeartBeat();
00541   }
00542 }

Generated on Wed Nov 8 00:20:08 2006 for IPPDME by  doxygen 1.4.1