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 #ifndef _ut_utility_h
26 #define _ut_utility_h
27
28 #include <vector>
29 #include <string>
30 #include "ut.h"
31 #include <common.h>
32
33 #if defined(CONFIG_POSIX)
34 # include <unistd.h>
35 #elif defined(CONFIG_OS_WINDOWS)
36 #endif
37
38 namespace ut
39 {
40 // collection of utility functions
41
42
43 mike 1.1 static inline int _StrToFalse(const char*)
44 {
45 return 0;
46 }
47
48 bool readFileContent( const std::string& file, std::vector< unsigned char >& content );
49
50 bool writeFileContent( const std::string& file, const std::vector< unsigned char >& content );
51
52 // looks for a file;
53 // start_dir - can be null (use curent directory); should be provided if test is playing with current directory
54 // sub_directory - related to unittet directory - for example "tools/mof"
55 // file - file name
56 // reutrns file if found or ASSERTs otherwise
57 std::string findSampleFile(const char* start_dir, const char* sub_directory, const char* file);
58
59
60 // compare two files; return true if they match; asserts otherwise
61 bool compareFiles( const char* generatedFile, const char* sampleFile, bool ignore_win_cr = true );
62
63 inline bool compareFiles(
64 mike 1.1 const std::string& generatedFile,
65 const std::string& sampleFile,
66 bool ignore_win_cr = true)
67 {
68 return compareFiles(
69 generatedFile.c_str(), sampleFile.c_str(), ignore_win_cr);
70 }
71
72 // copy 'src' to 'tgt'; return true if successful
73 bool copyFile( const char* targetFile, const char* sourceFile );
74
75 inline bool copyFile(
76 const std::string& destFile,
77 const std::string& srcFile)
78 {
79 return copyFile(destFile.c_str(), srcFile.c_str());
80 }
81
82 void removeIfExist( const char* file );
83
84
85 mike 1.1 // sleep - all variations
86 void sleep_sec(MI_Uint64 sec);
87 void sleep_ms(MI_Uint64 ms_sec);
88
89 // time funcitonality
90 #if defined(_MSC_VER)
91 typedef unsigned __int64 uint64;
92 #else
93 typedef unsigned long long uint64;
94 #endif
95 uint64 time_now();
96
97 #if defined(_MSC_VER)
98 inline unsigned short getUnittestPortNumber()
99 {
100 return 21718;
101 }
102 #else
103 inline unsigned short getUnittestPortNumber()
104 {
105 return 10000 + ((geteuid() % 200) * 200);
106 mike 1.1 }
107 #endif
108
109 inline unsigned short getUnittestPortNumberWSMANHTTP()
110 {
111 std::string v;
112
113 if (ut::testGetAttr("skipServer", v))
114 {
115 return 7778;
116 }
117
118 return getUnittestPortNumber() + 198;
119 }
120 inline unsigned short getUnittestPortNumberWSMANHTTPS()
121 {
122 return getUnittestPortNumber() + 197;
123 }
124
125 };
126
127 mike 1.1 #endif /* _ut_utility_h */
|