1 yi.zhou 1.1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 yi.zhou 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include <Pegasus/Common/PegasusAssert.h>
35 #include <Pegasus/Common/System.h>
36 #include <Pegasus/HandlerService/HandlerTable.h>
37 #include <Pegasus/Repository/CIMRepository.h>
38 #include <Pegasus/Config/ConfigManager.h>
39
40 PEGASUS_USING_PEGASUS;
41 PEGASUS_USING_STD;
42
43 yi.zhou 1.1 const String NAMESPACE("TestSnmpHandler");
44
45 const CIMNamespaceName NS = CIMNamespaceName ("TestSnmpHandler");
46
47 const CIMName testClass1 = CIMName ("SnmpTestClass1");
48 const CIMName testClass2 = CIMName ("SnmpTestClass2");
49 const CIMName testClass3 = CIMName ("SnmpTestClass3");
50 const CIMName testClass4 = CIMName ("SnmpTestClass4");
51 const CIMName testClass5 = CIMName ("SnmpTestClass5");
52 const CIMName testClass6 = CIMName ("SnmpTestClass6");
53 const CIMName testClass7 = CIMName ("SnmpTestClass7");
54
55 String repositoryRoot;
56
57 static CIMInstance CreateHandlerInstance()
58 {
59 CIMInstance handlerInstance(PEGASUS_CLASSNAME_INDHANDLER_SNMP);
60 handlerInstance.addProperty (CIMProperty (CIMName
61 ("SystemCreationClassName"), System::getSystemCreationClassName ()));
62 handlerInstance.addProperty (CIMProperty (CIMName ("SystemName"),
63 System::getFullyQualifiedHostName ()));
64 yi.zhou 1.1 handlerInstance.addProperty (CIMProperty (CIMName ("CreationClassName"),
65 PEGASUS_CLASSNAME_INDHANDLER_SNMP.getString ()));
66 handlerInstance.addProperty(CIMProperty(CIMName ("Name"),
67 String("Handler1")));
68 return (handlerInstance);
69 }
70
71 static void TestException(
72 CIMHandler* handler,
73 CIMInstance indicationHandlerInstance,
74 CIMInstance indicationInstance,
75 CIMStatusCode statusCode)
76 {
77 OperationContext context;
78 CIMInstance indicationSubscriptionInstance;
79 ContentLanguageList contentLanguages;
80
81 Boolean exceptionCaught = false;
82 CIMException testException;
83
84 try
85 yi.zhou 1.1 {
86 handler->handleIndication(context, NAMESPACE, indicationInstance,
87 indicationHandlerInstance, indicationSubscriptionInstance,
88 contentLanguages);
89 }
90 catch (CIMException& e)
91 {
92 exceptionCaught = true;
93 testException = e;
94 }
95
96 PEGASUS_TEST_ASSERT(exceptionCaught &&
97 testException.getCode() == statusCode);
98 }
99
100 // Snmp traps are sent, but, only partial data are in the trap since
101 // there are errors in some data
102 static void TestError(
103 CIMHandler* handler,
104 CIMInstance indicationHandlerInstance,
105 CIMInstance indicationInstance)
106 yi.zhou 1.1 {
107 OperationContext context;
108 CIMInstance indicationSubscriptionInstance;
109 ContentLanguageList contentLanguages;
110
111 Boolean exceptionCaught = false;
112
113 try
114 {
115 handler->handleIndication(context, NAMESPACE, indicationInstance,
116 indicationHandlerInstance, indicationSubscriptionInstance,
117 contentLanguages);
118 }
|
120 yi.zhou 1.1 {
121 exceptionCaught = true;
122 }
123
124 PEGASUS_TEST_ASSERT(!exceptionCaught);
125 }
126
127 static void CreateRepository(CIMRepository & repository)
128 {
129 repository.createNameSpace(NS);
130
131 CIMQualifierDecl q1(CIMName ("MappingStrings"), String(),
132 CIMScope::PROPERTY + CIMScope::CLASS);
133
134 // Qualifier name must be "MappingStrings", test the qualifier
135 // name is not "MappingStrings"
136 CIMQualifierDecl q2(CIMName ("NotMappingStrings"), String(),
137 CIMScope::CLASS);
138
139 repository.setQualifier(NS, q1);
140 repository.setQualifier(NS, q2);
141 yi.zhou 1.1
142 String mappingStr = "OID.IETF | SNMP.1.3.6.1.4.1.892.2.3.9000.8600";
143
144 CIMClass class1(testClass1);
145 class1.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
146 String(mappingStr)));
147
148 // create wrong format property mappingStrings value
149 class1.addProperty(CIMProperty(CIMName ("OidDataType"), String("OctetString"))
150 .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
151 String("Wrong format OID.IETF | SNMP.1.3.6.1.4.1.2.3.9000.8600, DataType.IETF | OctetString "))));
152
153 repository.createClass(NS, class1);
154
155 // create invalid mapping string value
156 String mappingStr2 = "OID.IETF |Invalid Mapping String Value";
157
158 CIMClass class2(testClass2);
159 class2.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
160 String(mappingStr2)));
161
162 yi.zhou 1.1 class2.addProperty(CIMProperty(CIMName ("OidDataType"), String())
163 .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
164 String("OID.IETF | SNMP.1.3.6.1.4.1.2.3.9000.8600, DataType.IETF OctetString "))));
165 repository.createClass(NS, class2);
166
167 // create non MappingStrings qualifier
168 CIMClass class3(testClass3);
169 class3.addQualifier(CIMQualifier(CIMName ("NotMappingStrings"), String(mappingStr)));
170
171 repository.createClass(NS, class3);
172
173 // error building ASN.1 representation
174 String mappingStr4 = "OID.IETF | SNMP.1.204.6.1.6.3.1.330.5.1.0 ";
175
176 CIMClass class4(testClass4);
177 class4.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
178 String(mappingStr4)));
179
180 repository.createClass(NS, class4);
181
182 // create incorrect class mappingStrings value
183 yi.zhou 1.1 String mappingStr5 = "OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3 ";
184
185 CIMClass class5(testClass5);
186 class5.addQualifier(CIMQualifier(CIMName
187 ("MappingStrings"), String(mappingStr5)));
188
189 // create incorrect property name
190 class5.addProperty(
191 CIMProperty(CIMName ("WrongPropertyName"), String("OctetString"))
192 .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
193 String(mappingStr5))));
194
195 repository.createClass(NS, class5);
196
197 // create incorrect property mappingStrings value
198 String mappingStr6 =
199 "OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3, DataType.IETF | OctetString ";
200
201 CIMClass class6(testClass6);
202 class6.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
203 String("OID.IETF | SNMP.1.3.6.1.6.3.1.1.0.1 ")));
204 yi.zhou 1.1 class6.addProperty(
205 CIMProperty(CIMName ("OidDataType"), String("OctetString"))
206 .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
207 String(mappingStr6))));
208
209 repository.createClass(NS, class6);
210
211 // create unsupportted SNMP Data Type for the CIM property
212 String mappingStr7 =
213 "OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3, DataType.IETF | test ";
214
215 CIMClass class7(testClass7);
216 class7.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
217 String("OID.IETF | SNMP.1.3.6.1.6.3.1.1.5.1 ")));
218 class7.addProperty(
219 CIMProperty(CIMName ("OidDataType"), String("test"))
220 .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
221 String(mappingStr7))));
222
223 repository.createClass(NS, class7);
224 }
225 yi.zhou 1.1
226 static void TestExceptionHandling(CIMHandler* handler)
227 {
228 CIMInstance indicationHandlerInstance;
229
230 // Test "invalid IndicationHandlerSNMPMapper instance" exception
231 CIMInstance indicationInstance(testClass1);
232 indicationHandlerInstance = CreateHandlerInstance();
233 TestException(handler, indicationHandlerInstance, indicationInstance,
234 CIM_ERR_FAILED);
235
236 // Test "no required property TargetHostFormat" exception
237 indicationInstance = CIMInstance(testClass1);
238 indicationInstance.addProperty(CIMProperty(
239 CIMName ("OidDataType"), String("OctetString")));
240 indicationHandlerInstance = CreateHandlerInstance();
241 indicationHandlerInstance.addProperty(CIMProperty(
242 CIMName("TargetHost"), String("15.13.140.120")));
243 indicationHandlerInstance.addProperty(CIMProperty(
244 CIMName("SNMPVersion"), Uint16(2)));
245 TestException(handler, indicationHandlerInstance, indicationInstance,
246 yi.zhou 1.1 CIM_ERR_FAILED);
247
248 // Test "no required property SNMPVersion" exception
249 indicationInstance = CIMInstance(testClass1);
250 indicationHandlerInstance = CreateHandlerInstance();
251 indicationHandlerInstance.addProperty(CIMProperty(
252 CIMName("TargetHost"), String("15.13.140.120")));
253 indicationHandlerInstance.addProperty(CIMProperty(
254 CIMName("TargetHostFormat"), Uint16(3)));
255 TestException(handler, indicationHandlerInstance, indicationInstance,
256 CIM_ERR_FAILED);
257
258 // Test "unsupported snmp Version" exception
259 indicationInstance = CIMInstance(testClass1);
260 indicationHandlerInstance = CreateHandlerInstance();
261 indicationHandlerInstance.addProperty(CIMProperty(
262 CIMName("TargetHost"), String("15.13.140.120")));
263 indicationHandlerInstance.addProperty(CIMProperty(
264 CIMName("TargetHostFormat"), Uint16(3)));
265 indicationHandlerInstance.addProperty(CIMProperty(
266 CIMName("SNMPVersion"), Uint16(5)));
267 yi.zhou 1.1 TestException(handler, indicationHandlerInstance, indicationInstance,
268 CIM_ERR_FAILED);
269
270 // Test "SNMP session open failed" exception
271 indicationInstance = CIMInstance(testClass1);
272 indicationHandlerInstance = CreateHandlerInstance();
273 indicationHandlerInstance.addProperty(CIMProperty(
274 CIMName("TargetHost"), String("")));
275 indicationHandlerInstance.addProperty(CIMProperty(
276 CIMName("TargetHostFormat"), Uint16(3)));
277 indicationHandlerInstance.addProperty(CIMProperty(
278 CIMName("SNMPVersion"), Uint16(2)));
279 indicationHandlerInstance.addProperty(CIMProperty(
280 CIMName("OtherTargetHostFormat"), String("testOtherTargetHostFormat")));
281 indicationHandlerInstance.addProperty(CIMProperty(
282 CIMName("PortNumber"), Uint32(200)));
283 TestException(handler, indicationHandlerInstance, indicationInstance,
284 CIM_ERR_FAILED);
285
286 // Test "invalid MappingStrings value" exception
287 indicationInstance = CIMInstance(testClass2);
288 yi.zhou 1.1 indicationInstance.addProperty(CIMProperty(
289 CIMName ("OidDataType"), String("OctetString")));
290 indicationHandlerInstance = CreateHandlerInstance();
291 indicationHandlerInstance.addProperty(CIMProperty(
292 CIMName("TargetHost"), String("15.13.140.120")));
293 indicationHandlerInstance.addProperty(CIMProperty(
294 CIMName("TargetHostFormat"), Uint16(3)));
295 indicationHandlerInstance.addProperty(CIMProperty(
296 CIMName("SNMPVersion"), Uint16(2)));
297 TestException(handler, indicationHandlerInstance, indicationInstance,
298 CIM_ERR_FAILED);
299
300 // Test "no MappingStrings qualifier" exception
301 indicationInstance = CIMInstance(testClass3);
302 indicationHandlerInstance = CreateHandlerInstance();
303 indicationHandlerInstance.addProperty(CIMProperty(
304 CIMName("TargetHost"), String("15.13.140.120")));
305 indicationHandlerInstance.addProperty(CIMProperty(
306 CIMName("TargetHostFormat"), Uint16(3)));
307 indicationHandlerInstance.addProperty(CIMProperty(
308 CIMName("SNMPVersion"), Uint16(2)));
309 yi.zhou 1.1 TestException(handler, indicationHandlerInstance, indicationInstance,
310 CIM_ERR_FAILED);
311
312 // Test "send trap failed" exception
313 indicationInstance = CIMInstance(testClass4);
314 indicationHandlerInstance = CreateHandlerInstance();
315 indicationHandlerInstance.addProperty(CIMProperty(
316 CIMName("TargetHost"), String("15.13.140.120")));
317 indicationHandlerInstance.addProperty(CIMProperty(
318 CIMName("TargetHostFormat"), Uint16(3)));
319 indicationHandlerInstance.addProperty(CIMProperty(
320 CIMName("SNMPVersion"), Uint16(2)));
321 indicationHandlerInstance.addProperty(CIMProperty(
322 CIMName("SNMPSecurityName"), String("t")));
323 indicationHandlerInstance.addProperty(CIMProperty(
324 CIMName("OtherTargetHostFormat"), String("testOtherTargetHostFormat")));
325 TestException(handler, indicationHandlerInstance, indicationInstance,
326 CIM_ERR_FAILED);
327
328 // Test "failed to add snmp variables to PDU",
329 // Both a DiscardedData message and an error message
330 yi.zhou 1.1 // are logged to log file
331 indicationInstance = CIMInstance(testClass5);
332 indicationInstance.addProperty(CIMProperty(
333 CIMName ("OidDataType"), String("OctetString")));
334 indicationHandlerInstance = CreateHandlerInstance();
335 indicationHandlerInstance.addProperty(CIMProperty(
336 CIMName("TargetHost"), String("15.13.140.120")));
337 indicationHandlerInstance.addProperty(CIMProperty(
338 CIMName("TargetHostFormat"), Uint16(3)));
339 indicationHandlerInstance.addProperty(CIMProperty(
340 CIMName("SNMPVersion"), Uint16(2)));
341 indicationHandlerInstance.addProperty(CIMProperty(
342 CIMName("SNMPSecurityName"), String("t")));
343 indicationHandlerInstance.addProperty(CIMProperty(
344 CIMName("OtherTargetHostFormat"), String("testOtherTargetHostFormat")));
345 TestError(handler, indicationHandlerInstance, indicationInstance);
346
347 // Test "convert enterprise OID from numeric form to a list of"
348 // "subidentifiers failed".
349 // Both a DiscardedData message and an error message
350 // are logged to log file
351 yi.zhou 1.1 indicationInstance = CIMInstance(testClass5);
352 indicationInstance.addProperty(CIMProperty(
353 CIMName ("OidDataType"), String("OctetString")));
354 indicationHandlerInstance = CreateHandlerInstance();
355 indicationHandlerInstance.addProperty(CIMProperty(
356 CIMName("TargetHost"), String("15.13.140.120")));
357 indicationHandlerInstance.addProperty(CIMProperty(
358 CIMName("TargetHostFormat"), Uint16(3)));
359 indicationHandlerInstance.addProperty(CIMProperty(
360 CIMName("SNMPVersion"), Uint16(3)));
361 indicationHandlerInstance.addProperty(CIMProperty(
362 CIMName("SNMPSecurityName"), String("t")));
363 indicationHandlerInstance.addProperty(CIMProperty(
364 CIMName("OtherTargetHostFormat"), String("testOtherTargetHostFormat")));
365 TestError(handler, indicationHandlerInstance, indicationInstance);
366
367 // Test "convert property OID from numeric form to a list of"
368 // "subidentifiers failed".
369 // Both a DiscardedData message and an error message
370 // are logged to log file
371 indicationInstance = CIMInstance(testClass6);
372 yi.zhou 1.1 indicationInstance.addProperty(CIMProperty(
373 CIMName ("OidDataType"), String("OctetString")));
374 indicationHandlerInstance = CreateHandlerInstance();
375 indicationHandlerInstance.addProperty(CIMProperty(
376 CIMName("TargetHost"), String("15.13.140.120")));
377 indicationHandlerInstance.addProperty(CIMProperty(
378 CIMName("TargetHostFormat"), Uint16(3)));
379 indicationHandlerInstance.addProperty(CIMProperty(
380 CIMName("SNMPVersion"), Uint16(2)));
381 indicationHandlerInstance.addProperty(CIMProperty(
382 CIMName("OtherTargetHostFormat"), String("testOtherTargetHostFormat")));
383 TestError(handler, indicationHandlerInstance, indicationInstance);
384
385 // Test "unsupported SNMP data type for the CIM property"
386 // Both a DiscardedData message and an error message
387 // are logged to log file
388 indicationInstance = CIMInstance(testClass7);
389 indicationInstance.addProperty(CIMProperty(
390 CIMName ("OidDataType"), String("test")));
391 indicationHandlerInstance = CreateHandlerInstance();
392 indicationHandlerInstance.addProperty(CIMProperty(
393 yi.zhou 1.1 CIMName("TargetHost"), String("15.13.140.120")));
394 indicationHandlerInstance.addProperty(CIMProperty(
395 CIMName("TargetHostFormat"), Uint16(3)));
396 indicationHandlerInstance.addProperty(CIMProperty(
397 CIMName("SNMPVersion"), Uint16(2)));
398 TestError(handler, indicationHandlerInstance, indicationInstance);
399 }
400
401
402 int main(int argc, char** argv)
403 {
404
405 const char* pegasusHome = getenv("PEGASUS_HOME");
406 if (!pegasusHome)
407 {
408 cerr << "PEGASUS_HOME environment variable not set" << endl;
409 exit(1);
410 }
411
412 String repositoryRoot = pegasusHome;
413 repositoryRoot.append("/repository");
414 yi.zhou 1.1
|