1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 "plugin_service.h" 17 18 #include <hwext/gtest-ext.h> 19 #include <hwext/gtest-tag.h> 20 21 #include "common_types.pb.h" 22 #include "plugin_command_builder.h" 23 #include "plugin_service.ipc.h" 24 #include "plugin_service_impl.h" 25 #include "plugin_session_manager.h" 26 #include "profiler_data_repeater.h" 27 #include "profiler_service_types.pb.h" 28 #include "socket_context.h" 29 30 using namespace testing::ext; 31 32 namespace { 33 constexpr uint32_t BUFFER_SIZE = 4096; 34 constexpr size_t QUEUE_MAX_SIZE = 4096; 35 constexpr uint32_t SAMPLE_INTERVAL = 20; 36 37 class PluginClientTest final : public IPluginServiceClient { 38 public: OnGetCommandResponse(SocketContext & context,::GetCommandResponse & response)39 bool OnGetCommandResponse(SocketContext& context, ::GetCommandResponse& response) override 40 { 41 return true; 42 } 43 }; 44 45 class UnitTestPluginService : public testing::Test { 46 public: SetUpTestCase()47 static void SetUpTestCase() {} TearDownTestCase()48 static void TearDownTestCase() {} 49 SetUp()50 void SetUp() 51 { 52 pluginService_ = std::make_shared<PluginService>(); 53 usleep(100000); // sleep for 100000 us. 54 pluginClient_ = std::make_unique<PluginClientTest>(); 55 pluginServiceImp_ = std::make_unique<PluginServiceImpl>(*pluginService_); 56 commandBuilder_ = std::make_unique<PluginCommandBuilder>(); 57 pluginSessionMgr_ = std::make_shared<PluginSessionManager>(pluginService_); 58 pluginClient_->Connect(DEFAULT_UNIX_SOCKET_FULL_PATH); 59 pluginService_->SetPluginSessionManager(pluginSessionMgr_); 60 } 61 TearDown()62 void TearDown() {} 63 64 public: 65 std::shared_ptr<PluginService> pluginService_ = nullptr; 66 std::unique_ptr<PluginClientTest> pluginClient_ = nullptr; 67 std::unique_ptr<PluginServiceImpl> pluginServiceImp_ = nullptr; 68 std::unique_ptr<PluginCommandBuilder> commandBuilder_ = nullptr; 69 std::shared_ptr<PluginSessionManager> pluginSessionMgr_ = nullptr; 70 uint32_t pluginId_; 71 }; 72 73 /** 74 * @tc.name: Service 75 * @tc.desc: Set plugin registration information. 76 * @tc.type: FUNC 77 */ 78 HWTEST_F(UnitTestPluginService, AddPluginInfo, TestSize.Level1) 79 { 80 RegisterPluginRequest request; 81 RegisterPluginResponse response; 82 PluginInfo plugin; 83 84 request.set_request_id(1); 85 request.set_path("abc.so"); 86 request.set_sha256("asdfasdfasdfasfd"); 87 request.set_name("abc.so"); 88 ASSERT_TRUE(response.status() != ResponseStatus::OK); 89 pluginId_ = response.plugin_id(); 90 91 plugin.name = "abc.so"; 92 plugin.bufferSizeHint = 0; 93 plugin.sha256 = ""; 94 ASSERT_TRUE(pluginService_->AddPluginInfo(plugin)); 95 } 96 97 /** 98 * @tc.name: Service 99 * @tc.desc: Set plugin configuration information. 100 * @tc.type: FUNC 101 */ 102 HWTEST_F(UnitTestPluginService, CreatePluginSession1, TestSize.Level1) 103 { 104 ProfilerPluginConfig ppc; 105 ppc.set_name("abc1.so"); 106 ppc.set_plugin_sha256("ASDFAADSF"); 107 ppc.set_sample_interval(SAMPLE_INTERVAL); 108 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 109 PluginInfo plugin; 110 plugin.name = "abc1.so"; 111 plugin.bufferSizeHint = 0; 112 plugin.sha256 = "ASDFAADSF"; 113 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 114 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 115 } 116 117 /** 118 * @tc.name: Service 119 * @tc.desc: Set session configuration. 120 * @tc.type: FUNC 121 */ 122 HWTEST_F(UnitTestPluginService, CreatePluginSession2, TestSize.Level1) 123 { 124 ProfilerPluginConfig ppc; 125 ppc.set_name("abc2.so"); 126 ppc.set_plugin_sha256("ASDFAADSF"); 127 ppc.set_sample_interval(SAMPLE_INTERVAL); 128 ProfilerSessionConfig::BufferConfig bc; 129 bc.set_pages(1); 130 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 131 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 132 PluginInfo plugin; 133 plugin.name = "abc2.so"; 134 plugin.bufferSizeHint = 0; 135 plugin.sha256 = "ASDFAADSF"; 136 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 137 pluginService_->AddPluginInfo(plugin); 138 ProfilerSessionConfig psc; 139 psc.set_session_mode(ProfilerSessionConfig::OFFLINE); 140 std::vector<string> names = {"abc2.so"}; 141 pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names); 142 TraceFileWriterPtr traceWriter = std::make_shared<TraceFileWriter>("/data/local/tmp/tracewrite.trace"); 143 pluginService_->SetTraceWriter(traceWriter); 144 EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 145 uint32_t pluginId = pluginService_->GetPluginIdByName("abc2.so"); 146 PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId); 147 pluginService_->ReadShareMemoryOffline(*pluginCtx); 148 EXPECT_TRUE(pluginService_->DestroyPluginSession("abc2.so")); 149 EXPECT_TRUE(pluginService_->RemovePluginSessionCtx("abc2.so")); 150 pluginService_->RemovePluginInfo(plugin); 151 } 152 153 /** 154 * @tc.name: Service 155 * @tc.desc: Start plugin session. 156 * @tc.type: FUNC 157 */ 158 HWTEST_F(UnitTestPluginService, StartPluginSession, TestSize.Level1) 159 { 160 ProfilerPluginConfig ppc; 161 ppc.set_name("abc3.so"); 162 ppc.set_plugin_sha256("ASDFAADSF"); 163 ppc.set_sample_interval(SAMPLE_INTERVAL); 164 ASSERT_FALSE(pluginService_->StartPluginSession(ppc)); 165 PluginInfo plugin; 166 plugin.name = "abc3.so"; 167 plugin.bufferSizeHint = 0; 168 plugin.sha256 = "ASDFAADSF"; 169 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 170 pluginService_->AddPluginInfo(plugin); 171 EXPECT_TRUE(pluginService_->StartPluginSession(ppc)); 172 } 173 174 /** 175 * @tc.name: Service 176 * @tc.desc: Stop plugin session. 177 * @tc.type: FUNC 178 */ 179 HWTEST_F(UnitTestPluginService, StopPluginSession, TestSize.Level1) 180 { 181 ASSERT_FALSE(pluginService_->StopPluginSession("abc.so")); 182 PluginInfo plugin; 183 SocketContext ctx; 184 plugin.name = "abc4.so"; 185 plugin.bufferSizeHint = 0; 186 plugin.sha256 = "ASDFAADSF"; 187 plugin.context = &ctx; 188 pluginService_->AddPluginInfo(plugin); 189 EXPECT_FALSE(pluginService_->StopPluginSession("abc4.so")); 190 } 191 192 /** 193 * @tc.name: Service 194 * @tc.desc: Destroy receiving test. 195 * @tc.type: FUNC 196 */ 197 HWTEST_F(UnitTestPluginService, DestroyPluginSession, TestSize.Level1) 198 { 199 ASSERT_FALSE(pluginService_->DestroyPluginSession("abc.so")); 200 PluginInfo plugin; 201 plugin.name = "abc5.so"; 202 plugin.bufferSizeHint = 0; 203 plugin.sha256 = "ASDFAADSF"; 204 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 205 pluginService_->AddPluginInfo(plugin); 206 EXPECT_TRUE(pluginService_->DestroyPluginSession("abc5.so")); 207 } 208 /** 209 * @tc.name: Service 210 * @tc.desc: RefreshPluginSession test. 211 * @tc.type: FUNC 212 */ 213 HWTEST_F(UnitTestPluginService, RefreshPluginSession, TestSize.Level1) 214 { 215 ASSERT_FALSE(pluginService_->RefreshPluginSession("abc.so")); 216 PluginInfo plugin; 217 SocketContext ctx; 218 plugin.name = "abc6.so"; 219 plugin.bufferSizeHint = 0; 220 plugin.sha256 = "ASDFAADSF"; 221 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 222 pluginService_->AddPluginInfo(plugin); 223 EXPECT_TRUE(pluginService_->RefreshPluginSession("abc6.so")); 224 } 225 /** 226 * @tc.name: Service 227 * @tc.desc: GetPluginInfo test. 228 * @tc.type: FUNC 229 */ 230 HWTEST_F(UnitTestPluginService, GetPluginInfo, TestSize.Level1) 231 { 232 PluginInfo plugin; 233 EXPECT_FALSE(pluginService_->GetPluginInfo("edf.so", plugin)); 234 SocketContext ctx; 235 plugin.name = "abc7.so"; 236 plugin.bufferSizeHint = 0; 237 plugin.sha256 = "ASDFAADSF"; 238 plugin.context = &ctx; 239 pluginService_->AddPluginInfo(plugin); 240 EXPECT_TRUE(pluginService_->GetPluginInfo("abc7.so", plugin)); 241 } 242 243 /** 244 * @tc.name: Service 245 * @tc.desc: RemovePluginSessionCtx test. 246 * @tc.type: FUNC 247 */ 248 HWTEST_F(UnitTestPluginService, RemovePluginSessionCtx, TestSize.Level1) 249 { 250 PluginInfo plugin; 251 SocketContext ctx; 252 plugin.name = "abc8.so"; 253 plugin.bufferSizeHint = 0; 254 plugin.sha256 = "ASDFAADSF"; 255 plugin.context = &ctx; 256 pluginService_->AddPluginInfo(plugin); 257 EXPECT_TRUE(pluginService_->RemovePluginSessionCtx("abc8.so")); 258 } 259 260 /** 261 * @tc.name: Service 262 * @tc.desc: Remove the specified plugin. 263 * @tc.type: FUNC 264 */ 265 HWTEST_F(UnitTestPluginService, RemovePluginInfo, TestSize.Level1) 266 { 267 UnregisterPluginRequest request; 268 PluginInfo pi; 269 pi.id = 0; 270 pi.name = "abc9.so"; 271 pi.path = "abc9.so"; 272 pi.sha256 = "asdfasdf"; 273 ASSERT_FALSE(pluginService_->RemovePluginInfo(pi)); 274 pluginService_->AddPluginInfo(pi); 275 pi.id = pluginService_->GetPluginIdByName("abc9.so"); 276 EXPECT_TRUE(pluginService_->RemovePluginInfo(pi)); 277 } 278 279 /** 280 * @tc.name: Service 281 * @tc.desc: Setting report results. 282 * @tc.type: FUNC 283 */ 284 HWTEST_F(UnitTestPluginService, AppendResult, TestSize.Level1) 285 { 286 NotifyResultRequest nrr; 287 nrr.set_request_id(1); 288 nrr.set_command_id(1); 289 ASSERT_TRUE(pluginService_->AppendResult(nrr)); 290 } 291 292 /** 293 * @tc.name: Service 294 * @tc.desc: Get plugin status. 295 * @tc.type: FUNC 296 */ 297 HWTEST_F(UnitTestPluginService, GetPluginStatus, TestSize.Level1) 298 { 299 auto status = pluginService_->GetPluginStatus(); 300 ASSERT_TRUE(status.size() == 0); 301 } 302 303 /** 304 * @tc.name: Service 305 * @tc.desc: Gets the plugin with the specified name. 306 * @tc.type: FUNC 307 */ 308 HWTEST_F(UnitTestPluginService, GetPluginIdByName, TestSize.Level1) 309 { 310 ASSERT_TRUE(pluginService_->GetPluginIdByName("abc.so") == 0); 311 } 312 313 /** 314 * @tc.name: Service 315 * @tc.desc: Set plugin registration information. 316 * @tc.type: FUNC 317 */ 318 HWTEST_F(UnitTestPluginService, AddPluginInfo2, TestSize.Level1) 319 { 320 RegisterPluginRequest request; 321 RegisterPluginResponse response; 322 PluginInfo plugin; 323 324 request.set_request_id(2); 325 request.set_path("mem.so"); 326 request.set_sha256("asdfasdfasdfasfd"); 327 request.set_name("mem.so"); 328 ASSERT_TRUE(response.status() != ResponseStatus::OK); 329 pluginId_ = response.plugin_id(); 330 331 plugin.name = "mem.so"; 332 plugin.bufferSizeHint = 0; 333 plugin.sha256 = ""; 334 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 335 } 336 337 /** 338 * @tc.name: Service 339 * @tc.desc: Set plugin configuration information. 340 * @tc.type: FUNC 341 */ 342 HWTEST_F(UnitTestPluginService, CreatePluginSession12, TestSize.Level1) 343 { 344 ProfilerPluginConfig ppc; 345 ppc.set_name("mem.so"); 346 ppc.set_plugin_sha256("ASDFAADSF"); 347 ppc.set_sample_interval(SAMPLE_INTERVAL); 348 349 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 350 } 351 352 /** 353 * @tc.name: Service 354 * @tc.desc: Set session configuration. 355 * @tc.type: FUNC 356 */ 357 HWTEST_F(UnitTestPluginService, CreatePluginSession22, TestSize.Level1) 358 { 359 ProfilerPluginConfig ppc; 360 ppc.set_name("mem.so"); 361 ppc.set_plugin_sha256("ASDFAADSF"); 362 ppc.set_sample_interval(SAMPLE_INTERVAL); 363 364 ProfilerSessionConfig::BufferConfig bc; 365 bc.set_pages(1); 366 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 367 368 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 369 } 370 371 /** 372 * @tc.name: Service 373 * @tc.desc: Start plugin session. 374 * @tc.type: FUNC 375 */ 376 HWTEST_F(UnitTestPluginService, StartPluginSession2, TestSize.Level1) 377 { 378 ProfilerPluginConfig ppc; 379 ppc.set_name("mem.so"); 380 ppc.set_plugin_sha256("ASDFAADSF"); 381 ppc.set_sample_interval(SAMPLE_INTERVAL); 382 383 ASSERT_FALSE(pluginService_->StartPluginSession(ppc)); 384 } 385 386 /** 387 * @tc.name: Service 388 * @tc.desc: Stop plugin session. 389 * @tc.type: FUNC 390 */ 391 HWTEST_F(UnitTestPluginService, StopPluginSession2, TestSize.Level1) 392 { 393 ASSERT_FALSE(pluginService_->StopPluginSession("mem.so")); 394 } 395 396 /** 397 * @tc.name: Service 398 * @tc.desc: Destroy receiving test. 399 * @tc.type: FUNC 400 */ 401 HWTEST_F(UnitTestPluginService, DestroyPluginSession2, TestSize.Level1) 402 { 403 ASSERT_FALSE(pluginService_->DestroyPluginSession("mem.so")); 404 } 405 406 /** 407 * @tc.name: Service 408 * @tc.desc: Remove the specified plugin. 409 * @tc.type: FUNC 410 */ 411 HWTEST_F(UnitTestPluginService, RemovePluginInfo2, TestSize.Level1) 412 { 413 UnregisterPluginRequest request; 414 PluginInfo pi; 415 pi.id = 0; 416 pi.name = "mem.so"; 417 pi.path = "mem.so"; 418 pi.sha256 = "asdfasdf"; 419 ASSERT_FALSE(pluginService_->RemovePluginInfo(pi)); 420 } 421 422 /** 423 * @tc.name: Service 424 * @tc.desc: Setting report results. 425 * @tc.type: FUNC 426 */ 427 HWTEST_F(UnitTestPluginService, AppendResult2, TestSize.Level1) 428 { 429 NotifyResultRequest nrr; 430 nrr.set_request_id(2); 431 nrr.set_command_id(2); 432 ASSERT_TRUE(pluginService_->AppendResult(nrr)); 433 } 434 435 /** 436 * @tc.name: Service 437 * @tc.desc: Get plugin status. 438 * @tc.type: FUNC 439 */ 440 HWTEST_F(UnitTestPluginService, GetPluginStatus2, TestSize.Level1) 441 { 442 auto status = pluginService_->GetPluginStatus(); 443 ASSERT_TRUE(status.size() == 0); 444 } 445 446 /** 447 * @tc.name: Service 448 * @tc.desc: Gets the plugin with the specified name. 449 * @tc.type: FUNC 450 */ 451 HWTEST_F(UnitTestPluginService, GetPluginIdByName2, TestSize.Level1) 452 { 453 ASSERT_TRUE(pluginService_->GetPluginIdByName("mem.so") == 0); 454 } 455 456 /** 457 * @tc.name: Service 458 * @tc.desc: Set plugin registration information. 459 * @tc.type: FUNC 460 */ 461 HWTEST_F(UnitTestPluginService, AddPluginInfo3, TestSize.Level1) 462 { 463 RegisterPluginRequest request; 464 RegisterPluginResponse response; 465 PluginInfo plugin; 466 467 request.set_request_id(3); 468 request.set_path("cpu.so"); 469 request.set_sha256("asdfasdfasdfasfd"); 470 request.set_name("cpu.so"); 471 ASSERT_TRUE(response.status() != ResponseStatus::OK); 472 pluginId_ = response.plugin_id(); 473 474 plugin.name = "cpu.so"; 475 plugin.bufferSizeHint = 0; 476 plugin.sha256 = ""; 477 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 478 } 479 480 /** 481 * @tc.name: Service 482 * @tc.desc: Set plugin configuration information. 483 * @tc.type: FUNC 484 */ 485 HWTEST_F(UnitTestPluginService, CreatePluginSession13, TestSize.Level1) 486 { 487 ProfilerPluginConfig ppc; 488 ppc.set_name("cpu.so"); 489 ppc.set_plugin_sha256("ASDFAADSF"); 490 ppc.set_sample_interval(SAMPLE_INTERVAL); 491 492 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 493 } 494 495 /** 496 * @tc.name: Service 497 * @tc.desc: Set session configuration. 498 * @tc.type: FUNC 499 */ 500 HWTEST_F(UnitTestPluginService, CreatePluginSession23, TestSize.Level1) 501 { 502 ProfilerPluginConfig ppc; 503 ppc.set_name("cpu.so"); 504 ppc.set_plugin_sha256("ASDFAADSF"); 505 ppc.set_sample_interval(SAMPLE_INTERVAL); 506 507 ProfilerSessionConfig::BufferConfig bc; 508 bc.set_pages(1); 509 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 510 511 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 512 } 513 514 /** 515 * @tc.name: Service 516 * @tc.desc: Start plugin session. 517 * @tc.type: FUNC 518 */ 519 HWTEST_F(UnitTestPluginService, StartPluginSession3, TestSize.Level1) 520 { 521 ProfilerPluginConfig ppc; 522 ppc.set_name("cpu.so"); 523 ppc.set_plugin_sha256("ASDFAADSF"); 524 ppc.set_sample_interval(SAMPLE_INTERVAL); 525 526 ASSERT_FALSE(pluginService_->StartPluginSession(ppc)); 527 } 528 529 /** 530 * @tc.name: Service 531 * @tc.desc: Stop plugin session. 532 * @tc.type: FUNC 533 */ 534 HWTEST_F(UnitTestPluginService, StopPluginSession3, TestSize.Level1) 535 { 536 ASSERT_FALSE(pluginService_->StopPluginSession("cpu.so")); 537 } 538 539 /** 540 * @tc.name: Service 541 * @tc.desc: Destroy receiving test. 542 * @tc.type: FUNC 543 */ 544 HWTEST_F(UnitTestPluginService, DestroyPluginSession3, TestSize.Level1) 545 { 546 ASSERT_FALSE(pluginService_->DestroyPluginSession("cpu.so")); 547 } 548 549 /** 550 * @tc.name: Service 551 * @tc.desc: Remove the specified plugin. 552 * @tc.type: FUNC 553 */ 554 HWTEST_F(UnitTestPluginService, RemovePluginInfo3, TestSize.Level1) 555 { 556 UnregisterPluginRequest request; 557 PluginInfo pi; 558 pi.id = 0; 559 pi.name = "cpu.so"; 560 pi.path = "cpu.so"; 561 pi.sha256 = "asdfasdf"; 562 ASSERT_FALSE(pluginService_->RemovePluginInfo(pi)); 563 } 564 565 /** 566 * @tc.name: Service 567 * @tc.desc: Setting report results. 568 * @tc.type: FUNC 569 */ 570 HWTEST_F(UnitTestPluginService, AppendResult3, TestSize.Level1) 571 { 572 NotifyResultRequest nrr; 573 nrr.set_request_id(3); 574 nrr.set_command_id(3); 575 ASSERT_TRUE(pluginService_->AppendResult(nrr)); 576 } 577 578 /** 579 * @tc.name: Service 580 * @tc.desc: Get plugin status. 581 * @tc.type: FUNC 582 */ 583 HWTEST_F(UnitTestPluginService, GetPluginStatus3, TestSize.Level1) 584 { 585 auto status = pluginService_->GetPluginStatus(); 586 ASSERT_TRUE(status.size() == 0); 587 } 588 589 /** 590 * @tc.name: Service 591 * @tc.desc: Gets the plugin with the specified name. 592 * @tc.type: FUNC 593 */ 594 HWTEST_F(UnitTestPluginService, GetPluginIdByName3, TestSize.Level1) 595 { 596 ASSERT_TRUE(pluginService_->GetPluginIdByName("cpu.so") == 0); 597 } 598 599 /** 600 * @tc.name: Service 601 * @tc.desc: Set plugin registration information. 602 * @tc.type: FUNC 603 */ 604 HWTEST_F(UnitTestPluginService, AddPluginInfo4, TestSize.Level1) 605 { 606 RegisterPluginRequest request; 607 RegisterPluginResponse response; 608 PluginInfo plugin; 609 610 request.set_request_id(4); 611 request.set_path("stream.so"); 612 request.set_sha256("asdfasdfasdfasfd"); 613 request.set_name("stream.so"); 614 ASSERT_TRUE(response.status() != ResponseStatus::OK); 615 pluginId_ = response.plugin_id(); 616 617 plugin.name = "stream.so"; 618 plugin.bufferSizeHint = 0; 619 plugin.sha256 = ""; 620 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 621 plugin.bufferSizeHint = 10; 622 plugin.isStandaloneFileData = true; 623 plugin.outFileName = "test-file.bin"; 624 plugin.pluginVersion = "1.02"; 625 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 626 } 627 628 /** 629 * @tc.name: Service 630 * @tc.desc: Set plugin configuration information. 631 * @tc.type: FUNC 632 */ 633 HWTEST_F(UnitTestPluginService, CreatePluginSession14, TestSize.Level1) 634 { 635 ProfilerPluginConfig ppc; 636 ppc.set_name("stream.so"); 637 ppc.set_plugin_sha256("ASDFAADSF"); 638 ppc.set_sample_interval(SAMPLE_INTERVAL); 639 640 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 641 } 642 643 /** 644 * @tc.name: Service 645 * @tc.desc: Set session configuration. 646 * @tc.type: FUNC 647 */ 648 HWTEST_F(UnitTestPluginService, CreatePluginSession24, TestSize.Level1) 649 { 650 ProfilerPluginConfig ppc; 651 ppc.set_name("stream.so"); 652 ppc.set_plugin_sha256("ASDFAADSF"); 653 ppc.set_sample_interval(SAMPLE_INTERVAL); 654 655 ProfilerSessionConfig::BufferConfig bc; 656 bc.set_pages(1); 657 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 658 659 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 660 } 661 662 /** 663 * @tc.name: Service 664 * @tc.desc: Start plugin session. 665 * @tc.type: FUNC 666 */ 667 HWTEST_F(UnitTestPluginService, StartPluginSession4, TestSize.Level1) 668 { 669 ProfilerPluginConfig ppc; 670 ppc.set_name("stream.so"); 671 ppc.set_plugin_sha256("ASDFAADSF"); 672 ppc.set_sample_interval(SAMPLE_INTERVAL); 673 674 ASSERT_FALSE(pluginService_->StartPluginSession(ppc)); 675 } 676 677 /** 678 * @tc.name: Service 679 * @tc.desc: Stop plugin session. 680 * @tc.type: FUNC 681 */ 682 HWTEST_F(UnitTestPluginService, StopPluginSession4, TestSize.Level1) 683 { 684 ASSERT_FALSE(pluginService_->StopPluginSession("stream.so")); 685 } 686 687 /** 688 * @tc.name: Service 689 * @tc.desc: Destroy receiving test. 690 * @tc.type: FUNC 691 */ 692 HWTEST_F(UnitTestPluginService, DestroyPluginSession4, TestSize.Level1) 693 { 694 ASSERT_FALSE(pluginService_->DestroyPluginSession("stream.so")); 695 } 696 697 /** 698 * @tc.name: Service 699 * @tc.desc: Remove the specified plugin. 700 * @tc.type: FUNC 701 */ 702 HWTEST_F(UnitTestPluginService, RemovePluginInfo4, TestSize.Level1) 703 { 704 UnregisterPluginRequest request; 705 PluginInfo pi; 706 pi.id = 0; 707 pi.name = "stream.so"; 708 pi.path = "stream.so"; 709 pi.sha256 = "asdfasdf"; 710 ASSERT_FALSE(pluginService_->RemovePluginInfo(pi)); 711 } 712 713 /** 714 * @tc.name: Service 715 * @tc.desc: Setting report results. 716 * @tc.type: FUNC 717 */ 718 HWTEST_F(UnitTestPluginService, AppendResult4, TestSize.Level1) 719 { 720 NotifyResultRequest nrr; 721 nrr.set_request_id(4); 722 nrr.set_command_id(4); 723 ASSERT_TRUE(pluginService_->AppendResult(nrr)); 724 } 725 726 /** 727 * @tc.name: Service 728 * @tc.desc: Get plugin status. 729 * @tc.type: FUNC 730 */ 731 HWTEST_F(UnitTestPluginService, GetPluginStatus4, TestSize.Level1) 732 { 733 auto status = pluginService_->GetPluginStatus(); 734 ASSERT_TRUE(status.size() == 0); 735 } 736 737 /** 738 * @tc.name: Service 739 * @tc.desc: Gets the plugin with the specified name. 740 * @tc.type: FUNC 741 */ 742 HWTEST_F(UnitTestPluginService, GetPluginIdByName4, TestSize.Level1) 743 { 744 ASSERT_TRUE(pluginService_->GetPluginIdByName("stream.so") == 0); 745 } 746 747 /** 748 * @tc.name: Service 749 * @tc.desc: Set plugin registration information. 750 * @tc.type: FUNC 751 */ 752 HWTEST_F(UnitTestPluginService, AddPluginInfo5, TestSize.Level1) 753 { 754 RegisterPluginRequest request; 755 RegisterPluginResponse response; 756 PluginInfo plugin; 757 758 request.set_request_id(5); 759 request.set_path("sample.so"); 760 request.set_sha256("asdfasdfasdfasfd"); 761 request.set_name("sample.so"); 762 ASSERT_TRUE(response.status() != ResponseStatus::OK); 763 pluginId_ = response.plugin_id(); 764 765 plugin.name = "sample.so"; 766 plugin.bufferSizeHint = 0; 767 plugin.sha256 = ""; 768 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 769 } 770 771 /** 772 * @tc.name: Service 773 * @tc.desc: Set plugin configuration information. 774 * @tc.type: FUNC 775 */ 776 HWTEST_F(UnitTestPluginService, CreatePluginSession15, TestSize.Level1) 777 { 778 ProfilerPluginConfig ppc; 779 ppc.set_name("sample.so"); 780 ppc.set_plugin_sha256("ASDFAADSF"); 781 ppc.set_sample_interval(SAMPLE_INTERVAL); 782 783 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 784 } 785 786 /** 787 * @tc.name: Service 788 * @tc.desc: Set session configuration. 789 * @tc.type: FUNC 790 */ 791 HWTEST_F(UnitTestPluginService, CreatePluginSession25, TestSize.Level1) 792 { 793 ProfilerPluginConfig ppc; 794 ppc.set_name("sample.so"); 795 ppc.set_plugin_sha256("ASDFAADSF"); 796 ppc.set_sample_interval(SAMPLE_INTERVAL); 797 798 ProfilerSessionConfig::BufferConfig bc; 799 bc.set_pages(1); 800 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 801 802 ASSERT_FALSE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 803 } 804 805 /** 806 * @tc.name: Service 807 * @tc.desc: Start plugin session. 808 * @tc.type: FUNC 809 */ 810 HWTEST_F(UnitTestPluginService, StartPluginSession5, TestSize.Level1) 811 { 812 ProfilerPluginConfig ppc; 813 ppc.set_name("sample.so"); 814 ppc.set_plugin_sha256("ASDFAADSF"); 815 ppc.set_sample_interval(SAMPLE_INTERVAL); 816 817 ASSERT_FALSE(pluginService_->StartPluginSession(ppc)); 818 } 819 820 /** 821 * @tc.name: Service 822 * @tc.desc: Stop plugin session. 823 * @tc.type: FUNC 824 */ 825 HWTEST_F(UnitTestPluginService, StopPluginSession5, TestSize.Level1) 826 { 827 ASSERT_FALSE(pluginService_->StopPluginSession("sample.so")); 828 } 829 830 /** 831 * @tc.name: Service 832 * @tc.desc: Destroy receiving test. 833 * @tc.type: FUNC 834 */ 835 HWTEST_F(UnitTestPluginService, DestroyPluginSession5, TestSize.Level1) 836 { 837 ASSERT_FALSE(pluginService_->DestroyPluginSession("sample.so")); 838 } 839 840 /** 841 * @tc.name: Service 842 * @tc.desc: Remove the specified plugin. 843 * @tc.type: FUNC 844 */ 845 HWTEST_F(UnitTestPluginService, RemovePluginInfo5, TestSize.Level1) 846 { 847 UnregisterPluginRequest request; 848 PluginInfo pi; 849 pi.id = 0; 850 pi.name = "sample.so"; 851 pi.path = "sample.so"; 852 pi.sha256 = "asdfasdf"; 853 ASSERT_FALSE(pluginService_->RemovePluginInfo(pi)); 854 } 855 856 /** 857 * @tc.name: Service 858 * @tc.desc: Setting report results. 859 * @tc.type: FUNC 860 */ 861 HWTEST_F(UnitTestPluginService, AppendResult5, TestSize.Level1) 862 { 863 NotifyResultRequest nrr; 864 nrr.set_request_id(5); 865 nrr.set_command_id(5); 866 ASSERT_TRUE(pluginService_->AppendResult(nrr)); 867 } 868 869 /** 870 * @tc.name: Service 871 * @tc.desc: Get plugin status. 872 * @tc.type: FUNC 873 */ 874 HWTEST_F(UnitTestPluginService, GetPluginStatus5, TestSize.Level1) 875 { 876 auto status = pluginService_->GetPluginStatus(); 877 ASSERT_TRUE(status.size() == 0); 878 } 879 880 /** 881 * @tc.name: Service 882 * @tc.desc: Gets the plugin with the specified name. 883 * @tc.type: FUNC 884 */ 885 HWTEST_F(UnitTestPluginService, GetPluginIdByName5, TestSize.Level1) 886 { 887 ASSERT_TRUE(pluginService_->GetPluginIdByName("sample.so") == 0); 888 } 889 890 /** 891 * @tc.name: Service 892 * @tc.desc: test function RegisterPlugin. 893 * @tc.type: FUNC 894 */ 895 HWTEST_F(UnitTestPluginService, PluginServiceImpl_RegisterPlugin, TestSize.Level1) 896 { 897 RegisterPluginRequest request; 898 RegisterPluginResponse response; 899 SocketContext ctx; 900 901 request.set_request_id(5); 902 request.set_path("sample2.so"); 903 request.set_sha256(""); 904 request.set_name("sample2.so"); 905 request.set_buffer_size_hint(0); 906 EXPECT_TRUE(pluginServiceImp_->RegisterPlugin(ctx, request, response)); 907 request.set_sha256("abcdsfdsfad"); 908 EXPECT_FALSE(pluginServiceImp_->RegisterPlugin(ctx, request, response)); 909 UnregisterPluginRequest unRequest; 910 UnregisterPluginResponse unResponse; 911 uint32_t plugId = pluginService_->GetPluginIdByName("sample2.so"); 912 unRequest.set_plugin_id(plugId); 913 EXPECT_TRUE(pluginServiceImp_->UnregisterPlugin(ctx, unRequest, unResponse)); 914 } 915 916 /** 917 * @tc.name: Service 918 * @tc.desc: test function UnregisterPlugin 919 * @tc.type: FUNC 920 */ 921 HWTEST_F(UnitTestPluginService, PluginServiceImpl_UnregisterPlugin, TestSize.Level1) 922 { 923 UnregisterPluginRequest request; 924 UnregisterPluginResponse response; 925 SocketContext ctx; 926 request.set_plugin_id(1); 927 EXPECT_FALSE(pluginServiceImp_->UnregisterPlugin(ctx, request, response)); 928 929 GetCommandRequest gcRequest; 930 GetCommandResponse gcResponse; 931 EXPECT_FALSE(pluginServiceImp_->GetCommand(ctx, gcRequest, gcResponse)); 932 933 NotifyResultRequest nResRequest; 934 NotifyResultResponse nResResponse; 935 EXPECT_TRUE(pluginServiceImp_->NotifyResult(ctx, nResRequest, nResResponse)); 936 } 937 /** 938 * @tc.name: BuildCreateSessionCmd 939 * @tc.desc: build create session command 940 * @tc.type: FUNC 941 */ 942 HWTEST_F(UnitTestPluginService, pluginCommandBuilder, TestSize.Level1) 943 { 944 ProfilerPluginConfig ppc; 945 ppc.set_name("abc.so"); 946 ppc.set_plugin_sha256("ASDFAADSF"); 947 ppc.set_sample_interval(SAMPLE_INTERVAL); 948 const uint32_t pluginId = 1; 949 EXPECT_TRUE(commandBuilder_->BuildCreateSessionCmd(ppc, BUFFER_SIZE) != nullptr); 950 EXPECT_TRUE(commandBuilder_->BuildStartSessionCmd(ppc, pluginId) != nullptr); 951 EXPECT_TRUE(commandBuilder_->BuildRefreshSessionCmd(pluginId) != nullptr); 952 EXPECT_TRUE(commandBuilder_->BuildStopSessionCmd(pluginId) != nullptr); 953 EXPECT_TRUE(commandBuilder_->BuildDestroySessionCmd(pluginId) != nullptr); 954 EXPECT_FALSE(commandBuilder_->GetedCommandResponse(0)); 955 EXPECT_TRUE(commandBuilder_->GetedCommandResponse(1)); 956 } 957 958 /** 959 * @tc.name: PluginSessionManager 960 * @tc.desc: test plugin session manager checkBufferConfig 961 * @tc.type: FUNC 962 */ 963 HWTEST_F(UnitTestPluginService, PluginSessionManager_Check, TestSize.Level1) 964 { 965 PluginInfo plugin; 966 SocketContext ctx; 967 plugin.name = "abc_check.so"; 968 plugin.bufferSizeHint = 0; 969 plugin.sha256 = "asdfasdfasdfasfd"; 970 plugin.context = &ctx; 971 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 972 ProfilerSessionConfig::BufferConfig bc; 973 bc.set_pages(1); 974 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 975 EXPECT_TRUE(pluginSessionMgr_->CheckBufferConfig(bc)); 976 ProfilerPluginConfig ppc; 977 ppc.set_name("abc_check.so"); 978 ppc.set_plugin_sha256("asdfasdfasdfasfd"); 979 EXPECT_TRUE(pluginSessionMgr_->CheckPluginSha256(ppc)); 980 } 981 /** 982 * @tc.name: PluginSessionManager 983 * @tc.desc: test plugin session manager by creating sesssion 984 * @tc.type: FUNC 985 */ 986 HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Level1) 987 { 988 PluginInfo plugin; 989 plugin.name = "testsample.so"; 990 plugin.bufferSizeHint = 0; 991 plugin.sha256 = "ASDFAADSF"; 992 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 993 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 994 ProfilerPluginConfig ppc; 995 ppc.set_name("testsample.so"); 996 ppc.set_plugin_sha256("ASDFAADSF"); 997 ppc.set_sample_interval(SAMPLE_INTERVAL); 998 999 ProfilerSessionConfig::BufferConfig bc; 1000 bc.set_pages(1); 1001 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 1002 ProfilerSessionConfig psc; 1003 psc.set_session_mode(ProfilerSessionConfig::OFFLINE); 1004 std::vector<string> names = {"testsample.so"}; 1005 pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names); 1006 EXPECT_TRUE(pluginSessionMgr_->CreatePluginSession( 1007 ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)) != nullptr); 1008 } 1009 /** 1010 * @tc.name: PluginSessionManager 1011 * @tc.desc: test plugin session manager by creating a lot of sesssions 1012 * @tc.type: FUNC 1013 */ 1014 HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSessions, TestSize.Level1) 1015 { 1016 PluginInfo plugin; 1017 plugin.name = "testsample1.so"; 1018 plugin.bufferSizeHint = 0; 1019 plugin.sha256 = "ASDFAADSF"; 1020 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 1021 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 1022 ProfilerPluginConfig ppc; 1023 ppc.set_name("testsample1.so"); 1024 ppc.set_plugin_sha256("ASDFAADSF"); 1025 ppc.set_sample_interval(SAMPLE_INTERVAL); 1026 1027 ProfilerSessionConfig::BufferConfig bc; 1028 bc.set_pages(1); 1029 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 1030 std::vector<ProfilerPluginConfig> vecConfig; 1031 std::vector<ProfilerSessionConfig::BufferConfig> vecBuf; 1032 vecConfig.push_back(ppc); 1033 vecBuf.push_back(bc); 1034 ProfilerSessionConfig psc; 1035 psc.set_session_mode(ProfilerSessionConfig::OFFLINE); 1036 std::vector<string> names = {"testsample1.so"}; 1037 pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names); 1038 EXPECT_TRUE(pluginSessionMgr_->CreatePluginSessions(vecConfig, vecBuf, 1039 std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 1040 std::vector<std::string> nameList{"testsample.so", "testsample1.so"}; 1041 EXPECT_TRUE(pluginSessionMgr_->InvalidatePluginSessions(nameList)); 1042 std::vector<PluginSession::State> vecRet = pluginSessionMgr_->GetStatus(nameList); 1043 EXPECT_TRUE(vecRet.size() != 0); 1044 EXPECT_TRUE(pluginSessionMgr_->RefreshPluginSession()); 1045 } 1046 1047 /** 1048 * @tc.name: plugin service 1049 * @tc.desc: test createpluginsession online 1050 * @tc.type: FUNC 1051 */ 1052 HWTEST_F(UnitTestPluginService, PluginService_CreatePluginSession_Online, TestSize.Level1) 1053 { 1054 ProfilerPluginConfig ppc; 1055 ppc.set_name("abconline.so"); 1056 ppc.set_plugin_sha256("ASDFAADSF"); 1057 ppc.set_sample_interval(SAMPLE_INTERVAL); 1058 ProfilerSessionConfig::BufferConfig bc; 1059 bc.set_pages(1); 1060 bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE); 1061 1062 PluginInfo plugin; 1063 plugin.name = "abconline.so"; 1064 plugin.bufferSizeHint = 0; 1065 plugin.sha256 = "ASDFAADSF"; 1066 plugin.context = (SocketContext*)pluginClient_->unixSocketClient_.get(); 1067 EXPECT_TRUE(pluginService_->AddPluginInfo(plugin)); 1068 ProfilerSessionConfig psc; 1069 psc.set_session_mode(ProfilerSessionConfig::ONLINE); 1070 std::vector<string> names = {"abconline.so"}; 1071 pluginService_->SetProfilerSessionConfig(std::make_shared<ProfilerSessionConfig>(psc), names); 1072 EXPECT_TRUE(pluginService_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE))); 1073 uint32_t pluginId = pluginService_->GetPluginIdByName("abconline.so"); 1074 PluginContextPtr pluginCtx = pluginService_->GetPluginContextById(pluginId); 1075 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED); 1076 pluginService_->ReadShareMemoryOnline(*pluginCtx); 1077 NotifyResultRequest nrr; 1078 nrr.set_request_id(5); 1079 nrr.set_command_id(5); 1080 PluginResult* result = nrr.add_result(); 1081 result->set_plugin_id(pluginId); 1082 result->set_out_file_name("/data/local/tmp/testonline.trace"); 1083 result->set_data("test plugin result!"); 1084 EXPECT_TRUE(pluginService_->AppendResult(nrr)); 1085 plugin.id = pluginId; 1086 EXPECT_TRUE(pluginService_->RemovePluginInfo(plugin)); 1087 } 1088 } // namespace