• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dlfcn.h>
17 #include <iostream>
18 #include <gtest/gtest.h>
19 #include <thread>
20 #include <vector>
21 #include <memory>
22 
23 #include "init_param.h"
24 #include "network_profiler_socker_client.h"
25 #include "network_profiler.h"
26 #include "network_profiler_manager.h"
27 #include "grpc/impl/codegen/log.h"
28 #include "plugin_service.h"
29 #include "plugin_service.ipc.h"
30 #include "socket_context.h"
31 #include "network_profiler_config.pb.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::Developtools::Profiler;
35 
36 namespace {
37 const std::string PARAM_KAY = "hiviewdfx.hiprofiler.networkprofiler.target";
38 constexpr uint32_t SMB1_SIZE = 10 * 4096;
39 const std::string WRITER_NAME = "NetworkProfilerWriterTest";
40 int g_smbFd1 = 0;
41 
42 class NetworkProfilerTest : public ::testing::Test {
43 public:
SetUpTestCase()44     static void SetUpTestCase()
45     {
46     };
47 
TearDownTestCase()48     static void TearDownTestCase()
49     {
50     }
SetUp()51     void SetUp() {}
TearDown()52     void TearDown() {}
53 };
54 
CallBackFunc()55 void CallBackFunc()
56 {
57     return;
58 }
59 
60 /**
61  * @tc.name: network profiler test
62  * @tc.desc: Test NetworkProfilerSocketClient IsProfilerEnable without SystemSetParameter
63  * @tc.type: FUNC
64  */
65 HWTEST_F(NetworkProfilerTest, NetworkProfilerTest001, TestSize.Level1)
66 {
67     auto networkProfilerMgr = std::make_shared<NetworkProfilerManager>();
68     auto networkProfilerService = std::make_unique<NetworkProfilerSocketService>(networkProfilerMgr);
69     ASSERT_TRUE(networkProfilerService != nullptr);
70     networkProfilerMgr->Init();
71     auto networkProfiler = NetworkProfiler::GetInstance();
72     auto ret = networkProfiler->IsProfilerEnable();
73     ASSERT_FALSE(ret);
74     NetworkProfilerSocketClient client(1, networkProfiler, CallBackFunc);
75     client.SendNetworkProfilerData(nullptr, 0, nullptr, 0);
76 }
77 
78 /**
79  * @tc.name: network profiler test
80  * @tc.desc: Test NetworkProfilerSocketClient send empty data to server
81  * @tc.type: FUNC
82  */
83 HWTEST_F(NetworkProfilerTest, NetworkProfilerTest002, TestSize.Level1)
84 {
85     SystemSetParameter("hiviewdfx.hiprofiler.plugins.start", "1");
86     auto networkProfiler = NetworkProfiler::GetInstance();
87     NetworkProfilerSocketClient client(1, networkProfiler, CallBackFunc);
88     sleep(2);
89     auto ret = client.SendNetworkProfilerData(nullptr, 0, nullptr, 0);
90     ASSERT_FALSE(ret);
91     NetworkConfig config;
92     SocketContext ctx;
93     ret = client.ProtocolProc(ctx, 0, reinterpret_cast<int8_t*>(&config), sizeof(config));
94     ASSERT_TRUE(ret);
95     SystemSetParameter("hiviewdfx.hiprofiler.plugins.start", "0");
96 }
97 
98 /**
99  * @tc.name: plugin
100  * @tc.desc: Write data to shared memory through writer.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(NetworkProfilerTest, NetworkProfilerWriterTest, TestSize.Level1)
104 {
105     auto write = std::make_shared<NetworkProfilerWriter>(WRITER_NAME, SMB1_SIZE, g_smbFd1, -1, false);
106     uint8_t data[] = {0x55, 0xAA, 0x55, 0xAA};
107 
__anon3fb0346f0202() 108     auto myCallback = []() -> bool {
109         return true;
110     };
111     auto ret = write->WriteTimeout(static_cast<void*>(data), sizeof(data), myCallback);
112     EXPECT_TRUE(ret == 0);
113 
114     uint8_t payload[] = {0x11, 0x22, 0x33, 0x44};
115     ret = write->WriteWithPayloadTimeout(static_cast<void*>(data), sizeof(data),
116                                         static_cast<void*>(payload), sizeof(payload), myCallback);
117     EXPECT_TRUE(ret == 0);
118 }
119 } // namespace
120