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, GetCurrentTime, TestSize.Level1) 231 { 232 bool shouldContinue = true; 233 SPUtils::GetCurrentTime(10); 234 EXPECT_TRUE(shouldContinue); 235 } 236 237 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest01, TestSize.Level1) 238 { 239 std::set<std::string> keys; 240 std::string param = "key1-value key1-value2"; 241 std::string errorInfo = ""; 242 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 243 EXPECT_FALSE(result); 244 EXPECT_NE(errorInfo, "duplicate parameters -- 'key1'"); 245 } 246 247 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest02, TestSize.Level1) 248 { 249 std::set<std::string> keys; 250 std::string param = "key1-value key2-value2"; 251 std::string errorInfo = ""; 252 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 253 EXPECT_FALSE(result); 254 EXPECT_NE(errorInfo, ""); 255 } 256 257 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest03, TestSize.Level1) 258 { 259 std::set<std::string> keys = {"key1"}; 260 std::string param = "key2-value1"; 261 std::string errorInfo = ""; 262 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 263 EXPECT_FALSE(result); 264 EXPECT_NE(errorInfo, ""); 265 } 266 267 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest04, TestSize.Level1) 268 { 269 std::set<std::string> keys = {"key1"}; 270 std::string param = "key1-value1"; 271 std::string errorInfo = ""; 272 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 273 EXPECT_FALSE(result); 274 EXPECT_NE(errorInfo, ""); 275 } 276 277 HWTEST_F(SPdaemonUtilsTest, VeriyParameterTest05, TestSize.Level1) 278 { 279 std::set<std::string> keys = {"key1"}; 280 std::string param = "key1-value1"; 281 std::string errorInfo = ""; 282 bool result = SPUtils::VeriyParameter(keys, param, errorInfo); 283 EXPECT_FALSE(result); 284 EXPECT_NE(errorInfo, ""); 285 } 286 287 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest01, TestSize.Level1) 288 { 289 std::string errorInfo = ""; 290 std::string outOfRangeString = ""; 291 bool result = SPUtils::IntegerVerification(outOfRangeString, errorInfo); 292 EXPECT_FALSE(result); 293 EXPECT_EQ(errorInfo, "option requires an argument"); 294 } 295 296 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest02, TestSize.Level1) 297 { 298 std::string errorInfo; 299 std::string longString(11, '1'); 300 EXPECT_FALSE(SPUtils::IntegerVerification(longString, errorInfo)); 301 EXPECT_EQ(errorInfo, "invalid option parameters"); 302 } 303 304 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest03, TestSize.Level1) 305 { 306 std::string errorInfo; 307 EXPECT_FALSE(SPUtils::IntegerVerification("123a456", errorInfo)); 308 EXPECT_EQ(errorInfo, "invalid option parameters"); 309 } 310 311 HWTEST_F(SPdaemonUtilsTest, IntegerValueVerificationTest05, TestSize.Level1) 312 { 313 std::string errorInfo = ""; 314 std::string outOfRangeString = "18446744073709551616"; 315 bool result = SPUtils::IntegerVerification(outOfRangeString, errorInfo); 316 EXPECT_FALSE(result); 317 EXPECT_NE(errorInfo, "option parameter out of range"); 318 } 319 320 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest01, TestSize.Level1) 321 { 322 std::string str = " Hello World "; 323 SPUtils::RemoveSpace(str); 324 EXPECT_EQ(str, "Hello World"); 325 } 326 327 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest02, TestSize.Level1) 328 { 329 std::string str = " HelloWorld "; 330 SPUtils::RemoveSpace(str); 331 EXPECT_EQ(str, "HelloWorld"); 332 } 333 334 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest03, TestSize.Level1) 335 { 336 std::string str = "Hello World "; 337 SPUtils::RemoveSpace(str); 338 EXPECT_EQ(str, "Hello World"); 339 } 340 341 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest04, TestSize.Level1) 342 { 343 std::string str = " HelloWorld"; 344 SPUtils::RemoveSpace(str); 345 EXPECT_EQ(str, "HelloWorld"); 346 } 347 348 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest05, TestSize.Level1) 349 { 350 std::string str = "HelloWorld"; 351 SPUtils::RemoveSpace(str); 352 EXPECT_EQ(str, "HelloWorld"); 353 } 354 355 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest06, TestSize.Level1) 356 { 357 std::string str = " "; 358 SPUtils::RemoveSpace(str); 359 EXPECT_EQ(str, ""); 360 } 361 362 HWTEST_F(SPdaemonUtilsTest, RemoveSpaceTest07, TestSize.Level1) 363 { 364 std::string str = ""; 365 SPUtils::RemoveSpace(str); 366 EXPECT_EQ(str, ""); 367 } 368 369 HWTEST_F(SPdaemonUtilsTest, GetCurTimeTest002, TestSize.Level1) 370 { 371 long long timeStampFir = SPUtils::GetCurTime(); 372 usleep(1000); 373 long long timeStampSec = SPUtils::GetCurTime(); 374 EXPECT_GT(timeStampSec, timeStampFir); 375 } 376 377 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest01, TestSize.Level1) 378 { 379 std::string testStr = "Hello\rWorld\n"; 380 SPUtils::ReplaceString(testStr); 381 EXPECT_EQ(testStr, "HelloWorld"); 382 } 383 384 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest02, TestSize.Level1) 385 { 386 std::string testStr = "\r\r\r"; 387 SPUtils::ReplaceString(testStr); 388 EXPECT_EQ(testStr, ""); 389 } 390 391 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest03, TestSize.Level1) 392 { 393 std::string testStr = "Hello\nWorld\n"; 394 SPUtils::ReplaceString(testStr); 395 EXPECT_EQ(testStr, "HelloWorld"); 396 } 397 398 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest04, TestSize.Level1) 399 { 400 std::string testStr = "\n\n\n"; 401 SPUtils::ReplaceString(testStr); 402 EXPECT_EQ(testStr, ""); 403 } 404 405 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest05, TestSize.Level1) 406 { 407 std::string testStr = "Hello\r\nWorld\r\n"; 408 SPUtils::ReplaceString(testStr); 409 EXPECT_EQ(testStr, "HelloWorld"); 410 } 411 412 HWTEST_F(SPdaemonUtilsTest, ReplaceStringTest06, TestSize.Level1) 413 { 414 std::string testStr = "Hello\r\n\r\nWorld\r\n\r\n"; 415 SPUtils::ReplaceString(testStr); 416 EXPECT_EQ(testStr, "HelloWorld"); 417 } 418 } 419 }