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 <common.h>
26 #include <string>
27 #include <vector>
28 #include <cctype>
29 #include <ut/ut.h>
30 #include <base/process.h>
31 #include <base/time.h>
32 #include <base/paths.h>
33 #include <base/io.h>
34 #include <base/dir.h>
35 #include <omiclient/client.h>
36
37 using namespace std;
38
39 static string s_socketFile;
40
41 extern int climain(int argc, const char* argv[]);
42
43 mike 1.1 // Parse the command line into tokens.
44 bool ParseCommandLine(const string& str, vector<char*>& args)
45 {
46 char* p = (char*)str.c_str();
47
48 for (;;)
49 {
50 // Skip leading whitespace:
51 while (*p && isspace(*p))
52 p++;
53
54 if (!*p)
55 break;
56
57 // Handle string literal:
58 if (*p == '"')
59 {
60 p++;
61 char* start = p;
62
63 if (!*p)
64 mike 1.1 return false;
65
66 while (*p && *p != '"')
67 {
68 if (*p == '\\')
69 {
70 p++;
71
72 if (!*p)
73 return false;
74 }
75
76 p++;
77 }
78
79 if (*p != '"')
80 return false;
81
82 *p++ = '\0';
83 args.push_back(start);
84 }
85 mike 1.1 else
86 {
87 char* start = p;
88
89 while (*p && !isspace(*p))
90 {
91 if (*p == '\\')
92 {
93 p++;
94
95 if (!*p)
96 return false;
97 }
98
99 p++;
100 }
101
102 if (isspace(*p))
103 *p++ = '\0';
104
105 args.push_back(start);
106 mike 1.1 }
107 }
108
109 args.push_back((char*)"--socketfile");
110 args.push_back((char*)s_socketFile.c_str());
111 return true;
112 }
113
114 static Process serverProcess;
115
116 static int StartServer()
117 {
118 const char* path = GetPath(ID_SERVERPROGRAM);
119 const char* argv[14];
120 char http[32];
121 char https[32];
122 std::string v;
123
124 Snprintf(http, sizeof(http),"%d", ut::getUnittestPortNumberWSMANHTTP());
125 Snprintf(https, sizeof(https),"%d", ut::getUnittestPortNumberWSMANHTTPS());
126
127 mike 1.1 s_socketFile = GetPath(ID_SOCKETFILE);
128
129 if (ut::testGetAttr("skipServer", v))
130 return 0;
131
132 argv[0] = path;
133 argv[1] = "--rundir";
134 #if defined(CONFIG_OS_WINDOWS)
135 argv[2] = "..";
136 #else
137 argv[2] = GetPath(ID_PREFIX);
138 #endif
139 argv[3] = "--ignoreAuthentication";
140 argv[4] = "--socketfile";
141 argv[5] = s_socketFile.c_str();
142 argv[6] = "--httpport";
143 argv[7] = http;
144 argv[8] = "--httpsport";
145 argv[9] = https;
146 argv[10] = "--livetime";
147 argv[11] = "300";
148 mike 1.1
149 argv[12] = NULL;
150
151 if (Process_StartChild(&serverProcess, path, (char**)argv) != 0)
152 return -1;
153
154 // wait for server to start
155 // trying to connect in a loop:
156 // since connect may fail quickly if server is not running
157 // keep doing it in a loop
158 for (int i = 0; i < 400; i++)
159 {
160 mi::Client cl;
161 const MI_Uint64 TIMEOUT = 1 * 1000 * 1000;
162
163 if (cl.Connect(s_socketFile.c_str(), "unittest", "unittest", TIMEOUT))
164 break;
165
166 Time_Sleep(10);
167 }
168
169 mike 1.1 return 0;
170 }
171
172 static int StopServer()
173 {
174 std::string v;
175
176 if (ut::testGetAttr("skipServer", v))
177 return 0;
178
179 if (Process_StopChild(&serverProcess) != 0)
180 return -1;
181 return 0;
182 }
183
184 static bool Inhale(const char* path, string& str)
185 {
186 char buf[1024];
187
188 str.clear();
189
190 mike 1.1 /* Open file */
191 FILE* is = Fopen(path, "rb");
192 if (!is)
193 return false;
194
195 /* Read file into str parameter */
196 for (;;)
197 {
198 ssize_t n = fread(buf, 1, sizeof(buf)-1, is);
199
200 if (n <= 0)
201 break;
202
203 buf[n] = '\0';
204 str += buf;
205 }
206
207 fclose(is);
208 return true;
209 }
210
211 mike 1.1 static bool InhaleTmpFile(const char* file, string& str)
212 {
213 char path[MAX_PATH_SIZE];
214 return Inhale(TempPath(path, file), str);
215 }
216
217 static bool InhaleTestFile(const char* file, string& str)
218 {
219 char path[MAX_PATH_SIZE];
220
221 Strlcpy(path, GetPath(ID_PREFIX), sizeof(path));
222 Strlcat(path, "/cli/tests/", sizeof(path));
223 Strlcat(path, file, sizeof(path));
224
225 return Inhale(path, str);
226 }
227
228 static int Exec(const string& cmd, string& out, string& err)
229 {
230 vector<char*> args;
231 char path1[MAX_PATH_SIZE];
232 mike 1.1 char path2[MAX_PATH_SIZE];
233
234 if (!ParseCommandLine(cmd, args))
235 return 1;
236
237 // Capture stdout:
238 args.push_back((char*)"--stdout");
239 args.push_back(TempPath(path1, "STDOUT"));
240
241 // Capture stderr:
242 args.push_back((char*)"--stderr");
243 args.push_back(TempPath(path2, "STDERR"));
244
245 // usernmae and password (ignored by server, but should be provided)
246 args.push_back((char*)"-u");
247 args.push_back((char*)"user");
248 args.push_back((char*)"-p");
249 args.push_back((char*)"pw");
250
251 args.push_back(NULL);
252
253 mike 1.1 #if 0
254 printf("====\n");
255 for (size_t i = 0; i < args.size(); i++)
256 {
257 printf("[%s]\n", args[i]);
258 }
259 #endif
260
261 char** argv = &args[0];
262 int argc = int(args.size() - 1);
263
264 int r = climain(argc, (const char**)argv);
265
266 UT_ASSERT(InhaleTmpFile("STDOUT", out));
267 UT_ASSERT(InhaleTmpFile("STDERR", err));
268
269 return r;
270 }
271
272 static void TestOMICLI1()
273 {
274 mike 1.1 const char CMD[] = "omicli noop";
275 string out;
276 string err;
277
278 UT_ASSERT(Exec(CMD, out, err) == 0);
279
280 UT_ASSERT(out == "got noop response\n");
281 UT_ASSERT(err == "");
282 }
283
284 static void TestOMICLI2()
285 {
286 string out;
287 string err;
288 UT_ASSERT(Exec("omicli ei root/test MSFT_President", out, err) == 0);
289 string str;
290
291 string expect;
292 UT_ASSERT(InhaleTestFile("TestOMICL12.txt", expect));
293 UT_ASSERT(out == expect);
294 UT_ASSERT(err == "");
295 mike 1.1 }
296
297 static void TestOMICLI3()
298 {
299 string out;
300 string err;
301 UT_ASSERT(Exec("omicli enc { X Array [ 1 2 3 ] }", out, err) == 0);
302
303 string expect;
304 UT_ASSERT(InhaleTestFile("TestOMICL13.txt", expect));
305 UT_ASSERT(out == expect);
306 UT_ASSERT(err == "");
307 }
308
309 static void TestOMICLI4()
310 {
311 string out;
312 string err;
313 UT_ASSERT(Exec("omicli gi root/test { MSFT_President Key 1 }",
314 out, err) == 0);
315
316 mike 1.1 string expect;
317 UT_ASSERT(InhaleTestFile("TestOMICL14.txt", expect));
318 UT_ASSERT(out == expect);
319 UT_ASSERT(err == "");
320 }
321
322 static void TestOMICLI5()
323 {
324 string out;
325 string err;
326 UT_ASSERT(Exec("omicli ci -s root/test "
327 "{ MSFT_President Key 3 First John Last Smith }",
328 out, err) == 2);
329
330 UT_ASSERT(out == "");
331 UT_ASSERT(err == "");
332 }
333
334 static void TestOMICLI6()
335 {
336 string out;
337 mike 1.1 string err;
338 UT_ASSERT(Exec("omicli -s mi root/test "
339 "{ MSFT_President Key 1 First Geo Last Washington }",
340 out, err) == 2);
341
342 UT_ASSERT(out == "");
343 UT_ASSERT(err == "");
344 }
345
346 static void TestOMICLI7()
347 {
348 string out;
349 string err;
350 UT_ASSERT(Exec("omicli ei test/cpp X_SmallNumber", out, err) == 0);
351
352 string expect;
353 UT_ASSERT(InhaleTestFile("TestOMICLI7.txt", expect));
354 UT_ASSERT(out == expect);
355 UT_ASSERT(err == "");
356 }
357
358 mike 1.1 static void TestOMICLI8()
359 {
360 string out;
361 string err;
362 UT_ASSERT(Exec("omicli enc { Link Left { Gadget Key 1 } Right "
363 "{ Gadget Key 2 } }", out, err) == 0);
364
365 string expect;
366 UT_ASSERT(InhaleTestFile("TestOMICLI8.txt", expect));
367 UT_ASSERT(out == expect);
368 UT_ASSERT(err == "");
369 }
370
371 static void TestOMICLI9()
372 {
373 string out;
374 string err;
375 UT_ASSERT(Exec("omicli enc "
376 "{ Class1 Key1 Value1 Key2 Value2 Key3 Value3 }", out, err) == 0);
377
378 string expect;
379 mike 1.1 UT_ASSERT(InhaleTestFile("TestOMICLI9.txt", expect));
380 UT_ASSERT(out == expect);
381 UT_ASSERT(err == "");
382 }
383
384 static void TestOMICLI10()
385 {
386 string out;
387 string err;
388 UT_ASSERT(Exec("omicli enc { W Left { X Key Hello } Right "
389 "{ Y Key { Z ID Mine } } }", out, err) == 0);
390
391 string expect;
392 UT_ASSERT(InhaleTestFile("TestOMICLI10.txt", expect));
393 UT_ASSERT(out == expect);
394 UT_ASSERT(err == "");
395 }
396
397 static void TestOMICLI11()
398 {
399 string out;
400 mike 1.1 string err;
401 UT_ASSERT(Exec(
402 "omicli iv test/cpp { X_SmallNumber } SpellNumber { num 123 }",
403 out, err) == 0);
404
405 string expect;
406 UT_ASSERT(InhaleTestFile("TestOMICLI11.txt", expect));
407 UT_ASSERT(out == expect);
408 UT_ASSERT(err == "");
409 }
410
411 static void TestOMICLI12()
412 {
413 string out;
414 string err;
415 UT_ASSERT(Exec(
416 "omicli iv test/cpp { X_SmallNumber Number 135 } GetFactors",
417 out, err) == 0);
418
419 string expect;
420 UT_ASSERT(InhaleTestFile("TestOMICLI12.txt", expect));
421 mike 1.1 UT_ASSERT(out == expect);
422 UT_ASSERT(err == "");
423 }
424
425 static void TestOMICLI13()
426 {
427 string out;
428 string err;
429 UT_ASSERT(Exec("omicli a test/cpp { MSFT_Person Key 1 }", out, err) == 0);
430
431 UT_ASSERT(out == "");
432 UT_ASSERT(err == "");
433 }
434
435 static void TestOMICLI14()
436 {
437 string out;
438 string err;
439 UT_ASSERT(Exec("omicli a test/cpp { X_numberWorld Name theWorld }",
440 out, err) == 0);
441
442 mike 1.1 string expect;
443 UT_ASSERT(InhaleTestFile("TestOMICLI14.txt", expect));
444 UT_ASSERT(out == expect);
445 UT_ASSERT(err == "");
446 }
447
448 static void TestOMICLI15()
449 {
450 string out;
451 string err;
452 UT_ASSERT(Exec("omicli r test/cpp { X_Profile InstanceID world } "
453 "-ac X_NumberWorldConformsToProfile", out, err) == 0);
454
455 string expect;
456 UT_ASSERT(InhaleTestFile("TestOMICLI15.txt", expect));
457 UT_ASSERT(out == expect);
458 UT_ASSERT(err == "");
459 }
460
461 static void TestOMICLI16()
462 {
463 mike 1.1 string out;
464 string err;
465 UT_ASSERT(Exec("omicli ei root/cimv2 XYZ_Widget", out, err) == 0);
466
467 /* No way to compare since result contains current timestamp */
468 UT_ASSERT(err == "");
469 }
470
471 static void TestOMICLI17()
472 {
473 string out;
474 string err;
475 UT_ASSERT(Exec("omicli -s ci root/cimv2 "
476 "{ XYZ_Widget SerialNumber 3 Colors [ Red Green Blue ] }",
477 out, err) == 2);
478
479 UT_ASSERT(out == "");
480 UT_ASSERT(err == "");
481 }
482
483 static void TestOMICLI18()
484 mike 1.1 {
485 string out;
486 string err;
487 UT_ASSERT(Exec("omicli ei root/test MSFT_VicePresident", out, err) == 0);
488
489 string expect;
490 UT_ASSERT(InhaleTestFile("TestOMICLI18.txt", expect));
491 UT_ASSERT(out == expect);
492 UT_ASSERT(err == "");
493 }
494
495 static void TestOMICLI19()
496 {
497 string out;
498 string err;
499 UT_ASSERT(Exec("omicli gi root/test { MSFT_VicePresident Key 2 }",
500 out, err) == 0);
501
502 string expect;
503 UT_ASSERT(InhaleTestFile("TestOMICLI19.txt", expect));
504 UT_ASSERT(out == expect);
505 mike 1.1 UT_ASSERT(err == "");
506 }
507
508 static void setUp()
509 {
510 }
511
512 static void cleanup()
513 {
514 }
515
516 static void RunTests()
517 {
518 StartServer();
519 UT_TEST(TestOMICLI1);
520 UT_TEST(TestOMICLI2);
521 UT_TEST(TestOMICLI3);
522 UT_TEST(TestOMICLI4);
523 UT_TEST(TestOMICLI5);
524 UT_TEST(TestOMICLI6);
525 UT_TEST(TestOMICLI7);
526 mike 1.1 UT_TEST(TestOMICLI8);
527 UT_TEST(TestOMICLI9);
528 UT_TEST(TestOMICLI10);
529 UT_TEST(TestOMICLI11);
530 UT_TEST(TestOMICLI12);
531 UT_TEST(TestOMICLI13);
532 UT_TEST(TestOMICLI14);
533 UT_TEST(TestOMICLI15);
534 UT_TEST(TestOMICLI16);
535 UT_TEST(TestOMICLI17);
536 UT_TEST(TestOMICLI18);
537 UT_TEST(TestOMICLI19);
538 StopServer();
539 }
540
541 UT_ENTRY_POINT(RunTests);
|