1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 #include <ut/ut.h>
26 #include <pal/palcommon.h>
27 #include <pal/strings.h>
28 #include <base/messages.h>
29 #include <provmgr/SubMgr.h>
30 #include <provmgr/SubscriptionContext.h>
31 #include "StrandHelper.h"
32 #include <provmgr/indicationSchema.h>
33
34 using namespace std;
35
36 MI_ConstString subMgrDefaultFilter = MI_T("SELECT * FROM CIM_InstCreation WHERE SourceInstanceModelPath = \"MSFT:CreationIndicationIdentifier\"");
37 MI_ConstString subMgrLanguageWql = MI_T("WQL");
38
39 MI_Boolean SubMgr_IsSubscribed(
40 _In_opt_ const SubscriptionManager* mgr )
41 {
42 if (!mgr)
43 krisbash 1.1 {
44 return MI_FALSE;
45 }
46
47 if (MI_FALSE == SubMgr_IsEnabled(mgr) )
48 {
49 return MI_FALSE;
50 }
51
52 if (mgr->subscrList.head)
53 {
54 return MI_TRUE;
55 }
56 return MI_FALSE;
57 }
58
59
60 NitsTest(TestSubMgr_NewAndDelete)
61 {
62 SubscriptionManager* subMgr = SubMgr_New();
63
64 krisbash 1.1 NitsAssert(NULL != subMgr, PAL_T("expected allocation of SubscriptionManager*") );
65
66 if (subMgr)
67 {
68 SubMgr_Delete( subMgr );
69 }
70 }
71 NitsEndTest
72
73 struct TestSubMgr_Struct
74 {
75 SubscriptionManager* subMgr;
76 Strand leftSideStrand;
77 };
78
79 TestSubMgr_Struct setupTemplate = { 0 };
80 STRAND_DEBUGNAME( test_SubMgr_Strand0 );
81
82 /*
83 * Allocates and initializes a default SubscriptionManager for use in a variety
84 * of tests.
85 krisbash 1.1 */
86 NitsSetup0(TestSubMgr_SetupSubMgr, TestSubMgr_Struct)
87 {
88 NitsContext()->_TestSubMgr_Struct->subMgr = SubMgr_New();
89 SubscriptionManager* subMgr = NitsContext()->_TestSubMgr_Struct->subMgr;
90 if ( ! subMgr )
91 {
92 NitsAssertOrReturn(false, PAL_T("expected allocation of SubscriptionManager*") );
93 }
94
95 SubMgr_Init( subMgr, NULL );
96 NitsAssert( MI_FALSE == SubMgr_IsEnabled(subMgr), PAL_T("invalid state after Init") );
97 }
98 NitsEndSetup
99
100 /*
101 * Cleans up and de-allocates SubscriptionManager.
102 */
103 NitsCleanup(TestSubMgr_SetupSubMgr)
104 {
105 if (NitsContext()->_TestSubMgr_Struct->subMgr)
106 krisbash 1.1 {
107 SubscriptionManager* submgr = NitsContext()->_TestSubMgr_Struct->subMgr;
108 SubMgr_CancelAllSubscriptions( submgr, MI_RESULT_FAILED, NULL, NULL );
109 SubMgr_Delete( NitsContext()->_TestSubMgr_Struct->subMgr );
110 }
111 }
112 NitsEndCleanup
113
114 NitsTest1(TestSubMgr_IsSubscribed_failures, TestSubMgr_SetupSubMgr, setupTemplate )
115 {
116 SubscriptionManager *subMgr = NitsContext()->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
117 SubscriptionManager *nullSubMgr = NULL;
118
119 NitsAssert( MI_FALSE == SubMgr_IsSubscribed( nullSubMgr ), PAL_T("No support expected") );
120
121 NitsAssert( MI_FALSE == SubMgr_IsSubscribed( subMgr ), PAL_T("Invalid state expected") );
122
123 SubMgr_SetEnabled( subMgr, MI_TRUE );
124 NitsAssert( MI_FALSE == SubMgr_IsSubscribed( subMgr ), PAL_T("No subscription detected expected") );
125 }
126 NitsEndTest
127 krisbash 1.1
128 NitsTest1(TestSubMgr_BasicAddAndDeleteSubscription, TestSubMgr_SetupSubMgr, setupTemplate )
129 {
130 TestSubMgr_Struct* tss = NitsContext()->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct;
131 SubscriptionManager *subMgr = tss->subMgr;
132
133 SubscribeReq* msg = SubscribeReq_New( 0, 0 );
134 SubscriptionContext* context;
135 Provider provider;
136
137 if(!NitsAssert(msg != NULL, PAL_T("failed to allocate msg")))
138 NitsReturn;
139 context = NULL;
140 memset( &provider, 0, sizeof(Provider) );
141
142 msg->targetType = SUBSCRIP_TARGET_LIFECYCLE_CREATE;
143 msg->filter = subMgrDefaultFilter;
144 msg->language = subMgrLanguageWql;
145 msg->subscriptionID = 1337;
146
147 provider.refusedUnload = MI_TRUE; // Prevents Provider_Release from releasing
148 krisbash 1.1 provider.classDecl = &CIM_InstCreation_rtti;
149
150 // Strand initialization
151 Strand_Init( STRAND_DEBUG(test_SubMgr_Strand0) &tss->leftSideStrand, &ContextTest_Left_InteractionFT, 0, NULL );
152 // Simulate opening with message mode
153 tss->leftSideStrand.info.opened = MI_TRUE;
154 tss->leftSideStrand.info.thisAckPending = MI_TRUE;
155
156 InteractionOpenParams params;
157
158 InteractionOpenParams_Init(¶ms);
159 params.interaction = &tss->leftSideStrand.info.interaction;
160 params.msg = &msg->base.base;
161
162 // Add subscription
163 MI_Result r = CreateAndAddSubscriptionHelper(
164 subMgr,
165 &provider,
166 ¶ms,
167 &context);
168
169 krisbash 1.1 NitsAssert( MI_RESULT_OK == r, PAL_T("Failed to add subscription") );
170
171 SubMgrSubscription* subscription = SubMgr_GetSubscription(subMgr, 1337 );
172 NitsAssert( NULL != subscription, PAL_T("Failed to retrieve newly created subscription") );
173
174 if (subscription)
175 {
176 NitsAssert( MI_RESULT_OK == SubMgr_DeleteSubscription( subMgr, subscription), PAL_T("Expected subscription, none found") );
177 //
178 // release the refcount added by by SubscrContext_Init
179 //
180 SubMgrSubscription_Release( subscription );
181 }
182
183 if ( context )
184 {
185 _Context_Destroy(&context->baseCtx);
186 }
187
188 if (msg)
189 {
190 krisbash 1.1 SubscribeReq_Release( msg );
191 }
192 }
193 NitsEndTest
194
195 struct TestSubMgr_Subscription_Struct
196 {
197 SubscribeReq* msg1;
198 SubscriptionContext* context1;
199
200 SubscribeReq* msg2;
201 SubscriptionContext* context2;
202
203 Provider provider;
204
205 Strand leftSideStrand1;
206 Strand leftSideStrand2;
207 };
208
209 TestSubMgr_Subscription_Struct subMgrTemplate = { 0 };
210
211 krisbash 1.1 STRAND_DEBUGNAME( test_SubMgr_Strand1 );
212 STRAND_DEBUGNAME( test_SubMgr_Strand2 );
213
214 NitsSetup1(TestSubMgr_AddSubscriptions, TestSubMgr_Subscription_Struct, TestSubMgr_SetupSubMgr, setupTemplate )
215 {
216 TestSubMgr_Subscription_Struct* subStruct = NitsContext()->_TestSubMgr_Subscription_Struct;
217 SubscriptionManager* subMgr = NitsContext()->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
218
219 subStruct->msg1 = SubscribeReq_New( 0, 0 );
220 subStruct->msg2 = SubscribeReq_New( 0, 0 );
221
222 if(!NitsAssert((subStruct->msg1 != NULL) && (subStruct->msg2 != NULL), PAL_T("allocation failed")))
223 NitsReturn;
224 subStruct->context1 = NULL;
225 subStruct->context2 = NULL;
226
227 memset( &subStruct->provider, 0, sizeof(Provider) );
228
229 subStruct->msg1->targetType = SUBSCRIP_TARGET_LIFECYCLE_CREATE;
230 subStruct->msg1->filter = subMgrDefaultFilter;
231 subStruct->msg1->language = subMgrLanguageWql;
232 krisbash 1.1 subStruct->msg1->subscriptionID = 1337;
233
234 subStruct->msg2->targetType = SUBSCRIP_TARGET_LIFECYCLE_CREATE;
235 subStruct->msg2->filter = subMgrDefaultFilter;
236 subStruct->msg2->language = subMgrLanguageWql;
237 subStruct->msg2->subscriptionID = 1338;
238
239 subStruct->provider.refusedUnload = MI_TRUE; // Prevents Provider_Release from releasing
240 subStruct->provider.classDecl = &CIM_InstCreation_rtti;
241
242 // Strand initialization
243 Strand_Init( STRAND_DEBUG(test_SubMgr_Strand1) &subStruct->leftSideStrand1, &ContextTest_Left_InteractionFT, 0, NULL );
244 subStruct->leftSideStrand1.info.thisAckPending = MI_TRUE;
245 subStruct->leftSideStrand1.info.opened = MI_TRUE;
246 Strand_Init( STRAND_DEBUG(test_SubMgr_Strand2) &subStruct->leftSideStrand2, &ContextTest_Left_InteractionFT, 0, NULL );
247 subStruct->leftSideStrand2.info.thisAckPending = MI_TRUE;
248 subStruct->leftSideStrand2.info.opened = MI_TRUE;
249
250 InteractionOpenParams params;
251
252 InteractionOpenParams_Init(¶ms);
253 krisbash 1.1 params.interaction = &subStruct->leftSideStrand1.info.interaction;
254 params.msg = &subStruct->msg1->base.base;
255
256 //
257 // Add subscription
258 //
259 MI_Result r = CreateAndAddSubscriptionHelper(
260 subMgr,
261 &subStruct->provider,
262 ¶ms,
263 &subStruct->context1);
264 NitsAssert( MI_RESULT_OK == r, PAL_T("Failed to add subscription") );
265
266 InteractionOpenParams_Init(¶ms);
267 params.interaction = &subStruct->leftSideStrand2.info.interaction;
268 params.msg = &subStruct->msg2->base.base;
269
270 //
271 // Add subscription
272 //
273 r = CreateAndAddSubscriptionHelper(
274 krisbash 1.1 subMgr,
275 &subStruct->provider,
276 ¶ms,
277 &subStruct->context2);
278 NitsAssert( MI_RESULT_OK == r, PAL_T("Failed to add subscription") );
279
280 SubMgr_SetEnabled( subMgr, MI_TRUE);
281 NitsAssert( MI_TRUE == SubMgr_IsEnabled( subMgr ), PAL_T("Unable to set state") );
282 }
283 NitsEndSetup
284
285 NitsCleanup(TestSubMgr_AddSubscriptions)
286 {
287 SubscriptionManager* subMgr = NitsContext()->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
288 TestSubMgr_Subscription_Struct* subStruct = NitsContext()->_TestSubMgr_Subscription_Struct;
289
290 SubMgrSubscription* subscription = SubMgr_GetSubscription(subMgr, 1337 );
291
292 NitsAssert( NULL != subscription, PAL_T("Failed to retrieve newly created subscription") );
293 if (subscription)
294 {
295 krisbash 1.1 //
296 // Simulate PostResult to subscribecontext.
297 // TODO: to rely on SubMgr_CancelAllSubscriptions to clean up subscriptions,
298 // which needs to setup all dependent structures
299 //
300 NitsAssert( MI_RESULT_OK == SubMgr_DeleteSubscription( subMgr, subscription), PAL_T("Expected subscription, none found") );
301 SubMgrSubscription_Release( subscription );
302 }
303
304 subscription = SubMgr_GetSubscription(subMgr, 1338 );
305
306 NitsAssert( NULL != subscription, PAL_T("Failed to retrieve newly created subscription") );
307 if (subscription)
308 {
309 //
310 // Simulate PostResult to subscribecontext
311 // TODO: to rely on SubMgr_CancelAllSubscriptions to clean up subscriptions,
312 // which needs to setup all dependent structures
313 //
314 NitsAssert( MI_RESULT_OK == SubMgr_DeleteSubscription( subMgr, subscription), PAL_T("Expected subscription, none found") );
315 SubMgrSubscription_Release( subscription );
316 krisbash 1.1 }
317
318 _Context_Destroy(&subStruct->context1->baseCtx);
319 _Context_Destroy(&subStruct->context2->baseCtx);
320
321 if (subStruct->msg1)
322 {
323 NitsCompare(1, (size_t)subStruct->msg1->base.base.refCounter, PAL_T("RefCount of request message is wrong. Leak memory"));
324 SubscribeReq_Release( subStruct->msg1 );
325 }
326
327 if (subStruct->msg2)
328 {
329 NitsCompare(1, (size_t)subStruct->msg2->base.base.refCounter, PAL_T("RefCount of request message is wrong. Leak memory"));
330 SubscribeReq_Release( subStruct->msg2 );
331 }
332 }
333 NitsEndCleanup
334
335 NitsTest1(TestSubMgr_IsSubscribed_success, TestSubMgr_AddSubscriptions, subMgrTemplate )
336 {
337 krisbash 1.1 SubscriptionManager *subMgr = NitsContext()->_TestSubMgr_AddSubscriptions->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
338
339 NitsAssert( MI_TRUE == SubMgr_IsSubscribed( subMgr ), PAL_T("No subscription detected expected") );
340 }
341 NitsEndTest
342
343 NitsTest1(TestSubMgr_GetSubscription_NotFound, TestSubMgr_AddSubscriptions, subMgrTemplate )
344 {
345 SubscriptionManager *subMgr = NitsContext()->_TestSubMgr_AddSubscriptions->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
346
347 NitsAssert( NULL == SubMgr_GetSubscription( subMgr, 2000 ), PAL_T("Subscription ID present when not expected") );
348 }
349 NitsEndTest
350
351 NitsTest1(TestSubMgr_GetSubscription_NotPresent, TestSubMgr_SetupSubMgr, setupTemplate )
352 {
353 SubscriptionManager *subMgr = NitsContext()->_TestSubMgr_SetupSubMgr->_TestSubMgr_Struct->subMgr;
354 NitsAssert( NULL == SubMgr_GetSubscription( subMgr, 1337 ), PAL_T("Subscription ID present when not expected (empty list)") );
355 }
356 NitsEndTest
357
358 krisbash 1.1 void Verify_SubMgrSubscription_New_Input(
359 MI_ConstString filter,
360 MI_ConstString language,
361 MI_Boolean shouldSucceed)
362 {
363 SubscribeReq* msg = SubscribeReq_New( 0, 0 );
364
365 if(!NitsAssert(msg != NULL, PAL_T("msg alloc failed")))
366 return;
367
368 msg->targetType = SUBSCRIP_TARGET_LIFECYCLE_CREATE;
369 msg->filter = filter;
370 msg->language = language;
371 msg->subscriptionID = 1337;
372
373 SubMgrSubscription* testSub = SubMgrSubscription_New( msg );
374
375 if (shouldSucceed)
376 NitsAssert( NULL != testSub, PAL_T("Subscription allocation failure not expected") );
377 else
378 NitsAssert( NULL == testSub, PAL_T("Subscription allocation succeeded. Failure expected") );
379 krisbash 1.1
380 NitsIgnoringError(); // negative test case; same failure in OOM or otherwise
381 if ( testSub)
382 {
383 SubMgrSubscription_Release( testSub );
384 }
385 if (msg)
386 {
387 SubscribeReq_Release( msg );
388 }
389 }
390
391 NitsTest(TestSubMgrSubscription_New_Fails_if_FilterError)
392 {
393 // NULL filter and lanugage will trigger a failure
394 Verify_SubMgrSubscription_New_Input( NULL, NULL, MI_FALSE );
395 }
396 NitsEndTest
397
398 NitsTest(TestSubMgrSubscription_New_Success)
399 {
400 krisbash 1.1 // CIM_InstCreation matches SourceInstanceModelPath in class decl's
401 // property list
402 Verify_SubMgrSubscription_New_Input( subMgrDefaultFilter, subMgrLanguageWql, MI_TRUE );
403 }
404 NitsEndTest
405
406
407 struct TestSubMgrSubscription_Struct
408 {
409 SubscribeReq* msg;
410 SubMgrSubscription* testSubscription;
411 };
412
413 TestSubMgrSubscription_Struct subscriptionTemplate = { 0 };
414
415
416 NitsSetup0(TestSubMgrSubscription_Setup, TestSubMgrSubscription_Struct)
417 {
418 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Struct;
419
420 setupStruct->msg = SubscribeReq_New( 0, 0 );
421 krisbash 1.1
422 setupStruct->msg->targetType = SUBSCRIP_TARGET_LIFECYCLE_CREATE;
423 setupStruct->msg->filter = subMgrDefaultFilter;
424 setupStruct->msg->language = subMgrLanguageWql;
425 setupStruct->msg->subscriptionID = 1337;
426
427 setupStruct->testSubscription = SubMgrSubscription_New( setupStruct->msg );
428 }
429 NitsEndSetup
430
431 NitsCleanup(TestSubMgrSubscription_Setup)
432 {
433 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Struct;
434
435 if ( setupStruct->testSubscription)
436 {
437 SubMgrSubscription_Release( setupStruct->testSubscription );
438 }
439
440 if (setupStruct->msg)
441 {
442 krisbash 1.1 SubscribeReq_Release( setupStruct->msg );
443 }
444 }
445 NitsEndCleanup
446
447 NitsTest1(TestSubMgrSubscription_NewAndDelete, TestSubMgrSubscription_Setup, subscriptionTemplate)
448 {
449 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Setup->_TestSubMgrSubscription_Struct;
450 NitsAssert( NULL != setupStruct->testSubscription, PAL_T("Subscription allocation failed") );
451 }
452 NitsEndTest
453
454 NitsTest1(TestSubMgrSubscription_SetState, TestSubMgrSubscription_Setup, subscriptionTemplate)
455 {
456 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Setup->_TestSubMgrSubscription_Struct;
457
458 SubscriptionState originalState = setupStruct->testSubscription->state;
459 NitsAssert( MI_RESULT_OK == SubMgrSubscription_SetState( setupStruct->testSubscription, SubscriptionState_Unsubscribed ), PAL_T("Failed to set state") );
460 NitsAssert( SubscriptionState_Unsubscribed == setupStruct->testSubscription->state, PAL_T("Unexpected subscription state") );
461 NitsAssert( MI_RESULT_OK == SubMgrSubscription_SetState( setupStruct->testSubscription, originalState ), PAL_T("Failed to set state") ); // reset the original state for cleanup
462 }
463 krisbash 1.1 NitsEndTest
464
465 NitsTest1(TestSubMgrSubscription_GetSupportedTypes, TestSubMgrSubscription_Setup, subscriptionTemplate)
466 {
467 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Setup->_TestSubMgrSubscription_Struct;
468 // Default case based on test setup
469 NitsAssert( SUBSCRIP_TARGET_LIFECYCLE_CREATE == SubMgrSubscription_GetSupportedTypes( setupStruct->testSubscription ), PAL_T("Failed to get supported types") );
470
471 // Base cases
472 NitsAssert( SUBSCRIP_TARGET_UNSUPPORTED == SubMgrSubscription_GetSupportedTypes( NULL ), PAL_T("Unexpected supported types") );
473
474 SubMgrSubscription emptySub;
475 memset( &emptySub, 0, sizeof(SubMgrSubscription) );
476 NitsAssert( SUBSCRIP_TARGET_UNSUPPORTED == SubMgrSubscription_GetSupportedTypes( &emptySub ), PAL_T("Unexpected supported types") );
477 }
478 NitsEndTest
479
480 NitsTest1(TestSubMgrSubscription_IsQueryValid, TestSubMgrSubscription_Setup, subscriptionTemplate)
481 {
482 TestSubMgrSubscription_Struct *setupStruct = NitsContext()->_TestSubMgrSubscription_Setup->_TestSubMgrSubscription_Struct;
483
484 krisbash 1.1 // Returns MI_TRUE for valid queries:
485 // CIM_InstCreation matches SourceInstanceModelPath in class decl's
486 // property list
487 NitsAssert( MI_TRUE == SubMgrSubscription_IsQueryValid( setupStruct->testSubscription, &CIM_InstCreation_rtti), PAL_T("Invalid subscription query. Valid expected") );
488
489
490 // Returns MI_FALSE for invalid queries
491 // CIM_InstCreation doesn't match SourceInstanceModelPath in class decl's
492 // property list
493 NitsAssert( MI_FALSE == SubMgrSubscription_IsQueryValid( setupStruct->testSubscription, &CIM_Indication_rtti), PAL_T("Valid subscription query. Invalid expected") );
494 }
495 NitsEndTest
496
|