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 <protocol/wsman.h>
32 #include <protocol/sock.h>
33 #include <protocol/thread.h>
34 #include <base/io.h>
35 #include <base/user.h>
36
37 using namespace std;
38 //using namespace mi;
39
40 static WSMAN* s_wsman;
41 static bool s_running;
42 static ThreadHandle s_t;
43 mike 1.1 static Selector s_selector;
44 static MI_Uint16 PORT = ut::getUnittestPortNumber() + 30;
45
46 #if defined(_MSC_VER)
47 #undef BEGIN_EXTERNC
48 #undef END_EXTERNC
49 #define BEGIN_EXTERNC
50 #define END_EXTERNC
51 #endif
52
53 static void _StopWSMAN()
54 {
55 if (s_running)
56 {
57 s_running = false;
58
59 if (s_t)
60 {
61 UT_ASSERT(MI_RESULT_OK == Thread_Destroy( s_t, MI_TRUE ));
62 }
63
64 mike 1.1 UT_ASSERT( MI_RESULT_OK == Selector_RemoveAllHandlers(&s_selector) );
65 UT_ASSERT( MI_RESULT_OK == WSMAN_Delete(s_wsman) );
66
67 s_t = 0;
68 s_wsman = 0;
69 }
70 }
71
72 static void setUp()
73 {
74 //PORT++;
75 }
76
77 static void cleanup()
78 {
79 _StopWSMAN();
80 }
81
82 BEGIN_EXTERNC
83 static void* MI_CALL _WSMANServerProc(void* )
84 {
85 mike 1.1 // pump messages
86 for (; s_running; )
87 WSMAN_Run( s_wsman, 1000 );
88
89 return 0;
90 }
91 END_EXTERNC
92
93 static void _StartWSMAN(
94 WSMANCallback callback,
95 void* callbackData,
96 WSMAN_Options* options = 0)
97 {
98 if (s_running)
99 return;
100
101 s_running = true;
102
103 UT_ASSERT( MI_RESULT_OK == WSMAN_New_Listener(
104 &s_wsman,
105 &s_selector,
106 mike 1.1 PORT,
107 0,
108 callback,
109 callbackData));
110
111 if (options)
112 {
113 UT_ASSERT( MI_RESULT_OK == WSMAN_SetOptions(s_wsman,options));
114 }
115
116 UT_ASSERT(MI_RESULT_OK == Thread_Create(
117 _WSMANServerProc, 0, &s_t));
118
119 }
120
121 BEGIN_EXTERNC
122 static string _CreateEnumRequestXML()
123 {
124 string res =
125 "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\""
126 " xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
127 mike 1.1 " xmlns:n=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\""
128 " xmlns:w=\"http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd\">"
129 "<s:Header>"
130 "<a:To>http://linux-22kv:22000/wsman</a:To>"
131 "<w:ResourceURI s:mustUnderstand=\"true\">http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
132 "cn"
133 "</w:ResourceURI>"
134 "<a:ReplyTo>"
135 "<a:Address s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>"
136 "</a:ReplyTo>"
137 "<a:Action s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate</a:Action>"
138 "<w:MaxEnvelopeSize s:mustUnderstand=\"true\">10240</w:MaxEnvelopeSize>"
139 "<a:MessageID>uuid:FEF3DF41-FFEC-4ABE-ADFC-A8305DAB71C9</a:MessageID>"
140 "<w:Locale xml:lang=\"en-US\" s:mustUnderstand=\"false\" /><w:SelectorSet>"
141 "<w:Selector Name=\"__cimnamespace\">"
142 "ns"
143 "</w:Selector>"
144 "</w:SelectorSet>"
145 "<w:OperationTimeout>PT60.000S</w:OperationTimeout>"
146 "</s:Header>"
147 "<s:Body>"
148 mike 1.1 "<n:Enumerate>"
149 "<w:MaxElements>10</w:MaxElements>"
150 "</n:Enumerate></s:Body></s:Envelope>"
151 ;
152
153 return res;
154 }
155 END_EXTERNC
156
157 BEGIN_EXTERNC
158 static string _CreatePullRequestXML(const string& ctxID)
159 {
160 string res =
161 "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\""
162 " xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
163 " xmlns:n=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\""
164 " xmlns:w=\"http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd\">"
165 "<s:Header>"
166 "<a:To>http://linux-22kv:22000/wsman</a:To>"
167 "<w:ResourceURI s:mustUnderstand=\"true\">http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
168 "cn"
169 mike 1.1 "</w:ResourceURI>"
170 "<a:ReplyTo>"
171 "<a:Address s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>"
172 "</a:ReplyTo>"
173 "<a:Action s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/09/enumeration/Pull</a:Action>"
174 "<w:MaxEnvelopeSize s:mustUnderstand=\"true\">10240</w:MaxEnvelopeSize>"
175 "<a:MessageID>uuid:FEF3DF41-FFEC-4ABE-ADFC-A8305DAB71C9</a:MessageID>"
176 "<w:Locale xml:lang=\"en-US\" s:mustUnderstand=\"false\" /><w:SelectorSet>"
177 "<w:Selector Name=\"__cimnamespace\">"
178 "ns"
179 "</w:Selector>"
180 "</w:SelectorSet>"
181 "<w:OperationTimeout>PT60.000S</w:OperationTimeout>"
182 "</s:Header>"
183 "<s:Body>"
184 "<n:Pull>"
185 "<wsen:EnumerationContext xmlns:wsen=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\">";
186
187 res += ctxID;
188 res +=
189 "</wsen:EnumerationContext>"
190 mike 1.1 "<n:MaxElements>10000</n:MaxElements>"
191 "</n:Pull>"
192 "</s:Body></s:Envelope>"
193 ;
194
195 return res;
196 }
197 END_EXTERNC
198
199 BEGIN_EXTERNC
200 static string _CreateReleaseRequestXML(const string& ctxID)
201 {
202 string res =
203 "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\""
204 " xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\""
205 " xmlns:n=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\""
206 " xmlns:w=\"http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd\">"
207 "<s:Header>"
208 "<a:To>http://linux-22kv:22000/wsman</a:To>"
209 "<w:ResourceURI s:mustUnderstand=\"true\">http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"
210 "cn"
211 mike 1.1 "</w:ResourceURI>"
212 "<a:ReplyTo>"
213 "<a:Address s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>"
214 "</a:ReplyTo>"
215 "<a:Action s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/09/enumeration/Release</a:Action>"
216 "<w:MaxEnvelopeSize s:mustUnderstand=\"true\">10240</w:MaxEnvelopeSize>"
217 "<a:MessageID>uuid:FEF3DF41-FFEC-4ABE-ADFC-A8305DAB71C9</a:MessageID>"
218 "<w:Locale xml:lang=\"en-US\" s:mustUnderstand=\"false\" /><w:SelectorSet>"
219 "<w:Selector Name=\"__cimnamespace\">"
220 "ns"
221 "</w:Selector>"
222 "</w:SelectorSet>"
223 "<w:OperationTimeout>PT60.000S</w:OperationTimeout>"
224 "</s:Header>"
225 "<s:Body>"
226 "<n:Release>"
227 "<wsen:EnumerationContext xmlns:wsen=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\">";
228
229 res += ctxID;
230 res +=
231 "</wsen:EnumerationContext>"
232 mike 1.1 "</n:Release>"
233 "</s:Body></s:Envelope>"
234 ;
235
236 return res;
237 }
238 END_EXTERNC
239
240 BEGIN_EXTERNC
241 static string _GetCtxID(const string& s)
242 {
243 // llok for <wsen:EnumerationContext>NNN</wsen:EnumerationContext>
244 // and return NNN (or empty string if not found)
245 string::size_type pos = s.find("<wsen:EnumerationContext>");
246 string::size_type posEnd = s.find("</wsen:EnumerationContext>");
247
248 if ( pos == string::npos ||
249 posEnd == string::npos ||
250 pos > posEnd )
251 return string();
252
253 mike 1.1 return s.substr(pos + 25, posEnd - pos -25);
254 }
255 END_EXTERNC
256
257 BEGIN_EXTERNC
258 static MI_Boolean _callback(
259 WSMAN* protocol,
260 Message* message,
261 void* data)
262 {
263 // reply with result
264 MI_Result r = (MI_Result) (long)data;
265
266 PostResultMsg* resp;
267
268 resp = PostResultMsg_New( message->msgID );
269 resp->base.clientID = message->clientID;
270
271 UT_ASSERT (resp != 0);
272
273 resp->result = r;
274 mike 1.1 UT_ASSERT(MI_RESULT_OK == WSMAN_Send(protocol, &resp->base));
275
276 PostResultMsg_Release(resp);
277 return MI_TRUE;
278 }
279 END_EXTERNC
280
281
282 static void TestWSMAN_EnumPull()
283 {
284 _StartWSMAN( _callback, (void*)MI_RESULT_OK);
285
286 // send enum request, expect enum context back (since no OptimzeEnum tag specified)
287 Sock s = SockConnectLocal(PORT);
288 string r_b, r_h;
289
290 SockSendRecvHTTP(s, false, _CreateEnumRequestXML(), r_h, r_b );
291
292 UT_ASSERT(r_b.find("wsen:EnumerationContext") != string::npos);
293
294 string ctxID = _GetCtxID(r_b);
295 mike 1.1
296 //cout << r_h << endl << r_b <<endl << ctxID << endl;
297 UT_ASSERT(!ctxID.empty());
298
299 // sedn pull request, expecting 'ok' back
300 SockSendRecvHTTP(s, false, _CreatePullRequestXML(ctxID), r_h, r_b );
301
302 //cout << r_h << endl << r_b <<endl << ctxID << endl;
303 UT_ASSERT(r_b.find("wsen:EndOfSequence") != string::npos);
304
305 Sock_Close(s);
306 }
307
308 static void TestWSMAN_EnumRelease()
309 {
310 _StartWSMAN( _callback, (void*)MI_RESULT_OK);
311
312 // send enum request, expect enum context back (since no OptimzeEnum tag specified)
313 Sock s = SockConnectLocal(PORT);
314 string r_b, r_h;
315
316 mike 1.1 SockSendRecvHTTP(s, false, _CreateEnumRequestXML(), r_h, r_b );
317
318 UT_ASSERT(r_b.find("wsen:EnumerationContext") != string::npos);
319
320 string ctxID = _GetCtxID(r_b);
321
322 //cout << r_h << endl << r_b <<endl << ctxID << endl;
323 UT_ASSERT(!ctxID.empty());
324
325 // sedn release request, expecting 'ok' back
326 SockSendRecvHTTP(s, false, _CreateReleaseRequestXML(ctxID), r_h, r_b );
327
328 //cout << r_h << endl << r_b <<endl << ctxID << endl;
329 UT_ASSERT(r_b.find("ReleaseResponse") != string::npos);
330
331 // send pull and verify context is invalid
332 SockSendRecvHTTP(s, false, _CreatePullRequestXML(ctxID), r_h, r_b );
333
334 //cout << r_h << endl << r_b <<endl << ctxID << endl;
335 UT_ASSERT(r_b.find("wsa:DestinationUnreachable") != string::npos);
336 UT_ASSERT(r_b.find("Enumeration context not found") != string::npos);
337 mike 1.1
338 Sock_Close(s);
339 }
340 static void TestWSMAN_PullWithInvalidCtxID()
341 {
342 _StartWSMAN( _callback, (void*)MI_RESULT_OK);
343
344 // send enum request, expect enum context back (since no OptimzeEnum tag specified)
345 Sock s = SockConnectLocal(PORT);
346 string r_b, r_h;
347
348 // sedn pull request with invalid ctx id, expecting 'error' back
349 SockSendRecvHTTP(s, false, _CreatePullRequestXML("invalid"), r_h, r_b );
350 UT_ASSERT(r_b.find("wsa:DestinationUnreachable") != string::npos);
351 UT_ASSERT(r_b.find("Enumeration context not found") != string::npos);
352
353 //cout << r_h << endl << r_b <<endl;
354 Sock_Close(s);
355 }
356
357 static void TestWSMAN_PullWithExpiredCtxID()
358 mike 1.1 {
359 WSMAN_Options options = {1000}; // 1 ms
360
361 _StartWSMAN( _callback, (void*)MI_RESULT_OK, &options);
362
363 // send enum request, expect enum context back (since no OptimzeEnum tag specified)
364 Sock s = SockConnectLocal(PORT);
365 string r_b, r_h;
366
367 SockSendRecvHTTP(s, false, _CreateEnumRequestXML(), r_h, r_b );
368
369 UT_ASSERT(r_b.find("wsen:EnumerationContext") != string::npos);
370
371 string ctxID = _GetCtxID(r_b);
372
373 //cout << r_h << endl << r_b <<endl << ctxID << endl;
374 UT_ASSERT(!ctxID.empty());
375
376 // sleep enough time for context ot expire
377 ut::sleep_ms(50);
378
379 mike 1.1 // sedn pull request, expecting 'error' back
380 SockSendRecvHTTP(s, false, _CreatePullRequestXML(ctxID), r_h, r_b );
381
382 //cout << r_h << endl << r_b <<endl << ctxID << endl;
383 UT_ASSERT(r_b.find("wsa:DestinationUnreachable") != string::npos);
384 UT_ASSERT(r_b.find("Enumeration context not found") != string::npos);
385
386 Sock_Close(s);
387 }
388
389 static void AllProviderTests()
390 {
391 #if defined(CONFIG_POSIX)
392 /* Disable Auth for unit-tests */
393 IgnoreAuthCalls(1);
394 #endif
395
396
397 // setup
398 Sock_Start();
399 Selector_Init(&s_selector);
400 mike 1.1
401 // test happy-pass case
402 UT_TEST(TestWSMAN_EnumPull);
403 UT_TEST(TestWSMAN_EnumRelease);
404
405 // general errors
406 UT_TEST(TestWSMAN_PullWithInvalidCtxID);
407 UT_TEST(TestWSMAN_PullWithExpiredCtxID);
408
409 // cleanup
410 Selector_Destroy(&s_selector);
411 Sock_Stop();
412 }
413
414 UT_ENTRY_POINT(AllProviderTests);
|