• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #ifdef UNIFIED_COLLECTOR_NETWORK_ENABLE
16 #include <iostream>
17 
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #endif
21 
22 #include "network_collector.h"
23 
24 #ifdef UNIFIED_COLLECTOR_NETWORK_ENABLE
25 #include "token_setproc.h"
26 #ifdef COMMUNICATION_WIFI_ENABLE
27 #include "wifi_device.h"
28 #endif
29 #endif
30 
31 #include <gtest/gtest.h>
32 
33 using namespace testing::ext;
34 using namespace OHOS::HiviewDFX;
35 using namespace OHOS::HiviewDFX::UCollectUtil;
36 using namespace OHOS::HiviewDFX::UCollect;
37 
38 #ifdef UNIFIED_COLLECTOR_NETWORK_ENABLE
39 namespace {
NativeTokenGet(const char * perms[],int size)40 void NativeTokenGet(const char* perms[], int size)
41 {
42     uint64_t tokenId;
43     NativeTokenInfoParams infoInstance = {
44         .dcapsNum = 0,
45         .permsNum = size,
46         .aclsNum = 0,
47         .dcaps = nullptr,
48         .perms = perms,
49         .acls = nullptr,
50         .aplStr = "system_basic",
51     };
52 
53     infoInstance.processName = "UCollectionUtilityUnitTest";
54     tokenId = GetAccessTokenId(&infoInstance);
55     SetSelfTokenID(tokenId);
56     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
57 }
58 
EnablePermissionAccess()59 void EnablePermissionAccess()
60 {
61     const char* perms[] = {
62         "ohos.permission.GET_WIFI_INFO",
63     };
64     NativeTokenGet(perms, 1); // 1 is the size of the array which consists of required permissions.
65 }
66 
DisablePermissionAccess()67 void DisablePermissionAccess()
68 {
69     NativeTokenGet(nullptr, 0); // empty permission array.
70 }
71 
IsWifiEnabled()72 bool IsWifiEnabled()
73 {
74 #ifdef COMMUNICATION_WIFI_ENABLE
75     std::shared_ptr<OHOS::Wifi::WifiDevice> wifiDevicePtr =
76         OHOS::Wifi::WifiDevice::GetInstance(OHOS::WIFI_DEVICE_SYS_ABILITY_ID);
77     if (wifiDevicePtr == nullptr) {
78         return false;
79     }
80     bool isActive = false;
81     wifiDevicePtr->IsWifiActive(isActive);
82     if (!isActive) {
83         return false;
84     }
85     OHOS::Wifi::WifiLinkedInfo linkInfo;
86     int ret = wifiDevicePtr->GetLinkedInfo(linkInfo);
87     return ret == OHOS::Wifi::WIFI_OPT_SUCCESS;
88 #else
89     return false;
90 #endif
91 }
92 }
93 #endif
94 
95 class NetworkCollectorTest : public testing::Test {
96 public:
SetUp()97     void SetUp() {};
TearDown()98     void TearDown() {};
SetUpTestCase()99     static void SetUpTestCase() {};
TearDownTestCase()100     static void TearDownTestCase() {};
101 };
102 
103 #ifdef UNIFIED_COLLECTOR_NETWORK_ENABLE
104 /**
105  * @tc.name: NetworkCollectorTest001
106  * @tc.desc: used to test NetworkCollector.CollectRate
107  * @tc.type: FUNC
108 */
109 HWTEST_F(NetworkCollectorTest, NetworkCollectorTest001, TestSize.Level1)
110 {
111     EnablePermissionAccess();
112     std::shared_ptr<NetworkCollector> collector = NetworkCollector::Create();
113     CollectResult<NetworkRate> data = collector->CollectRate();
114     std::cout << "collect network rate result" << data.retCode << std::endl;
115     if (IsWifiEnabled()) {
116         ASSERT_TRUE(data.retCode == UcError::SUCCESS);
117     }
118     DisablePermissionAccess();
119 }
120 #else
121 /**
122  * @tc.name: NetworkCollectorTest001
123  * @tc.desc: used to test empty NetworkCollector
124  * @tc.type: FUNC
125 */
126 HWTEST_F(NetworkCollectorTest, NetworkCollectorTest001, TestSize.Level1)
127 {
128     std::shared_ptr<NetworkCollector> collector = NetworkCollector::Create();
129     CollectResult<NetworkRate> data = collector->CollectRate();
130     ASSERT_TRUE(data.retCode == UcError::FEATURE_CLOSED);
131 }
132 #endif
133