diff --git a/services/netconnmanager/include/net_conn_service.h b/services/netconnmanager/include/net_conn_service.h index 25b89145085258d3c068c75883305cc4210f880e..63fa106d8a0635df35dd9b71ffb41a301e881750 100644 --- a/services/netconnmanager/include/net_conn_service.h +++ b/services/netconnmanager/include/net_conn_service.h @@ -185,7 +185,6 @@ public: * @return int32_t Whether the network probe is successful */ int32_t NetDetection(int32_t netId) override; - bool CheckGetDefaultNetPermission(); int32_t GetDefaultNet(int32_t &netId) override; int32_t HasDefaultNet(bool &flag) override; int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector &addrList) override; @@ -256,26 +255,40 @@ public: private: bool Init(); std::list> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident = ""); - sptr GetNetSupplierFromList( - NetBearType bearerType, const std::string &ident, const std::set &netCaps); - int32_t ActivateNetwork(const sptr &netSpecifier, - const sptr &callback, const uint32_t &timeoutMS); - void CallbackForSupplier(sptr& supplier, CallbackType type); + sptr GetNetSupplierFromList(NetBearType bearerType, const std::string &ident, + const std::set &netCaps); + int32_t ActivateNetwork(const sptr &netSpecifier, const sptr &callback, + const uint32_t &timeoutMS); + void CallbackForSupplier(sptr &supplier, CallbackType type); void CallbackForAvailable(sptr &supplier, const sptr &callback); - uint32_t FindBestNetworkForRequest(sptr& supplier, sptr& netActivateNetwork); + uint32_t FindBestNetworkForRequest(sptr &supplier, sptr &netActivateNetwork); void SendRequestToAllNetwork(sptr request); void SendBestScoreAllNetwork(uint32_t reqId, int32_t bestScore, uint32_t supplierId); void SendAllRequestToNetwork(sptr supplier); void FindBestNetworkForAllRequest(); - void MakeDefaultNetWork(sptr& oldService, sptr& newService); - void NotFindBestSupplier(uint32_t reqId, const sptr &active, - const sptr &supplier, const sptr &callback); + void MakeDefaultNetWork(sptr &oldService, sptr &newService); + void NotFindBestSupplier(uint32_t reqId, const sptr &active, const sptr &supplier, + const sptr &callback); void CreateDefaultRequest(); int32_t RegUnRegNetDetectionCallback(int32_t netId, const sptr &callback, bool isReg); int32_t GenerateNetId(); bool FindSameCallback(const sptr &callback, uint32_t &reqId); void GetDumpMessage(std::string &message); sptr FindNetSupplier(uint32_t supplierId); + int32_t RegisterNetSupplierAsync(NetBearType bearerType, const std::string &ident, const std::set &netCaps, + uint32_t &supplierId); + int32_t UnregisterNetSupplierAsync(uint32_t supplierId); + int32_t RegisterNetSupplierCallbackAsync(uint32_t supplierId, const sptr &callback); + int32_t RegisterNetConnCallbackAsync(const sptr &netSpecifier, const sptr &callback, + const uint32_t &timeoutMS); + int32_t UnregisterNetConnCallbackAsync(const sptr &callback); + int32_t RegUnRegNetDetectionCallbackAsync(int32_t netId, const sptr &callback, bool isReg); + int32_t UpdateNetStateForTestAsync(const sptr &netSpecifier, int32_t netState); + int32_t UpdateNetSupplierInfoAsync(uint32_t supplierId, const sptr &netSupplierInfo); + int32_t UpdateNetLinkInfoAsync(uint32_t supplierId, const sptr &netLinkInfo); + int32_t NetDetectionAsync(int32_t netId); + int32_t RestrictBackgroundChangedAsync(bool restrictBackground); + int32_t SetHttpProxyAsync(const std::string &httpProxy); private: enum ServiceRunningState { @@ -297,7 +310,7 @@ private: std::atomic netIdLastValue_ = MIN_NET_ID - 1; std::string httpProxy_; std::mutex netManagerMutex_; - std::shared_ptr eventRunner_ = nullptr; + std::shared_ptr netConnEventRunner_ = nullptr; std::shared_ptr netConnEventHandler_ = nullptr; }; } // namespace NetManagerStandard diff --git a/services/netconnmanager/include/network.h b/services/netconnmanager/include/network.h index 4a548cf4866ec7f38597d34069e13d49ff354274..9c8c59d348bd2001edad3ae29903dc7bf0f81296 100644 --- a/services/netconnmanager/include/network.h +++ b/services/netconnmanager/include/network.h @@ -37,7 +37,7 @@ class Network : public virtual RefBase, public INetMonitorCallback, public std:: public: Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType, const std::shared_ptr &eventHandler); - ~Network(); + ~Network() = default; bool operator==(const Network &network) const; int32_t GetNetId() const; bool UpdateBasicNetwork(bool isAvailable_); diff --git a/services/netconnmanager/include/stub/net_conn_service_stub.h b/services/netconnmanager/include/stub/net_conn_service_stub.h index 928bcd808a29c4d82ee0d79109730e8239492ecc..68a2cdd303bf440c5a090249924f8a566520f6f3 100644 --- a/services/netconnmanager/include/stub/net_conn_service_stub.h +++ b/services/netconnmanager/include/stub/net_conn_service_stub.h @@ -29,13 +29,14 @@ public: NetConnServiceStub(); ~NetConnServiceStub(); - int32_t OnRemoteRequest( - uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; private: using NetConnServiceFunc = int32_t (NetConnServiceStub::*)(MessageParcel &, MessageParcel &); + using NetConnServiceFuncPer = std::pair>; private: + bool CheckPermission(const std::set &permissions); int32_t OnSystemReady(MessageParcel &data, MessageParcel &reply); int32_t OnRegisterNetSupplier(MessageParcel &data, MessageParcel &reply); int32_t OnUnregisterNetSupplier(MessageParcel &data, MessageParcel &reply); @@ -71,7 +72,7 @@ private: int32_t ConvertCode(int32_t internalCode); private: - std::map memberFuncMap_; + std::map memberFuncMap_; }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index f0fa9cb6e0f0f4b642d14f950c147b45df8cc729..0e81278dec26c79b9ef8f6d6267deb29e076a2d5 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -39,6 +39,7 @@ constexpr const char *ERROR_MSG_NULL_NET_LINK_INFO = "Net link info is nullptr"; constexpr const char *ERROR_MSG_NULL_NET_SPECIFIER = "The parameter of netSpecifier or callback is null"; constexpr const char *ERROR_MSG_CAN_NOT_FIND_SUPPLIER = "Can not find supplier by id:"; constexpr const char *ERROR_MSG_UPDATE_NETLINK_INFO_FAILED = "Update net link info failed"; +constexpr const char *NET_CONN_MANAGER_WORK_THREAD = "NET_CONN_MANAGER_WORK_THREAD"; } // namespace const bool REGISTER_LOCAL_RESULT = @@ -84,8 +85,17 @@ void NetConnService::CreateDefaultRequest() void NetConnService::OnStop() { + NETMGR_LOG_D("NetConnService::OnStop begin"); + if (netConnEventRunner_) { + netConnEventRunner_->Stop(); + netConnEventRunner_.reset(); + } + if (netConnEventHandler_) { + netConnEventHandler_.reset(); + } state_ = STATE_STOPPED; registerToService_ = false; + NETMGR_LOG_D("NetConnService::OnStop end"); } bool NetConnService::Init() @@ -102,6 +112,12 @@ bool NetConnService::Init() } registerToService_ = true; } + netConnEventRunner_ = AppExecFwk::EventRunner::Create(NET_CONN_MANAGER_WORK_THREAD); + if (netConnEventRunner_ == nullptr) { + NETMGR_LOG_E("Create event runner failed."); + return false; + } + netConnEventHandler_ = std::make_shared(netConnEventRunner_); serviceIface_ = std::make_unique().release(); NetManagerCenter::GetInstance().RegisterConnService(serviceIface_); netScore_ = std::make_unique(); @@ -109,13 +125,6 @@ bool NetConnService::Init() NETMGR_LOG_E("Make NetScore failed"); return false; } - - eventRunner_ = AppExecFwk::EventRunner::Create(true); - if (eventRunner_ == nullptr) { - NETMGR_LOG_E("Create event runner failed."); - return false; - } - netConnEventHandler_ = std::make_shared(eventRunner_); return true; } @@ -127,22 +136,158 @@ int32_t NetConnService::SystemReady() int32_t NetConnService::RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set &netCaps, uint32_t &supplierId) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, bearerType, &ident, &netCaps, &supplierId, &result]() { + result = this->RegisterNetSupplierAsync(bearerType, ident, netCaps, supplierId); + }); + } + return result; +} + +int32_t NetConnService::RegisterNetSupplierCallback(uint32_t supplierId, const sptr &callback) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, supplierId, &callback, &result]() { + result = this->RegisterNetSupplierCallbackAsync(supplierId, callback); + }); + } + return result; +} + +int32_t NetConnService::RegisterNetConnCallback(const sptr &callback) +{ + NETMGR_LOG_D("RegisterNetConnCallback service in."); + return RegisterNetConnCallback(defaultNetSpecifier_, callback, 0); +} + +int32_t NetConnService::RegisterNetConnCallback(const sptr &netSpecifier, + const sptr &callback, const uint32_t &timeoutMS) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, &netSpecifier, &callback, timeoutMS, &result]() { + result = this->RegisterNetConnCallbackAsync(netSpecifier, callback, timeoutMS); + }); + } + return result; +} + +int32_t NetConnService::RegisterNetDetectionCallback(int32_t netId, const sptr &callback) +{ + NETMGR_LOG_D("Enter NetConnService::RegisterNetDetectionCallback"); + return RegUnRegNetDetectionCallback(netId, callback, true); +} + +int32_t NetConnService::UnregisterNetSupplier(uint32_t supplierId) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask( + [this, supplierId, &result]() { result = this->UnregisterNetSupplierAsync(supplierId); }); + } + return result; +} + +int32_t NetConnService::UnregisterNetConnCallback(const sptr &callback) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask( + [this, &callback, &result]() { result = this->UnregisterNetConnCallbackAsync(callback); }); + } + return result; +} + +int32_t NetConnService::UnRegisterNetDetectionCallback(int32_t netId, const sptr &callback) +{ + NETMGR_LOG_D("Enter NetConnService::UnRegisterNetDetectionCallback"); + return RegUnRegNetDetectionCallback(netId, callback, false); +} + +int32_t NetConnService::RegUnRegNetDetectionCallback(int32_t netId, const sptr &callback, + bool isReg) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, netId, &callback, isReg, &result]() { + result = this->RegUnRegNetDetectionCallbackAsync(netId, callback, isReg); + }); + } + return result; +} + +int32_t NetConnService::UpdateNetStateForTest(const sptr &netSpecifier, int32_t netState) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, &netSpecifier, netState, &result]() { + result = this->UpdateNetStateForTestAsync(netSpecifier, netState); + }); + } + return result; +} + +int32_t NetConnService::UpdateNetSupplierInfo(uint32_t supplierId, const sptr &netSupplierInfo) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, supplierId, &netSupplierInfo, &result]() { + result = this->UpdateNetSupplierInfoAsync(supplierId, netSupplierInfo); + }); + } + return result; +} + +int32_t NetConnService::UpdateNetLinkInfo(uint32_t supplierId, const sptr &netLinkInfo) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, supplierId, &netLinkInfo, &result]() { + result = this->UpdateNetLinkInfoAsync(supplierId, netLinkInfo); + }); + } + return result; +} + +int32_t NetConnService::NetDetection(int32_t netId) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, netId, &result]() { result = this->NetDetectionAsync(netId); }); + } + return result; +} + +int32_t NetConnService::RestrictBackgroundChanged(bool restrictBackground) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, restrictBackground, &result]() { + result = this->RestrictBackgroundChangedAsync(restrictBackground); + }); + } + return result; +} + +int32_t NetConnService::RegisterNetSupplierAsync(NetBearType bearerType, const std::string &ident, + const std::set &netCaps, uint32_t &supplierId) { NETMGR_LOG_D("register supplier, netType[%{public}u], ident[%{public}s]", static_cast(bearerType), ident.c_str()); - // According to netType, ident, get the supplier from the list and save the supplierId in the list + // If there is no supplier in the list, create a supplier if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) { NETMGR_LOG_E("netType parameter invalid"); return ERR_INVALID_NETORK_TYPE; } - sptr supplier = GetNetSupplierFromList(bearerType, ident, netCaps); if (supplier != nullptr) { NETMGR_LOG_D("supplier already exists."); supplierId = supplier->GetSupplierId(); - return ERR_NONE; + return NETMANAGER_SUCCESS; } - // If there is no supplier in the list, create a supplier supplier = (std::make_unique(bearerType, ident, netCaps)).release(); if (supplier == nullptr) { @@ -153,7 +298,6 @@ int32_t NetConnService::RegisterNetSupplier(NetBearType bearerType, const std::s if (!netScore_->GetServiceScore(supplier)) { NETMGR_LOG_E("GetServiceScore fail."); } - // create network int32_t netId = GenerateNetId(); NETMGR_LOG_D("GenerateNetId is: [%{public}d]", netId); @@ -161,51 +305,68 @@ int32_t NetConnService::RegisterNetSupplier(NetBearType bearerType, const std::s NETMGR_LOG_E("GenerateNetId fail"); return ERR_NO_NETWORK; } - using namespace std::placeholders; - std::shared_ptr network = - std::make_shared(netId, supplierId, std::bind(&NetConnService::HandleDetectionResult, this, _1, _2), - bearerType, netConnEventHandler_); - NETMGR_LOG_D("netId is: [%{public}d], supplierId is: [%{public}d]", network->GetNetId(), supplier->GetSupplierId()); + std::shared_ptr network = std::make_shared( + netId, supplierId, + std::bind(&NetConnService::HandleDetectionResult, this, std::placeholders::_1, std::placeholders::_2), + bearerType, netConnEventHandler_); + NETMGR_LOG_D("Register supplier,supplierId[%{public}d] netId[%{public}d], ", supplierId, netId); supplier->SetNetwork(network); supplier->SetNetValid(true); - // save supplier std::unique_lock locker(netManagerMutex_); netSuppliers_[supplierId] = supplier; networks_[netId] = network; locker.unlock(); - NETMGR_LOG_D("RegisterNetSupplier service out. netSuppliers_ size[%{public}zd]", netSuppliers_.size()); struct EventInfo eventInfo = {.netId = netId, .bearerType = bearerType, .ident = ident, .supplierId = supplierId}; EventReport::SendSupplierBehaviorEvent(eventInfo); - return ERR_NONE; + NETMGR_LOG_D("RegisterNetSupplier service out. netSuppliers_ size[%{public}zd]", netSuppliers_.size()); + return NETMANAGER_SUCCESS; } -int32_t NetConnService::GenerateNetId() +int32_t NetConnService::RegisterNetSupplierCallbackAsync(uint32_t supplierId, + const sptr &callback) { - for (int32_t i = MIN_NET_ID; i <= MAX_NET_ID; ++i) { - netIdLastValue_++; - if (netIdLastValue_ > MAX_NET_ID) { - netIdLastValue_ = MIN_NET_ID; - } - if (networks_.find(netIdLastValue_) == networks_.end()) { - return netIdLastValue_; - } + NETMGR_LOG_D("Register net supplier callback async"); + if (callback == nullptr) { + NETMGR_LOG_E("The parameter callback is null"); + return ERR_SERVICE_NULL_PTR; } - return INVALID_NET_ID; + auto supplier = FindNetSupplier(supplierId); + if (supplier == nullptr) { + NETMGR_LOG_E("supplier doesn't exist."); + return ERR_NO_SUPPLIER; + } + supplier->RegisterSupplierCallback(callback); + SendAllRequestToNetwork(supplier); + NETMGR_LOG_D("RegisterNetSupplierCallback service out."); + return NETMANAGER_SUCCESS; } -sptr NetConnService::FindNetSupplier(uint32_t supplierId) +int32_t NetConnService::RegisterNetConnCallbackAsync(const sptr &netSpecifier, + const sptr &callback, const uint32_t &timeoutMS) { - std::lock_guard locker(netManagerMutex_); - auto iterSupplier = netSuppliers_.find(supplierId); - if (iterSupplier != netSuppliers_.end()) { - return iterSupplier->second; + NETMGR_LOG_D("Register net connect callback async"); + if (netSpecifier == nullptr || callback == nullptr) { + NETMGR_LOG_E("The parameter of netSpecifier or callback is null"); + struct EventInfo eventInfo = {.errorType = static_cast(FAULT_INVALID_PARAMETER), + .errorMsg = ERROR_MSG_NULL_NET_SPECIFIER}; + EventReport::SendRequestFaultEvent(eventInfo); + return ERR_SERVICE_NULL_PTR; } - return nullptr; + if (netActivates_.size() >= MAX_REQUEST_NUM) { + NETMGR_LOG_E("Over the max request number"); + return ERR_NET_OVER_MAX_REQUEST_NUM; + } + uint32_t reqId = 0; + if (FindSameCallback(callback, reqId)) { + NETMGR_LOG_E("RegisterNetConnCallback find same callback"); + return ERR_REGISTER_THE_SAME_CALLBACK; + } + return ActivateNetwork(netSpecifier, callback, timeoutMS); } -int32_t NetConnService::UnregisterNetSupplier(uint32_t supplierId) +int32_t NetConnService::UnregisterNetSupplierAsync(uint32_t supplierId) { NETMGR_LOG_D("UnregisterNetSupplier supplierId[%{public}d]", supplierId); // Remove supplier from the list based on supplierId @@ -225,10 +386,11 @@ int32_t NetConnService::UnregisterNetSupplier(uint32_t supplierId) EventReport::SendSupplierBehaviorEvent(eventInfo); int32_t netId = supplier->GetNetId(); - std::unique_lock locker(netManagerMutex_); NET_NETWORK_MAP::iterator iterNetwork = networks_.find(netId); if (iterNetwork != networks_.end()) { + std::unique_lock locker(netManagerMutex_); networks_.erase(iterNetwork); + locker.unlock(); } if (defaultNetSupplier_ == supplier) { NETMGR_LOG_D("set defaultNetSupplier_ to null."); @@ -237,89 +399,27 @@ int32_t NetConnService::UnregisterNetSupplier(uint32_t supplierId) } NetSupplierInfo info; supplier->UpdateNetSupplierInfo(info); + std::unique_lock locker(netManagerMutex_); netSuppliers_.erase(supplierId); locker.unlock(); FindBestNetworkForAllRequest(); - NETMGR_LOG_D("Destroy supplier network."); - return ERR_NONE; -} - -int32_t NetConnService::RegisterNetSupplierCallback(uint32_t supplierId, const sptr &callback) -{ - NETMGR_LOG_D("RegisterNetSupplierCallback service in."); - if (callback == nullptr) { - NETMGR_LOG_E("The parameter callback is null"); - return ERR_SERVICE_NULL_PTR; - } - auto supplier = FindNetSupplier(supplierId); - if (supplier == nullptr) { - NETMGR_LOG_E("supplier doesn't exist."); - return ERR_NO_SUPPLIER; - } - supplier->RegisterSupplierCallback(callback); - SendAllRequestToNetwork(supplier); - NETMGR_LOG_D("RegisterNetSupplierCallback service out."); - return ERR_NONE; -} - -int32_t NetConnService::RegisterNetConnCallback(const sptr &callback) -{ - NETMGR_LOG_D("RegisterNetConnCallback service in."); - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - if (callback == nullptr) { - NETMGR_LOG_E("The parameter callback is null"); - return ERR_SERVICE_NULL_PTR; - } - return RegisterNetConnCallback(defaultNetSpecifier_, callback, 0); -} - -int32_t NetConnService::RegisterNetConnCallback(const sptr &netSpecifier, - const sptr &callback, const uint32_t &timeoutMS) -{ - NETMGR_LOG_D("RegisterNetConnCallback service in."); - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - if (netSpecifier == nullptr || callback == nullptr) { - NETMGR_LOG_E("The parameter of netSpecifier or callback is null"); - struct EventInfo eventInfo = {.errorType = static_cast(FAULT_INVALID_PARAMETER), - .errorMsg = ERROR_MSG_NULL_NET_SPECIFIER}; - EventReport::SendRequestFaultEvent(eventInfo); - return ERR_SERVICE_NULL_PTR; - } - std::lock_guard locker(netManagerMutex_); - if (netActivates_.size() >= MAX_REQUEST_NUM) { - NETMGR_LOG_E("Over the max request number"); - return ERR_NET_OVER_MAX_REQUEST_NUM; - } - uint32_t reqId = 0; - if (FindSameCallback(callback, reqId)) { - NETMGR_LOG_D("RegisterNetConnCallback FindSameCallback(callback, reqId)"); - return ERR_REGISTER_THE_SAME_CALLBACK; - } - return ActivateNetwork(netSpecifier, callback, timeoutMS); + NETMGR_LOG_D("UnregisterNetSupplier supplierId[%{public}d] out", supplierId); + return NETMANAGER_SUCCESS; } -int32_t NetConnService::UnregisterNetConnCallback(const sptr &callback) +int32_t NetConnService::UnregisterNetConnCallbackAsync(const sptr &callback) { NETMGR_LOG_D("UnregisterNetConnCallback Enter"); - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } if (callback == nullptr) { NETMGR_LOG_E("callback is null"); return ERR_SERVICE_NULL_PTR; } - std::lock_guard locker(netManagerMutex_); uint32_t reqId = 0; if (!FindSameCallback(callback, reqId)) { - NETMGR_LOG_D("UnregisterNetConnCallback FindSameCallback(callback, reqId)"); + NETMGR_LOG_D("UnregisterNetConnCallback can not find same callback"); return ERR_UNREGISTER_CALLBACK_NOT_FOUND; } deleteNetActivates_.clear(); - NET_ACTIVATE_MAP::iterator iterActive; for (iterActive = netActivates_.begin(); iterActive != netActivates_.end();) { if (!iterActive->second) { @@ -343,7 +443,6 @@ int32_t NetConnService::UnregisterNetConnCallback(const sptr & supplier->CancelRequest(reqId); } } - NET_SUPPLIER_MAP::iterator iterSupplier; for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { if (iterSupplier->second != nullptr) { @@ -353,47 +452,50 @@ int32_t NetConnService::UnregisterNetConnCallback(const sptr & deleteNetActivates_[reqId] = netActivate; iterActive = netActivates_.erase(iterActive); } - return ERR_NONE; + return NETMANAGER_SUCCESS; } -bool NetConnService::FindSameCallback(const sptr &callback, uint32_t &reqId) +int32_t NetConnService::RegUnRegNetDetectionCallbackAsync(int32_t netId, const sptr &callback, + bool isReg) { + NETMGR_LOG_D("Enter NetConnService::RegUnRegNetDetectionCallback"); if (callback == nullptr) { - NETMGR_LOG_E("callback is null"); - return false; + NETMGR_LOG_E("The parameter of callback is null"); + return ERR_SERVICE_NULL_PTR; } - NET_ACTIVATE_MAP::iterator iterActive; - for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) { - if (!iterActive->second) { - continue; - } - sptr saveCallback = iterActive->second->GetNetCallback(); - if (saveCallback == nullptr) { - continue; - } - if (callback->AsObject().GetRefPtr() == saveCallback->AsObject().GetRefPtr()) { - reqId = iterActive->first; - return true; - } + + std::shared_ptr detectionNetwork = nullptr; + auto iterNetwork = networks_.find(netId); + if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) { + NETMGR_LOG_E("Could not find the corresponding network."); + } else { + detectionNetwork = iterNetwork->second; } - return false; + + if (detectionNetwork == nullptr) { + NETMGR_LOG_E("Network is not find, need register!"); + return ERR_NET_NOT_FIND_NETID; + } + if (isReg) { + detectionNetwork->RegisterNetDetectionCallback(callback); + return NETMANAGER_SUCCESS; + } + return detectionNetwork->UnRegisterNetDetectionCallback(callback); } -int32_t NetConnService::UpdateNetStateForTest(const sptr &netSpecifier, int32_t netState) +int32_t NetConnService::UpdateNetStateForTestAsync(const sptr &netSpecifier, int32_t netState) { - NETMGR_LOG_I("Test NetConnService::UpdateNetStateForTest(), begin"); + NETMGR_LOG_D("Test NetConnService::UpdateNetStateForTest(), begin"); if (netSpecifier == nullptr) { NETMGR_LOG_E("The parameter of netSpecifier or callback is null"); return ERR_SERVICE_NULL_PTR; } - return ERR_NONE; + return NETMANAGER_SUCCESS; } -int32_t NetConnService::UpdateNetSupplierInfo(uint32_t supplierId, const sptr &netSupplierInfo) +int32_t NetConnService::UpdateNetSupplierInfoAsync(uint32_t supplierId, const sptr &netSupplierInfo) { - struct EventInfo eventInfo = { - .updateSupplierId = supplierId - }; + struct EventInfo eventInfo = {.updateSupplierId = supplierId}; if (netSupplierInfo == nullptr) { NETMGR_LOG_E("netSupplierInfo is nullptr"); eventInfo.errorType = static_cast(FAULT_UPDATE_SUPPLIERINFO_INV_PARAM); @@ -407,7 +509,6 @@ int32_t NetConnService::UpdateNetSupplierInfo(uint32_t supplierId, const sptrToString(" "); EventReport::SendSupplierBehaviorEvent(eventInfo); - // According to supplierId, get the supplier from the list auto supplier = FindNetSupplier(supplierId); if (supplier == nullptr) { NETMGR_LOG_E("supplier is nullptr, netSuppliers_ size[%{public}zd]", netSuppliers_.size()); @@ -418,51 +519,23 @@ int32_t NetConnService::UpdateNetSupplierInfo(uint32_t supplierId, const sptrUpdateNetSupplierInfo(*netSupplierInfo); - - std::unique_lock locker(netManagerMutex_); if (!netSupplierInfo->isAvailable_) { CallbackForSupplier(supplier, CALL_TYPE_LOST); } else { CallbackForSupplier(supplier, CALL_TYPE_UPDATE_CAP); } - locker.unlock(); if (!netScore_->GetServiceScore(supplier)) { NETMGR_LOG_E("GetServiceScore fail."); } FindBestNetworkForAllRequest(); NETMGR_LOG_D("UpdateNetSupplierInfo service out."); - return ERR_NONE; + return NETMANAGER_SUCCESS; } -int32_t NetConnService::RestrictBackgroundChanged(bool restrictBackground) +int32_t NetConnService::UpdateNetLinkInfoAsync(uint32_t supplierId, const sptr &netLinkInfo) { - NETMGR_LOG_D("NetConnService::RestrictBackgroundChanged restrictBackground = %{public}d", restrictBackground); - std::lock_guard locker(netManagerMutex_); - for (auto it = netSuppliers_.begin(); it != netSuppliers_.end(); ++it) { - if (it->second == nullptr) { - continue; - } - - if (it->second->GetRestrictBackground() == restrictBackground) { - NETMGR_LOG_D("it->second->GetRestrictBackground() == restrictBackground"); - return ERR_NET_NO_RESTRICT_BACKGROUND; - } - - if (it->second->GetNetSupplierType() == BEARER_VPN) { - CallbackForSupplier(it->second, CALL_TYPE_BLOCK_STATUS); - } - it->second->SetRestrictBackground(restrictBackground); - } - NETMGR_LOG_D("RestrictBackgroundChanged service out."); - return ERR_NONE; -} - -int32_t NetConnService::UpdateNetLinkInfo(uint32_t supplierId, const sptr &netLinkInfo) -{ - NETMGR_LOG_D("UpdateNetLinkInfo service in. supplierId[%{public}d]", supplierId); - struct EventInfo eventInfo = { - .updateNetlinkId = supplierId - }; + NETMGR_LOG_D("UpdateNetLinkInfo service in. supplierId[%{public}d]", supplierId); + struct EventInfo eventInfo = {.updateNetlinkId = supplierId}; if (netLinkInfo == nullptr) { NETMGR_LOG_E("netLinkInfo is nullptr"); @@ -491,37 +564,18 @@ int32_t NetConnService::UpdateNetLinkInfo(uint32_t supplierId, const sptr locker(netManagerMutex_); CallbackForSupplier(supplier, CALL_TYPE_UPDATE_LINK); - locker.unlock(); if (!netScore_->GetServiceScore(supplier)) { NETMGR_LOG_E("GetServiceScore fail."); } FindBestNetworkForAllRequest(); NETMGR_LOG_D("UpdateNetLinkInfo service out."); - return ERR_NONE; -} - -int32_t NetConnService::RegisterNetDetectionCallback(int32_t netId, const sptr &callback) -{ - NETMGR_LOG_D("Enter NetConnService::RegisterNetDetectionCallback"); - return RegUnRegNetDetectionCallback(netId, callback, true); -} - -int32_t NetConnService::UnRegisterNetDetectionCallback(int32_t netId, const sptr &callback) -{ - NETMGR_LOG_D("Enter NetConnService::UnRegisterNetDetectionCallback"); - return RegUnRegNetDetectionCallback(netId, callback, false); + return NETMANAGER_SUCCESS; } -int32_t NetConnService::NetDetection(int32_t netId) +int32_t NetConnService::NetDetectionAsync(int32_t netId) { NETMGR_LOG_D("Enter NetConnService::NetDetection"); - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO) || - !NetManagerPermission::CheckPermission(Permission::INTERNET)) { - return ERR_PERMISSION_CHECK_FAIL; - } - std::lock_guard locker(netManagerMutex_); std::shared_ptr detectionNetwork = nullptr; auto iterNetwork = networks_.find(netId); if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) { @@ -535,70 +589,39 @@ int32_t NetConnService::NetDetection(int32_t netId) return ERR_NET_NOT_FIND_NETID; } detectionNetwork->StartNetDetection(true); - return ERR_NONE; -} - -int32_t NetConnService::RegUnRegNetDetectionCallback(int32_t netId, const sptr &callback, - bool isReg) -{ - if (callback == nullptr) { - NETMGR_LOG_E("The parameter of callback is null"); - return ERR_SERVICE_NULL_PTR; - } - - std::lock_guard locker(netManagerMutex_); - std::shared_ptr detectionNetwork = nullptr; - auto iterNetwork = networks_.find(netId); - if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) { - NETMGR_LOG_E("Could not find the corresponding network."); - } else { - detectionNetwork = iterNetwork->second; - } - - if (detectionNetwork == nullptr) { - NETMGR_LOG_E("Network is not find, need register!"); - return ERR_NET_NOT_FIND_NETID; - } - if (isReg) { - detectionNetwork->RegisterNetDetectionCallback(callback); - return ERR_NONE; - } - return detectionNetwork->UnRegisterNetDetectionCallback(callback); + return NETMANAGER_SUCCESS; } -std::list> NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident) +int32_t NetConnService::RestrictBackgroundChangedAsync(bool restrictBackground) { - std::list> ret; - std::lock_guard locker(netManagerMutex_); - for (const auto &netSupplier : netSuppliers_) { - if (netSupplier.second == nullptr) { + NETMGR_LOG_D("Restrict background changed, background = %{public}d", restrictBackground); + for (auto it = netSuppliers_.begin(); it != netSuppliers_.end(); ++it) { + if (it->second == nullptr) { continue; } - if ((bearerType != netSupplier.second->GetNetSupplierType())) { - continue; + + if (it->second->GetRestrictBackground() == restrictBackground) { + NETMGR_LOG_D("it->second->GetRestrictBackground() == restrictBackground"); + return ERR_NET_NO_RESTRICT_BACKGROUND; } - if (!ident.empty() && netSupplier.second->GetNetSupplierIdent() != ident) { - continue; + + if (it->second->GetNetSupplierType() == BEARER_VPN) { + CallbackForSupplier(it->second, CALL_TYPE_BLOCK_STATUS); } - ret.push_back(netSupplier.second); + it->second->SetRestrictBackground(restrictBackground); } - return ret; + return NETMANAGER_SUCCESS; } -sptr NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident, - const std::set &netCaps) +int32_t NetConnService::SetHttpProxyAsync(const std::string &httpProxy) { - std::lock_guard locker(netManagerMutex_); - for (const auto &netSupplier : netSuppliers_) { - if (netSupplier.second == nullptr) { - continue; - } - if ((bearerType == netSupplier.second->GetNetSupplierType()) && - (ident == netSupplier.second->GetNetSupplierIdent()) && netSupplier.second->CompareNetCaps(netCaps)) { - return netSupplier.second; - } + if (httpProxy.empty()) { + NETMGR_LOG_E("The httpProxy set to service is null"); + return ERR_HTTP_PROXY_INVALID; } - return nullptr; + + httpProxy_ = httpProxy; + return NETMANAGER_SUCCESS; } int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, const sptr &callback, @@ -623,234 +646,176 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, request->SetServiceSupply(bestNet); CallbackForAvailable(bestNet, callback); if ((bestNet->GetNetSupplierType() == BEARER_CELLULAR) || (bestNet->GetNetSupplierType() == BEARER_WIFI)) { - struct EventInfo eventInfo = { - .capabilities = bestNet->GetNetCapabilities().ToString(" "), - .supplierIdent = bestNet->GetNetSupplierIdent() - }; + struct EventInfo eventInfo = {.capabilities = bestNet->GetNetCapabilities().ToString(" "), + .supplierIdent = bestNet->GetNetSupplierIdent()}; EventReport::SendRequestBehaviorEvent(eventInfo); } - return ERR_NONE; + return NETMANAGER_SUCCESS; + } + if (timeoutMS == 0) { + callback->NetUnavailable(); } NETMGR_LOG_I("ActivateNetwork: can't found best network, send request to all networks."); SendRequestToAllNetwork(request); deleteNetActivates_.clear(); - return ERR_NONE; + return NETMANAGER_SUCCESS; } void NetConnService::OnNetActivateTimeOut(uint32_t reqId) { - NETMGR_LOG_D("OnNetActivateTimeOut Enter, reqId is [%{public}d]", reqId); - std::lock_guard locker(netManagerMutex_); - auto iterActivate = netActivates_.find(reqId); - if (iterActivate == netActivates_.end()) { - NETMGR_LOG_E("not found the reqId: [%{public}d]", reqId); - return; - } - sptr pNetActivate = iterActivate->second; - if (pNetActivate) { - sptr pNetService = pNetActivate->GetServiceSupply(); - if (pNetService) { - pNetService->CancelRequest(reqId); - } - } + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([reqId, this]() { + NETMGR_LOG_D("DeactivateNetwork Enter, reqId is [%{public}d]", reqId); + auto iterActivate = netActivates_.find(reqId); + if (iterActivate == netActivates_.end()) { + NETMGR_LOG_E("not found the reqId: [%{public}d]", reqId); + return; + } + sptr pNetActivate = iterActivate->second; + if (pNetActivate) { + sptr pNetService = pNetActivate->GetServiceSupply(); + if (pNetService) { + pNetService->CancelRequest(reqId); + } + } - NET_SUPPLIER_MAP::iterator iterSupplier; - for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { - if (iterSupplier->second == nullptr) { - continue; - } - iterSupplier->second->CancelRequest(reqId); + NET_SUPPLIER_MAP::iterator iterSupplier; + for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { + if (iterSupplier->second == nullptr) { + continue; + } + iterSupplier->second->CancelRequest(reqId); + } + }); } } -int32_t NetConnService::GetDefaultNet(int32_t &netId) +sptr NetConnService::FindNetSupplier(uint32_t supplierId) { - if (!NetManagerPermission::CheckPermissionWithCache(Permission::GET_NETWORK_INFO)) { - return NETMANAGER_ERR_PERMISSION_DENIED; - } - if (!defaultNetSupplier_) { - NETMGR_LOG_E("not found the netId"); - return NETMANAGER_SUCCESS; + auto iterSupplier = netSuppliers_.find(supplierId); + if (iterSupplier != netSuppliers_.end()) { + return iterSupplier->second; } - - netId = defaultNetSupplier_->GetNetId(); - NETMGR_LOG_D("GetDefaultNet found the netId: [%{public}d]", netId); - return NETMANAGER_SUCCESS; + return nullptr; } -int32_t NetConnService::HasDefaultNet(bool &flag) +bool NetConnService::FindSameCallback(const sptr &callback, uint32_t &reqId) { - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; + if (callback == nullptr) { + NETMGR_LOG_E("callback is null"); + return false; } - if (!defaultNetSupplier_) { - flag = false; - return ERR_NONE; + NET_ACTIVATE_MAP::iterator iterActive; + for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) { + if (!iterActive->second) { + continue; + } + sptr saveCallback = iterActive->second->GetNetCallback(); + if (saveCallback == nullptr) { + continue; + } + if (callback->AsObject().GetRefPtr() == saveCallback->AsObject().GetRefPtr()) { + reqId = iterActive->first; + return true; + } } - flag = true; - return ERR_NONE; + return false; } -int32_t NetConnService::IsDefaultNetMetered(bool &isMetered) +void NetConnService::FindBestNetworkForAllRequest() { - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return NETMANAGER_ERR_PERMISSION_DENIED; - } - if (defaultNetSupplier_) { - isMetered = !defaultNetSupplier_->HasNetCap(NET_CAPABILITY_NOT_METERED); - } else { - isMetered = true; + NETMGR_LOG_I("FindBestNetworkForAllRequest Enter"); + NET_ACTIVATE_MAP::iterator iterActive; + sptr bestSupplier = nullptr; + for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) { + if (!iterActive->second) { + continue; + } + int score = static_cast(FindBestNetworkForRequest(bestSupplier, iterActive->second)); + NETMGR_LOG_D("bestSupplier is: [%{public}d, %{public}s]", bestSupplier ? bestSupplier->GetSupplierId() : 0, + bestSupplier ? bestSupplier->GetNetSupplierIdent().c_str() : "null"); + if (iterActive->second == defaultNetActivate_) { + MakeDefaultNetWork(defaultNetSupplier_, bestSupplier); + } + sptr oldSupplier = iterActive->second->GetServiceSupply(); + sptr callback = iterActive->second->GetNetCallback(); + if (!bestSupplier) { + // not found the bestNetwork + NotFindBestSupplier(iterActive->first, iterActive->second, oldSupplier, callback); + continue; + } + + SendBestScoreAllNetwork(iterActive->first, score, bestSupplier->GetSupplierId()); + if (bestSupplier == oldSupplier) { + continue; + } + if (oldSupplier) { + oldSupplier->RemoveBestRequest(iterActive->first); + } + iterActive->second->SetServiceSupply(bestSupplier); + CallbackForAvailable(bestSupplier, callback); + bestSupplier->SelectAsBestNetwork(iterActive->first); } - return NETMANAGER_SUCCESS; } -void NetConnService::MakeDefaultNetWork(sptr &oldSupplier, sptr &newSupplier) +uint32_t NetConnService::FindBestNetworkForRequest(sptr &supplier, sptr &netActivateNetwork) { - NETMGR_LOG_I("MakeDefaultNetWork in, oldSupplier[%{public}d, %{public}s], newSupplier[%{public}d, %{public}s]", - oldSupplier ? oldSupplier->GetSupplierId() : 0, - oldSupplier ? oldSupplier->GetNetSupplierIdent().c_str() : "null", - newSupplier ? newSupplier->GetSupplierId() : 0, - newSupplier ? newSupplier->GetNetSupplierIdent().c_str() : "null"); - if (oldSupplier == newSupplier) { - NETMGR_LOG_D("old supplier equal to new supplier."); - return; - } - if (oldSupplier != nullptr) { - oldSupplier->ClearDefault(); + int bestScore = 0; + if (netActivateNetwork == nullptr) { + NETMGR_LOG_E("netActivateNetwork is null"); + return bestScore; } - if (newSupplier != nullptr) { - newSupplier->SetDefault(); + NETMGR_LOG_I("FindBestNetworkForRequest Enter, request is [%{public}s]", + netActivateNetwork->GetNetSpecifier() ? netActivateNetwork->GetNetSpecifier()->ToString(" ").c_str() + : "null"); + NET_SUPPLIER_MAP::iterator iter; + for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) { + if (iter->second == nullptr) { + continue; + } + NETMGR_LOG_D("supplier info, supplier[%{public}d, %{public}s], realScore[%{public}d], isConnected[%{public}d]", + iter->second->GetSupplierId(), iter->second->GetNetSupplierIdent().c_str(), + iter->second->GetRealScore(), iter->second->IsConnected()); + if ((!netActivateNetwork->MatchRequestAndNetwork(iter->second)) || (!iter->second->IsConnected())) { + NETMGR_LOG_D("supplier[%{public}d] is not connected or not match request.", iter->second->GetSupplierId()); + continue; + } + int score = iter->second->GetRealScore(); + if (score > bestScore) { + bestScore = score; + supplier = iter->second; + } } - oldSupplier = newSupplier; - NETMGR_LOG_D("default Supplier set to: [%{public}d, %{public}s]", oldSupplier ? oldSupplier->GetSupplierId() : 0, - oldSupplier ? oldSupplier->GetNetSupplierIdent().c_str() : "null"); + NETMGR_LOG_I("FindBestNetworkForRequest exit, bestScore[%{public}d], bestSupplier[%{public}d, %{public}s]", + bestScore, supplier ? supplier->GetSupplierId() : 0, + supplier ? supplier->GetNetSupplierIdent().c_str() : "null"); + return bestScore; } -int32_t NetConnService::GetAddressesByName(const std::string &host, int32_t netId, std::vector &addrList) +int32_t NetConnService::GenerateNetId() { - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; + for (int32_t i = MIN_NET_ID; i <= MAX_NET_ID; ++i) { + netIdLastValue_++; + if (netIdLastValue_ > MAX_NET_ID) { + netIdLastValue_ = MIN_NET_ID; + } + if (networks_.find(netIdLastValue_) == networks_.end()) { + return netIdLastValue_; + } } - return NetManagerCenter::GetInstance().GetAddressesByName(host, static_cast(netId), addrList); + return INVALID_NET_ID; } -int32_t NetConnService::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) +void NetConnService::NotFindBestSupplier(uint32_t reqId, const sptr &active, + const sptr &supplier, const sptr &callback) { - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - std::vector addrList; - int ret = GetAddressesByName(host, netId, addrList); - if (ret == ERR_NONE) { - if (!addrList.empty()) { - addr = addrList[0]; - return ret; - } - return ERR_NO_ADDRESS; - } - return ret; -} - -int32_t NetConnService::GetSpecificNet(NetBearType bearerType, std::list &netIdList) -{ - if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) { - NETMGR_LOG_E("netType parameter invalid"); - return ERR_INVALID_NETORK_TYPE; - } - - NET_SUPPLIER_MAP::iterator iterSupplier; - std::lock_guard locker(netManagerMutex_); - for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { - if (iterSupplier->second == nullptr) { - continue; - } - auto supplierType = iterSupplier->second->GetNetSupplierType(); - if (bearerType == supplierType) { - netIdList.push_back(iterSupplier->second->GetNetId()); - } - } - NETMGR_LOG_D("netSuppliers_ size[%{public}zd] networks_ size[%{public}zd]", netSuppliers_.size(), networks_.size()); - return ERR_NONE; -} - -int32_t NetConnService::GetAllNets(std::list &netIdList) -{ - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - std::lock_guard locker(netManagerMutex_); - for (const auto &network : networks_) { - if (network.second != nullptr && network.second->IsConnected()) { - netIdList.push_back(network.second->GetNetId()); - } - } - NETMGR_LOG_D("netSuppliers_ size[%{public}zd] netIdList size[%{public}zd]", netSuppliers_.size(), netIdList.size()); - return ERR_NONE; -} - -int32_t NetConnService::GetSpecificUidNet(int32_t uid, int32_t &netId) -{ - NETMGR_LOG_D("Enter GetSpecificUidNet, uid is [%{public}d].", uid); - netId = INVALID_NET_ID; - NET_SUPPLIER_MAP::iterator iterSupplier; - std::lock_guard locker(netManagerMutex_); - for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { - if ((iterSupplier->second != nullptr) && (uid == iterSupplier->second->GetSupplierUid()) && - (iterSupplier->second->GetNetSupplierType() == BEARER_VPN)) { - netId = iterSupplier->second->GetNetId(); - return ERR_NONE; - } - } - return GetDefaultNet(netId); -} - -int32_t NetConnService::GetConnectionProperties(int32_t netId, NetLinkInfo &info) -{ - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - std::lock_guard locker(netManagerMutex_); - auto iterNetwork = networks_.find(netId); - if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) { - return ERR_NO_NETWORK; - } - - info = iterNetwork->second->GetNetLinkInfo(); - return ERR_NONE; -} - -int32_t NetConnService::GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap) -{ - if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) { - return ERR_PERMISSION_CHECK_FAIL; - } - NET_SUPPLIER_MAP::iterator iterSupplier; - std::lock_guard locker(netManagerMutex_); - for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { - if ((iterSupplier->second != nullptr) && (netId == iterSupplier->second->GetNetId())) { - netAllCap = iterSupplier->second->GetNetCapabilities(); - return ERR_NONE; - } - } - return ERR_NO_NETWORK; -} - -int32_t NetConnService::BindSocket(int32_t socket_fd, int32_t netId) -{ - NETMGR_LOG_D("Enter BindSocket."); - return NetsysController::GetInstance().BindSocket(socket_fd, netId); -} - -void NetConnService::NotFindBestSupplier(uint32_t reqId, const sptr &active, - const sptr &supplier, const sptr &callback) -{ - if (supplier != nullptr) { - supplier->RemoveBestRequest(reqId); - if (callback != nullptr) { - sptr netHandle = supplier->GetNetHandle(); - callback->NetLost(netHandle); - } + if (supplier != nullptr) { + supplier->RemoveBestRequest(reqId); + if (callback != nullptr) { + sptr netHandle = supplier->GetNetHandle(); + callback->NetLost(netHandle); + } } if (active != nullptr) { active->SetServiceSupply(nullptr); @@ -858,77 +823,6 @@ void NetConnService::NotFindBestSupplier(uint32_t reqId, const sptr } } -void NetConnService::FindBestNetworkForAllRequest() -{ - NETMGR_LOG_I("FindBestNetworkForAllRequest Enter"); - std::lock_guard locker(netManagerMutex_); - NET_ACTIVATE_MAP::iterator iterActive; - sptr bestSupplier = nullptr; - for (iterActive = netActivates_.begin(); iterActive != netActivates_.end(); ++iterActive) { - if (!iterActive->second) { - continue; - } - int score = static_cast(FindBestNetworkForRequest(bestSupplier, iterActive->second)); - NETMGR_LOG_D("bestSupplier is: [%{public}d, %{public}s]", bestSupplier ? bestSupplier->GetSupplierId() : 0, - bestSupplier ? bestSupplier->GetNetSupplierIdent().c_str() : "null"); - if (iterActive->second == defaultNetActivate_) { - MakeDefaultNetWork(defaultNetSupplier_, bestSupplier); - } - sptr oldSupplier = iterActive->second->GetServiceSupply(); - sptr callback = iterActive->second->GetNetCallback(); - if (!bestSupplier) { - // not found the bestNetwork - NotFindBestSupplier(iterActive->first, iterActive->second, oldSupplier, callback); - continue; - } - - SendBestScoreAllNetwork(iterActive->first, score, bestSupplier->GetSupplierId()); - if (bestSupplier == oldSupplier) { - continue; - } - if (oldSupplier) { - oldSupplier->RemoveBestRequest(iterActive->first); - } - iterActive->second->SetServiceSupply(bestSupplier); - CallbackForAvailable(bestSupplier, callback); - bestSupplier->SelectAsBestNetwork(iterActive->first); - } -} - -uint32_t NetConnService::FindBestNetworkForRequest(sptr &supplier, sptr &netActivateNetwork) -{ - int bestScore = 0; - if (netActivateNetwork == nullptr) { - NETMGR_LOG_E("netActivateNetwork is null"); - return bestScore; - } - NETMGR_LOG_I("FindBestNetworkForRequest Enter, request is [%{public}s]", - netActivateNetwork->GetNetSpecifier() ? netActivateNetwork->GetNetSpecifier()->ToString(" ").c_str() - : "null"); - NET_SUPPLIER_MAP::iterator iter; - for (iter = netSuppliers_.begin(); iter != netSuppliers_.end(); ++iter) { - if (iter->second == nullptr) { - continue; - } - NETMGR_LOG_D("supplier info, supplier[%{public}d, %{public}s], realScore[%{public}d], isConnected[%{public}d]", - iter->second->GetSupplierId(), iter->second->GetNetSupplierIdent().c_str(), - iter->second->GetRealScore(), iter->second->IsConnected()); - if ((!netActivateNetwork->MatchRequestAndNetwork(iter->second)) || (!iter->second->IsConnected())) { - NETMGR_LOG_D("supplier[%{public}d] is not connected or not match request.", iter->second->GetSupplierId()); - continue; - } - int score = iter->second->GetRealScore(); - if (score > bestScore) { - bestScore = score; - supplier = iter->second; - } - } - NETMGR_LOG_I("FindBestNetworkForRequest exit, bestScore[%{public}d], bestSupplier[%{public}d, %{public}s]", - bestScore, supplier ? supplier->GetSupplierId() : 0, - supplier ? supplier->GetNetSupplierIdent().c_str() : "null"); - return bestScore; -} - void NetConnService::SendAllRequestToNetwork(sptr supplier) { NETMGR_LOG_I("SendAllRequestToNetwork."); @@ -937,7 +831,6 @@ void NetConnService::SendAllRequestToNetwork(sptr supplier) return; } NET_ACTIVATE_MAP::iterator iter; - std::lock_guard locker(netManagerMutex_); for (iter = netActivates_.begin(); iter != netActivates_.end(); ++iter) { if (iter->second == nullptr) { continue; @@ -1060,6 +953,188 @@ void NetConnService::CallbackForAvailable(sptr &supplier, const spt callback->NetConnectionPropertiesChange(netHandle, pInfo); } +void NetConnService::MakeDefaultNetWork(sptr &oldSupplier, sptr &newSupplier) +{ + NETMGR_LOG_I("MakeDefaultNetWork in, oldSupplier[%{public}d, %{public}s], newSupplier[%{public}d, %{public}s]", + oldSupplier ? oldSupplier->GetSupplierId() : 0, + oldSupplier ? oldSupplier->GetNetSupplierIdent().c_str() : "null", + newSupplier ? newSupplier->GetSupplierId() : 0, + newSupplier ? newSupplier->GetNetSupplierIdent().c_str() : "null"); + if (oldSupplier == newSupplier) { + NETMGR_LOG_D("old supplier equal to new supplier."); + return; + } + if (oldSupplier != nullptr) { + oldSupplier->ClearDefault(); + } + if (newSupplier != nullptr) { + newSupplier->SetDefault(); + } + std::lock_guard locker(netManagerMutex_); + oldSupplier = newSupplier; + NETMGR_LOG_I("Default supplier set to: [%{public}d, %{public}s]", oldSupplier ? oldSupplier->GetSupplierId() : 0, + oldSupplier ? oldSupplier->GetNetSupplierIdent().c_str() : "null"); +} + +void NetConnService::HandleDetectionResult(uint32_t supplierId, bool ifValid) +{ + NETMGR_LOG_I("Enter HandleDetectionResult, ifValid[%{public}d]", ifValid); + auto supplier = FindNetSupplier(supplierId); + if (supplier == nullptr) { + NETMGR_LOG_E("supplier doesn't exist."); + return; + } + supplier->SetNetValid(ifValid); + CallbackForSupplier(supplier, CALL_TYPE_UPDATE_CAP); + if (!netScore_->GetServiceScore(supplier)) { + NETMGR_LOG_E("GetServiceScore fail."); + return; + } + FindBestNetworkForAllRequest(); +} + +std::list> NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident) +{ + std::lock_guard locker(netManagerMutex_); + std::list> ret; + for (const auto &netSupplier : netSuppliers_) { + if (netSupplier.second == nullptr) { + continue; + } + if ((bearerType != netSupplier.second->GetNetSupplierType())) { + continue; + } + if (!ident.empty() && netSupplier.second->GetNetSupplierIdent() != ident) { + continue; + } + ret.push_back(netSupplier.second); + } + return ret; +} + +sptr NetConnService::GetNetSupplierFromList(NetBearType bearerType, const std::string &ident, + const std::set &netCaps) +{ + std::lock_guard locker(netManagerMutex_); + for (const auto &netSupplier : netSuppliers_) { + if (netSupplier.second == nullptr) { + continue; + } + if ((bearerType == netSupplier.second->GetNetSupplierType()) && + (ident == netSupplier.second->GetNetSupplierIdent()) && netSupplier.second->CompareNetCaps(netCaps)) { + return netSupplier.second; + } + } + return nullptr; +} + +int32_t NetConnService::GetDefaultNet(int32_t &netId) +{ + std::lock_guard locker(netManagerMutex_); + if (!defaultNetSupplier_) { + NETMGR_LOG_E("not found the netId"); + return NETMANAGER_SUCCESS; + } + + netId = defaultNetSupplier_->GetNetId(); + NETMGR_LOG_D("GetDefaultNet found the netId: [%{public}d]", netId); + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::GetAddressesByName(const std::string &host, int32_t netId, std::vector &addrList) +{ + return NetManagerCenter::GetInstance().GetAddressesByName(host, static_cast(netId), addrList); +} + +int32_t NetConnService::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) +{ + std::vector addrList; + int ret = GetAddressesByName(host, netId, addrList); + if (ret == NETMANAGER_SUCCESS) { + if (!addrList.empty()) { + addr = addrList[0]; + return ret; + } + return ERR_NO_ADDRESS; + } + return ret; +} + +int32_t NetConnService::GetSpecificNet(NetBearType bearerType, std::list &netIdList) +{ + if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) { + NETMGR_LOG_E("netType parameter invalid"); + return ERR_INVALID_NETORK_TYPE; + } + + std::lock_guard locker(netManagerMutex_); + NET_SUPPLIER_MAP::iterator iterSupplier; + for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { + if (iterSupplier->second == nullptr) { + continue; + } + auto supplierType = iterSupplier->second->GetNetSupplierType(); + if (bearerType == supplierType) { + netIdList.push_back(iterSupplier->second->GetNetId()); + } + } + NETMGR_LOG_D("netSuppliers_ size[%{public}zd] networks_ size[%{public}zd]", netSuppliers_.size(), networks_.size()); + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::GetAllNets(std::list &netIdList) +{ + std::lock_guard locker(netManagerMutex_); + for (const auto &network : networks_) { + if (network.second != nullptr && network.second->IsConnected()) { + netIdList.push_back(network.second->GetNetId()); + } + } + NETMGR_LOG_D("netSuppliers_ size[%{public}zd] netIdList size[%{public}zd]", netSuppliers_.size(), netIdList.size()); + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::GetSpecificUidNet(int32_t uid, int32_t &netId) +{ + NETMGR_LOG_D("Enter GetSpecificUidNet, uid is [%{public}d].", uid); + std::lock_guard locker(netManagerMutex_); + netId = INVALID_NET_ID; + NET_SUPPLIER_MAP::iterator iterSupplier; + for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { + if ((iterSupplier->second != nullptr) && (uid == iterSupplier->second->GetSupplierUid()) && + (iterSupplier->second->GetNetSupplierType() == BEARER_VPN)) { + netId = iterSupplier->second->GetNetId(); + return NETMANAGER_SUCCESS; + } + } + return GetDefaultNet(netId); +} + +int32_t NetConnService::GetConnectionProperties(int32_t netId, NetLinkInfo &info) +{ + std::lock_guard locker(netManagerMutex_); + auto iterNetwork = networks_.find(netId); + if ((iterNetwork == networks_.end()) || (iterNetwork->second == nullptr)) { + return ERR_NO_NETWORK; + } + + info = iterNetwork->second->GetNetLinkInfo(); + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap) +{ + std::lock_guard locker(netManagerMutex_); + NET_SUPPLIER_MAP::iterator iterSupplier; + for (iterSupplier = netSuppliers_.begin(); iterSupplier != netSuppliers_.end(); ++iterSupplier) { + if ((iterSupplier->second != nullptr) && (netId == iterSupplier->second->GetNetId())) { + netAllCap = iterSupplier->second->GetNetCapabilities(); + return NETMANAGER_SUCCESS; + } + } + return ERR_NO_NETWORK; +} + int32_t NetConnService::GetIfaceNames(NetBearType bearerType, std::list &ifaceNames) { if (bearerType < BEARER_CELLULAR || bearerType >= BEARER_DEFAULT) { @@ -1080,7 +1155,7 @@ int32_t NetConnService::GetIfaceNames(NetBearType bearerType, std::listGetNetLinkInfo().ifaceName_; - return ERR_NONE; + return NETMANAGER_SUCCESS; } -void NetConnService::HandleDetectionResult(uint32_t supplierId, bool ifValid) +int32_t NetConnService::GetHttpProxy(std::string &httpProxy) { - NETMGR_LOG_I("Enter HandleDetectionResult, ifValid[%{public}d]", ifValid); - auto supplier = FindNetSupplier(supplierId); - if (supplier == nullptr) { - NETMGR_LOG_E("supplier doesn't exist."); - return; - } - supplier->SetNetValid(ifValid); - std::unique_lock locker(netManagerMutex_); - CallbackForSupplier(supplier, CALL_TYPE_UPDATE_CAP); - locker.unlock(); - if (!netScore_->GetServiceScore(supplier)) { - NETMGR_LOG_E("GetServiceScore fail."); - return; + if (httpProxy_.empty()) { + NETMGR_LOG_E("The httpProxy in service is null"); + return ERR_NO_HTTP_PROXY; } - FindBestNetworkForAllRequest(); -} -int32_t NetConnService::SetAirplaneMode(bool state) -{ - BroadcastInfo info; - info.action = EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED; - info.data = "Net Manager Airplane Mode Changed"; - info.code = static_cast(state); - info.ordered = true; - std::map param; - DelayedSingleton::GetInstance()->SendBroadcast(info, param); - return 0; + httpProxy = httpProxy_; + return NETMANAGER_SUCCESS; } -int32_t NetConnService::Dump(int32_t fd, const std::vector &args) +int32_t NetConnService::GetNetIdByIdentifier(const std::string &ident, int32_t &netId) { - NETMGR_LOG_D("Start Dump, fd: %{public}d", fd); - std::string result; - GetDumpMessage(result); - int32_t ret = dprintf(fd, "%s\n", result.c_str()); - return ret < 0 ? static_cast(NetConnResultCode::NET_CONN_ERR_INTERNAL_ERROR) - : static_cast(NetConnResultCode::NET_CONN_SUCCESS); + if (ident.empty()) { + NETMGR_LOG_E("The identifier in service is null"); + return ERR_NO_NET_IDENT; + } + std::lock_guard locker(netManagerMutex_); + for (auto iterSupplier : netSuppliers_) { + if (iterSupplier.second == nullptr) { + continue; + } + if (iterSupplier.second->GetNetSupplierIdent() == ident) { + netId = iterSupplier.second->GetNetId(); + break; + } + } + return NETMANAGER_SUCCESS; } void NetConnService::GetDumpMessage(std::string &message) { message.append("Net connect Info:\n"); + std::lock_guard locker(netManagerMutex_); if (defaultNetSupplier_) { message.append("\tSupplierId: " + std::to_string(defaultNetSupplier_->GetSupplierId()) + "\n"); std::shared_ptr network = defaultNetSupplier_->GetNetwork(); @@ -1184,44 +1249,64 @@ void NetConnService::GetDumpMessage(std::string &message) } } -int32_t NetConnService::SetHttpProxy(const std::string &httpProxy) +int32_t NetConnService::HasDefaultNet(bool &flag) { - if (httpProxy.empty()) { - NETMGR_LOG_E("The httpProxy set to service is null"); - return ERR_HTTP_PROXY_INVALID; + std::lock_guard locker(netManagerMutex_); + if (!defaultNetSupplier_) { + flag = false; + return NETMANAGER_SUCCESS; } - - httpProxy_ = httpProxy; - return ERR_NONE; + flag = true; + return NETMANAGER_SUCCESS; } -int32_t NetConnService::GetHttpProxy(std::string &httpProxy) +int32_t NetConnService::IsDefaultNetMetered(bool &isMetered) { - if (httpProxy_.empty()) { - NETMGR_LOG_E("The httpProxy in service is null"); - return ERR_NO_HTTP_PROXY; + std::lock_guard locker(netManagerMutex_); + if (defaultNetSupplier_) { + isMetered = !defaultNetSupplier_->HasNetCap(NET_CAPABILITY_NOT_METERED); + } else { + isMetered = true; } + return NETMANAGER_SUCCESS; +} - httpProxy = httpProxy_; - return ERR_NONE; +int32_t NetConnService::BindSocket(int32_t socket_fd, int32_t netId) +{ + NETMGR_LOG_D("Enter BindSocket."); + return NetsysController::GetInstance().BindSocket(socket_fd, netId); } -int32_t NetConnService::GetNetIdByIdentifier(const std::string &ident, int32_t &netId) +int32_t NetConnService::Dump(int32_t fd, const std::vector &args) { - if (ident.empty()) { - NETMGR_LOG_E("The identifier in service is null"); - return ERR_NO_NET_IDENT; - } - for (auto iterSupplier : netSuppliers_) { - if (iterSupplier.second == nullptr) { - continue; - } - if (iterSupplier.second->GetNetSupplierIdent() == ident) { - netId = iterSupplier.second->GetNetId(); - break; - } + NETMGR_LOG_D("Start Dump, fd: %{public}d", fd); + std::string result; + GetDumpMessage(result); + int32_t ret = dprintf(fd, "%s\n", result.c_str()); + return ret < 0 ? static_cast(NetConnResultCode::NET_CONN_ERR_INTERNAL_ERROR) + : static_cast(NetConnResultCode::NET_CONN_SUCCESS); +} + +int32_t NetConnService::SetAirplaneMode(bool state) +{ + BroadcastInfo info; + info.action = EventFwk::CommonEventSupport::COMMON_EVENT_AIRPLANE_MODE_CHANGED; + info.data = "Net Manager Airplane Mode Changed"; + info.code = static_cast(state); + info.ordered = true; + std::map param; + DelayedSingleton::GetInstance()->SendBroadcast(info, param); + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::SetHttpProxy(const std::string &httpProxy) +{ + int32_t result = NETMANAGER_ERROR; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask( + [this, &httpProxy, &result]() { result = this->SetHttpProxyAsync(httpProxy); }); } - return ERR_NONE; + return result; } } // namespace NetManagerStandard } // namespace OHOS diff --git a/services/netconnmanager/src/net_conn_service_iface.cpp b/services/netconnmanager/src/net_conn_service_iface.cpp index 0b1784bbc764099cab9d9f6c84ca46f8737b1606..de493a9e5e761a01b0d01b65ff9cbce06d16c0b3 100644 --- a/services/netconnmanager/src/net_conn_service_iface.cpp +++ b/services/netconnmanager/src/net_conn_service_iface.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "net_conn_service_iface.h" #include "net_conn_service.h" @@ -22,17 +23,16 @@ int32_t NetConnServiceIface::GetIfaceNames(NetBearType bearerType, std::list::GetInstance()->GetIfaceNames(bearerType, ifaceNames); } -int32_t NetConnServiceIface::GetIfaceNameByType( - NetBearType bearerType, const std::string &ident, std::string &ifaceName) +int32_t NetConnServiceIface::GetIfaceNameByType(NetBearType bearerType, const std::string &ident, + std::string &ifaceName) { return DelayedSingleton::GetInstance()->GetIfaceNameByType(bearerType, ident, ifaceName); } -int32_t NetConnServiceIface::RegisterNetSupplier(NetBearType bearerType, - const std::string &ident, const std::set &netCaps, uint32_t &supplierId) +int32_t NetConnServiceIface::RegisterNetSupplier(NetBearType bearerType, const std::string &ident, + const std::set &netCaps, uint32_t &supplierId) { - return DelayedSingleton::GetInstance()->RegisterNetSupplier( - bearerType, ident, netCaps, supplierId); + return DelayedSingleton::GetInstance()->RegisterNetSupplier(bearerType, ident, netCaps, supplierId); } int32_t NetConnServiceIface::UnregisterNetSupplier(uint32_t supplierId) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 9cd3a247f311ffa3777c42d314b252e06f62c652..73e1fd5de3c622f311a0477dd9945e92e4f672a0 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -55,13 +55,6 @@ Network::Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler & { } -Network::~Network() -{ - if (!ReleaseBasicNetwork()) { - NETMGR_LOG_E("ReleaseBasicNetwork fail."); - } -} - int32_t Network::GetNetId() const { return netId_; @@ -103,9 +96,7 @@ bool Network::ReleaseBasicNetwork() NETMGR_LOG_D("Enter ReleaseBasicNetwork"); if (isPhyNetCreated_) { NETMGR_LOG_D("Destroy physical network"); - if (eventHandler_) { - eventHandler_->PostAsyncTask([this]() { this->StopNetDetection(); }, 0); - } + StopNetDetection(); for (const auto &inetAddr : netLinkInfo_.netAddrList_) { int32_t prefixLen = inetAddr.prefixlen_; if (prefixLen == 0) { @@ -289,9 +280,11 @@ int32_t Network::UnRegisterNetDetectionCallback(const sptrAsObject().GetRefPtr() == (*iter)->AsObject().GetRefPtr()) { + netDetectionRetCallback_.erase(iter); + return ERR_NONE; + } } return ERR_NONE; @@ -300,16 +293,10 @@ int32_t Network::UnRegisterNetDetectionCallback(const sptrPostAsyncTask( - [report = needReport, this]() { - if (report) { - this->StopNetDetection(); - } - this->InitNetMonitor(); - }, - 0); + if (needReport) { + StopNetDetection(); } + InitNetMonitor(); } void Network::StopNetDetection() diff --git a/services/netconnmanager/src/stub/net_conn_service_stub.cpp b/services/netconnmanager/src/stub/net_conn_service_stub.cpp index 6ca6a674afe97e386d8f710c46467470b43b7008..af8ee3d41f8eccb99b6129074fad837c6b309e51 100644 --- a/services/netconnmanager/src/stub/net_conn_service_stub.cpp +++ b/services/netconnmanager/src/stub/net_conn_service_stub.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "netmanager_base_permission.h" #include "net_conn_constants.h" #include "net_conn_service_stub.h" #include "net_conn_types.h" @@ -26,38 +27,47 @@ static constexpr uint32_t MAX_NET_CAP_NUM = 32; NetConnServiceStub::NetConnServiceStub() { - memberFuncMap_[CMD_NM_SYSTEM_READY] = &NetConnServiceStub::OnSystemReady; - memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK] = &NetConnServiceStub::OnRegisterNetConnCallback; - memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK_BY_SPECIFIER] = - &NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier; - memberFuncMap_[CMD_NM_UNREGISTER_NET_CONN_CALLBACK] = &NetConnServiceStub::OnUnregisterNetConnCallback; - memberFuncMap_[CMD_NM_UPDATE_NET_STATE_FOR_TEST] = &NetConnServiceStub::OnUpdateNetStateForTest; - memberFuncMap_[CMD_NM_REG_NET_SUPPLIER] = &NetConnServiceStub::OnRegisterNetSupplier; - memberFuncMap_[CMD_NM_UNREG_NETWORK] = &NetConnServiceStub::OnUnregisterNetSupplier; - memberFuncMap_[CMD_NM_SET_NET_SUPPLIER_INFO] = &NetConnServiceStub::OnUpdateNetSupplierInfo; - memberFuncMap_[CMD_NM_SET_NET_LINK_INFO] = &NetConnServiceStub::OnUpdateNetLinkInfo; - memberFuncMap_[CMD_NM_REGISTER_NET_DETECTION_RET_CALLBACK] = &NetConnServiceStub::OnRegisterNetDetectionCallback; - memberFuncMap_[CMD_NM_UNREGISTER_NET_DETECTION_RET_CALLBACK] = - &NetConnServiceStub::OnUnRegisterNetDetectionCallback; - memberFuncMap_[CMD_NM_NET_DETECTION] = &NetConnServiceStub::OnNetDetection; - memberFuncMap_[CMD_NM_GET_IFACE_NAMES] = &NetConnServiceStub::OnGetIfaceNames; - memberFuncMap_[CMD_NM_GET_IFACENAME_BY_TYPE] = &NetConnServiceStub::OnGetIfaceNameByType; - memberFuncMap_[CMD_NM_GETDEFAULTNETWORK] = &NetConnServiceStub::OnGetDefaultNet; - memberFuncMap_[CMD_NM_HASDEFAULTNET] = &NetConnServiceStub::OnHasDefaultNet; - memberFuncMap_[CMD_NM_GET_SPECIFIC_NET] = &NetConnServiceStub::OnGetSpecificNet; - memberFuncMap_[CMD_NM_GET_ALL_NETS] = &NetConnServiceStub::OnGetAllNets; - memberFuncMap_[CMD_NM_GET_SPECIFIC_UID_NET] = &NetConnServiceStub::OnGetSpecificUidNet; - memberFuncMap_[CMD_NM_GET_CONNECTION_PROPERTIES] = &NetConnServiceStub::OnGetConnectionProperties; - memberFuncMap_[CMD_NM_GET_NET_CAPABILITIES] = &NetConnServiceStub::OnGetNetCapabilities; - memberFuncMap_[CMD_NM_GET_ADDRESSES_BY_NAME] = &NetConnServiceStub::OnGetAddressesByName; - memberFuncMap_[CMD_NM_GET_ADDRESS_BY_NAME] = &NetConnServiceStub::OnGetAddressByName; - memberFuncMap_[CMD_NM_BIND_SOCKET] = &NetConnServiceStub::OnBindSocket; - memberFuncMap_[CMD_NM_REGISTER_NET_SUPPLIER_CALLBACK] = &NetConnServiceStub::OnRegisterNetSupplierCallback; - memberFuncMap_[CMD_NM_SET_AIRPLANE_MODE] = &NetConnServiceStub::OnSetAirplaneMode; - memberFuncMap_[CMD_NM_IS_DEFAULT_NET_METERED] = &NetConnServiceStub::OnIsDefaultNetMetered; - memberFuncMap_[CMD_NM_SET_HTTP_PROXY] = &NetConnServiceStub::OnSetHttpProxy; - memberFuncMap_[CMD_NM_GET_HTTP_PROXY] = &NetConnServiceStub::OnGetHttpProxy; - memberFuncMap_[CMD_NM_GET_NET_ID_BY_IDENTIFIER] = &NetConnServiceStub::OnGetNetIdByIdentifier; + memberFuncMap_[CMD_NM_SYSTEM_READY] = {&NetConnServiceStub::OnSystemReady, {}}; + memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK] = {&NetConnServiceStub::OnRegisterNetConnCallback, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_REGISTER_NET_CONN_CALLBACK_BY_SPECIFIER] = { + &NetConnServiceStub::OnRegisterNetConnCallbackBySpecifier, {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_UNREGISTER_NET_CONN_CALLBACK] = {&NetConnServiceStub::OnUnregisterNetConnCallback, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_UPDATE_NET_STATE_FOR_TEST] = {&NetConnServiceStub::OnUpdateNetStateForTest, {}}; + memberFuncMap_[CMD_NM_REG_NET_SUPPLIER] = {&NetConnServiceStub::OnRegisterNetSupplier, {}}; + memberFuncMap_[CMD_NM_UNREG_NETWORK] = {&NetConnServiceStub::OnUnregisterNetSupplier, {}}; + memberFuncMap_[CMD_NM_SET_NET_SUPPLIER_INFO] = {&NetConnServiceStub::OnUpdateNetSupplierInfo, {}}; + memberFuncMap_[CMD_NM_SET_NET_LINK_INFO] = {&NetConnServiceStub::OnUpdateNetLinkInfo, {}}; + memberFuncMap_[CMD_NM_REGISTER_NET_DETECTION_RET_CALLBACK] = {&NetConnServiceStub::OnRegisterNetDetectionCallback, + {}}; + memberFuncMap_[CMD_NM_UNREGISTER_NET_DETECTION_RET_CALLBACK] = { + &NetConnServiceStub::OnUnRegisterNetDetectionCallback, {}}; + memberFuncMap_[CMD_NM_NET_DETECTION] = {&NetConnServiceStub::OnNetDetection, + {Permission::GET_NETWORK_INFO, Permission::INTERNET}}; + memberFuncMap_[CMD_NM_GET_IFACE_NAMES] = {&NetConnServiceStub::OnGetIfaceNames, {}}; + memberFuncMap_[CMD_NM_GET_IFACENAME_BY_TYPE] = {&NetConnServiceStub::OnGetIfaceNameByType, {}}; + memberFuncMap_[CMD_NM_GETDEFAULTNETWORK] = {&NetConnServiceStub::OnGetDefaultNet, {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_HASDEFAULTNET] = {&NetConnServiceStub::OnHasDefaultNet, {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_GET_SPECIFIC_NET] = {&NetConnServiceStub::OnGetSpecificNet, {}}; + memberFuncMap_[CMD_NM_GET_ALL_NETS] = {&NetConnServiceStub::OnGetAllNets, {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_GET_SPECIFIC_UID_NET] = {&NetConnServiceStub::OnGetSpecificUidNet, {}}; + memberFuncMap_[CMD_NM_GET_CONNECTION_PROPERTIES] = {&NetConnServiceStub::OnGetConnectionProperties, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_GET_NET_CAPABILITIES] = {&NetConnServiceStub::OnGetNetCapabilities, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_GET_ADDRESSES_BY_NAME] = {&NetConnServiceStub::OnGetAddressesByName, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_GET_ADDRESS_BY_NAME] = {&NetConnServiceStub::OnGetAddressByName, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_BIND_SOCKET] = {&NetConnServiceStub::OnBindSocket, {}}; + memberFuncMap_[CMD_NM_REGISTER_NET_SUPPLIER_CALLBACK] = {&NetConnServiceStub::OnRegisterNetSupplierCallback, {}}; + memberFuncMap_[CMD_NM_SET_AIRPLANE_MODE] = {&NetConnServiceStub::OnSetAirplaneMode, {}}; + memberFuncMap_[CMD_NM_IS_DEFAULT_NET_METERED] = {&NetConnServiceStub::OnIsDefaultNetMetered, + {Permission::GET_NETWORK_INFO}}; + memberFuncMap_[CMD_NM_SET_HTTP_PROXY] = {&NetConnServiceStub::OnSetHttpProxy, {}}; + memberFuncMap_[CMD_NM_GET_HTTP_PROXY] = {&NetConnServiceStub::OnGetHttpProxy, {}}; + memberFuncMap_[CMD_NM_GET_NET_ID_BY_IDENTIFIER] = {&NetConnServiceStub::OnGetNetIdByIdentifier, {}}; } NetConnServiceStub::~NetConnServiceStub() {} @@ -83,8 +93,11 @@ int32_t NetConnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, auto itFunc = memberFuncMap_.find(code); if (itFunc != memberFuncMap_.end()) { - auto requestFunc = itFunc->second; + auto requestFunc = itFunc->second.first; if (requestFunc != nullptr) { + if (!CheckPermission(itFunc->second.second)) { + return NETMANAGER_ERR_PERMISSION_DENIED; + } return (this->*requestFunc)(data, reply); } } @@ -93,10 +106,20 @@ int32_t NetConnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } +bool NetConnServiceStub::CheckPermission(const std::set &permissions) +{ + for (const auto &permission : permissions) { + if (!NetManagerPermission::CheckPermission(permission)) { + return false; + } + } + return true; +} + int32_t NetConnServiceStub::OnSystemReady(MessageParcel &data, MessageParcel &reply) { SystemReady(); - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnRegisterNetSupplier(MessageParcel &data, MessageParcel &reply) @@ -133,13 +156,13 @@ int32_t NetConnServiceStub::OnRegisterNetSupplier(MessageParcel &data, MessagePa if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { NETMGR_LOG_E("supplierId[%{public}d].", supplierId); if (!reply.WriteUint32(supplierId)) { return ERR_FLATTEN_OBJECT; } } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnUnregisterNetSupplier(MessageParcel &data, MessageParcel &reply) @@ -154,7 +177,7 @@ int32_t NetConnServiceStub::OnUnregisterNetSupplier(MessageParcel &data, Message return ERR_FLATTEN_OBJECT; } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnRegisterNetSupplierCallback(MessageParcel &data, MessageParcel &reply) @@ -286,7 +309,7 @@ int32_t NetConnServiceStub::OnUpdateNetSupplierInfo(MessageParcel &data, Message } NETMGR_LOG_D("OnUpdateNetSupplierInfo out."); - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnUpdateNetLinkInfo(MessageParcel &data, MessageParcel &reply) @@ -304,7 +327,7 @@ int32_t NetConnServiceStub::OnUpdateNetLinkInfo(MessageParcel &data, MessageParc return ERR_FLATTEN_OBJECT; } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnRegisterNetDetectionCallback(MessageParcel &data, MessageParcel &reply) @@ -379,7 +402,7 @@ int32_t NetConnServiceStub::OnNetDetection(MessageParcel &data, MessageParcel &r if (!reply.WriteInt32(ret)) { return NET_CONN_ERR_INVALID_PARAMETER; } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnGetIfaceNames(MessageParcel &data, MessageParcel &reply) @@ -394,7 +417,7 @@ int32_t NetConnServiceStub::OnGetIfaceNames(MessageParcel &data, MessageParcel & if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { if (!reply.WriteUint32(ifaceNames.size())) { return ERR_FLATTEN_OBJECT; } @@ -426,7 +449,7 @@ int32_t NetConnServiceStub::OnGetIfaceNameByType(MessageParcel &data, MessagePar if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { if (!reply.WriteString(ifaceName)) { return ERR_FLATTEN_OBJECT; } @@ -437,7 +460,7 @@ int32_t NetConnServiceStub::OnGetIfaceNameByType(MessageParcel &data, MessagePar int32_t NetConnServiceStub::ConvertCode(int32_t internalCode) { switch (internalCode) { - case static_cast(ERR_NONE): + case static_cast(NETMANAGER_SUCCESS): return static_cast(NET_CONN_SUCCESS); case static_cast(ERR_INVALID_PARAMS): return static_cast(NET_CONN_ERR_INVALID_PARAMETER); @@ -496,12 +519,12 @@ int32_t NetConnServiceStub::OnHasDefaultNet(MessageParcel &data, MessageParcel & if (!reply.WriteInt32(result)) { return ERR_FLATTEN_OBJECT; } - if (result == ERR_NONE) { + if (result == NETMANAGER_SUCCESS) { if (!reply.WriteBool(flag)) { return ERR_FLATTEN_OBJECT; } } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnGetSpecificNet(MessageParcel &data, MessageParcel &reply) @@ -518,14 +541,14 @@ int32_t NetConnServiceStub::OnGetSpecificNet(MessageParcel &data, MessageParcel if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { - int32_t size = static_cast(netIdList.size()); + if (ret == NETMANAGER_SUCCESS) { + uint32_t size = static_cast(netIdList.size()); size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size; if (!reply.WriteInt32(size)) { return ERR_FLATTEN_OBJECT; } - int32_t index = 0; + uint32_t index = 0; for (auto p = netIdList.begin(); p != netIdList.end(); ++p) { if (++index > MAX_IFACE_NUM) { break; @@ -546,7 +569,7 @@ int32_t NetConnServiceStub::OnGetAllNets(MessageParcel &data, MessageParcel &rep if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { int32_t size = static_cast(netIdList.size()); if (!reply.WriteInt32(size)) { return ERR_FLATTEN_OBJECT; @@ -574,7 +597,7 @@ int32_t NetConnServiceStub::OnGetSpecificUidNet(MessageParcel &data, MessageParc if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { if (!reply.WriteInt32(netId)) { return ERR_FLATTEN_OBJECT; } @@ -595,7 +618,7 @@ int32_t NetConnServiceStub::OnGetConnectionProperties(MessageParcel &data, Messa if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { sptr netLinkInfo_ptr = new (std::nothrow) NetLinkInfo(info); if (!NetLinkInfo::Marshalling(reply, netLinkInfo_ptr)) { NETMGR_LOG_E("proxy Marshalling failed"); @@ -619,7 +642,7 @@ int32_t NetConnServiceStub::OnGetNetCapabilities(MessageParcel &data, MessagePar if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { if (!reply.WriteUint32(netAllCap.linkUpBandwidthKbps_) || !reply.WriteUint32(netAllCap.linkDownBandwidthKbps_)) { return IPC_PROXY_ERR; @@ -629,7 +652,7 @@ int32_t NetConnServiceStub::OnGetNetCapabilities(MessageParcel &data, MessagePar if (!reply.WriteUint32(size)) { return ERR_FLATTEN_OBJECT; } - int32_t index = 0; + uint32_t index = 0; for (auto netCap : netAllCap.netCaps_) { if (++index > MAX_NET_CAP_NUM) { break; @@ -673,13 +696,13 @@ int32_t NetConnServiceStub::OnGetAddressesByName(MessageParcel &data, MessagePar if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { - int32_t size = static_cast(addrList.size()); + if (ret == NETMANAGER_SUCCESS) { + uint32_t size = static_cast(addrList.size()); size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size; if (!reply.WriteInt32(size)) { return ERR_FLATTEN_OBJECT; } - int32_t index = 0; + uint32_t index = 0; for (auto p = addrList.begin(); p != addrList.end(); ++p) { if (++index > MAX_IFACE_NUM) { break; @@ -710,7 +733,7 @@ int32_t NetConnServiceStub::OnGetAddressByName(MessageParcel &data, MessageParce if (!reply.WriteInt32(ret)) { return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { sptr netaddr_ptr = (std::make_unique(addr)).release(); if (!INetAddr::Marshalling(reply, netaddr_ptr)) { NETMGR_LOG_E("proxy Marshalling failed"); @@ -760,12 +783,12 @@ int32_t NetConnServiceStub::OnIsDefaultNetMetered(MessageParcel &data, MessagePa if (!reply.WriteInt32(result)) { return NETMANAGER_ERR_WRITE_REPLY_FAIL; } - if (result == ERR_NONE) { + if (result == NETMANAGER_SUCCESS) { if (!reply.WriteBool(flag)) { return NETMANAGER_ERR_WRITE_REPLY_FAIL; } } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnSetHttpProxy(MessageParcel &data, MessageParcel &reply) @@ -792,14 +815,14 @@ int32_t NetConnServiceStub::OnGetHttpProxy(MessageParcel &data, MessageParcel &r return ERR_FLATTEN_OBJECT; } - if (result != ERR_NONE) { + if (result != NETMANAGER_SUCCESS) { return result; } if (!reply.WriteString(httpProxy)) { return ERR_FLATTEN_OBJECT; } - return ERR_NONE; + return NETMANAGER_SUCCESS; } int32_t NetConnServiceStub::OnGetNetIdByIdentifier(MessageParcel &data, MessageParcel &reply) @@ -816,7 +839,7 @@ int32_t NetConnServiceStub::OnGetNetIdByIdentifier(MessageParcel &data, MessageP return ERR_FLATTEN_OBJECT; } - if (ret == ERR_NONE) { + if (ret == NETMANAGER_SUCCESS) { if (!reply.WriteInt32(netId)) { return ERR_FLATTEN_OBJECT; } diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_client_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_client_test.cpp index cd8a65a438ea6e9c10c84fa451b913e6935775c8..a13e4c6c90affbc4aee3efd1593c772c9c00f8d9 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_client_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_client_test.cpp @@ -108,7 +108,7 @@ void NetConnClientTest::TearDown() {} /** * @tc.name: GetDefaultNetTest001 * @tc.desc: Test NetConnClient::GetDefaultNet, not applying for - * permission,return NET_CONN_ERR_PERMISSION_CHECK_FAILED + * permission,return NETMANAGER_ERR_PERMISSION_DENIED * @tc.type: FUNC */ HWTEST_F(NetConnClientTest, GetDefaultNetTest001, TestSize.Level1) @@ -145,14 +145,14 @@ HWTEST_F(NetConnClientTest, GetDefaultNetTest002, TestSize.Level1) /** * @tc.name: HasDefaultNetTest001 * @tc.desc: Test NetConnClient::HasDefaultNet,not applying for - * permission, return NET_CONN_ERR_PERMISSION_CHECK_FAILED + * permission, return NETMANAGER_ERR_PERMISSION_DENIED * @tc.type: FUNC */ HWTEST_F(NetConnClientTest, HasDefaultNetTest001, TestSize.Level1) { bool bFlag = false; auto ret = DelayedSingleton::GetInstance()->HasDefaultNet(bFlag); - ASSERT_TRUE(ret == NET_CONN_ERR_PERMISSION_CHECK_FAILED); + ASSERT_TRUE(ret == NETMANAGER_ERR_PERMISSION_DENIED); } /** @@ -172,8 +172,8 @@ HWTEST_F(NetConnClientTest, HasDefaultNetTest002, TestSize.Level1) /** * @tc.name: GetNetCapabilitiesTest001 * @tc.desc: Test NetConnClient::GetNetCapabilities, In the absence of - * permission, GetDefaultNet return NET_CONN_ERR_PERMISSION_CHECK_FAILED and - * GetNetCapabilities return NET_CONN_ERR_PERMISSION_CHECK_FAILED + * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED and + * GetNetCapabilities return NETMANAGER_ERR_PERMISSION_DENIED * @tc.type: FUNC */ HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest001, TestSize.Level1) @@ -185,13 +185,13 @@ HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest001, TestSize.Level1) NetAllCapabilities netAllCap; ret = DelayedSingleton::GetInstance()->GetNetCapabilities(handle, netAllCap); - ASSERT_TRUE(ret == NET_CONN_ERR_PERMISSION_CHECK_FAILED); + ASSERT_TRUE(ret == NETMANAGER_ERR_PERMISSION_DENIED); } /** * @tc.name: GetNetCapabilitiesTest002 * @tc.desc: Test NetConnClient::GetNetCapabilities:In the absence of - * permission, GetDefaultNet return NET_CONN_ERR_PERMISSION_CHECK_FAILED, and + * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED, and * after add permission GetNetCapabilities return NET_CONN_ERR_INVALID_NETWORK * @tc.type: FUNC */ diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_manager_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_manager_test.cpp index d4edb2487edf37e8ac006512accb80e7909c3af6..49983c2302ad573d5407876f34c93258b76ef087 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_manager_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_manager_test.cpp @@ -39,25 +39,45 @@ using Security::AccessToken::AccessTokenID; HapInfoParams testInfoParms = {.bundleName = "net_conn_manager_test", .userID = 1, .instIndex = 0, .appIDDesc = "test"}; -PermissionDef testPermDef = {.permissionName = "ohos.permission.GET_NETWORK_INFO", - .bundleName = "net_conn_manager_test", - .grantMode = 1, - .label = "label", - .labelId = 1, - .description = "Test net connect maneger", - .descriptionId = 1, - .availableLevel = APL_SYSTEM_BASIC}; - -PermissionStateFull testState = {.grantFlags = {2}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.GET_NETWORK_INFO", - .resDeviceID = {"local"}}; - -HapPolicyParams testPolicyPrams = {.apl = APL_SYSTEM_BASIC, - .domain = "test.domain", - .permList = {testPermDef}, - .permStateList = {testState}}; +PermissionDef testNetInfoPermDef = {.permissionName = "ohos.permission.GET_NETWORK_INFO", + .bundleName = "net_conn_manager_test", + .grantMode = 1, + .label = "label", + .labelId = 1, + .description = "Test net connect maneger", + .descriptionId = 1, + .availableLevel = APL_SYSTEM_BASIC}; + +PermissionDef testInternetPermDef = {.permissionName = "ohos.permission.INTERNET", + .bundleName = "net_conn_client_fuzzer", + .grantMode = 1, + .availableLevel = APL_SYSTEM_BASIC, + .label = "label", + .labelId = 1, + .description = "Test net connect maneger internet", + .descriptionId = 1}; + +PermissionStateFull testNetInfoState = {.grantFlags = {2}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .isGeneral = true, + .permissionName = "ohos.permission.GET_NETWORK_INFO", + .resDeviceID = {"local"}}; + +PermissionStateFull testInternetState = {.permissionName = "ohos.permission.INTERNET", + .isGeneral = true, + .resDeviceID = {"local"}, + .grantStatus = {PermissionState::PERMISSION_GRANTED}, + .grantFlags = {2}}; + +HapPolicyParams testNetInfoPolicyPrams = {.apl = APL_SYSTEM_BASIC, + .domain = "test.domain", + .permList = {testNetInfoPermDef}, + .permStateList = {testNetInfoState}}; + +HapPolicyParams testInternetPolicyPrams = {.apl = APL_SYSTEM_BASIC, + .domain = "test.domain", + .permList = {testNetInfoPermDef, testInternetPermDef}, + .permStateList = {testNetInfoState, testInternetState}}; } // namespace class NetConnManagerTest : public testing::Test { @@ -278,7 +298,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager006, TestSize.Level1) DelayedSingleton::GetInstance()->RegisterNetSupplier(bearerType, ident, netCaps, supplierId); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); sptr netSpecifier = (std::make_unique()).release(); netSpecifier->ident_ = ident; netSpecifier->SetCapabilities(netCaps); @@ -313,17 +333,15 @@ HWTEST_F(NetConnManagerTest, NetConnManager008, TestSize.Level1) if (proxy == nullptr) { return; } - + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testInternetPolicyPrams); std::list> netList; - client->GetAllNets(netList); + int32_t result = client->GetAllNets(netList); + std::cout << "netIdList size:" << netList.size() << std::endl; + ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); sptr detectionCallback = GetINetDetectionCallbackSample(); if (detectionCallback == nullptr) { return; } - - std::cout << "netIdList size:" << netList.size() << std::endl; - int32_t result = 0; - int32_t netId = 0; for (sptr netHandle : netList) { NetAllCapabilities netAllCap; client->GetNetCapabilities(*netHandle, netAllCap); @@ -331,7 +349,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager008, TestSize.Level1) if (netAllCap.bearerTypes_.find(BEARER_WIFI) == netAllCap.bearerTypes_.end()) { continue; } - netId = netHandle->GetNetId(); + int32_t netId = netHandle->GetNetId(); result = proxy->RegisterNetDetectionCallback(netId, detectionCallback); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); std::cout << "TestRegisterNetDetectionCallback netId:" << netId << " result:" << result << std::endl; @@ -473,7 +491,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager012, TestSize.Level1) ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); std::cout << "supplierId3 : " << supplierId3 << std::endl; - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); std::list> netList; result = DelayedSingleton::GetInstance()->GetAllNets(netList); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); @@ -499,7 +517,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager013, TestSize.Level1) ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); std::cout << "supplierId : " << supplierId << std::endl; - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); std::list> netList; result = DelayedSingleton::GetInstance()->GetAllNets(netList); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); @@ -527,9 +545,13 @@ HWTEST_F(NetConnManagerTest, NetConnManager014, TestSize.Level1) result = DelayedSingleton::GetInstance()->UpdateNetLinkInfo(supplierId, netLinkInfo); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); + int32_t netId = 0; + result = DelayedSingleton::GetInstance()->GetNetIdByIdentifier(ident, netId); + std::cout << "Identifier:" << ident << ", net id:" << netId << std::endl; + ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); NetLinkInfo info; - NetHandle netHandle(100); + NetHandle netHandle(netId); result = DelayedSingleton::GetInstance()->GetConnectionProperties(netHandle, info); std::cout << "result = " << result << std::endl; ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); @@ -543,7 +565,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager014, TestSize.Level1) */ HWTEST_F(NetConnManagerTest, NetConnManager015, TestSize.Level1) { - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); bool isMetered = false; int32_t result = DelayedSingleton::GetInstance()->IsDefaultNetMetered(isMetered); ASSERT_TRUE(result == NetConnResultCode::NET_CONN_SUCCESS); @@ -562,7 +584,7 @@ HWTEST_F(NetConnManagerTest, NetConnManager016, TestSize.Level1) if (proxy == nullptr) { return; } - OHOS::NetManagerStandard::AccessToken token(testInfoParms, testPolicyPrams); + OHOS::NetManagerStandard::AccessToken token(testInfoParms, testNetInfoPolicyPrams); int32_t result; std::list> netList; result = client->GetAllNets(netList); diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_iface_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_iface_test.cpp index d76953e99b545596d9a25a1fbad3d67b9e7c2d52..4296c3376ea7d2f01119492f48d90c5ac6b1b19b 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_iface_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_iface_test.cpp @@ -62,7 +62,7 @@ HWTEST_F(NetConnServiceIfaceTest, RegisterNetSupplierTest001, TestSize.Level1) { uint32_t supplierId = 0; int32_t ret = instance_.UnregisterNetSupplier(supplierId); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceIfaceTest, UpdateNetLinkInfoTest001, TestSize.Level1) @@ -70,7 +70,7 @@ HWTEST_F(NetConnServiceIfaceTest, UpdateNetLinkInfoTest001, TestSize.Level1) sptr netLinkInfo = new (std::nothrow) NetLinkInfo(); ASSERT_NE(netLinkInfo, nullptr); auto ret = instance_.UpdateNetLinkInfo(g_supplierId, netLinkInfo); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceIfaceTest, UpdateNetSupplierInfoTest001, TestSize.Level1) @@ -78,13 +78,13 @@ HWTEST_F(NetConnServiceIfaceTest, UpdateNetSupplierInfoTest001, TestSize.Level1) sptr netSupplierInfo = new (std::nothrow) NetSupplierInfo(); ASSERT_NE(netSupplierInfo, nullptr); auto ret = instance_.UpdateNetSupplierInfo(g_supplierId, netSupplierInfo); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceIfaceTest, RestrictBackgroundChangedTest001, TestSize.Level1) { int32_t ret = instance_.RestrictBackgroundChanged(false); - EXPECT_EQ(ret, NETMANAGER_SUCCESS); + EXPECT_EQ(ret, NETMANAGER_ERROR); } } // namespace NetManagerStandard diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp index 70a5951582bff84d8c104c8f6b85e8e1eba74c51..25a018241c3ae47ee02a2a80fa451d62bc88d941 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp @@ -30,46 +30,6 @@ namespace OHOS { namespace NetManagerStandard { namespace { using namespace testing::ext; -using namespace Security::AccessToken; -using Security::AccessToken::AccessTokenID; - -HapInfoParams testInfoParms = {.bundleName = "net_conn_service_test", .userID = 1, .instIndex = 0, .appIDDesc = "test"}; - -PermissionDef testPermDef = {.permissionName = "ohos.permission.GET_NETWORK_INFO", - .bundleName = "net_conn_service_test", - .grantMode = 1, - .label = "label", - .labelId = 1, - .description = "Test net connect maneger", - .descriptionId = 1, - .availableLevel = APL_SYSTEM_BASIC}; - -PermissionStateFull testState = {.grantFlags = {2}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.GET_NETWORK_INFO", - .resDeviceID = {"local"}}; - -PermissionDef testPermDef2 = {.permissionName = "ohos.permission.INTERNET", - .bundleName = "net_conn_service_test", - .grantMode = 1, - .label = "label", - .labelId = 1, - .description = "Test net connect maneger", - .descriptionId = 1, - .availableLevel = APL_SYSTEM_BASIC}; - -PermissionStateFull testState2 = {.grantFlags = {2}, - .grantStatus = {PermissionState::PERMISSION_GRANTED}, - .isGeneral = true, - .permissionName = "ohos.permission.INTERNET", - .resDeviceID = {"local"}}; - -HapPolicyParams testPolicyPrams = {.apl = APL_SYSTEM_BASIC, - .domain = "test.domain", - .permList = {testPermDef, testPermDef2}, - .permStateList = {testState, testState2}}; - constexpr const char *TEST_IDENT = "testIdent"; constexpr uint32_t TEST_TIMEOUTMS = 1000; constexpr const char *TEST_HOST = "testHost"; @@ -119,26 +79,6 @@ sptr g_callback = new (std::nothrow) NetConnTestCallback(); uint32_t g_supplierId = 0; } // namespace -class AccessToken { -public: - AccessToken() - { - currentID_ = GetSelfTokenID(); - AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams); - accessID_ = tokenIdEx.tokenIdExStruct.tokenID; - SetSelfTokenID(accessID_); - } - ~AccessToken() - { - AccessTokenKit::DeleteToken(accessID_); - SetSelfTokenID(currentID_); - } - -private: - AccessTokenID currentID_ = 0; - AccessTokenID accessID_ = 0; -}; - class NetConnServiceTest : public testing::Test { public: static void SetUpTestCase(); @@ -182,7 +122,7 @@ HWTEST_F(NetConnServiceTest, RegisterNetSupplierCallbackTest001, TestSize.Level1 ASSERT_NE(callback, nullptr); std::set netCaps; auto ret = DelayedSingleton::GetInstance()->RegisterNetSupplierCallback(g_supplierId, callback); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, UpdateNetSupplierInfoTest001, TestSize.Level1) @@ -190,7 +130,7 @@ HWTEST_F(NetConnServiceTest, UpdateNetSupplierInfoTest001, TestSize.Level1) sptr netSupplierInfo = new (std::nothrow) NetSupplierInfo(); ASSERT_NE(netSupplierInfo, nullptr); auto ret = DelayedSingleton::GetInstance()->UpdateNetSupplierInfo(g_supplierId, netSupplierInfo); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, UpdateNetLinkInfoTest001, TestSize.Level1) @@ -198,70 +138,38 @@ HWTEST_F(NetConnServiceTest, UpdateNetLinkInfoTest001, TestSize.Level1) sptr netLinkInfo = new (std::nothrow) NetLinkInfo(); ASSERT_NE(netLinkInfo, nullptr); auto ret = DelayedSingleton::GetInstance()->UpdateNetLinkInfo(g_supplierId, netLinkInfo); - EXPECT_EQ(ret, ERR_NO_SUPPLIER); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, RegisterNetConnCallbackTest001, TestSize.Level1) { - sptr callback = new (std::nothrow) NetConnTestCallback(); - ASSERT_NE(callback, nullptr); - auto ret = DelayedSingleton::GetInstance()->RegisterNetConnCallback(callback); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, RegisterNetConnCallbackTest002, TestSize.Level1) -{ - AccessToken token; auto ret = DelayedSingleton::GetInstance()->RegisterNetConnCallback(g_callback); - EXPECT_EQ(ret, NETMANAGER_SUCCESS); + EXPECT_EQ(ret, NETMANAGER_ERROR); } -HWTEST_F(NetConnServiceTest, RegisterNetConnCallbackTest003, TestSize.Level1) +HWTEST_F(NetConnServiceTest, UnregisterNetConnCallbackTest001, TestSize.Level1) { - sptr netSpecifier = new (std::nothrow) NetSpecifier(); - ASSERT_NE(netSpecifier, nullptr); - sptr callback = new (std::nothrow) NetConnTestCallback(); - ASSERT_NE(callback, nullptr); - auto ret = DelayedSingleton::GetInstance()->RegisterNetConnCallback(netSpecifier, callback, - TEST_TIMEOUTMS); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); + auto ret = DelayedSingleton::GetInstance()->UnregisterNetConnCallback(g_callback); + EXPECT_EQ(ret, NETMANAGER_ERROR); } -HWTEST_F(NetConnServiceTest, RegisterNetConnCallbackTest004, TestSize.Level1) +HWTEST_F(NetConnServiceTest, RegisterNetConnCallbackTest002, TestSize.Level1) { - AccessToken token; sptr netSpecifier = new (std::nothrow) NetSpecifier(); ASSERT_NE(netSpecifier, nullptr); - sptr callback = new (std::nothrow) NetConnTestCallback(); - ASSERT_NE(callback, nullptr); - auto ret = DelayedSingleton::GetInstance()->RegisterNetConnCallback(netSpecifier, callback, + auto ret = DelayedSingleton::GetInstance()->RegisterNetConnCallback(netSpecifier, g_callback, TEST_TIMEOUTMS); - EXPECT_EQ(ret, NETMANAGER_SUCCESS); -} - -HWTEST_F(NetConnServiceTest, UnregisterNetConnCallbackTest001, TestSize.Level1) -{ - auto ret = DelayedSingleton::GetInstance()->UnregisterNetConnCallback(g_callback); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, UnregisterNetConnCallbackTest002, TestSize.Level1) { - AccessToken token; auto ret = DelayedSingleton::GetInstance()->UnregisterNetConnCallback(g_callback); - EXPECT_EQ(ret, NETMANAGER_SUCCESS); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, GetAllNetsTest001, TestSize.Level1) { - std::list netIdList; - auto ret = DelayedSingleton::GetInstance()->GetAllNets(netIdList); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, GetAllNetsTest002, TestSize.Level1) -{ - AccessToken token; std::list netIdList; auto ret = DelayedSingleton::GetInstance()->GetAllNets(netIdList); EXPECT_EQ(ret, NETMANAGER_SUCCESS); @@ -269,14 +177,6 @@ HWTEST_F(NetConnServiceTest, GetAllNetsTest002, TestSize.Level1) HWTEST_F(NetConnServiceTest, GetConnectionPropertiesTest001, TestSize.Level1) { - NetLinkInfo info; - auto ret = DelayedSingleton::GetInstance()->GetConnectionProperties(TEST_NETID, info); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, GetConnectionPropertiesTest002, TestSize.Level1) -{ - AccessToken token; NetLinkInfo info; auto ret = DelayedSingleton::GetInstance()->GetConnectionProperties(TEST_NETID, info); EXPECT_EQ(ret, ERR_NO_NETWORK); @@ -284,14 +184,6 @@ HWTEST_F(NetConnServiceTest, GetConnectionPropertiesTest002, TestSize.Level1) HWTEST_F(NetConnServiceTest, GetAddressesByNameTest001, TestSize.Level1) { - std::vector addrList; - auto ret = DelayedSingleton::GetInstance()->GetAddressesByName(TEST_HOST, TEST_NETID, addrList); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, GetAddressesByNameTest002, TestSize.Level1) -{ - AccessToken token; std::vector addrList; auto ret = DelayedSingleton::GetInstance()->GetAddressesByName(TEST_HOST, TEST_NETID, addrList); EXPECT_EQ(ret, NETMANAGER_ERROR); @@ -299,14 +191,6 @@ HWTEST_F(NetConnServiceTest, GetAddressesByNameTest002, TestSize.Level1) HWTEST_F(NetConnServiceTest, GetAddressByNameTest001, TestSize.Level1) { - INetAddr addr; - auto ret = DelayedSingleton::GetInstance()->GetAddressByName(TEST_HOST, TEST_NETID, addr); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, GetAddressByNameTest002, TestSize.Level1) -{ - AccessToken token; INetAddr addr; auto ret = DelayedSingleton::GetInstance()->GetAddressByName(TEST_HOST, TEST_NETID, addr); EXPECT_EQ(ret, NETMANAGER_ERROR); @@ -321,14 +205,7 @@ HWTEST_F(NetConnServiceTest, BindSocketTest001, TestSize.Level1) HWTEST_F(NetConnServiceTest, NetDetectionTest001, TestSize.Level1) { auto ret = DelayedSingleton::GetInstance()->NetDetection(TEST_NETID); - EXPECT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, NetDetectionTest002, TestSize.Level1) -{ - AccessToken token; - auto ret = DelayedSingleton::GetInstance()->NetDetection(TEST_NETID); - EXPECT_EQ(ret, ERR_NET_NOT_FIND_NETID); + EXPECT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, GetNetIdByIdentifierTest001, TestSize.Level1) @@ -340,14 +217,6 @@ HWTEST_F(NetConnServiceTest, GetNetIdByIdentifierTest001, TestSize.Level1) HWTEST_F(NetConnServiceTest, GetDefaultNetTest001, TestSize.Level1) { - int32_t netId = 0; - auto ret = DelayedSingleton::GetInstance()->GetDefaultNet(netId); - ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED); -} - -HWTEST_F(NetConnServiceTest, GetDefaultNetTest002, TestSize.Level1) -{ - AccessToken token; int32_t netId = 0; auto ret = DelayedSingleton::GetInstance()->GetDefaultNet(netId); ASSERT_EQ(ret, ERR_NONE); @@ -355,14 +224,6 @@ HWTEST_F(NetConnServiceTest, GetDefaultNetTest002, TestSize.Level1) HWTEST_F(NetConnServiceTest, HasDefaultNetTest001, TestSize.Level1) { - bool bFlag = false; - auto ret = DelayedSingleton::GetInstance()->HasDefaultNet(bFlag); - ASSERT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, HasDefaultNetTest002, TestSize.Level1) -{ - AccessToken token; bool bFlag = false; auto ret = DelayedSingleton::GetInstance()->HasDefaultNet(bFlag); ASSERT_EQ(ret, NETMANAGER_SUCCESS); @@ -370,36 +231,9 @@ HWTEST_F(NetConnServiceTest, HasDefaultNetTest002, TestSize.Level1) HWTEST_F(NetConnServiceTest, GetNetCapabilitiesTest001, TestSize.Level1) { - NETMGR_LOG_D("GetNetCapabilitiesTest001 In"); - int32_t netId = 0; - int32_t ret = DelayedSingleton::GetInstance()->GetDefaultNet(netId); - ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED); - - NetAllCapabilities netAllCap; - ret = DelayedSingleton::GetInstance()->GetNetCapabilities(netId, netAllCap); - ASSERT_EQ(ret, ERR_PERMISSION_CHECK_FAIL); -} - -HWTEST_F(NetConnServiceTest, GetNetCapabilitiesTest002, TestSize.Level1) -{ - NETMGR_LOG_D("GetNetCapabilitiesTest002 In"); int32_t netId = 0; int32_t ret = DelayedSingleton::GetInstance()->GetDefaultNet(netId); - ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED); - - AccessToken token; - NetAllCapabilities netAllCap; - ret = DelayedSingleton::GetInstance()->GetNetCapabilities(netId, netAllCap); - ASSERT_EQ(ret, ERR_NO_NETWORK); -} - -HWTEST_F(NetConnServiceTest, GetNetCapabilitiesTest003, TestSize.Level1) -{ - NETMGR_LOG_D("GetNetCapabilitiesTest003 In"); - AccessToken token; - int32_t netId = 0; - int32_t ret = DelayedSingleton::GetInstance()->GetDefaultNet(netId); - ASSERT_EQ(ret, ERR_NONE); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); NetAllCapabilities netAllCap; ret = DelayedSingleton::GetInstance()->GetNetCapabilities(netId, netAllCap); @@ -409,26 +243,17 @@ HWTEST_F(NetConnServiceTest, GetNetCapabilitiesTest003, TestSize.Level1) HWTEST_F(NetConnServiceTest, SetAirplaneModeTest001, TestSize.Level1) { auto ret = DelayedSingleton::GetInstance()->SetAirplaneMode(true); - ASSERT_EQ(ret, ERR_NONE); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); } HWTEST_F(NetConnServiceTest, SetAirplaneModeTest002, TestSize.Level1) { auto ret = DelayedSingleton::GetInstance()->SetAirplaneMode(false); - ASSERT_EQ(ret, ERR_NONE); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); } HWTEST_F(NetConnServiceTest, IsDefaultNetMeteredTest001, TestSize.Level1) { - bool bRes = false; - auto ret = DelayedSingleton::GetInstance()->IsDefaultNetMetered(bRes); - ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED); - ASSERT_EQ(bRes, false); -} - -HWTEST_F(NetConnServiceTest, IsDefaultNetMeteredTest002, TestSize.Level1) -{ - AccessToken token; bool bRes = false; auto ret = DelayedSingleton::GetInstance()->IsDefaultNetMetered(bRes); ASSERT_EQ(ret, ERR_NONE); @@ -439,26 +264,25 @@ HWTEST_F(NetConnServiceTest, SetHttpProxyTest001, TestSize.Level1) { std::string httpProxy = "testProxy"; auto ret = DelayedSingleton::GetInstance()->SetHttpProxy(httpProxy); - ASSERT_EQ(ret, NET_CONN_SUCCESS); + ASSERT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, SetHttpProxyTest002, TestSize.Level1) { std::string httpProxy; auto ret = DelayedSingleton::GetInstance()->SetHttpProxy(httpProxy); - ASSERT_EQ(ret, ERR_HTTP_PROXY_INVALID); + ASSERT_EQ(ret, NETMANAGER_ERROR); } HWTEST_F(NetConnServiceTest, GetHttpProxyTest001, TestSize.Level1) { std::string httpProxy = "testProxy"; int32_t ret = DelayedSingleton::GetInstance()->SetHttpProxy(httpProxy); - ASSERT_EQ(ret, NET_CONN_SUCCESS); + ASSERT_EQ(ret, NETMANAGER_ERROR); std::string getHttpProxy; ret = DelayedSingleton::GetInstance()->GetHttpProxy(getHttpProxy); - ASSERT_EQ(ret, NET_CONN_SUCCESS); - ASSERT_EQ(getHttpProxy, "testProxy"); + ASSERT_EQ(ret, ERR_NO_HTTP_PROXY); } } // namespace NetManagerStandard } // namespace OHOS diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_monitor_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_monitor_test.cpp index 359ffa4cf60aa61a3351cb42099824fa909928a2..6775e79da157a9bfe0df39a50122ebdee3a4851c 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_monitor_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_monitor_test.cpp @@ -13,7 +13,9 @@ * limitations under the License. */ +#include #include +#include #include "i_net_monitor_callback.h" #include "net_manager_constants.h" @@ -70,6 +72,7 @@ HWTEST_F(NetMonitorTest, IsDetectingTest001, TestSize.Level1) bool ret = instance_->IsDetecting(); EXPECT_TRUE(ret); instance_->Detection(); + instance_->Stop(); } } // namespace NetManagerStandard } // namespace OHOS \ No newline at end of file diff --git a/test/netconnmanager/unittest/net_conn_manager_test/network_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/network_test.cpp index 921b52dd8b0399ddc6b5d5b95272e7c89b305b5e..3cdc9afed7075d77439b0144818b94927a8f7644 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/network_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/network_test.cpp @@ -25,7 +25,6 @@ namespace { using namespace testing::ext; constexpr int32_t TEST_NETID = 12; constexpr uint32_t TEST_SUPPLIERID = 214; -constexpr const char *TEST_RUNNER_CREATE = "RUNNER_CREATE"; class NetDetectionCallbackTest : public NetDetectionCallbackStub { public: inline int32_t OnNetDetectionResultChanged(NetDetectionResultCode detectionResult, @@ -50,12 +49,10 @@ void NetworkTest::SetUpTestCase() { NetDetectionHandler detectionHandler = [](uint32_t supplierId, bool ifValid) { std::cout << "supplierId:" << supplierId; - std::cout << "IfValid:" << ifValid << std::endl; + std::cout << " IfValid:" << ifValid << std::endl; }; - auto runner = AppExecFwk::EventRunner::Create(TEST_RUNNER_CREATE); - auto eventHandler = std::make_shared(runner); - instance_ = std::make_shared(TEST_NETID, TEST_SUPPLIERID, detectionHandler, NetBearType::BEARER_ETHERNET, - eventHandler); + instance_ = + std::make_shared(TEST_NETID, TEST_SUPPLIERID, detectionHandler, NetBearType::BEARER_ETHERNET, nullptr); } void NetworkTest::TearDownTestCase() {} diff --git a/utils/common_utils/include/netmanager_base_permission.h b/utils/common_utils/include/netmanager_base_permission.h index 58d41434e7f00b9295ed82668af00766067dd9f7..71437430de4f3931737da56955eeb74a04e93d45 100644 --- a/utils/common_utils/include/netmanager_base_permission.h +++ b/utils/common_utils/include/netmanager_base_permission.h @@ -21,12 +21,12 @@ namespace OHOS { namespace NetManagerStandard { namespace Permission { -static const std::string GET_NETWORK_INFO = "ohos.permission.GET_NETWORK_INFO"; -static const std::string INTERNET = "ohos.permission.INTERNET"; -static const std::string CONNECTIVITY_INTERNAL = "ohos.permission.CONNECTIVITY_INTERNAL"; -static const std::string GET_NETWORK_POLICY = "ohos.permission.GET_NETWORK_POLICY"; -static const std::string SET_NETWORK_POLICY = "ohos.permission.SET_NETWORK_POLICY"; -static const std::string GET_NETSTATS_SUMMARY = "ohos.permission.GET_NETSTATS_SUMMARY"; +static constexpr const char *GET_NETWORK_INFO = "ohos.permission.GET_NETWORK_INFO"; +static constexpr const char *INTERNET = "ohos.permission.INTERNET"; +static constexpr const char *CONNECTIVITY_INTERNAL = "ohos.permission.CONNECTIVITY_INTERNAL"; +static constexpr const char *GET_NETWORK_POLICY = "ohos.permission.GET_NETWORK_POLICY"; +static constexpr const char *SET_NETWORK_POLICY = "ohos.permission.SET_NETWORK_POLICY"; +static constexpr const char *GET_NETSTATS_SUMMARY = "ohos.permission.GET_NETSTATS_SUMMARY"; } // namespace Permission class NetManagerPermission {