1 mike 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 mike 1.1 **==============================================================================
23 */
24
25 #include <vector>
26 #include <set>
27 #include <cstdlib>
28 #include <iostream>
29 #include <ut/ut.h>
30 #include <unittest/utils.h>
31 #include <provmgr/provmgr.h>
32 #include <protocol/thread.h>
33 #include <base/io.h>
34 #include <base/user.h>
35 #include <base/paths.h>
36
37 using namespace std;
38
39 static ProvMgr s_provmgr;
40 static Selector s_selector;
41
42 #if defined(_MSC_VER)
43 mike 1.1 #undef BEGIN_EXTERNC
44 #undef END_EXTERNC
45 #define BEGIN_EXTERNC
46 #define END_EXTERNC
47 #endif
48
49
50 static void setUp()
51 {
52 }
53
54 static void cleanup()
55 {
56 }
57
58 BEGIN_EXTERNC
59 static void _idleCallback(
60 ProvMgr* mgr,
61 void* callbackData)
62 {
63 int* p = (int*)callbackData;
64 mike 1.1
65 MI_UNUSED(mgr);
66
67 (*p)++;
68 }
69 END_EXTERNC
70
71 BEGIN_EXTERNC
72 static void _ResponseCallback(
73 Message* msg,
74 void* callbackData)
75 {
76 MI_UNUSED(msg);
77 MI_UNUSED(callbackData);
78 }
79 END_EXTERNC
80
81
82 static void _PostMessage(const char* cn)
83 {
84 EnumerateInstancesReq* msg;
85 mike 1.1 MI_Result r;
86
87 /* Create new request to send to provider */
88 msg = EnumerateInstancesReq_New(0, 0);
89 msg->nameSpace = Batch_Zdup(msg->base.batch, MI_T("test/cpp"));
90 msg->className = Batch_Strdup2(msg->base.batch, cn);
91
92 /* Setup callback */
93 msg->base.callback = _ResponseCallback;
94 msg->base.callbackData = 0;
95
96 /* Send the request to provider manager */
97 r = ProvMgr_PostMessage(&s_provmgr, "PersonProviderCXX", &msg->base);
98
99 UT_ASSERT_EQUAL(r, MI_RESULT_OK);
100
101 /* Release the original message */
102 EnumerateInstancesReq_Release(msg);
103 }
104
105 static void _PostInvokeMessage(const char* fn)
106 mike 1.1 {
107 InvokeReq* msg;
108 MI_Result r;
109 MI_Instance* dynamicInstanceParams = 0;
110 Batch dynamicBatch = BATCH_INITIALIZER;
111 MI_String params_cn;
112
113 // Create new request.
114 msg = InvokeReq_New(0, 0);
115
116 /* Setup callback */
117 msg->base.callback = _ResponseCallback;
118 msg->base.callbackData = 0;
119
120 // Extract arguments.
121 msg->nameSpace = Batch_Strdup2(msg->base.batch, "test/cpp");
122 msg->className = Batch_Strdup2(msg->base.batch, "X_RefuseUnload");
123 msg->function = Batch_Strdup2(msg->base.batch, fn);
124
125
126 /* params */
127 mike 1.1 {
128 /* parameters instance classname is ignored, but has to have valid syntax */
129 params_cn = Batch_Strdup2(&dynamicBatch, "param");
130
131 r = Instance_NewDynamic(
132 &dynamicInstanceParams,
133 params_cn,
134 MI_FLAG_CLASS,
135 &dynamicBatch);
136
137 UT_ASSERT_EQUAL (MI_RESULT_OK, r);
138
139 msg->instanceParams = dynamicInstanceParams;
140 }
141
142 /* Send the request to provider manager */
143 r = ProvMgr_PostMessage(&s_provmgr, "PersonProviderCXX", &msg->base);
144
145 UT_ASSERT_EQUAL(r, MI_RESULT_OK);
146
147 InvokeReq_Release(msg);
148 mike 1.1 Batch_Destroy(&dynamicBatch);
149
150 }
151
152 static void TestProvmgr_UnloadIdle()
153 {
154 int num = 0;
155
156 UT_ASSERT_EQUAL(
157 ProvMgr_Init(&s_provmgr, &s_selector, _idleCallback, &num, GetPath(ID_PROVIDERDIR)),
158 MI_RESULT_OK);
159
160 s_provmgr.idleTimeoutUsec = 1000;
161
162
163 // post message
164 _PostMessage("MSFT_Person");
165
166 // run selector to catch timeout
167 for (int attempt = 0; attempt < 500 && num == 0; attempt++)
168 {
169 mike 1.1 Selector_Run(&s_selector, 1000);
170 }
171
172 // expect idle callback to be invoked once
173 UT_ASSERT_EQUAL(1, num);
174
175 UT_ASSERT( MI_RESULT_OK == Selector_RemoveAllHandlers(&s_selector) );
176 UT_ASSERT_EQUAL(ProvMgr_Destroy(&s_provmgr), MI_RESULT_OK);
177 }
178
179 static void TestProvmgr_RefuseRequestUnload()
180 {
181 int num = 0;
182
183 UT_ASSERT_EQUAL(
184 ProvMgr_Init(&s_provmgr, &s_selector, _idleCallback, &num, GetPath(ID_PROVIDERDIR)),
185 MI_RESULT_OK);
186
187 s_provmgr.idleTimeoutUsec = 1000;
188
189
190 mike 1.1 // post message
191 _PostMessage("X_RefuseUnload");
192
193 // run selector to catch timeout
194 for (int attempt = 0; attempt < 500 && num == 0; attempt++)
195 {
196 Selector_Run(&s_selector, 1000);
197 }
198
199 // expect idle callback not to be invoked
200 UT_ASSERT_EQUAL(0, num);
201
202 // call 'request-unload'
203 _PostInvokeMessage("RequestUnload");
204
205 // run selector to catch timeout
206 for (int attempt = 0; attempt < 500 && num == 0; attempt++)
207 {
208 Selector_Run(&s_selector, 1000);
209 }
210
211 mike 1.1 // expect idle provider to be unloaded by now
212 UT_ASSERT_EQUAL(1, num);
213
214 UT_ASSERT( MI_RESULT_OK == Selector_RemoveAllHandlers(&s_selector) );
215 UT_ASSERT_EQUAL(ProvMgr_Destroy(&s_provmgr), MI_RESULT_OK);
216 }
217
218 static void AllTests()
219 {
220 // setup
221 Sock_Start();
222 Selector_Init(&s_selector);
223
224 // test happy-pass case
225 UT_TEST(TestProvmgr_UnloadIdle);
226 UT_TEST(TestProvmgr_RefuseRequestUnload);
227
228 // cleanup
229 Selector_Destroy(&s_selector);
230 Sock_Stop();
231 }
232 mike 1.1
233 UT_ENTRY_POINT(AllTests);
|