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 "ut.h"
26 #if defined(CONFIG_OS_WINDOWS)
27 # include <time.h>
28 #else
29 # include <unistd.h>
30 # include <sys/time.h>
31 # include <sys/types.h>
32 #endif
33
34 #ifdef T
35 #undef T
36 #endif
37
38 using namespace std;
39
40 namespace ut
41 {
42
43 mike 1.1 static FILE* Fopen(const char* path, const char* mode)
44 {
45 #if defined(_MSC_VER)
46 FILE* fp;
47 return fopen_s(&fp, path, mode) == 0 ? fp : NULL;
48 #else
49 return fopen(path, mode);
50 #endif
51 }
52
53
54 bool readFileContent( const std::string& file, std::vector< unsigned char >& content )
55 {
56 FILE* f = Fopen(file.c_str(), "r");
57 if ( !f )
58 return false;
59
60 content.resize( 1024 * 64 );
61 size_t res = fread(&content[0], 1, content.size(), f );
62 fclose(f);
63 content.resize( res );
64 mike 1.1 return true;
65 }
66
67
68 bool writeFileContent( const std::string& file, const std::vector< unsigned char >& content )
69 {
70 if (content.empty())
71 return false;
72
73 FILE* f = Fopen(file.c_str(), "w");
74 if ( !f )
75 return false;
76
77 size_t res = fwrite(&content[0], 1, content.size(), f );
78 fclose(f);
79 return res == content.size();
80 }
81
82
83 // looks for a file;
84 // start_dir - can be null (use curent directory); should be provided if test is playing with current directory
85 mike 1.1 // sub_directory - related to unittet directory - for example "tools/mof"
86 // file - file name
87 std::string findSampleFile(const char* start_directory, const char* sub_directory, const char* file)
88 {
89 string starting_dir( start_directory ? (string(start_directory) + "/") : string());
90
91 string res = starting_dir + file;
92
93 if ( access(res.c_str(), F_OK) == 0 )
94 return res;
95
96 res = starting_dir + string(sub_directory);
97 res += file;
98
99 if ( access(res.c_str(), F_OK) == 0 )
100 return res;
101
102 res = starting_dir + "../../../../unittest/" + sub_directory;
103 res += file;
104
105 if ( access(res.c_str(), F_OK) == 0 )
106 mike 1.1 return res;
107
108 UT_ASSERT_FAILED_MSG( string(string("file not found: ") + file).c_str() );
109 //return string();
110 }
111
112
113 static void removeAllCharsFromArray(vector< unsigned char >& content, unsigned char c)
114 {
115 for( size_t i = 0; i < content.size(); )
116 {
117 if ( content[i] == c )
118 content.erase(content.begin() + i);
119 else
120 i++;
121 }
122 }
123
124 // compare two files; return true if they match; asserts otherwise
125 bool compareFiles( const char* file1, const char* file2, bool ignore_win_cr /* = true */ )
126 {
127 mike 1.1 vector< unsigned char > content1, content2;
128
129 UT_ASSERT(readFileContent( file1, content1 ));
130 UT_ASSERT(readFileContent( file2, content2 ));
131
132 if ( ignore_win_cr )
133 {
134 removeAllCharsFromArray(content1, '\r');
135 removeAllCharsFromArray(content2, '\r');
136 }
137
138 if ( content1 != content2 )
139 {
140 #if defined(CONFIG_POSIX)
141 string cmd = "diff ";
142 cmd += file1;
143 cmd += " ";
144 cmd += file2;
145 system(cmd.c_str());
146 #endif
147 UT_ASSERT_FAILED_MSG( string(
148 mike 1.1 string("files ") + file1 + " and " + file2 + " are different" ).c_str() );
149 }
150 return true;
151 }
152
153 // copy 'src' to 'tgt'; return true if successful
154 bool copyFile( const char* targetFile, const char* sourceFile )
155 {
156 vector< unsigned char > content;
157 UT_ASSERT(readFileContent( sourceFile, content ));
158 UT_ASSERT(writeFileContent( targetFile, content ));
159 return true;
160 }
161
162 // removes file if it exists
163 void removeIfExist( const char* file )
164 {
165 if ( access(file, F_OK) == 0 )
166 UT_ASSERT( 0 == remove(file) );
167 }
168
169 mike 1.1 void sleep_sec(MI_Uint64 sec)
170 {
171 sleep_ms(sec * 1000);
172 }
173
174 void sleep_ms(MI_Uint64 ms_sec)
175 {
176 #if defined(CONFIG_OS_WINDOWS)
177 Sleep((DWORD)ms_sec);
178 #else
179 struct timespec rqtp;
180
181 rqtp.tv_sec = static_cast<long>(ms_sec/1000);
182 rqtp.tv_nsec = static_cast<long>((ms_sec%1000)*1000*1000);
183
184 nanosleep(&rqtp, NULL);
185 #endif
186 }
187
188 uint64 time_now()
189 {
190 mike 1.1 #if defined(CONFIG_OS_WINDOWS)
191 FILETIME ft;
192 ULARGE_INTEGER tmp;
193
194 GetSystemTimeAsFileTime(&ft);
195 tmp.u.LowPart = ft.dwLowDateTime;
196 tmp.u.HighPart = ft.dwHighDateTime;
197 tmp.QuadPart -= 0X19DB1DED53E8000;
198 return (tmp.QuadPart / (UINT64)10);
199 #else
200 struct timeval tv;
201 struct timezone tz;
202 memset(&tv, 0, sizeof(tv));
203 memset(&tz, 0, sizeof(tz));
204
205 if (gettimeofday(&tv, &tz) != 0)
206 return 0;
207
208 return ((MI_Uint64)tv.tv_sec * (MI_Uint64)1000000 + (MI_Uint64)tv.tv_usec);
209 #endif
210 }
211 mike 1.1
212 }
|