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