• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "compare_tools.h"
17 #include <cstring>
18 #include "common/screen.h"
19 #include "dock/screen_device_proxy.h"
20 #include "draw/draw_utils.h"
21 #include "gfx_utils/file.h"
22 #include "gfx_utils/graphic_log.h"
23 #include "gfx_utils/graphic_math.h"
24 #include "graphic_config.h"
25 #include "securec.h"
26 
27 #ifdef _WIN32
28     #define STR(STRING) #STRING
29     #define STRPATH(STRING) STR(STRING)
30 #endif
31 
32 namespace OHOS {
33 bool CompareTools::enableLog_ = false;
34 char* CompareTools::logPath_ = nullptr;
35 
WaitSuspend()36 void CompareTools::WaitSuspend()
37 {
38 #ifdef _WIN32
39     Sleep(DEFAULT_WAIT_TIME_MS);
40 #else
41     usleep(1000 * DEFAULT_WAIT_TIME_MS); // 1000: us to ms
42 #endif // _WIN32
43 }
44 
StrnCatPath(char * filePath,size_t pathMax,const char * fileName,size_t count)45 bool CompareTools::StrnCatPath(char* filePath, size_t pathMax, const char* fileName, size_t count)
46 {
47     if ((filePath == nullptr) || (pathMax > DEFAULT_FILE_NAME_MAX_LENGTH)) {
48         return false;
49     }
50 #ifdef _WIN32
51     char dest[DEFAULT_FILE_NAME_MAX_LENGTH] = STRPATH(AUTO_TEST_RESOURCE_PATH);
52 #else
53     char dest[DEFAULT_FILE_NAME_MAX_LENGTH] = AUTO_TEST_RESOURCE_PATH;
54 #endif // _WIN32
55     if (strncat_s(dest, DEFAULT_FILE_NAME_MAX_LENGTH, fileName, count) != EOK) {
56         return false;
57     }
58     if (memcpy_s(static_cast<void *>(filePath), pathMax, dest, DEFAULT_FILE_NAME_MAX_LENGTH) != EOK) {
59         return false;
60     }
61     return true;
62 }
63 
CompareByBit(uint32_t fd)64 bool CompareTools::CompareByBit(uint32_t fd)
65 {
66     ImageInfo imageBit;
67     if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
68         return false;
69     }
70     struct BitmapInfoHeader bitmapInfo = {0};
71     lseek(fd, sizeof(uint16_t), SEEK_SET);
72     if (read(fd, &bitmapInfo, sizeof(bitmapInfo)) < 0) {
73         ImageCacheFree(imageBit);
74         return false;
75     }
76     if (bitmapInfo.biSizeImage != imageBit.dataSize) {
77         ImageCacheFree(imageBit);
78         return false;
79     }
80     bool flag = true;
81     uint32_t buffSize = bitmapInfo.biSizeImage / MATH_ABS(bitmapInfo.biHeight);
82     auto buff = new uint8_t[buffSize];
83     for (uint32_t i = 0; i < MATH_ABS(bitmapInfo.biHeight); i++) {
84         if (flag && (memset_s(buff, buffSize, 0, buffSize) != EOK)) {
85             flag = false;
86             break;
87         }
88         uint32_t ret = read(fd, buff, buffSize);
89         if (ret < 0) {
90             flag = false;
91             break;
92         }
93         for (uint32_t j = 0; j < ret; j++) {
94             if (buff[j] != imageBit.data[i * buffSize + j]) {
95                 flag = false;
96                 break;
97             }
98         }
99     }
100     ImageCacheFree(imageBit);
101     delete [] buff;
102     buff = nullptr;
103     return flag;
104 }
105 
CompareFile(const char * filePath,size_t length)106 bool CompareTools::CompareFile(const char* filePath, size_t length)
107 {
108     if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
109         return false;
110     }
111 #ifdef _WIN32
112     uint32_t fd = open(filePath, O_RDONLY | O_BINARY);
113 #else
114     uint32_t fd = open(filePath, O_RDONLY);
115 #endif
116     if (fd == -1) {
117         return false;
118     }
119     bool flag = CompareByBit(fd);
120     close(fd);
121     if (flag) {
122         GRAPHIC_LOGI("[COMPARE_SUCCESS]:fileName=%s", filePath);
123         if (enableLog_) {
124             char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
125             if (sprintf_s(logInfo, sizeof(logInfo), "[COMPARE_SUCCESS]:fileName=%s\n", filePath) < 0) {
126                 return false;
127             }
128             SaveLog(logInfo, strlen(logInfo));
129         }
130     } else {
131         GRAPHIC_LOGI("[COMPARE_FAILURE]:fileName=%s", filePath);
132         if (enableLog_) {
133             char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
134             if (sprintf_s(logInfo, sizeof(logInfo), "[COMPARE_FAILURE]:fileName=%s\n", filePath) < 0) {
135                 return false;
136             }
137             SaveLog(logInfo, strlen(logInfo));
138         }
139     }
140     return flag;
141 }
142 
SaveByBit(uint32_t fd)143 bool CompareTools::SaveByBit(uint32_t fd)
144 {
145     ImageInfo imageBit;
146     if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
147         return false;
148     }
149     bool flag = false;
150     uint8_t sizeByColorMode = DrawUtils::GetByteSizeByColorMode(ScreenDeviceProxy::GetInstance()->GetBufferMode());
151     uint16_t bfType = 0x4D42;
152     struct BitmapInfoHeader bitmapInfo = {0};
153     bitmapInfo.bfSize = imageBit.dataSize + BITMAP_HEADER_SIZE;
154     bitmapInfo.bfOffBits = BITMAP_HEADER_SIZE;
155     bitmapInfo.biSize = 40; // 40: bitmap infomation header size
156     bitmapInfo.biWidth = imageBit.header.width;
157     bitmapInfo.biHeight = -imageBit.header.height;
158     bitmapInfo.biPlanes = 1;
159     bitmapInfo.biBitCount = sizeByColorMode * 8; // 8: uint8_t bit
160     bitmapInfo.biSizeImage = imageBit.dataSize;
161     if (!flag && (write(fd, &bfType, sizeof(bfType)) > 0)) {
162         if (write(fd, &bitmapInfo, sizeof(bitmapInfo)) > 0) {
163             if (write(fd, imageBit.data, imageBit.dataSize) > 0) {
164                 flag = true;
165             }
166         }
167     }
168     ImageCacheFree(imageBit);
169     return flag;
170 }
171 
SaveFile(const char * filePath,size_t length)172 bool CompareTools::SaveFile(const char* filePath, size_t length)
173 {
174     if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
175         return false;
176     }
177 #ifdef _WIN32
178     uint32_t fd = open(filePath, O_WRONLY | O_CREAT | O_BINARY, DEFAULT_FILE_PERMISSION);
179 #else
180     uint32_t fd = open(filePath, O_WRONLY | O_CREAT, DEFAULT_FILE_PERMISSION);
181 #endif
182     if (fd == -1) {
183         return false;
184     }
185     bool flag = SaveByBit(fd);
186     close(fd);
187     if (flag) {
188         GRAPHIC_LOGI("[SAVE_SUCCESS]:filePath = %s", filePath);
189         if (enableLog_) {
190             char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
191             if (sprintf_s(logInfo, sizeof(logInfo), "[SAVE_SUCCESS]:fileName=%s\n", filePath) < 0) {
192                 return false;
193             }
194             SaveLog(logInfo, strlen(logInfo));
195         }
196     } else {
197         GRAPHIC_LOGI("[SAVE_FAILURE]:filePath = %s", filePath);
198         if (enableLog_) {
199             char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
200             if (sprintf_s(logInfo, sizeof(logInfo), "[SAVE_FAILURE]:fileName=%s\n", filePath) < 0) {
201                 return false;
202             }
203             SaveLog(logInfo, strlen(logInfo));
204         }
205     }
206     return flag;
207 }
208 
CheckFileExist(const char * filePath,size_t length)209 bool CompareTools::CheckFileExist(const char* filePath, size_t length)
210 {
211     if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
212         return false;
213     }
214     uint32_t fd = open(filePath, O_RDONLY);
215     if (fd == -1) {
216         return false;
217     }
218     close(fd);
219     return true;
220 }
221 
SetLogPath(const char * filePath,size_t length)222 void CompareTools::SetLogPath(const char* filePath, size_t length)
223 {
224     if (logPath_ == nullptr) {
225         logPath_ = new char[length];
226         if (logPath_ == nullptr) {
227             return;
228         }
229         if (memcpy_s(logPath_, length, filePath, length) != EOK) {
230             GRAPHIC_LOGE("memcpy filepath failed");
231             return;
232         }
233         enableLog_ = true;
234     }
235 }
236 
UnsetLogPath()237 void CompareTools::UnsetLogPath()
238 {
239     if (logPath_ != nullptr) {
240         delete[] logPath_;
241         logPath_ = nullptr;
242         enableLog_ = false;
243     }
244 }
245 
SaveLog(const char * buff,size_t bufSize)246 bool CompareTools::SaveLog(const char* buff, size_t bufSize)
247 {
248     if ((buff == nullptr) || (logPath_ == nullptr)) {
249         return false;
250     }
251     uint32_t logFd = open(logPath_, O_WRONLY | O_CREAT | O_APPEND, DEFAULT_FILE_PERMISSION);
252     if (logFd == -1) {
253         GRAPHIC_LOGE("open log failed");
254         return false;
255     }
256     if (write(logFd, buff, bufSize) < 0) {
257         close(logFd);
258         GRAPHIC_LOGE("write log failed");
259         return false;
260     }
261     close(logFd);
262     return true;
263 }
264 } // namespace OHOS
265