1 /* 2 * Copyright (C) 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 #include <exception> 16 #include <iostream> 17 #include <string> 18 #include <gtest/gtest.h> 19 #include <unistd.h> 20 #include <cstring> 21 #include <cstdint> 22 #include <cstdio> 23 #include <functional> 24 #include "sp_utils.h" 25 26 using namespace testing::ext; 27 using namespace std; 28 29 namespace OHOS { 30 namespace SmartPerf { 31 class SPdaemonUtilsTest : public testing::Test { 32 public: SetUpTestCase()33 static void SetUpTestCase() {} TearDownTestCase()34 static void TearDownTestCase() {} SetUp()35 void SetUp() {} TearDown()36 void TearDown() {} 37 }; 38 39 /** 40 * @tc.name: SPUtils::IntegerValueVerification 41 * @tc.desc: Test IntegerValueVerification 42 * @tc.type: FUNC 43 */ 44 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest001, TestSize.Level1) 45 { 46 std::string errorInfo; 47 std::map<std::string, std::string> mapInfo; 48 std::set<std::string> keys; 49 50 keys.insert("N"); 51 keys.insert("fl"); 52 keys.insert("ftl"); 53 54 mapInfo["N"] = ""; 55 mapInfo["fl"] = ""; 56 mapInfo["ftl"] = ""; 57 58 bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo); 59 EXPECT_EQ(ret, false); 60 } 61 62 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest002, TestSize.Level1) 63 { 64 std::string errorInfo; 65 std::map<std::string, std::string> mapInfo; 66 std::set<std::string> keys; 67 68 keys.insert("N"); 69 keys.insert("fl"); 70 keys.insert("ftl"); 71 72 mapInfo["N"] = "A"; 73 mapInfo["fl"] = "B"; 74 mapInfo["ftl"] = "C"; 75 76 bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo); 77 EXPECT_EQ(ret, false); 78 } 79 80 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest003, TestSize.Level1) 81 { 82 std::string errorInfo; 83 std::map<std::string, std::string> mapInfo; 84 std::set<std::string> keys; 85 86 keys.insert("N"); 87 keys.insert("fl"); 88 keys.insert("ftl"); 89 90 mapInfo["N"] = "1"; 91 mapInfo["fl"] = "2"; 92 mapInfo["ftl"] = "3"; 93 94 bool ret = SPUtils::IntegerValueVerification(keys, mapInfo, errorInfo); 95 EXPECT_EQ(ret, true); 96 } 97 98 /** 99 * @tc.name: SPUtils::VerifyValueStr 100 * @tc.desc: Test VerifyValueStr 101 * @tc.type: FUNC 102 */ 103 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest001, TestSize.Level1) 104 { 105 std::string errorInfo; 106 std::map<std::string, std::string> mapInfo; 107 mapInfo["VIEW"] = ""; 108 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 109 EXPECT_EQ(ret, false); 110 } 111 112 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest002, TestSize.Level1) 113 { 114 std::string errorInfo; 115 std::map<std::string, std::string> mapInfo; 116 mapInfo["VIEW"] = "TestVIEW"; 117 mapInfo["PKG"] = ""; 118 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 119 EXPECT_EQ(ret, false); 120 } 121 122 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest003, TestSize.Level1) 123 { 124 std::string errorInfo; 125 std::map<std::string, std::string> mapInfo; 126 mapInfo["VIEW"] = "TestVIEW"; 127 mapInfo["PKG"] = "TestPKG"; 128 mapInfo["OUT"] = ""; 129 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 130 EXPECT_EQ(ret, false); 131 } 132 133 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest004, TestSize.Level1) 134 { 135 std::string errorInfo; 136 std::map<std::string, std::string> mapInfo; 137 mapInfo["VIEW"] = "TestVIEW"; 138 mapInfo["PKG"] = "TestPKG"; 139 mapInfo["OUT"] = "Test/sp_utils_test/"; 140 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 141 EXPECT_EQ(ret, false); 142 } 143 144 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest005, TestSize.Level1) 145 { 146 std::string errorInfo; 147 std::map<std::string, std::string> mapInfo; 148 mapInfo["VIEW"] = "TestVIEW"; 149 mapInfo["PKG"] = "TestPKG"; 150 mapInfo["OUT"] = "/sp_utils_test"; 151 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 152 EXPECT_EQ(ret, false); 153 } 154 155 HWTEST_F(SPdaemonUtilsTest, VerifyValueStrTest006, TestSize.Level1) 156 { 157 std::string errorInfo; 158 std::map<std::string, std::string> mapInfo; 159 mapInfo["VIEW"] = "TestVIEW"; 160 mapInfo["PKG"] = "TestPKG"; 161 bool ret = SPUtils::VerifyValueStr(mapInfo, errorInfo); 162 EXPECT_EQ(ret, true); 163 } 164 165 /** 166 * @tc.name: SPUtils::VeriyKey 167 * @tc.desc: Test VeriyKey 168 * @tc.type: FUNC 169 */ 170 HWTEST_F(SPdaemonUtilsTest, VeriyKey001, TestSize.Level1) 171 { 172 std::set<std::string> keys; 173 std::map<std::string, std::string> mapInfo; 174 std::string errorInfo; 175 176 keys.insert("apple"); 177 keys.insert("banana"); 178 keys.insert("cherry"); 179 keys.insert("orange"); 180 keys.insert("pineapple"); 181 182 mapInfo["A"] = ""; 183 mapInfo["B"] = ""; 184 mapInfo["C"] = ""; 185 186 bool ret = SPUtils::VeriyKey(keys, mapInfo, errorInfo); 187 EXPECT_EQ(ret, false); 188 } 189 190 HWTEST_F(SPdaemonUtilsTest, VeriyKey002, TestSize.Level1) 191 { 192 std::set<std::string> keys; 193 std::map<std::string, std::string> mapInfo; 194 std::string errorInfo; 195 196 keys.insert("apple"); 197 keys.insert("banana"); 198 keys.insert("cherry"); 199 keys.insert("orange"); 200 keys.insert("pineapple"); 201 202 mapInfo["apple"] = ""; 203 mapInfo["cherry"] = ""; 204 mapInfo["pineapple"] = ""; 205 206 bool ret = SPUtils::VeriyKey(keys, mapInfo, errorInfo); 207 EXPECT_EQ(ret, true); 208 } 209 210 HWTEST_F(SPdaemonUtilsTest, GetIsGameAppTest, TestSize.Level1) 211 { 212 std::string pkg = "com.example.game"; 213 std::string cmd = "hidumper -s 66006 -a '-t " + pkg + "'"; 214 FILE *fd = popen(cmd.c_str(), "r"); 215 char buf[1024] = {'\0'}; 216 bool ret = false; 217 while ((fgets(buf, sizeof(buf), fd)) != nullptr) { 218 std::string line(buf); 219 if (line.find("---") != std::string::npos || line.length() <= 1) { 220 continue; 221 } 222 if (line.find("for help") != std::string::npos) { 223 ret = true; 224 break; 225 } 226 } 227 EXPECT_EQ(ret, false); 228 } 229 230 HWTEST_F(SPdaemonUtilsTest, GetPathPermissionsTest, TestSize.Level1) 231 { 232 std::string pathDataCsv = "/data/local/tmp/data.csv"; 233 std::string pathIndexInfoCsv = "/data/local/tmp/smartperf/1/t_index_info.csv"; 234 std::string dataCsv = "/data/local/tmp/data.csv"; 235 std::string indexInfoCsv = "/data/local/tmp/smartperf/1/t_index_info.csv"; 236 std::string cmdResult = ""; 237 bool findData = false; 238 bool findIndex = false; 239 int isDataCsv = strcmp(pathDataCsv.c_str(), dataCsv.c_str()); 240 int isIndexInfoCsv = strcmp(pathIndexInfoCsv.c_str(), indexInfoCsv.c_str()); 241 if (isDataCsv == 0) { 242 std::string cmd = "ls -l " + pathDataCsv; 243 SPUtils::LoadCmd(cmd, cmdResult); 244 if (cmdResult.find("No such file or directory") != std::string::npos) { 245 findData = false; 246 } else { 247 std::string result = cmdResult.substr(0, 10); 248 findData = (result == "-rw-r--r--"); 249 } 250 } 251 if (isIndexInfoCsv == 0) { 252 std::string cmd = "ls -l " + pathIndexInfoCsv; 253 SPUtils::LoadCmd(cmd, cmdResult); 254 if (cmdResult.find("No such file or directory") != std::string::npos) { 255 findIndex = false; 256 } else { 257 std::string result = cmdResult.substr(0, 10); 258 findIndex = (result == "-rw-r--r--"); 259 } 260 } 261 EXPECT_EQ(findData, false); 262 EXPECT_EQ(findIndex, true); 263 } 264 265 HWTEST_F(SPdaemonUtilsTest, GetCurrentTime, TestSize.Level1) 266 { 267 bool shouldContinue = true; 268 SPUtils::GetCurrentTime(10); 269 EXPECT_TRUE(shouldContinue); 270 } 271 272 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest01, TestSize.Level1) 273 { 274 std::set<std::string> keys; 275 std::string param = "key1-value key1-value2"; 276 std::string errorInfo = ""; 277 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 278 EXPECT_FALSE(result); 279 EXPECT_NE(errorInfo, "duplicate parameters -- 'key1'"); 280 } 281 282 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest02, TestSize.Level1) 283 { 284 std::set<std::string> keys; 285 std::string param = "key1-value key2-value2"; 286 std::string errorInfo = ""; 287 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 288 EXPECT_FALSE(result); 289 EXPECT_NE(errorInfo, ""); 290 } 291 292 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest03, TestSize.Level1) 293 { 294 std::set<std::string> keys = {"key1"}; 295 std::string param = "key2-value1"; 296 std::string errorInfo = ""; 297 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 298 EXPECT_FALSE(result); 299 EXPECT_NE(errorInfo, ""); 300 } 301 302 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest04, TestSize.Level1) 303 { 304 std::set<std::string> keys = {"key1"}; 305 std::string param = "key1-value1"; 306 std::string errorInfo = ""; 307 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 308 EXPECT_FALSE(result); 309 EXPECT_NE(errorInfo, ""); 310 } 311 312 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest05, TestSize.Level1) 313 { 314 std::set<std::string> keys = {"key1"}; 315 std::string param = "key1-value1"; 316 std::string errorInfo = ""; 317 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 318 EXPECT_FALSE(result); 319 EXPECT_NE(errorInfo, ""); 320 } 321 322 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest01, TestSize.Level1) 323 { 324 std::string errorInfo = ""; 325 bool result = SPUtils::IntegerVerification("", errorInfo); 326 EXPECT_FALSE(result); 327 EXPECT_NE(errorInfo, "option requires an argument"); 328 } 329 330 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest02, TestSize.Level1) 331 { 332 std::string errorInfo = ""; 333 std::string longString(11, '1'); 334 bool result = SPUtils::IntegerVerification(longString, errorInfo); 335 EXPECT_FALSE(result); 336 EXPECT_NE(errorInfo, "invalid option parameters"); 337 } 338 339 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest03, TestSize.Level1) 340 { 341 std::string errorInfo = ""; 342 bool result = SPUtils::IntegerVerification("abc", errorInfo); 343 EXPECT_FALSE(result); 344 EXPECT_NE(errorInfo, "invalid option parameters"); 345 } 346 347 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest05, TestSize.Level1) 348 { 349 std::string errorInfo = ""; 350 std::string outOfRangeString = "18446744073709551616"; 351 bool result = SPUtils::IntegerVerification(outOfRangeString, errorInfo); 352 EXPECT_FALSE(result); 353 EXPECT_NE(errorInfo, "option parameter out of range"); 354 } 355 356 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest01, TestSize.Level1) 357 { 358 std::string str = " Hello World "; 359 SPUtils::RemoveSpace(str); 360 EXPECT_EQ(str, "Hello World"); 361 } 362 363 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest02, TestSize.Level1) 364 { 365 std::string str = " HelloWorld "; 366 SPUtils::RemoveSpace(str); 367 EXPECT_EQ(str, "HelloWorld"); 368 } 369 370 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest03, TestSize.Level1) 371 { 372 std::string str = "Hello World "; 373 SPUtils::RemoveSpace(str); 374 EXPECT_EQ(str, "Hello World"); 375 } 376 377 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest04, TestSize.Level1) 378 { 379 std::string str = " HelloWorld"; 380 SPUtils::RemoveSpace(str); 381 EXPECT_EQ(str, "HelloWorld"); 382 } 383 384 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest05, TestSize.Level1) 385 { 386 std::string str = "HelloWorld"; 387 SPUtils::RemoveSpace(str); 388 EXPECT_EQ(str, "HelloWorld"); 389 } 390 391 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest06, TestSize.Level1) 392 { 393 std::string str = " "; 394 SPUtils::RemoveSpace(str); 395 EXPECT_EQ(str, ""); 396 } 397 398 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest07, TestSize.Level1) 399 { 400 std::string str = ""; 401 SPUtils::RemoveSpace(str); 402 EXPECT_EQ(str, ""); 403 } 404 405 HWTEST_F(SPdaemonUtilsTest, GetCurTimeTest002, TestSize.Level1) 406 { 407 long long timeStampFir = SPUtils::GetCurTime(); 408 usleep(1000); 409 long long timeStampSec = SPUtils::GetCurTime(); 410 EXPECT_GT(timeStampSec, timeStampFir); 411 } 412 413 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest01, TestSize.Level1) 414 { 415 std::string testStr = "Hello\rWorld\n"; 416 SPUtils::ReplaceString(testStr); 417 EXPECT_EQ(testStr, "HelloWorld"); 418 } 419 420 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest02, TestSize.Level1) 421 { 422 std::string testStr = "\r\r\r"; 423 SPUtils::ReplaceString(testStr); 424 EXPECT_EQ(testStr, ""); 425 } 426 427 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest03, TestSize.Level1) 428 { 429 std::string testStr = "Hello\nWorld\n"; 430 SPUtils::ReplaceString(testStr); 431 EXPECT_EQ(testStr, "HelloWorld"); 432 } 433 434 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest04, TestSize.Level1) 435 { 436 std::string testStr = "\n\n\n"; 437 SPUtils::ReplaceString(testStr); 438 EXPECT_EQ(testStr, ""); 439 } 440 441 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest05, TestSize.Level1) 442 { 443 std::string testStr = "Hello\r\nWorld\r\n"; 444 SPUtils::ReplaceString(testStr); 445 EXPECT_EQ(testStr, "HelloWorld"); 446 } 447 448 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest06, TestSize.Level1) 449 { 450 std::string testStr = "Hello\r\n\r\nWorld\r\n\r\n"; 451 SPUtils::ReplaceString(testStr); 452 EXPECT_EQ(testStr, "HelloWorld"); 453 } 454 } 455 }