(file) Return to testSnmpHandler.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler / tests / testclient

   1 martin 1.17 //%LICENSE////////////////////////////////////////////////////////////////
   2 martin 1.18 //
   3 martin 1.17 // Licensed to The Open Group (TOG) under one or more contributor license
   4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
   5             // this work for additional information regarding copyright ownership.
   6             // Each contributor licenses this file to you under the OpenPegasus Open
   7             // Source License; you may not use this file except in compliance with the
   8             // License.
   9 martin 1.18 //
  10 martin 1.17 // Permission is hereby granted, free of charge, to any person obtaining a
  11             // copy of this software and associated documentation files (the "Software"),
  12             // to deal in the Software without restriction, including without limitation
  13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14             // and/or sell copies of the Software, and to permit persons to whom the
  15             // Software is furnished to do so, subject to the following conditions:
  16 martin 1.18 //
  17 martin 1.17 // The above copyright notice and this permission notice shall be included
  18             // in all copies or substantial portions of the Software.
  19 martin 1.18 //
  20 martin 1.17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21 martin 1.18 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22 martin 1.17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27 martin 1.18 //
  28 martin 1.17 //////////////////////////////////////////////////////////////////////////
  29 yi.zhou 1.1  //
  30              //%/////////////////////////////////////////////////////////////////////////////
  31              
  32              #include <Pegasus/Common/PegasusAssert.h>
  33              #include <Pegasus/Common/Thread.h>
  34              #include <Pegasus/Common/Constants.h>
  35              #include <Pegasus/Common/FileSystem.h>
  36 thilo.boehm 1.20 #include <Pegasus/General/Stopwatch.h>
  37 yi.zhou     1.1  #include <Pegasus/Client/CIMClient.h>
  38 dave.sudlik 1.6  #include <Pegasus/Common/HostAddress.h>
  39 sahana.prabhakar 1.22 #ifdef PEGASUS_USE_NET_SNMP
  40                       # include <net-snmp/net-snmp-config.h>
  41                       # include <net-snmp/net-snmp-includes.h>
  42                       #endif
  43 yi.zhou          1.1  
  44                       PEGASUS_USING_PEGASUS;
  45                       PEGASUS_USING_STD;
  46                       
  47 karl             1.4  // Interop namespace used with PEGASUS_NAMESPACENAME_INTEROP in Constants.h
  48 kumpf            1.7  const CIMNamespaceName SOURCE_NAMESPACE =
  49 kumpf            1.9      CIMNamespaceName ("test/TestProvider");
  50 yi.zhou          1.1  
  51 kumpf            1.9  const String INDICATION_CLASS_NAME = "Test_IndicationProviderClass";
  52 yi.zhou          1.1  
  53 kumpf            1.9  const String SNMPV1_HANDLER_NAME = "SNMPHandler01";
  54                       const String SNMPV2C_HANDLER_NAME = "SNMPHandler02";
  55                       const String SNMPV2C_IPV6_HANDLER_NAME = "SNMPHandler03";
  56 sahana.prabhakar 1.21 const String SNMPV3_HANDLER_NAME = "SNMPHandler04";
  57 denise.eckstein  1.16 const String FILTER_NAME = "SNMPIPFilter01";
  58 yi.zhou          1.1  
  59 sahana.prabhakar 1.21 enum SNMPVersion {_SNMPV1_TRAP = 2, _SNMPV2C_TRAP = 3, _SNMPV3_TRAP=5};
  60 dave.sudlik      1.6  enum TargetHostFormat {_HOST_NAME = 2, _IPV4_ADDRESS = 3, _IPV6_ADDRESS = 4};
  61 yi.zhou          1.1  
  62                       #define PORT_NUMBER 2006
  63                       
  64                       AtomicInt errorsEncountered(0);
  65                       
  66                       ////////////////////////////////////////////////////////////////////////////////
  67                       //
  68                       // Thread Parameters Class
  69                       //
  70                       ////////////////////////////////////////////////////////////////////////////////
  71                       
  72 kumpf            1.7  class T_Parms
  73                       {
  74                       public:
  75 kumpf            1.15     CIMClient* client;
  76 yi.zhou          1.1      Uint32 indicationSendCount;
  77                           Uint32 uniqueID;
  78                       };
  79                       
  80                       ///////////////////////////////////////////////////////////////////////////
  81                       
  82 kumpf            1.7  CIMObjectPath _getFilterObjectPath(const String & name)
  83 yi.zhou          1.1  {
  84                           Array<CIMKeyBinding> keyBindings;
  85                           keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
  86                               System::getSystemCreationClassName (), CIMKeyBinding::STRING));
  87                           keyBindings.append (CIMKeyBinding ("SystemName",
  88                               System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
  89                           keyBindings.append (CIMKeyBinding ("CreationClassName",
  90                               PEGASUS_CLASSNAME_INDFILTER.getString(), CIMKeyBinding::STRING));
  91                           keyBindings.append (CIMKeyBinding ("Name", name,
  92                               CIMKeyBinding::STRING));
  93                           return(CIMObjectPath("", CIMNamespaceName (),
  94                               PEGASUS_CLASSNAME_INDFILTER, keyBindings));
  95                       }
  96                       
  97 kumpf            1.7  CIMObjectPath _getHandlerObjectPath(const String & name)
  98 yi.zhou          1.1  {
  99                           Array<CIMKeyBinding> keyBindings;
 100                           keyBindings.append (CIMKeyBinding ("SystemCreationClassName",
 101                               System::getSystemCreationClassName (), CIMKeyBinding::STRING));
 102                           keyBindings.append (CIMKeyBinding ("SystemName",
 103                               System::getFullyQualifiedHostName (), CIMKeyBinding::STRING));
 104                           keyBindings.append (CIMKeyBinding ("CreationClassName",
 105                               PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString(),
 106                               CIMKeyBinding::STRING));
 107                           keyBindings.append (CIMKeyBinding ("Name", name,
 108                               CIMKeyBinding::STRING));
 109                           return(CIMObjectPath("", CIMNamespaceName (),
 110                               PEGASUS_CLASSNAME_INDHANDLER_SNMP, keyBindings));
 111                       }
 112                       
 113 kumpf            1.7  CIMObjectPath _getSubscriptionObjectPath(
 114                           const String & filterName,
 115                           const String & handlerName)
 116 yi.zhou          1.1  {
 117                           CIMObjectPath filterObjectPath = _getFilterObjectPath(filterName);
 118                       
 119                           CIMObjectPath handlerObjectPath = _getHandlerObjectPath(handlerName);
 120                       
 121                           Array<CIMKeyBinding> subscriptionKeyBindings;
 122                           subscriptionKeyBindings.append (CIMKeyBinding ("Filter",
 123                               CIMValue(filterObjectPath)));
 124                           subscriptionKeyBindings.append (CIMKeyBinding ("Handler",
 125                               CIMValue(handlerObjectPath)));
 126                           return(CIMObjectPath("", CIMNamespaceName (),
 127                               PEGASUS_CLASSNAME_INDSUBSCRIPTION, subscriptionKeyBindings));
 128                       }
 129                       
 130 kumpf            1.7  CIMObjectPath _createHandlerInstance(
 131                           CIMClient & client,
 132                           const String & name,
 133                           const String & targetHost,
 134                           const String & securityName,
 135                           const Uint16 targetHostFormat,
 136 sahana.prabhakar 1.21     const Uint16 snmpVersion,
 137                           const String & snmpEngineID,
 138                           const Uint8 & snmpSecLevel,
 139                           const Uint8 & snmpSecAuthProto,
 140                           const String & snmpSecAuthKey,
 141                           const Uint8 & snmpSecPrivProto,
 142                           const String & snmpSecPrivKey)
 143 yi.zhou          1.1  {
 144                           CIMInstance handlerInstance (PEGASUS_CLASSNAME_INDHANDLER_SNMP);
 145                           handlerInstance.addProperty (CIMProperty (CIMName
 146                               ("SystemCreationClassName"), System::getSystemCreationClassName ()));
 147                           handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"),
 148                               System::getFullyQualifiedHostName ()));
 149                           handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
 150                               PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ()));
 151                           handlerInstance.addProperty (CIMProperty (CIMName ("Name"), name));
 152                           handlerInstance.addProperty (CIMProperty (CIMName ("TargetHost"),
 153                               targetHost));
 154                           handlerInstance.addProperty (CIMProperty (CIMName ("TargetHostFormat"),
 155                               CIMValue ((Uint16) targetHostFormat)));
 156                           handlerInstance.addProperty (CIMProperty (CIMName ("SNMPSecurityName"),
 157                               securityName));
 158                           handlerInstance.addProperty (CIMProperty (CIMName ("SnmpVersion"),
 159                               CIMValue ((Uint16) snmpVersion)));
 160                           handlerInstance.addProperty (CIMProperty (CIMName ("PortNumber"),
 161                               CIMValue ((Uint32) PORT_NUMBER)));
 162 sahana.prabhakar 1.22 
 163                       #ifdef PEGASUS_ENABLE_NET_SNMPV3
 164 sahana.prabhakar 1.21     if(snmpVersion == _SNMPV3_TRAP)
 165                           {
 166                       
 167                               handlerInstance.addProperty (CIMProperty (
 168                                   CIMName ("SNMPEngineID"),snmpEngineID));
 169                               handlerInstance.addProperty (CIMProperty (
 170                                   CIMName ("SNMPSecurityLevel"),snmpSecLevel)); //AuthPriv
 171                               handlerInstance.addProperty (CIMProperty (
 172                                   CIMName ("SNMPSecurityAuthProtocol"),snmpSecAuthProto));
 173                       
 174                               oid *snmpSecAuthProtoOid;
 175                               size_t snmpSecAuthProtoLen=0;
 176                        
 177                               if(snmpSecAuthKey.size() > 0)
 178                               {
 179                                  if(snmpSecAuthProto == 1)
 180                                   {
 181                                       snmpSecAuthProtoOid = snmp_duplicate_objid(
 182                                           usmHMACMD5AuthProtocol,
 183                                           USM_AUTH_PROTO_MD5_LEN);
 184                                       snmpSecAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
 185 sahana.prabhakar 1.21             }
 186                                   else if(snmpSecAuthProto == 2)
 187                                   {
 188                                       snmpSecAuthProtoOid = snmp_duplicate_objid(
 189                                           usmHMACSHA1AuthProtocol,
 190                                           USM_AUTH_PROTO_SHA_LEN);
 191                                       snmpSecAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
 192                                   }
 193                                   else 
 194                                   {
 195                                       cout << "Invalid authentication protocol specified to " << 
 196                                           "create handler." << endl;
 197                                       PEGASUS_TEST_ASSERT(false);
 198                                   }
 199                       
 200                                   CString snmpSecAuthKeyCstr = snmpSecAuthKey.getCString();
 201                                   size_t authKeyLen = snmpSecAuthKey.size();
 202                                   u_char * snmpSecAuthKeyPtr = (u_char *)malloc(authKeyLen);
 203                                   u_char * encryptedSecurityAuthKey = (u_char *)malloc(authKeyLen);
 204                                   size_t securityAuthKeyLen = USM_AUTH_KU_LEN;
 205                                   memcpy(snmpSecAuthKeyPtr,(const char *)snmpSecAuthKeyCstr,
 206 sahana.prabhakar 1.21                 strlen(snmpSecAuthKeyCstr));
 207                                   snmpSecAuthKeyPtr[authKeyLen] = '\0';
 208                        
 209                                   if(generate_Ku(snmpSecAuthProtoOid,
 210                                       snmpSecAuthProtoLen,
 211                                       snmpSecAuthKeyPtr,
 212                                       strlen(snmpSecAuthKeyCstr),
 213                                       encryptedSecurityAuthKey,
 214                                       &securityAuthKeyLen) != SNMPERR_SUCCESS)
 215                                   {
 216                                       cout << "Failed to generate the snmp authentication key" 
 217                                           << endl;
 218                                       free(snmpSecAuthKeyPtr);
 219                                       free(encryptedSecurityAuthKey);
 220                                       PEGASUS_TEST_ASSERT(false);
 221                                   }
 222                       
 223                                   Array<Uint8> authKey;
 224                                   for(Uint32 i=0; i<securityAuthKeyLen; i++)
 225                                   {
 226                                       authKey.append(encryptedSecurityAuthKey[i]);
 227 sahana.prabhakar 1.21             }
 228                                   handlerInstance.addProperty (CIMProperty (
 229                                       CIMName ("SNMPSecurityAuthKey"),
 230                                       authKey));
 231                                   free(snmpSecAuthKeyPtr);
 232                                   free(encryptedSecurityAuthKey);
 233                               }
 234                       
 235                               handlerInstance.addProperty (CIMProperty (
 236                                   CIMName ("SNMPSecurityPrivProtocol"),snmpSecPrivProto)); 
 237                        
 238                               if(snmpSecPrivKey.size() > 0 )
 239                               {
 240                                   CString snmpSecPrivKeyCstr = snmpSecPrivKey.getCString();
 241                                   u_char * snmpSecPrivKeyPtr = (u_char *)malloc(USM_PRIV_KU_LEN);
 242                                   u_char * encryptedSecurityPrivKey = 
 243                                       (u_char *)malloc(USM_PRIV_KU_LEN);
 244                                   size_t securityPrivKeyLen = USM_PRIV_KU_LEN;
 245                                   memcpy(snmpSecPrivKeyPtr,(const char *)snmpSecPrivKeyCstr,
 246                                       USM_PRIV_KU_LEN);
 247                                   if(generate_Ku(snmpSecAuthProtoOid,
 248 sahana.prabhakar 1.21                 snmpSecAuthProtoLen,
 249                                       snmpSecPrivKeyPtr,
 250                                       strlen(snmpSecPrivKeyCstr),
 251                                       encryptedSecurityPrivKey,
 252                                       &securityPrivKeyLen) != SNMPERR_SUCCESS)
 253                                   {
 254                                       cout << "Failed to generate the snmp privacy key" 
 255                                           << endl;
 256                                       free(snmpSecPrivKeyPtr);
 257                                       free(encryptedSecurityPrivKey);
 258                                       PEGASUS_TEST_ASSERT(false);
 259                                   }
 260                            
 261                                   Array<Uint8> privKey;
 262                                   for(Uint32 i=0; i<securityPrivKeyLen; i++)
 263                                   {
 264                                       privKey.append(encryptedSecurityPrivKey[i]);
 265                                   }
 266                       
 267                                   handlerInstance.addProperty (CIMProperty (
 268                                       CIMName ("SNMPSecurityPrivKey"),
 269 sahana.prabhakar 1.21                 privKey));
 270                                   free(snmpSecPrivKeyPtr);
 271                                   free(encryptedSecurityPrivKey);
 272                               }  
 273                           }
 274 sahana.prabhakar 1.22 #endif // ifdef PEGASUS_ENABLE_NET_SNMPV3
 275 yi.zhou          1.1  
 276 kumpf            1.7      return client.createInstance(
 277                               PEGASUS_NAMESPACENAME_INTEROP, handlerInstance);
 278 yi.zhou          1.1  }
 279                       
 280 kumpf            1.7  CIMObjectPath _createFilterInstance(
 281                           CIMClient & client,
 282                           const String & name,
 283                           const String & query,
 284                           const String & qlang)
 285 yi.zhou          1.1  {
 286                           CIMInstance filterInstance (PEGASUS_CLASSNAME_INDFILTER);
 287                           filterInstance.addProperty (CIMProperty (CIMName
 288                               ("SystemCreationClassName"), System::getSystemCreationClassName ()));
 289                           filterInstance.addProperty (CIMProperty (CIMName ("SystemName"),
 290                               System::getFullyQualifiedHostName ()));
 291                           filterInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
 292                               PEGASUS_CLASSNAME_INDFILTER.getString ()));
 293                           filterInstance.addProperty (CIMProperty (CIMName ("Name"), name));
 294                           filterInstance.addProperty (CIMProperty (CIMName ("Query"), query));
 295                           filterInstance.addProperty (CIMProperty (CIMName ("QueryLanguage"),
 296                               String (qlang)));
 297                           filterInstance.addProperty (CIMProperty (CIMName ("SourceNamespace"),
 298                               SOURCE_NAMESPACE.getString ()));
 299                       
 300 kumpf            1.7      return client.createInstance(PEGASUS_NAMESPACENAME_INTEROP, filterInstance);
 301 yi.zhou          1.1  }
 302                       
 303 kumpf            1.7  CIMObjectPath _createSubscriptionInstance(
 304                           CIMClient & client,
 305                           const CIMObjectPath & filterPath,
 306                           const CIMObjectPath & handlerPath)
 307 yi.zhou          1.1  {
 308                           CIMInstance subscriptionInstance (PEGASUS_CLASSNAME_INDSUBSCRIPTION);
 309                           subscriptionInstance.addProperty (CIMProperty (CIMName ("Filter"),
 310                               filterPath, 0, PEGASUS_CLASSNAME_INDFILTER));
 311                           subscriptionInstance.addProperty (CIMProperty (CIMName ("Handler"),
 312                               handlerPath, 0, PEGASUS_CLASSNAME_INDHANDLER_SNMP));
 313                           subscriptionInstance.addProperty (CIMProperty
 314                               (CIMName ("SubscriptionState"), CIMValue ((Uint16) 2)));
 315                       
 316 kumpf            1.7      return client.createInstance(
 317                               PEGASUS_NAMESPACENAME_INTEROP, subscriptionInstance);
 318 yi.zhou          1.1  }
 319                       
 320 kumpf            1.7  void _sendTestIndication(
 321                           CIMClient* client,
 322                           const CIMName & methodName,
 323                           Uint32 indicationSendCount)
 324 yi.zhou          1.1  {
 325                           //
 326                           //  Invoke method to send test indication
 327                           //
 328                           Array <CIMParamValue> inParams;
 329                           Array <CIMParamValue> outParams;
 330                           Array <CIMKeyBinding> keyBindings;
 331                           Sint32 result;
 332                       
 333                           CIMObjectPath className (String::EMPTY, CIMNamespaceName (),
 334 kumpf            1.9          CIMName ("Test_IndicationProviderClass"), keyBindings);
 335 yi.zhou          1.1  
 336 kumpf            1.7      inParams.append(CIMParamValue(String("indicationSendCount"),
 337 yi.zhou          1.1          CIMValue(indicationSendCount)));
 338                       
 339                           CIMValue retValue = client->invokeMethod
 340                               (SOURCE_NAMESPACE,
 341                                className,
 342                                methodName,
 343                                inParams,
 344                                outParams);
 345                       
 346                           retValue.get (result);
 347                           PEGASUS_TEST_ASSERT (result == 0);
 348                       }
 349                       
 350 kumpf            1.7  void _deleteSubscriptionInstance(
 351                           CIMClient & client,
 352                           const String & filterName,
 353                           const String & handlerName)
 354 yi.zhou          1.1  {
 355                           CIMObjectPath subscriptionObjectPath =
 356                              _getSubscriptionObjectPath(filterName, handlerName);
 357 kumpf            1.7      client.deleteInstance(
 358                               PEGASUS_NAMESPACENAME_INTEROP, subscriptionObjectPath);
 359 yi.zhou          1.1  }
 360                       
 361 kumpf            1.7  void _deleteHandlerInstance(
 362                           CIMClient & client,
 363                           const String & name)
 364 yi.zhou          1.1  {
 365                           CIMObjectPath handlerObjectPath = _getHandlerObjectPath(name);
 366 karl             1.4      client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, handlerObjectPath);
 367 yi.zhou          1.1  }
 368                       
 369 kumpf            1.7  void _deleteFilterInstance(
 370                           CIMClient & client,
 371                           const String & name)
 372 yi.zhou          1.1  {
 373                           CIMObjectPath filterObjectPath = _getFilterObjectPath(name);
 374 karl             1.4      client.deleteInstance (PEGASUS_NAMESPACENAME_INTEROP, filterObjectPath);
 375 yi.zhou          1.1  }
 376                       
 377 kumpf            1.7  void _usage()
 378 yi.zhou          1.1  {
 379                          cerr << endl
 380                               << "Usage:" << endl
 381                               << "    TestSnmpHandler setup [ WQL | DMTF:CQL ]\n"
 382                               << "    TestSnmpHandler run <indicationSendCount> "
 383                               << "[<threads>]\n"
 384                               << "    where: " << endl
 385                               << "       <indicationSendCount> is the number of indications to\n"
 386                               << "            generate and has to be greater than zero." << endl
 387                               << "       <threads> is an optional number of client threads to\n"
 388                               << "            create, default is one." << endl
 389                               << "    TestSnmpHandler cleanup\n"
 390 sahana.prabhakar 1.21         << "    TestSnmpHandler removelog\n\n"
 391                               << "Note :\n"
 392                               << "For running snmp v3 tests create an user by name \"sahana\" in\n"
 393                               << "smpd.conf and snmptrapd.conf with the following credentials :- \n"
 394                               << "engineId = 0x80001f88808a67e858ee38ec4c \n"
 395                               << "Authentication protocol = MD5 \n"
 396                               << "Privacy Protocol = DES \n"
 397                               << "Authentication key = setup_passphrase \n"
 398 yi.zhou          1.1          << endl << endl;
 399                       }
 400                       
 401                       void _setup (CIMClient & client, const String& qlang)
 402                       {
 403                           CIMObjectPath filterObjectPath;
 404                           CIMObjectPath snmpv1HandlerObjectPath;
 405                           CIMObjectPath snmpv2HandlerObjectPath;
 406 yi.zhou          1.8      CIMObjectPath snmpv2IPV6HandlerObjectPath;
 407 sahana.prabhakar 1.21     CIMObjectPath snmpv3HandlerObjectPath;
 408 yi.zhou          1.1  
 409                           try
 410                           {
 411                               filterObjectPath = _createFilterInstance (client, FILTER_NAME,
 412 kumpf            1.9              String ("SELECT * FROM Test_IndicationProviderClass"),
 413 yi.zhou          1.1              qlang);
 414                           }
 415                           catch (CIMException& e)
 416                           {
 417                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 418                               {
 419                                   filterObjectPath = _getFilterObjectPath(FILTER_NAME);
 420                                   cerr << "----- Warning: Filter Instance Not Created: "
 421                                       << e.getMessage () << endl;
 422                               }
 423                               else
 424                               {
 425                                   cerr << "----- Error: Filter Instance Not Created: " << endl;
 426                                   throw;
 427                               }
 428                           }
 429                       
 430                           try
 431                           {
 432 kumpf            1.7          // Create SNMPv1 trap handler
 433 yi.zhou          1.1          snmpv1HandlerObjectPath = _createHandlerInstance (client,
 434                                   SNMPV1_HANDLER_NAME,
 435                                   System::getFullyQualifiedHostName(),
 436                                   "",
 437                                   _HOST_NAME,
 438 sahana.prabhakar 1.21             _SNMPV1_TRAP,
 439                                   String(),0,1,String(),1,String());
 440 yi.zhou          1.1      }
 441                           catch (CIMException& e)
 442                           {
 443                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 444                               {
 445                                   snmpv1HandlerObjectPath = _getHandlerObjectPath(
 446                                       SNMPV1_HANDLER_NAME);
 447                                   cerr << "----- Warning: SNMPv1 Trap Handler Instance Not Created: "
 448                                       << e.getMessage () << endl;
 449                               }
 450                               else
 451                               {
 452                                   cerr << "----- Error: SNMPv1 Trap Handler Instance Not Created: "
 453                                       << endl;
 454                                   throw;
 455                               }
 456                           }
 457                       
 458                           try
 459                           {
 460                               _createSubscriptionInstance (client, filterObjectPath,
 461 yi.zhou          1.1               snmpv1HandlerObjectPath);
 462                           }
 463                           catch (CIMException& e)
 464                           {
 465                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 466                               {
 467                                   cerr << "----- Warning: Client Subscription Instance: "
 468                                       << e.getMessage () << endl;
 469                               }
 470                               else
 471                               {
 472                                   cerr << "----- Error: Client Subscription Instance: " << endl;
 473                                   throw;
 474                               }
 475                           }
 476                       
 477                           try
 478                           {
 479 dave.sudlik      1.6          String ipAddress;
 480                               int af;
 481                               System::getHostIP(System::getFullyQualifiedHostName (), &af, ipAddress);
 482 kumpf            1.7          // Create SNMPv2 trap handler
 483 yi.zhou          1.1          snmpv2HandlerObjectPath = _createHandlerInstance (client,
 484                                   SNMPV2C_HANDLER_NAME,
 485 dave.sudlik      1.6              ipAddress,
 486 yi.zhou          1.1              "public",
 487 dave.sudlik      1.6              af == AF_INET ? _IPV4_ADDRESS : _IPV6_ADDRESS,
 488 sahana.prabhakar 1.21             _SNMPV2C_TRAP,
 489                                   String(),0,1,String(),1,String());
 490 yi.zhou          1.1      }
 491                           catch (CIMException& e)
 492                           {
 493                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 494                               {
 495                                   snmpv2HandlerObjectPath = _getHandlerObjectPath(
 496                                       SNMPV2C_HANDLER_NAME);
 497                                   cerr << "----- Warning: SNMPv2c Trap Handler Instance Not Created: "
 498                                       << e.getMessage () << endl;
 499                               }
 500                               else
 501                               {
 502                                   cerr << "----- Error: SNMPv2c Trap Handler Instance Not Created: "
 503                                       << endl;
 504                                   throw;
 505                               }
 506                           }
 507                       
 508                           try
 509                           {
 510                               _createSubscriptionInstance (client, filterObjectPath,
 511 yi.zhou          1.1               snmpv2HandlerObjectPath);
 512                           }
 513                           catch (CIMException& e)
 514                           {
 515                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 516                               {
 517                                   cerr << "----- Warning: Client Subscription Instance: "
 518                                       << e.getMessage () << endl;
 519                               }
 520                               else
 521                               {
 522                                   cerr << "----- Error: Client Subscription Instance: " << endl;
 523                                   throw;
 524                               }
 525                           }
 526 yi.zhou          1.8  
 527                       #if defined(PEGASUS_ENABLE_IPV6)
 528                           // create a subscription with trap destination of IPV6 address format
 529                           try
 530                           {
 531                               // Create SNMPv2 IPV6 trap handler
 532                               snmpv2IPV6HandlerObjectPath = _createHandlerInstance (client,
 533                                   SNMPV2C_IPV6_HANDLER_NAME,
 534                                   String("::1"),
 535                                   "public",
 536                                   _IPV6_ADDRESS,
 537 sahana.prabhakar 1.21             _SNMPV2C_TRAP,
 538                                   String(),0,1,String(),1,String());
 539 kumpf            1.19     }
 540 yi.zhou          1.8      catch (CIMException& e)
 541                           {
 542                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 543                               {
 544                                   snmpv2IPV6HandlerObjectPath = _getHandlerObjectPath(
 545                                       SNMPV2C_IPV6_HANDLER_NAME);
 546                                   cerr << "----- Warning: SNMPv2c IPV6 Trap Handler Instance "
 547                                       "Not Created: " << e.getMessage () << endl;
 548                               }
 549                               else
 550                               {
 551                                   cerr << "----- Error: SNMPv2c IPV6 Trap Handler Instance Not "
 552                                       "Created: " << endl;
 553                                   throw;
 554                               }
 555                           }
 556                       
 557                           try
 558                           {
 559                               _createSubscriptionInstance (client, filterObjectPath,
 560                                    snmpv2IPV6HandlerObjectPath);
 561 yi.zhou          1.8      }
 562                           catch (CIMException& e)
 563                           {
 564                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 565                               {
 566                                   cerr << "----- Warning: Client Subscription Instance: "
 567                                       << e.getMessage () << endl;
 568                               }
 569                               else
 570                               {
 571                                   cerr << "----- Error: Client Subscription Instance: " << endl;
 572                                   throw;
 573                               }
 574                           }
 575                       #endif
 576 sahana.prabhakar 1.21 
 577 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 578 sahana.prabhakar 1.21     // create a snmp V3 trap handler.
 579                           try
 580                           {
 581                               String ipAddress;
 582                               int af;
 583                               System::getHostIP(System::getFullyQualifiedHostName (), &af, ipAddress);
 584                               // Create SNMPv3 trap handler
 585                               snmpv3HandlerObjectPath = _createHandlerInstance (client,
 586                                   SNMPV3_HANDLER_NAME,
 587                                   System::getFullyQualifiedHostName(),
 588                                   "sahana",
 589                                   _HOST_NAME,
 590                                   _SNMPV3_TRAP, 
 591                                   "0x80001f88808a67e858ee38ec4c",
 592                                   3,
 593                                   1,
 594                                   "setup_passphrase",
 595                                   1,
 596                                   "setup_passphrase");
 597                       
 598                           }
 599 sahana.prabhakar 1.21     catch (CIMException& e)
 600                           {
 601                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 602                               {
 603                                   snmpv3HandlerObjectPath = _getHandlerObjectPath(
 604                                       SNMPV2C_IPV6_HANDLER_NAME);
 605                                   cerr << "----- Warning: SNMPv3 Trap Handler Instance "
 606                                       "Not Created: " << e.getMessage () << endl;
 607                               }
 608                               else
 609                               {
 610                                   cerr << "----- Error: SNMPv3 Trap Handler Instance Not "
 611                                       "Created: " << endl;
 612                                   throw;
 613                               }
 614                           }
 615                       
 616                           try
 617                           {
 618                               _createSubscriptionInstance (client, filterObjectPath,
 619                                    snmpv3HandlerObjectPath);
 620 sahana.prabhakar 1.21     }
 621                           catch (CIMException& e)
 622                           {
 623                               if (e.getCode() == CIM_ERR_ALREADY_EXISTS)
 624                               {
 625                                   cerr << "----- Warning: Client Subscription Instance: "
 626                                       << e.getMessage () << endl;
 627                               }
 628                               else
 629                               {
 630                                   cerr << "----- Error: Client Subscription Instance: " << endl;
 631                                   throw;
 632                               }
 633                           }
 634 sahana.prabhakar 1.22 #endif // ifdef PEGASUS_ENABLE_NET_SNMPV3
 635 yi.zhou          1.1  }
 636                       
 637                       void _cleanup (CIMClient & client)
 638                       {
 639                           try
 640                           {
 641                               _deleteSubscriptionInstance (client, FILTER_NAME,
 642                                   SNMPV1_HANDLER_NAME);
 643                           }
 644                           catch (CIMException& e)
 645                           {
 646                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 647                               {
 648                                   cerr << "----- Error: deleteSubscriptionInstance failure: "
 649                                        << endl;
 650                                   throw;
 651                               }
 652                           }
 653                           try
 654                           {
 655                               _deleteSubscriptionInstance (client, FILTER_NAME,
 656 yi.zhou          1.1              SNMPV2C_HANDLER_NAME);
 657                           }
 658                           catch (CIMException& e)
 659                           {
 660                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 661                               {
 662                                   cerr << "----- Error: deleteSubscriptionInstance failure: "
 663                                        << endl;
 664                                   throw;
 665                               }
 666                           }
 667 yi.zhou          1.8  
 668                       #if defined(PEGASUS_ENABLE_IPV6)
 669 kumpf            1.19     try
 670 yi.zhou          1.8      {
 671                               _deleteSubscriptionInstance (client, FILTER_NAME,
 672                                   SNMPV2C_IPV6_HANDLER_NAME);
 673                           }
 674                           catch (CIMException& e)
 675                           {
 676                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 677                               {
 678                                   cerr << "----- Error: deleteSubscriptionInstance failure: "
 679                                        << endl;
 680                                   throw;
 681                               }
 682                           }
 683                       #endif
 684                       
 685 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 686 yi.zhou          1.1      try
 687                           {
 688 sahana.prabhakar 1.21         _deleteSubscriptionInstance (client, FILTER_NAME,
 689                                   SNMPV3_HANDLER_NAME);
 690                           }
 691                           catch (CIMException& e)
 692                           {
 693                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 694                               {
 695                                   cerr << "----- Error: deleteSubscriptionInstance failure: "
 696                                        << endl;
 697                                   throw;
 698                               }
 699                           }
 700 sahana.prabhakar 1.22 #endif
 701 sahana.prabhakar 1.21 
 702                           try
 703                           {
 704 yi.zhou          1.1          _deleteFilterInstance (client, FILTER_NAME);
 705                           }
 706                           catch (CIMException& e)
 707                           {
 708                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 709                               {
 710                                   cerr << "----- Error: deleteFilterInstance failure: " << endl;
 711                                   throw;
 712                               }
 713                           }
 714                       
 715                           try
 716                           {
 717                               _deleteHandlerInstance (client, SNMPV1_HANDLER_NAME);
 718                           }
 719                           catch (CIMException& e)
 720                           {
 721                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 722                               {
 723                                   cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 724                                   throw;
 725 yi.zhou          1.1          }
 726                           }
 727                           try
 728                           {
 729                               _deleteHandlerInstance (client, SNMPV2C_HANDLER_NAME);
 730                           }
 731                           catch (CIMException& e)
 732                           {
 733                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 734                               {
 735                                   cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 736                                   throw;
 737                               }
 738                           }
 739 yi.zhou          1.8  #if defined(PEGASUS_ENABLE_IPV6)
 740                           try
 741                           {
 742                               _deleteHandlerInstance (client, SNMPV2C_IPV6_HANDLER_NAME);
 743                           }
 744                           catch (CIMException& e)
 745                           {
 746                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 747                               {
 748                                   cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 749                                   throw;
 750                               }
 751                           }
 752                       #endif
 753 sahana.prabhakar 1.21 
 754 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 755 sahana.prabhakar 1.21     try
 756                           {
 757                               _deleteHandlerInstance (client, SNMPV3_HANDLER_NAME);
 758                           }
 759                           catch (CIMException& e)
 760                           {
 761                               if (e.getCode() != CIM_ERR_NOT_FOUND)
 762                               {
 763                                   cerr << "----- Error: deleteHandlerInstance failure: " << endl;
 764                                   throw;
 765                               }
 766                           }
 767 sahana.prabhakar 1.22 #endif
 768 yi.zhou          1.1  }
 769                       
 770                       static void _testEnd(const String& uniqueID, const double elapsedTime)
 771                       {
 772                           cout << "+++++ thread" << uniqueID << ": passed in " << elapsedTime
 773                               << " seconds" << endl;
 774                       }
 775                       
 776 mike             1.3  ThreadReturnType PEGASUS_THREAD_CDECL _executeTests(void *parm)
 777 yi.zhou          1.1  {
 778                           Thread *my_thread = (Thread *)parm;
 779 kumpf            1.15     AutoPtr<T_Parms> parms((T_Parms *)my_thread->get_parm());
 780                           CIMClient* client = parms->client;
 781 yi.zhou          1.1      Uint32 indicationSendCount = parms->indicationSendCount;
 782                           Uint32 id = parms->uniqueID;
 783                           char id_[4];
 784                           memset(id_,0x00,sizeof(id_));
 785 kumpf            1.14     sprintf(id_,"%u",id);
 786 yi.zhou          1.1      String uniqueID = "_";
 787                           uniqueID.append(id_);
 788                       
 789                           try
 790                           {
 791                               Stopwatch elapsedTime;
 792                       
 793                               elapsedTime.start();
 794                               try
 795                               {
 796                                   _sendTestIndication (client, CIMName ("SendTestIndicationTrap"),
 797                                   indicationSendCount);
 798                               }
 799                               catch (Exception & e)
 800                               {
 801 kumpf            1.7              cerr << "----- sendTestIndication failed: " << e.getMessage () <<
 802                                       endl;
 803 yi.zhou          1.1              exit (-1);
 804                               }
 805                               elapsedTime.stop();
 806                               _testEnd(uniqueID, elapsedTime.getElapsed());
 807                           }
 808                           catch(Exception  & e)
 809                           {
 810                               cout << e.getMessage() << endl;
 811                           }
 812 kumpf            1.12 
 813                           return ThreadReturnType(0);
 814 yi.zhou          1.1  }
 815                       
 816                       Thread * _runTestThreads(
 817                           CIMClient* client,
 818                           Uint32 indicationSendCount,
 819                           Uint32 uniqueID)
 820                       {
 821                           // package parameters, create thread and run...
 822                           AutoPtr<T_Parms> parms(new T_Parms());
 823 kumpf            1.15     parms->client = client;
 824 yi.zhou          1.1      parms->indicationSendCount = indicationSendCount;
 825                           parms->uniqueID = uniqueID;
 826                           AutoPtr<Thread> t(new Thread(_executeTests, (void*)parms.release(), false));
 827                           t->run();
 828                           return t.release();
 829                       }
 830                       
 831 yi.zhou          1.13 Uint32 _getReceivedTrapCount(Uint16 snmpVersion, const String& logFile)
 832 yi.zhou          1.1  {
 833                           String trap1 = "Trap Info: TRAP, SNMP v1, community public";
 834                           String trap2 = "Trap Info: TRAP2, SNMP v2c, community public";
 835 sahana.prabhakar 1.21     String trap3 = "Trap Info: TRAP2, SNMP v3, user sahana, context ";
 836 yi.zhou          1.1  
 837                           Uint32 receivedTrap1Count = 0;
 838                           Uint32 receivedTrap2Count = 0;
 839 marek            1.23 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 840 sahana.prabhakar 1.21     Uint32 receivedTrap3Count = 0;
 841 marek            1.23 #endif
 842 kumpf            1.7  
 843 yi.zhou          1.13     ifstream ifs(logFile.getCString());
 844 yi.zhou          1.1      if (!ifs)
 845                           {
 846                               return (0);
 847                           }
 848                       
 849                           String line;
 850                           while (GetLine(ifs, line))
 851                           {
 852                               if (String::compare(line, trap1) == 0)
 853                               {
 854                                   receivedTrap1Count++;
 855                               }
 856                               if (String::compare(line, trap2) == 0)
 857                               {
 858                                   receivedTrap2Count++;
 859                               }
 860 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 861 sahana.prabhakar 1.21         if (String::compare(line, trap3) == 0)
 862                               {
 863                                   receivedTrap3Count++;
 864                               }
 865 sahana.prabhakar 1.22 #endif
 866 yi.zhou          1.1      }
 867                       
 868                           ifs.close();
 869                       
 870                           switch (snmpVersion)
 871                           {
 872                               case _SNMPV1_TRAP:
 873                               {
 874                                   return (receivedTrap1Count);
 875                               }
 876                               case _SNMPV2C_TRAP:
 877                               {
 878                                   return (receivedTrap2Count);
 879                               }
 880 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
 881 sahana.prabhakar 1.21         case _SNMPV3_TRAP:
 882                               {
 883                                   return (receivedTrap3Count);
 884                               }
 885 sahana.prabhakar 1.22 #endif
 886 yi.zhou          1.1          default:
 887                               {
 888                                   return (0);
 889                               }
 890                           }
 891                       
 892                       }
 893                       
 894                       #ifdef PEGASUS_USE_NET_SNMP
 895 kumpf            1.7  // Stop snmptrapd process if it is running and remove
 896 yi.zhou          1.1  // procIdFile file if it exists
 897 kumpf            1.7  //
 898 yi.zhou          1.13 void _stopSnmptrapd(const String& processIdFile)
 899 yi.zhou          1.1  {
 900                           Uint32 receiverPid;
 901                           FILE *fd;
 902 yi.zhou          1.13     if ((fd = fopen(processIdFile.getCString(), "r")) != NULL)
 903 yi.zhou          1.1      {
 904 kumpf            1.10         if (fscanf(fd, "%d\n", &receiverPid) != 1)
 905                               {
 906                                   throw Exception("Failed to read trapd pid from procIdFile.");
 907                               }
 908 kumpf            1.7  
 909 yi.zhou          1.1          kill(receiverPid, SIGTERM);
 910                       
 911                               fclose(fd);
 912                           }
 913                       
 914 yi.zhou          1.13     if (FileSystem::exists(processIdFile))
 915 yi.zhou          1.1      {
 916 yi.zhou          1.13         FileSystem::removeFile(processIdFile);
 917 yi.zhou          1.1      }
 918                       }
 919                       
 920                       static Boolean _startSnmptrapd(
 921 yi.zhou          1.13     FILE **trapInfo,
 922                           const String& processIdFile,
 923                           const String& logFile)
 924 yi.zhou          1.1  {
 925                           String snmptrapdCmd;
 926                       
 927                           Uint32 portNumber = PORT_NUMBER;
 928                           char portNumberStr[32];
 929                           sprintf(portNumberStr, "%lu", (unsigned long) portNumber);
 930                       
 931                           //
 932                           // build snmptrapd cmd options
 933                           //
 934                       
 935 kumpf            1.7      // Specify logging incoming traps to trapLogFile
 936 yi.zhou          1.1      // Save the process ID of the snmptrapd in procIdFile
 937                           snmptrapdCmd.append(
 938 yi.zhou          1.13         "/usr/sbin/snmptrapd -f -Lf ");
 939                           snmptrapdCmd.append(logFile);
 940                           snmptrapdCmd.append(" -p ");
 941                           snmptrapdCmd.append(processIdFile);
 942 yi.zhou          1.1  
 943                           // Specify incoming trap format
 944                           snmptrapdCmd.append( " -F \"\nTrap Info: %P\nVariable: %v\n\"");
 945                       
 946                           // Specify listening address
 947 yi.zhou          1.8  #if defined(PEGASUS_ENABLE_IPV6)
 948                           snmptrapdCmd.append(" UDP6:");
 949                           snmptrapdCmd.append(portNumberStr);
 950                           snmptrapdCmd.append(",UDP:");
 951                       #else
 952 yi.zhou          1.1      snmptrapdCmd.append(" UDP:");
 953 yi.zhou          1.8  #endif
 954 yi.zhou          1.1      snmptrapdCmd.append(System::getFullyQualifiedHostName ());
 955                       
 956                           snmptrapdCmd.append(":");
 957                           snmptrapdCmd.append(portNumberStr);
 958                       
 959                           if ((*trapInfo = popen(snmptrapdCmd.getCString(), "r")) == NULL)
 960                           {
 961                               throw Exception ("snmptrapd can not be started");
 962                           }
 963                       
 964                       #define MAX_ITERATIONS 300
 965                       #define SLEEP_SEC 1
 966                       
 967                           Uint32 iterations = 0;
 968                       
 969 kumpf            1.7      // Wait until snmptrapd started
 970 yi.zhou          1.1      while (iterations < MAX_ITERATIONS)
 971                           {
 972                               iterations++;
 973 yi.zhou          1.13         if (FileSystem::exists(processIdFile))
 974 yi.zhou          1.1          {
 975                                   return (true);
 976                               }
 977                               else
 978                               {
 979                                   System::sleep(SLEEP_SEC);
 980 kumpf            1.7  
 981 yi.zhou          1.1          }
 982                           }
 983                       
 984                           throw Exception ("snmptrapd can not be started");
 985                       }
 986                       #endif
 987                       
 988 yi.zhou          1.13 void _removeTrapLogFile(const String& logFile)
 989 yi.zhou          1.1  {
 990                           // if trapLogFile exists, remove it
 991                           if (FileSystem::exists(logFile))
 992                           {
 993                               FileSystem::removeFile(logFile);
 994                           }
 995                       }
 996                       
 997 kumpf            1.7  void _receiveExpectedTraps(
 998                           CIMClient& workClient,
 999 yi.zhou          1.1      Uint32 indicationSendCount,
1000 yi.zhou          1.13     Uint32 runClientThreadCount,
1001                           const String& logFile)
1002 yi.zhou          1.1  {
1003 yi.zhou          1.8      Uint32 indicationTrapV1SendCount = 0;
1004                           Uint32 indicationTrapV2SendCount = 0;
1005 sahana.prabhakar 1.21     Uint32 indicationTrapV3SendCount = 0;
1006 yi.zhou          1.8  
1007 yi.zhou          1.1      CIMClient * clientConnections = new CIMClient[runClientThreadCount];
1008                       
1009                           // determine total number of indication send count
1010 yi.zhou          1.8      indicationTrapV1SendCount =
1011                               indicationSendCount * runClientThreadCount;
1012                       
1013                           // if IPV6 is enabled, an additional SNMPv2c trap is sent to IPV6 address
1014                       #if defined(PEGASUS_ENABLE_IPV6)
1015 kumpf            1.19     indicationTrapV2SendCount = 2 * indicationTrapV1SendCount;
1016 yi.zhou          1.8  #else
1017                           indicationTrapV2SendCount = indicationTrapV1SendCount;
1018                       #endif
1019 yi.zhou          1.1  
1020 sahana.prabhakar 1.22 #ifdef PEGASUS_ENABLE_NET_SNMPV3
1021 sahana.prabhakar 1.21     indicationTrapV3SendCount =
1022                               indicationSendCount * runClientThreadCount;
1023 sahana.prabhakar 1.22 #endif
1024 sahana.prabhakar 1.21 
1025 yi.zhou          1.1      // calculate the timeout based on the total send count allowing
1026 kumpf            1.7      // using the MSG_PER_SEC rate
1027                           // allow 20 seconds of test overhead for very small tests
1028 yi.zhou          1.1  
1029                       #define MSG_PER_SEC 4
1030                       
1031 marek            1.5      Uint32 testTimeout = PEGASUS_DEFAULT_CLIENT_TIMEOUT_MILLISECONDS
1032 yi.zhou          1.8                          + (indicationTrapV2SendCount/MSG_PER_SEC)*1000;
1033 yi.zhou          1.1  
1034                           // connect the clients
1035                           for(Uint32 i = 0; i < runClientThreadCount; i++)
1036                           {
1037                               clientConnections[i].setTimeout(testTimeout);
1038                               clientConnections[i].connectLocal();
1039                           }
1040                       
1041                           // run tests
1042                           Thread ** clientThreads = new Thread *[runClientThreadCount];
1043                       
1044                           Stopwatch trapReceiverElapsedTime;
1045                       
1046                           trapReceiverElapsedTime.start();
1047                       
1048                           for(Uint32 i = 0; i < runClientThreadCount; i++)
1049                           {
1050                               clientThreads[i] = _runTestThreads(&clientConnections[i],
1051                                   indicationSendCount, i);
1052                           }
1053                       
1054 yi.zhou          1.1      for(Uint32 i=0; i< runClientThreadCount; i++)
1055                           {
1056                               clientThreads[i]->join();
1057 kumpf            1.15         delete clientThreads[i];
1058 yi.zhou          1.1      }
1059                       
1060                           delete[] clientConnections;
1061                           delete[] clientThreads;
1062                       
1063                           //
1064                           //  Allow time for the trap to be received
1065                           //  Wait in SLEEP_SEC second intervals.
1066                           //  Put msg out every MSG_SEC intervals
1067                           //
1068                       
1069                       #define SLEEP_SEC 1
1070                       #define COUT_TIME_INTERVAL 30
1071                       #define MAX_NO_CHANGE_ITERATIONS COUT_TIME_INTERVAL*3
1072                       
1073                           Uint32 noChangeIterations = 0;
1074                           Uint32 priorReceivedTrap1Count = 0;
1075                           Uint32 priorReceivedTrap2Count = 0;
1076 sahana.prabhakar 1.21     Uint32 priorReceivedTrap3Count = 0;
1077 yi.zhou          1.1      Uint32 currentReceivedTrap1Count = 0;
1078                           Uint32 currentReceivedTrap2Count = 0;
1079 sahana.prabhakar 1.21     Uint32 currentReceivedTrap3Count = 0;
1080 yi.zhou          1.1      Uint32 totalIterations = 0;
1081                       
1082                           //
1083                           // Wait for the trap receiver to receive the expected
1084 yi.zhou          1.8      // number of Indication traps, indicationTrapV1SendCount
1085                           // and indicationTrapV2SendCount.
1086 yi.zhou          1.1      //
1087 yi.zhou          1.8      // We will continue to wait until either (indicationTrapV1SendCount
1088                           // and indicationTrapV2SendCount) Indications have been received
1089                           // by the trap receiver or no new
1090 yi.zhou          1.1      // Indications have been received in the previous
1091                           // MAX_NO_CHANGE_ITERATIONS.
1092 kumpf            1.7      // iterations.
1093 yi.zhou          1.1      //
1094                       
1095                           Boolean receivedTrapCountComplete = false;
1096                           Boolean receiverTrap1NoChange = true;
1097                           Boolean receiverTrap2NoChange = true;
1098 sahana.prabhakar 1.21     Boolean receiverTrap3NoChange = true;
1099 yi.zhou          1.1  
1100                           while (noChangeIterations <= MAX_NO_CHANGE_ITERATIONS)
1101                           {
1102                               totalIterations++;
1103                       
1104 kumpf            1.19         currentReceivedTrap1Count =
1105 yi.zhou          1.13             _getReceivedTrapCount(_SNMPV1_TRAP, logFile);
1106 kumpf            1.19         currentReceivedTrap2Count =
1107 yi.zhou          1.13             _getReceivedTrapCount(_SNMPV2C_TRAP, logFile);
1108 sahana.prabhakar 1.21         currentReceivedTrap3Count =
1109                                   _getReceivedTrapCount(_SNMPV3_TRAP,logFile);
1110 yi.zhou          1.1  
1111                               if (totalIterations % COUT_TIME_INTERVAL == 1 &&
1112                                   !(receivedTrapCountComplete))
1113                               {
1114                                   cout << "++++ The trap receiver has received "
1115                                   << currentReceivedTrap1Count << " of "
1116 yi.zhou          1.8              << indicationTrapV1SendCount << " SNMPv1 trap."
1117 yi.zhou          1.1              << endl;
1118                                   cout << "++++ The trap receiver has received "
1119                                   << currentReceivedTrap2Count << " of "
1120 yi.zhou          1.8              << indicationTrapV2SendCount << " SNMPv2c trap."
1121 yi.zhou          1.1              << endl;
1122 sahana.prabhakar 1.21             cout << "++++ The trap receiver has received "
1123                                   << currentReceivedTrap3Count<< " of "
1124                                   << indicationTrapV3SendCount << " SNMPv3 trap."
1125                                   << endl;
1126 yi.zhou          1.1          }
1127                       
1128 yi.zhou          1.8          if ((indicationTrapV1SendCount == currentReceivedTrap1Count) &&
1129 sahana.prabhakar 1.21             (indicationTrapV2SendCount == currentReceivedTrap2Count) &&
1130                                   (indicationTrapV3SendCount == currentReceivedTrap3Count))
1131 yi.zhou          1.1          {
1132                                    receivedTrapCountComplete = true;
1133                                    trapReceiverElapsedTime.stop();
1134                               }
1135                               if (!(receiverTrap1NoChange =
1136                                       (priorReceivedTrap1Count == currentReceivedTrap1Count)))
1137                               {
1138                                    priorReceivedTrap1Count = currentReceivedTrap1Count;
1139                               }
1140                       
1141                               if (!(receiverTrap2NoChange =
1142                                       (priorReceivedTrap2Count == currentReceivedTrap2Count)))
1143                               {
1144                                    priorReceivedTrap2Count = currentReceivedTrap2Count;
1145                               }
1146 sahana.prabhakar 1.21         if (!(receiverTrap3NoChange =
1147                                       (priorReceivedTrap3Count == currentReceivedTrap3Count)))
1148                               {
1149                                    priorReceivedTrap3Count = currentReceivedTrap3Count;
1150                               }
1151 yi.zhou          1.1  
1152                               if (receivedTrapCountComplete)
1153                               {
1154                                   cout << "++++ The trap receiver has received "
1155                                   << currentReceivedTrap1Count << " of "
1156 yi.zhou          1.8              << indicationTrapV1SendCount << " SNMPv1 trap."
1157 yi.zhou          1.1              << endl;
1158                                   cout << "++++ The trap receiver has received "
1159                                   << currentReceivedTrap2Count << " of "
1160 yi.zhou          1.8              << indicationTrapV2SendCount << " SNMPv2c trap."
1161 yi.zhou          1.1              << endl;
1162 sahana.prabhakar 1.21             cout << "++++ The trap receiver has received "
1163                                   << currentReceivedTrap3Count << " of "
1164                                   << indicationTrapV3SendCount<< " SNMPv3 trap."
1165                                   << endl;
1166 yi.zhou          1.1  
1167                                   break;
1168                               }
1169 sahana.prabhakar 1.21         if (receiverTrap1NoChange || 
1170                                   receiverTrap2NoChange || 
1171                                   receiverTrap3NoChange)
1172 yi.zhou          1.1          {
1173                                  noChangeIterations++;
1174                               }
1175                               else
1176                               {
1177                                  noChangeIterations = 0;
1178                               }
1179                       
1180                               System::sleep (SLEEP_SEC);
1181                           }
1182                       
1183                           if (!receivedTrapCountComplete)
1184                           {
1185                               trapReceiverElapsedTime.stop();
1186                           }
1187                       
1188 kumpf            1.7      // assert that all indications sent have been received.
1189 yi.zhou          1.8      PEGASUS_TEST_ASSERT(indicationTrapV1SendCount ==
1190 yi.zhou          1.1         currentReceivedTrap1Count);
1191 yi.zhou          1.8      PEGASUS_TEST_ASSERT(indicationTrapV2SendCount ==
1192 yi.zhou          1.1         currentReceivedTrap2Count);
1193 sahana.prabhakar 1.21     PEGASUS_TEST_ASSERT(indicationTrapV3SendCount ==
1194                              currentReceivedTrap3Count);
1195 yi.zhou          1.2  }
1196                       
1197 kumpf            1.7  int _beginTest(CIMClient& workClient,
1198 yi.zhou          1.2      Uint32 indicationSendCount,
1199 yi.zhou          1.13     Uint32 runClientThreadCount,
1200                           const String& processIdFile,
1201                           const String& logFile)
1202 yi.zhou          1.2  {
1203 yi.zhou          1.1  
1204                       #ifdef PEGASUS_USE_NET_SNMP
1205 yi.zhou          1.2  
1206                           // Stop snmptrapd process if it is running
1207 yi.zhou          1.13     _stopSnmptrapd(processIdFile);
1208 yi.zhou          1.2  
1209                           // if trapLogFile exists, remove it
1210 yi.zhou          1.13     _removeTrapLogFile(logFile);
1211 yi.zhou          1.2  
1212                           FILE * trapInfo;
1213                       
1214                           try
1215                           {
1216 yi.zhou          1.13         _startSnmptrapd(&trapInfo, processIdFile, logFile);
1217 yi.zhou          1.2      }
1218                           catch (Exception & e)
1219                           {
1220                               cerr << e.getMessage() << endl;
1221                               return (-1);
1222                           }
1223                       
1224                           // Extended for all snmp implementation
1225 kumpf            1.7      _receiveExpectedTraps(workClient, indicationSendCount,
1226 yi.zhou          1.13         runClientThreadCount, logFile);
1227 yi.zhou          1.2  
1228 yi.zhou          1.1      // Stop snmptrapd process if it is running and remove procIdFile
1229 yi.zhou          1.13     _stopSnmptrapd(processIdFile);
1230 yi.zhou          1.1  
1231                           pclose(trapInfo);
1232                       
1233                           // if error encountered then fail the test.
1234                           if (errorsEncountered.get())
1235 kumpf            1.7      {
1236 yi.zhou          1.1          cout << "+++++ test failed" << endl;
1237                               return (-1);
1238                           }
1239                           else
1240                           {
1241                               cout << "+++++ passed all tests" << endl;
1242                           }
1243 yi.zhou          1.2  
1244                           return (0);
1245                       
1246                       #else
1247                           cerr << "Cannot create a trap receiver." << endl;
1248                           return (-1);
1249                       #endif
1250 yi.zhou          1.1  }
1251                       
1252                       int main (int argc, char** argv)
1253                       {
1254                           // This client connection is used solely to create and delete subscriptions.
1255                           CIMClient workClient;
1256                           try
1257                           {
1258                               workClient.connectLocal();
1259                       
1260 yi.zhou          1.13         String processIdFile = TRAP_DIR;
1261                               processIdFile.append("/procIdFile");
1262                       
1263                               String logFile = TRAP_DIR;
1264                               logFile.append("/trapLogFile");
1265                       
1266 yi.zhou          1.1          if (argc <= 1 || argc > 4)
1267                               {
1268                                   cerr << "Invalid argument count: " << argc << endl;
1269                                   _usage();
1270                                   return 1;
1271                               }
1272                               else if (strcmp(argv[1], "setup") == 0)
1273                               {
1274                                   if (argc < 3)
1275                                   {
1276                                       cerr << "Missing query language" << endl;
1277                                       _usage();
1278                                       return -1;
1279                                   }
1280                       
1281                                   if ((strcmp(argv[2], "WQL") != 0) &&
1282                                       (strcmp(argv[2], "DMTF:CQL") != 0))
1283                                   {
1284                                       cerr << "Invalid query language: '" << argv[2] << "'" << endl;
1285                                       _usage();
1286                                       return -1;
1287 yi.zhou          1.1              }
1288 kumpf            1.7  
1289 yi.zhou          1.1              _setup(workClient, argv[2]);
1290                       
1291                                   cout << "+++++ setup completed successfully" << endl;
1292                                   return 0;
1293 kumpf            1.7          }
1294 yi.zhou          1.1          else if (String::equalNoCase(argv[1], "run"))
1295                               {
1296                                   if (argc < 3)
1297                                   {
1298                                       cerr << "Invalid indicationSendCount." << endl;
1299                                       _usage ();
1300                                       return -1;
1301                                   }
1302                       
1303                                   Uint32 indicationSendCount = atoi(argv[2]);
1304                       
1305                                   Uint32 runClientThreadCount = 1;
1306                       
1307 kumpf            1.7              if (argc == 4)
1308 yi.zhou          1.1              {
1309                                       runClientThreadCount = atoi(argv[3]);
1310                                   }
1311                       
1312 kumpf            1.7              int rc = _beginTest(workClient, indicationSendCount,
1313 yi.zhou          1.13                 runClientThreadCount, processIdFile, logFile);
1314 yi.zhou          1.1              return rc;
1315 kumpf            1.7          }
1316 yi.zhou          1.1          else if (String::equalNoCase(argv[1], "cleanup"))
1317                               {
1318                                   if (argc > 2)
1319                                   {
1320                                       cerr << "Invalid argument count." << endl;
1321                                       _usage ();
1322                                       return -1;
1323                                   }
1324 kumpf            1.7  
1325 yi.zhou          1.1              _cleanup (workClient);
1326                       
1327                                   cout << "+++++ cleanup completed successfully" << endl;
1328                                   return 0;
1329                               }
1330                               else if (String::equalNoCase(argv[1], "removelog"))
1331                               {
1332                                   if (argc > 2)
1333                                   {
1334                                       cerr << "Invalid argument count." << endl;
1335                                       _usage ();
1336                                       return -1;
1337                                   }
1338 kumpf            1.7  
1339 yi.zhou          1.13             _removeTrapLogFile(logFile);
1340 yi.zhou          1.1              cout << "+++++ removelog completed successfully" << endl;
1341                                   return 0;
1342                               }
1343                               else
1344                               {
1345                                   cerr << "Invalid option: " << argv[1] << endl;
1346                                   _usage ();
1347                                   return -1;
1348                               }
1349                           }
1350                           catch (Exception & e)
1351                           {
1352                               cerr << "Error: " << e.getMessage() << endl;
1353                           }
1354                       
1355 kumpf            1.11     return -1;
1356 yi.zhou          1.1  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2