1/* 2 * Copyright (c) 2022 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 "resmgr_fuzzer.h" 17 18#include <string> 19#include <securec.h> 20#include <vector> 21#include "resource_manager.h" 22 23#undef private 24 25using namespace std; 26using namespace OHOS::Global::Resource; 27 28namespace OHOS { 29 30 constexpr int SINGULAR_NUM = 1; 31 constexpr int PLURAL_NUM = 2; 32 33 bool AddResourceFuzzTest(const char* data, size_t size, ResourceManager *rm) 34 { 35 bool result = false; 36 if (size > 0) { 37 std::string testName(data, size); 38 result = rm->AddResource(testName.c_str()); 39 } 40 return result; 41 } 42 43 bool RemoveResourceFuzzTest(const char* data, size_t size, ResourceManager *rm) 44 { 45 bool result = false; 46 std::vector<std::string> overlayPaths; 47 if (size > 0) { 48 std::string testName(data, size); 49 result = rm->RemoveResource(testName.c_str(), overlayPaths); 50 } 51 return result; 52 } 53 54 bool AddAppOverlayFuzzTest(const char* data, size_t size, ResourceManager *rm) 55 { 56 bool result = false; 57 if (size > 0) { 58 std::string testName(data, size); 59 result = rm->AddAppOverlay(testName.c_str()); 60 } 61 return result; 62 } 63 64 bool RemoveAppOverlayFuzzTest(const char* data, size_t size, ResourceManager *rm) 65 { 66 bool result = false; 67 if (size > 0) { 68 std::string testName(data, size); 69 result = rm->RemoveAppOverlay(testName.c_str()); 70 } 71 return result; 72 } 73 74 bool GetStringByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 75 { 76 bool result = false; 77 if (size > 0) { 78 uint32_t testId = static_cast<uint32_t>(atoi(data)); 79 std::string extraInfo = ""; 80 result = rm->GetStringById(testId, extraInfo); 81 } 82 return result; 83 } 84 85 bool GetStringByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 86 { 87 bool result = false; 88 if (size > 0) { 89 std::string testName(data, size); 90 std::string extraInfo = ""; 91 result = rm->GetStringByName(testName.c_str(), extraInfo); 92 } 93 return result; 94 } 95 96 bool GetStringArrayByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 97 { 98 bool result = false; 99 if (size > 0) { 100 uint32_t testId = static_cast<uint32_t>(atoi(data)); 101 std::vector<std::string> outValue; 102 result = rm->GetStringArrayById(testId, outValue); 103 } 104 return result; 105 } 106 107 bool GetStringArrayByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 108 { 109 bool result = false; 110 if (size > 0) { 111 std::string testName(data, size); 112 std::vector<std::string> outValue; 113 result = rm->GetStringArrayByName(testName.c_str(), outValue); 114 } 115 return result; 116 } 117 118 bool GetPatternByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 119 { 120 bool result = false; 121 if (size > 0) { 122 uint32_t testId = static_cast<uint32_t>(atoi(data)); 123 std::map<std::string, std::string> outValue; 124 result = rm->GetPatternById(testId, outValue); 125 } 126 return result; 127 } 128 129 bool GetPatternByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 130 { 131 bool result = false; 132 if (size > 0) { 133 std::string testName(data, size); 134 std::map<std::string, std::string> outValue; 135 result = rm->GetPatternByName(testName.c_str(), outValue); 136 } 137 return result; 138 } 139 140 bool GetPluralStringByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 141 { 142 bool result = false; 143 if (size > 0) { 144 uint32_t testId = static_cast<uint32_t>(atoi(data)); 145 std::string outValue = ""; 146 result = rm->GetPluralStringById(testId, SINGULAR_NUM, outValue); 147 } 148 return result; 149 } 150 151 bool GetPluralStringByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 152 { 153 bool result = false; 154 if (size > 0) { 155 std::string testName(data, size); 156 std::string outValue = ""; 157 result = rm->GetPluralStringByName(testName.c_str(), PLURAL_NUM, outValue); 158 } 159 return result; 160 } 161 162 bool GetThemeByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 163 { 164 bool result = false; 165 if (size > 0) { 166 uint32_t testId = static_cast<uint32_t>(atoi(data)); 167 std::map<std::string, std::string> outValue; 168 result = rm->GetThemeById(testId, outValue); 169 } 170 return result; 171 } 172 173 bool GetThemeByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 174 { 175 bool result = false; 176 if (size > 0) { 177 std::string testName(data, size); 178 std::map<std::string, std::string> outValue; 179 result = rm->GetThemeByName(testName.c_str(), outValue); 180 } 181 return result; 182 } 183 184 bool GetIntegerByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 185 { 186 bool result = false; 187 if (size > 0) { 188 uint32_t testId = static_cast<uint32_t>(atoi(data)); 189 int outValue; 190 result = rm->GetIntegerById(testId, outValue); 191 } 192 return result; 193 } 194 195 bool GetIntegerByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 196 { 197 bool result = false; 198 if (size > 0) { 199 std::string testName(data, size); 200 int outValue; 201 result = rm->GetIntegerByName(testName.c_str(), outValue); 202 } 203 return result; 204 } 205 206 bool GetBooleanByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 207 { 208 bool result = false; 209 if (size > 0) { 210 uint32_t testId = static_cast<uint32_t>(atoi(data)); 211 bool outValue; 212 result = rm->GetBooleanById(testId, outValue); 213 } 214 return result; 215 } 216 217 bool GetBooleanByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 218 { 219 bool result = false; 220 if (size > 0) { 221 std::string testName(data, size); 222 bool outValue = false; 223 result = rm->GetBooleanByName(testName.c_str(), outValue); 224 } 225 return result; 226 } 227 228 bool GetFloatByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 229 { 230 bool result = false; 231 if (size > 0) { 232 uint32_t testId = static_cast<uint32_t>(atoi(data)); 233 float outValue; 234 result = rm->GetFloatById(testId, outValue); 235 } 236 return result; 237 } 238 239 bool GetFloatByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 240 { 241 bool result = false; 242 if (size > 0) { 243 std::string testName(data, size); 244 float outValue = 0.0f; 245 result = rm->GetFloatByName(testName.c_str(), outValue); 246 } 247 return result; 248 } 249 250 bool GetIntArrayByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 251 { 252 bool result = false; 253 if (size > 0) { 254 uint32_t testId = static_cast<uint32_t>(atoi(data)); 255 std::vector<int> outValue; 256 result = rm->GetIntArrayById(testId, outValue); 257 } 258 return result; 259 } 260 261 bool GetIntArrayByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 262 { 263 bool result = false; 264 if (size > 0) { 265 std::string testName(data, size); 266 std::vector<int> outValue; 267 result = rm->GetIntArrayByName(testName.c_str(), outValue); 268 } 269 return result; 270 } 271 272 bool GetMediaByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 273 { 274 bool result = false; 275 if (size > 0) { 276 uint32_t testId = static_cast<uint32_t>(atoi(data)); 277 std::string outValue = ""; 278 result = rm->GetMediaById(testId, outValue); 279 } 280 return result; 281 } 282 283 bool GetMediaByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 284 { 285 bool result = false; 286 if (size > 0) { 287 std::string testName(data, size); 288 std::string outValue = ""; 289 result = rm->GetMediaByName(testName.c_str(), outValue); 290 } 291 return result; 292 } 293 294 bool GetRawFilePathByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 295 { 296 bool result = false; 297 if (size > 0) { 298 std::string testName(data, size); 299 std::string outValue = ""; 300 result = rm->GetRawFilePathByName(testName.c_str(), outValue); 301 } 302 return result; 303 } 304 305 bool GetRawFileDescriptorFuzzTest(const char* data, size_t size, ResourceManager *rm) 306 { 307 bool result = false; 308 if (size > 0) { 309 std::string testName(data, size); 310 ResourceManager::RawFileDescriptor outValue; 311 result = rm->GetRawFileDescriptor(testName.c_str(), outValue); 312 } 313 return result; 314 } 315 316 bool GetRawFdNdkFromHapFuzzTest(const char* data, size_t size, ResourceManager *rm) 317 { 318 bool result = false; 319 if (size > 0) { 320 std::string testName(data, size); 321 ResourceManager::RawFileDescriptor outValue; 322 result = rm->GetRawFdNdkFromHap(testName.c_str(), outValue); 323 } 324 return result; 325 } 326 327 bool CloseRawFileDescriptorFuzzTest(const char* data, size_t size, ResourceManager *rm) 328 { 329 bool result = false; 330 if (size > 0) { 331 std::string testName(data, size); 332 result = rm->CloseRawFileDescriptor(testName.c_str()); 333 } 334 return result; 335 } 336 337 bool GetMediaDataByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 338 { 339 bool result = false; 340 if (size > 0) { 341 uint32_t testId = static_cast<uint32_t>(atoi(data)); 342 size_t len = 0; 343 std::unique_ptr<uint8_t[]> outValue; 344 result = rm->GetMediaDataById(testId, len, outValue); 345 } 346 return result; 347 } 348 349 bool GetMediaDataByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 350 { 351 bool result = false; 352 if (size > 0) { 353 std::string testName(data, size); 354 size_t len = 0; 355 std::unique_ptr<uint8_t[]> outValue; 356 result = rm->GetMediaDataByName(testName.c_str(), len, outValue); 357 } 358 return result; 359 } 360 361 bool GetMediaBase64DataByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 362 { 363 bool result = false; 364 if (size > 0) { 365 uint32_t testId = static_cast<uint32_t>(atoi(data)); 366 std::string outValue; 367 result = rm->GetMediaBase64DataById(testId, outValue); 368 } 369 return result; 370 } 371 372 bool GetMediaBase64DataByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 373 { 374 bool result = false; 375 if (size > 0) { 376 std::string testName(data, size); 377 std::string outValue; 378 result = rm->GetMediaBase64DataByName(testName.c_str(), outValue); 379 } 380 return result; 381 } 382 383 bool GetProfileDataByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 384 { 385 bool result = false; 386 if (size > 0) { 387 uint32_t testId = static_cast<uint32_t>(atoi(data)); 388 size_t len = 0; 389 std::unique_ptr<uint8_t[]> outValue; 390 result = rm->GetProfileDataById(testId, len, outValue); 391 } 392 return result; 393 } 394 395 bool GetProfileDataByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 396 { 397 bool result = false; 398 if (size > 0) { 399 std::string testName(data, size); 400 size_t len = 0; 401 std::unique_ptr<uint8_t[]> outValue; 402 result = rm->GetProfileDataByName(testName.c_str(), len, outValue); 403 } 404 return result; 405 } 406 407 bool GetRawFileFromHapFuzzTest(const char* data, size_t size, ResourceManager *rm) 408 { 409 bool result = false; 410 if (size > 0) { 411 std::string testName(data, size); 412 size_t len = 0; 413 std::unique_ptr<uint8_t[]> outValue; 414 result = rm->GetRawFileFromHap(testName.c_str(), len, outValue); 415 } 416 return result; 417 } 418 419 bool GetRawFileDescriptorFromHapFuzzTest(const char* data, size_t size, ResourceManager *rm) 420 { 421 bool result = false; 422 if (size > 0) { 423 std::string testName(data, size); 424 ResourceManager::RawFileDescriptor outValue; 425 result = rm->GetRawFileDescriptorFromHap(testName.c_str(), outValue); 426 } 427 return result; 428 } 429 430 bool IsLoadHapFuzzTest(const char* data, size_t size, ResourceManager *rm) 431 { 432 bool result = false; 433 if (size > 0) { 434 std::string testName(data, size); 435 result = rm->IsLoadHap(testName); 436 } 437 return result; 438 } 439 440 bool GetRawFileListFuzzTest(const char* data, size_t size, ResourceManager *rm) 441 { 442 bool result = false; 443 if (size > 0) { 444 std::string testName(data, size); 445 std::vector<std::string> outValue; 446 result = rm->GetRawFileList(testName.c_str(), outValue); 447 } 448 return result; 449 } 450 451 bool GetResIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 452 { 453 bool result = false; 454 if (size > 0) { 455 uint32_t testId = static_cast<uint32_t>(atoi(data)); 456 std::string testName(data, size); 457 result = rm->GetResId(testName.c_str(), testId); 458 } 459 return result; 460 } 461 462 bool GetThemeIconsFuzzTest(const char* data, size_t size, ResourceManager *rm) 463 { 464 bool result = false; 465 if (size > 0) { 466 uint32_t testId = static_cast<uint32_t>(atoi(data)); 467 std::pair<std::unique_ptr<uint8_t[]>, size_t> foregroundInfo; 468 std::pair<std::unique_ptr<uint8_t[]>, size_t> backgroundInfo; 469 result = rm->GetThemeIcons(testId, foregroundInfo, backgroundInfo); 470 } 471 return result; 472 } 473 474 bool GetStringFormatByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 475 { 476 bool result = false; 477 if (size > 0) { 478 uint32_t testId = static_cast<uint32_t>(atoi(data)); 479 std::string outValue = ""; 480 result = rm->GetStringFormatById(outValue, testId); 481 } 482 return result; 483 } 484 485 bool GetColorByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 486 { 487 bool result = false; 488 if (size > 0) { 489 uint32_t testId = static_cast<uint32_t>(atoi(data)); 490 uint32_t outValue = 0; 491 result = rm->GetColorById(testId, outValue); 492 } 493 return result; 494 } 495 496 bool GetColorByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 497 { 498 bool result = false; 499 if (size > 0) { 500 std::string testName(data, size); 501 uint32_t outValue = 0; 502 result = rm->GetColorByName(testName.c_str(), outValue); 503 } 504 return result; 505 } 506 507 bool GetSymbolByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 508 { 509 bool result = false; 510 if (size > 0) { 511 uint32_t testId = static_cast<uint32_t>(atoi(data)); 512 uint32_t outValue = 0; 513 result = rm->GetSymbolById(testId, outValue); 514 } 515 return result; 516 } 517 518 bool GetSymbolByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 519 { 520 bool result = false; 521 if (size > 0) { 522 std::string testName(data, size); 523 uint32_t outValue = 0; 524 result = rm->GetSymbolByName(testName.c_str(), outValue); 525 } 526 return result; 527 } 528 529 bool GetProfileByIdFuzzTest(const char* data, size_t size, ResourceManager *rm) 530 { 531 bool result = false; 532 if (size > 0) { 533 uint32_t testId = static_cast<uint32_t>(atoi(data)); 534 std::string outValue; 535 result = rm->GetProfileById(testId, outValue); 536 } 537 return result; 538 } 539 540 bool GetProfileByNameFuzzTest(const char* data, size_t size, ResourceManager *rm) 541 { 542 bool result = false; 543 if (size > 0) { 544 std::string testName(data, size); 545 std::string outValue; 546 result = rm->GetProfileByName(testName.c_str(), outValue); 547 } 548 return result; 549 } 550 551 void ResourceManagerImplAdd(const char* data, size_t size, ResourceManager *rm) 552 { 553 AddResourceFuzzTest(data, size, rm); 554 RemoveResourceFuzzTest(data, size, rm); 555 AddAppOverlayFuzzTest(data, size, rm); 556 RemoveAppOverlayFuzzTest(data, size, rm); 557 GetStringByNameFuzzTest(data, size, rm); 558 GetStringArrayByNameFuzzTest(data, size, rm); 559 GetPatternByIdFuzzTest(data, size, rm); 560 GetPatternByNameFuzzTest(data, size, rm); 561 GetPluralStringByIdFuzzTest(data, size, rm); 562 GetPluralStringByNameFuzzTest(data, size, rm); 563 GetThemeByIdFuzzTest(data, size, rm); 564 GetThemeByNameFuzzTest(data, size, rm); 565 GetBooleanByNameFuzzTest(data, size, rm); 566 GetFloatByNameFuzzTest(data, size, rm); 567 GetIntArrayByIdFuzzTest(data, size, rm); 568 GetIntArrayByNameFuzzTest(data, size, rm); 569 GetRawFilePathByNameFuzzTest(data, size, rm); 570 GetRawFileDescriptorFuzzTest(data, size, rm); 571 GetRawFdNdkFromHapFuzzTest(data, size, rm); 572 CloseRawFileDescriptorFuzzTest(data, size, rm); 573 GetMediaDataByIdFuzzTest(data, size, rm); 574 GetMediaDataByNameFuzzTest(data, size, rm); 575 GetMediaBase64DataByIdFuzzTest(data, size, rm); 576 GetMediaBase64DataByNameFuzzTest(data, size, rm); 577 } 578 579 void ResourceManagerImplFuzzTest(const char* data, size_t size) 580 { 581 ResourceManager *rm = CreateResourceManager(); 582 if (rm == nullptr) { 583 return; 584 } 585 ResourceManagerImplAdd(data, size, rm); 586 GetProfileDataByIdFuzzTest(data, size, rm); 587 GetProfileDataByNameFuzzTest(data, size, rm); 588 GetRawFileFromHapFuzzTest(data, size, rm); 589 GetRawFileDescriptorFromHapFuzzTest(data, size, rm); 590 IsLoadHapFuzzTest(data, size, rm); 591 GetRawFileListFuzzTest(data, size, rm); 592 GetResIdFuzzTest(data, size, rm); 593 GetThemeIconsFuzzTest(data, size, rm); 594 GetColorByNameFuzzTest(data, size, rm); 595 GetSymbolByIdFuzzTest(data, size, rm); 596 GetSymbolByNameFuzzTest(data, size, rm); 597 GetProfileByIdFuzzTest(data, size, rm); 598 GetProfileByNameFuzzTest(data, size, rm); 599 GetStringByIdFuzzTest(data, size, rm); 600 GetStringArrayByIdFuzzTest(data, size, rm); 601 GetIntegerByIdFuzzTest(data, size, rm); 602 GetIntegerByNameFuzzTest(data, size, rm); 603 GetBooleanByIdFuzzTest(data, size, rm); 604 GetFloatByIdFuzzTest(data, size, rm); 605 GetMediaByIdFuzzTest(data, size, rm); 606 GetMediaByNameFuzzTest(data, size, rm); 607 GetStringFormatByIdFuzzTest(data, size, rm); 608 GetColorByIdFuzzTest(data, size, rm); 609 delete rm; 610 return; 611 } 612} 613 614/* Fuzzer entry point */ 615extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 616{ 617 /* Run your code on data */ 618 if (data == nullptr) { 619 return 0; 620 } 621 622 char* ch = (char *)malloc(size + 1); 623 if (ch == nullptr) { 624 return 0; 625 } 626 627 (void)memset_s(ch, size, 0x00, size); 628 if (memcpy_s(ch, size, data, size) != 0) { 629 free(ch); 630 ch = nullptr; 631 return 0; 632 } 633 OHOS::ResourceManagerImplFuzzTest(ch, size); 634 free(ch); 635 ch = nullptr; 636 return 0; 637} 638 639