diff --git a/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp index eeef9dc903436b92091be93a8a918c79e22aed3b..8a66a823d1d68e4b373852b8a84a373c239027de 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp @@ -219,6 +219,7 @@ void UserAuthCallbackV10::OnAcquireInfo(int32_t module, uint32_t acquireInfo, authTipInfoCallbackHolder->tipCode); if (ret != napi_ok) { IAM_LOGE("DoTipInfoCallBack ret = %{public}d", ret); + napi_close_handle_scope(authTipInfoCallbackHolder->env, scope); return; } napi_close_handle_scope(authTipInfoCallbackHolder->env, scope); diff --git a/services/base/src/thread_handler_impl.cpp b/services/base/src/thread_handler_impl.cpp index c9a7d8d2d65329dd179d980fe333d54434297c16..468dfd776486cb62082bd334f1aaffc31ea16a4b 100644 --- a/services/base/src/thread_handler_impl.cpp +++ b/services/base/src/thread_handler_impl.cpp @@ -20,9 +20,11 @@ #include #include "nocopyable.h" -#include "thread_handler_manager.h" #include "iam_logger.h" +#include "iam_ptr.h" +#include "thread_handler_manager.h" +#include "xcollie_helper.h" #define LOG_TAG "USER_AUTH_SA" @@ -30,6 +32,7 @@ namespace OHOS { namespace UserIam { namespace UserAuth { using namespace OHOS; +using namespace OHOS::UserIam::Common; ThreadHandlerImpl::ThreadHandlerImpl(std::string name, bool canSuspend) : pool_(name), canSuspend_(canSuspend) { pool_.Start(1); @@ -48,6 +51,12 @@ void ThreadHandlerImpl::PostTask(const Task &task) return; } pool_.AddTask(task); + + constexpr uint32_t TASK_BLOCK_MONITOR_TIMEOUT = 20; + auto taskBlockMonitor = MakeShared("taskBlockMonitor", TASK_BLOCK_MONITOR_TIMEOUT); + pool_.AddTask([taskBlockMonitor] { + (void)taskBlockMonitor; + }); } void ThreadHandlerImpl::EnsureTask(const Task &task) diff --git a/services/base/src/thread_handler_singleton_impl.cpp b/services/base/src/thread_handler_singleton_impl.cpp index c89505de58264a014cb14516884f3cc65af36718..dcf5b55ca84f801a21650d6cd5d5d9ac4b02e0d8 100644 --- a/services/base/src/thread_handler_singleton_impl.cpp +++ b/services/base/src/thread_handler_singleton_impl.cpp @@ -20,10 +20,12 @@ #include #include "nocopyable.h" -#include "relative_timer.h" -#include "thread_handler_manager.h" #include "iam_logger.h" +#include "iam_ptr.h" +#include "relative_timer.h" +#include "thread_handler_manager.h" +#include "xcollie_helper.h" #define LOG_TAG "USER_AUTH_SA" @@ -31,9 +33,18 @@ namespace OHOS { namespace UserIam { namespace UserAuth { using namespace OHOS; +using namespace OHOS::UserIam::Common; void ThreadHandlerSingletonImpl::PostTask(const Task &task) { RelativeTimer::GetInstance().Register(task, 0); + + constexpr uint32_t TASK_BLOCK_MONITOR_TIMEOUT = 20; + auto taskBlockMonitor = MakeShared("taskBlockMonitor", TASK_BLOCK_MONITOR_TIMEOUT); + RelativeTimer::GetInstance().Register( + [taskBlockMonitor] { + (void)taskBlockMonitor; + }, + 0); } void ThreadHandlerSingletonImpl::EnsureTask(const Task &task) diff --git a/services/remote_connect/src/soft_bus_server_socket.cpp b/services/remote_connect/src/soft_bus_server_socket.cpp index d40671c0a7e46f4b0e9265142189bc5264df8814..092607e7ac0a911cff6030a0c893937372569379 100644 --- a/services/remote_connect/src/soft_bus_server_socket.cpp +++ b/services/remote_connect/src/soft_bus_server_socket.cpp @@ -56,6 +56,11 @@ void ServerSocket::OnBind(int32_t socketId, PeerSocketInfo info) return; } + if (info.networkId == nullptr) { + IAM_LOGE("networkId is invalid."); + return; + } + std::string peerNetworkId(info.networkId); AddServerSocket(socketId, peerNetworkId); } diff --git a/test/unittest/services/BUILD.gn b/test/unittest/services/BUILD.gn index 80845ea6de0d3d7ca207f6e408d0ee964ed5865b..3bdfa6dd3ef3fe11b14b0e86f3dc9868f290ceda 100644 --- a/test/unittest/services/BUILD.gn +++ b/test/unittest/services/BUILD.gn @@ -130,6 +130,7 @@ ohos_unittest("iam_services_test") { "../../../services/remote_connect/src/soft_bus_socket_listener.cpp", "mocks/mock_ipc_common.cpp", "mocks/mock_iuser_auth_interface.cpp", + "mocks/mock_socket.cpp", "src/attributes_test.cpp", "src/auth_widget_helper_test.cpp", "src/authentication_impl_test.cpp", @@ -212,7 +213,6 @@ ohos_unittest("iam_services_test") { "c_utils:utils", "device_manager:devicemanagersdk", "drivers_interface_user_auth:libuser_auth_proxy_4.0", - "dsoftbus:softbus_client", "googletest:gmock", "hdf_core:libhdf_utils", "hdf_core:libhdi", diff --git a/test/unittest/services/mocks/mock_socket.cpp b/test/unittest/services/mocks/mock_socket.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32830ce1475a32bae92526221537985b09c9105e --- /dev/null +++ b/test/unittest/services/mocks/mock_socket.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mock_socket.h" + +#include "securec.h" +#include "socket.h" + +static int32_t g_fd = 0; + +int32_t Socket(SocketInfo info) +{ + g_fd += 1; + return g_fd; +} + +int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) +{ + return 0; +} + +void Shutdown(int32_t socket) +{ +} + +int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) +{ + return 0; +} + +int32_t SendBytes(int32_t socket, const void *data, uint32_t len) +{ + return 0; +} \ No newline at end of file diff --git a/test/unittest/services/mocks/mock_socket.h b/test/unittest/services/mocks/mock_socket.h new file mode 100644 index 0000000000000000000000000000000000000000..d9da83d887e23583441a58cbb3341de52c364852 --- /dev/null +++ b/test/unittest/services/mocks/mock_socket.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_SOCKET_H +#define MOCK_SOCKET_H + +#endif // MOCK_SOCKET_H diff --git a/test/unittest/services/mocks/socket.h b/test/unittest/services/mocks/socket.h new file mode 100644 index 0000000000000000000000000000000000000000..52116189da392a6417cb405f6d0e2e613999736c --- /dev/null +++ b/test/unittest/services/mocks/socket.h @@ -0,0 +1,507 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup Softbus + * @{ + * + * @brief Provides high-speed, secure communication between devices. + * + * This module implements unified distributed communication capability management between + * nearby devices, and provides link-independent device discovery and transmission interfaces + * to support service publishing and data transmission. + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file socket.h + * + * @brief Declares unified data transmission interfaces. + * + * This file provides data transmission capabilities, including creating and removing a socket server, + * opening and closing sessions, receiving data, and querying basic socket information. \n + * After multiple nearby devices are discovered and networked, these interfaces can be used to + * transmit data across devices. \n + * + * @since 1.0 + * @version 1.0 + */ +#ifndef SOCKET_H +#define SOCKET_H + +#include +#include +#ifdef __cplusplus +extern "C" { +#endif + +#define MAX_MAC_LEN 18 + +/** + * @brief Enumerates the data types. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + DATA_TYPE_MESSAGE = 1, /**< Message */ + DATA_TYPE_BYTES, /**< Bytes */ + DATA_TYPE_FILE, /**< File */ + DATA_TYPE_RAW_STREAM, /**< Raw data stream */ + DATA_TYPE_VIDEO_STREAM, /**< Video data stream*/ + DATA_TYPE_AUDIO_STREAM, /**< Audio data stream*/ + DATA_TYPE_SLICE_STREAM, /**< Video slice stream*/ + DATA_TYPE_BUTT, +} TransDataType; + +/** + * @brief Defines the description of the socket. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + char *name; /**< My socket name */ + char *peerName; /**< Peer socket name */ + char *peerNetworkId; /**< Peer network ID */ + char *pkgName; /**< Package name */ + TransDataType dataType; /**< Data type */ +} SocketInfo; + +/** + * @brief Defines the description of the socket. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + char *name; /**< Peer socket name */ + char *networkId; /**< Peer network ID */ + char *pkgName; /**< Peer package name */ + TransDataType dataType; /**< Data type of peer socket*/ +} PeerSocketInfo; + +/** + * @brief Defines the socket access information. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + int32_t userId; /**< User ID of the application that initiates link setup */ + uint64_t localTokenId; /**< Token ID of the application that initiates link setup */ + char *businessAccountId; /**< Business account ID of the application that initiates link setup (reserved)*/ + char *extraAccessInfo; /**< Extra Info of the application that initiates link setup, maximum length 256 bytes */ +} SocketAccessInfo; + +/** + * @brief Enumerates the reason of the shutdown event. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + SHUTDOWN_REASON_UNKNOWN, /**< Shutdown for unknown reason */ + SHUTDOWN_REASON_LOCAL, /**< Shutdown by local process */ + SHUTDOWN_REASON_PEER, /**< Shutdown by peer process */ + SHUTDOWN_REASON_LNN_CHANGED, /**< Shutdown for LNN changed */ + SHUTDOWN_REASON_CONN_CHANGED, /**< Shutdown for CONN Changed */ + SHUTDOWN_REASON_TIMEOUT, /**< Shutdown for timeout */ + SHUTDOWN_REASON_SEND_FILE_ERR, /**< Shutdown for sending file error */ + SHUTDOWN_REASON_RECV_FILE_ERR, /**< Shutdown for receiving file error */ + SHUTDOWN_REASON_RECV_DATA_ERR, /**< Shutdown for receiving data error */ + SHUTDOWN_REASON_UNEXPECTED, /**< Shutdown for unexpected reason */ + SHUTDOWN_REASON_SERVICE_DIED, +} ShutdownReason; + +/** + * @brief Enumerates the Qos transform reliability. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + QOS_RELIABILITY_NONE = 1, /**< Unreliable */ + QOS_RELIABILITY_FULL, /**< Reliable */ + QOS_RELIABILITY_HALF, /**< Semi-reliable */ + QOS_RELIABILITY_BUTT, +} QosTransReliability; + +/** + * @brief Enumerates the QoS Assurance Items. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + QOS_TYPE_MIN_BW, /**< Minimum bandwidth. */ + QOS_TYPE_MAX_LATENCY, /**< Maximum latency. */ + QOS_TYPE_MIN_LATENCY, /**< Minimum latency. */ + QOS_TYPE_MAX_WAIT_TIMEOUT, /**< Maximum timeout of transmission. */ + QOS_TYPE_MAX_BUFFER, /**< Maximum cache. */ + QOS_TYPE_FIRST_PACKAGE, /**< First packet size. */ + QOS_TYPE_MAX_IDLE_TIMEOUT, /**< Maximum idle time. */ + QOS_TYPE_TRANS_RELIABILITY, /**< Transmission reliability. */ + QOS_TYPE_BUTT, +} QosType; + +/** + * @brief Defines the Qos Type-Value structure. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + QosType qos; /**< Qos type {@link QosType} */ + int32_t value; /**< Value of Qos types */ +} QosTV; + +/** + * @brief Enumerates the file types. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + FILE_EVENT_SEND_PROCESS, /**< Sending file */ + FILE_EVENT_SEND_FINISH, /**< Send file end */ + FILE_EVENT_SEND_ERROR, /**< Send file failed */ + FILE_EVENT_RECV_UPDATE_PATH, /**< Update root directory*/ + FILE_EVENT_RECV_START, /**< Receive file start */ + FILE_EVENT_RECV_PROCESS, /**< Receiving file */ + FILE_EVENT_RECV_FINISH, /**< Receive file end */ + FILE_EVENT_RECV_ERROR, /**< Receive file failed */ + FILE_EVENT_BUTT, +} FileEventType; + +typedef struct { + FileEventType type; /**< Event data type of the files*/ + const char **files; /**< File path*/ + uint32_t fileCnt; /**< Count of the files*/ + uint64_t bytesProcessed; /**< Send or receive bytes of the files*/ + uint64_t bytesTotal; /**< Total bytes of the files*/ + const char *(*UpdateRecvPath)(void); /**< Update file receiving directory*/ +} FileEvent; + +/** + * @brief Defines the extended stream data. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + int32_t type; /**< Extended data type {@link TransEnumEventType} */ + int64_t value; /**< Value of the extended data */ +} TV; + +/** + * @brief Defines the frame information for stream transmission. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + int32_t frameType; /**< Frame type, which can be I-frame or P-frame. */ + int64_t timeStamp; /**< Timestamp. */ + int32_t seqNum; /**< Sequence number. */ + int32_t seqSubNum; /**< Sequence number of the slice. */ + int32_t level; /**< Scalable video coding level. 0 stands for the base level, + 1 for level 1, and 2 for level 2. */ + int32_t bitMap; /**< Bitmap, which indicates the start or end slice of a frame. */ + int32_t tvCount; /**< Number of scalable tag-values (TVs). */ + TV *tvList; /**< Pointer to the TV list. */ +} StreamFrameInfo; + +/** + * @brief Defines the stream data. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + char *buf; /**< Pointer to the buffer for storing the stream data */ + int bufLen; /**< Length of the buffer */ +} StreamData; + +/** + * @brief Enumerates the QoS feedback types. + * + * @since 2.0 + * @version 2.0 + */ +typedef enum { + QOS_SATISFIED, /**< Feedback on satisfied quality */ + QOS_NOT_SATISFIED, /**< Feedback on not satisfied quality */ +} QoSEvent; + +/** + * @brief Defines socket callbacks. + * + * When a socket is opened or closed, or there is data to process, the related callback is invoked. + * + * @since 2.0 + * @version 2.0 + */ +typedef struct { + /** + * @brief Called when a socket is bind. + * + * This callback is invoked to verify the socket or initialize resources related to the socket. + * + * @param socket Indicates the unique socket fd; socket fd = 0 if the bind is failed. + * @since 2.0 + * @version 2.0 + */ + void (*OnBind)(int32_t socket, PeerSocketInfo info); + + /** + * @brief Called when a socket is closed. + * + * This callback is invoked to release resources related to the socket. + * + * @param socket Indicates the unique socket fd. + * @param reason Indicates the reason for closing the socket. + * @since 2.0 + * @version 2.0 + */ + void (*OnShutdown)(int32_t socket, ShutdownReason reason); + + /** + * @brief Called when bytes data is received. + * + * This callback is invoked to notify that data is received. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the bytes data received. + * @param dataLen Indicates the length of the bytes data received. + * @since 2.0 + * @version 2.0 + */ + void (*OnBytes)(int32_t socket, const void *data, uint32_t dataLen); + + /** + * @brief Called when message data is received. + * + * This callback is invoked to notify that message data is received. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the message data received. + * @param dataLen Indicates the length of the message data received. + * @since 2.0 + * @version 2.0 + */ + void (*OnMessage)(int32_t socket, const void *data, uint32_t dataLen); + + /** + * @brief Called when stream data is received. + * + * This callback is invoked to notify that stream data is received. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the stream data received. + * @param ext Indicates the pointer to the extended service data received. + * @param param Indicates the pointer to the stream data frame information. + * @since 2.0 + * @version 2.0 + */ + void (*OnStream)(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param); + + /** + * @brief Called when file data is received. + * + * This callback is invoked to notify that file data is received. + * + * @param socket Indicates the unique socket fd. + * @param event Indicates the file event. + * @param data Indicates the pointer to the file data received. + * @since 2.0 + * @version 2.0 + */ + void (*OnFile)(int32_t socket, FileEvent *event); + + /** + * @brief Called when QoS state is changed. + * + * This callback is invoked to notify that QoS state is changed. + * + * @param socket Indicates the unique socket fd. + * @param event Indicates the type of QoS state change. + * @param qos[] Indicates the QoS status that we can provide. + * @since 2.0 + * @version 2.0 + */ + void (*OnQos)(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount); + + /** + * @brief Called when a socket is negotiating. + * + * This callback is invoked to negotiating the socket, this callback be called on the server side. + * The server can determine whether to bind the socket based on the negotiation result. + * + * + * @param socket Indicates the unique socket fd. + * @param info Indicates the information of peer socket. + * @return If true is returned, it indicates that the negotiation is successful. If this method is not implemented, + * the negotiation is successful by default. if false is returned, the negotiation fails and the client is notified + * that the connection is rejected. + * @since 2.0 + * @version 2.0 + */ + bool (*OnNegotiate)(int32_t socket, PeerSocketInfo info); +} ISocketListener; + +/** + * @brief Creates a socket. + * + * A maximum of 10 socket can be created. + * + * @param info Indicates the description of the socket structure. + * It is the unique identifier of the upper-layer service. The value cannot be empty or exceed 64 characters. + * + * @return Returns socket fd if the socket creation is successful; returns -1 otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t Socket(SocketInfo info); + +/** + * @brief Listens a socket, which is called by server. + * + * @param socket Indicates the the unique socket fd. + * @param qos Indicates the QoS requirements for socket. The value cannot be empty. + * @param listener Indicates the pointer to the socket callback. + * + * @return Returns 0 if the listen creation is successful; returns -1 otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener); + +/** + * @brief Binds a socket, which is called by client. + * + * {@link OnBind} is invoked to return whether the socket is successfully bind. + * Data can be transmitted only after the socket is successfully bind. + * + * @param socket Indicates the the unique socket fd. + * @param qos Indicates the QoS requirements for socket. The value cannot be empty. + * @param listener Indicates the pointer to the socket callback. + * + * @return Returns SOFTBUS_TRANS_INVALID_PARAM if invalid parameters are detected. + * @return Returns INVALID_SOCKET if the operation fails. + * @return Returns the socket fd (an integer greater than 0) if the socket is bind; + * returns an error code otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener); + +/** + * @example sendbytes_message_demo.c + */ + +/** + * @brief Sends bytes data. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the bytes data to send, which cannot be NULL. + * @param len Indicates the length of the bytes data to send. + * + * @return Returns SOFTBUS_INVALID_PARAM if invalid parameters are detected. + * @return Returns SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT if the bytes data exceeds the maximum limit. + * @return Returns SOFTBUS_TRANS_INVALID_SOCKET if socket is invalid. + * @return Returns SOFTBUS_TRANS_SOCKET_NO_ENABLE if the socket is not bind. + * @return Returns SOFTBUS_OK if the operation is successful; returns an error code otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t SendBytes(int32_t socket, const void *data, uint32_t len); + +/** + * @brief Sends message data. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the message data to send, which cannot be NULL. + * @param len Indicates the length of the message data to send. + * + * @return Returns SOFTBUS_INVALID_PARAM if data is NULL or len is zero. + * @return Returns SOFTBUS_TRANS_SEND_LEN_BEYOND_LIMIT if the message data length exceeds the limit. + * @return Returns SOFTBUS_INVALID_SOCKET if socket is invalid. + * @return Returns SOFTBUS_TRANS_SOCKET_NO_ENABLE if the socket is not bind. + * @return Returns SOFTBUS_OK if the operation is successful; returns an error code otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t SendMessage(int32_t socket, const void *data, uint32_t len); + +/** + * @example sendstream_demo.c + */ + +/** + * @brief Sends stream data. + * + * @param socket Indicates the unique socket fd. + * @param data Indicates the pointer to the stream data to send, which cannot be NULL. + * @param ext Indicates the pointer to the extended stream data to send, which cannot be NULL. + * @param param Indicates the pointer to the stream frame information, which cannot be NULL. + * + * @return Returns SOFTBUS_INVALID_PARAM if any of the input parameters is NULL. + * @return Returns SOFTBUS_INVALID_SOCKET if socket is invalid. + * @return Returns SOFTBUS_TRANS_SOCKET_NO_ENABLE if the socket is not bind. + * @return Returns SOFTBUS_OK if the operation is successful; returns an error code otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t SendStream(int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param); + +/** + * @example sendfile_demo.c + */ + +/** + * @brief Sends files data. + * + * @param socket Indicates the unique socket fd. + * @param sFileList Indicates the pointer to the source files data to send, which cannot be NULL. + * @param dFileList Indicates the pointer to the destination files data, which cannot be NULL. + * @param fileCnt Indicates the number of files data to send, which cannot be 0. + * + * @return Returns SOFTBUS_INVALID_PARAM if sFileList is NULL or fileCnt is 0. + * @return Returns SOFTBUS_INVALID_SOCKET if socket is invalid. + * @return Returns SOFTBUS_TRANS_SOCKET if the socket is not bind. + * @return Returns SOFTBUS_OK if the operation is successful; returns an error code otherwise. + * @since 2.0 + * @version 2.0 + */ +int32_t SendFile(int32_t socket, const char *sFileList[], const char *dFileList[], uint32_t fileCnt); + +/** + * @brief Get socket based on a socket fd. + * + * @param socket Indicates the unique socket fd. + * + * @return Returns no value. + * @since 2.0 + * @version 2.0 + */ +void Shutdown(int32_t socket); + +#ifdef __cplusplus +} +#endif +#endif // SOCKET_H diff --git a/test/unittest/services/mocks/softbus_error_code.h b/test/unittest/services/mocks/softbus_error_code.h new file mode 100644 index 0000000000000000000000000000000000000000..0524d60baf50256e5bf7d963f3a72edcacd55ae3 --- /dev/null +++ b/test/unittest/services/mocks/softbus_error_code.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup SoftBus + * @{ + * + * @brief Provides high-speed, secure communication between devices. + * + * This module implements unified distributed communication capability management between nearby devices, and provides + * link-independent device discovery and transmission interfaces to support service publishing and data transmission. + * + * @since 1.0 + * @version 1.0 + */ +/** @} */ + +/** + * @file softbus_error_code.h + * + * @brief Declares error code + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef SOFTBUS_ERROR_CODE_H +#define SOFTBUS_ERROR_CODE_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +enum SoftBusErrNo { + SOFTBUS_TIMOUT = 1, + SOFTBUS_INVALID_PARAM = 2, + + /* softbus ok */ + SOFTBUS_OK = 0, +}; + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ +#endif /* SOFTBUS_ERRCODE_H */ \ No newline at end of file diff --git a/test/unittest/services/src/soft_bus_base_socket_test.cpp b/test/unittest/services/src/soft_bus_base_socket_test.cpp index 2747945695ec39d68e89ca35793141c724b8985e..0bcb282712f58cf94dfd8f3456fdbef927ba9ebb 100644 --- a/test/unittest/services/src/soft_bus_base_socket_test.cpp +++ b/test/unittest/services/src/soft_bus_base_socket_test.cpp @@ -350,7 +350,7 @@ HWTEST_F(SoftBusBaseSocketTest, SoftBusBaseSocketTestSendRequest, TestSize.Level .attributes = attributes, .callback = callback }; - EXPECT_EQ(baseSocket->SendRequest(connectionInfo), GENERAL_ERROR); + EXPECT_EQ(baseSocket->SendRequest(connectionInfo), SUCCESS); delete baseSocket; } @@ -366,7 +366,7 @@ HWTEST_F(SoftBusBaseSocketTest, SoftBusBaseSocketTestSendResponse, TestSize.Leve attributes->SetInt32Value(Attributes::ATTR_MSG_TYPE, 1); uint32_t messageSeq = 123; EXPECT_EQ(baseSocket->SendResponse(socketId, connectionName, srcEndPoint, destEndPoint, attributes, - messageSeq), GENERAL_ERROR); + messageSeq), SUCCESS); delete baseSocket; } diff --git a/test/unittest/services/src/soft_bus_manager_test.cpp b/test/unittest/services/src/soft_bus_manager_test.cpp index d0a19d79f8e73df7e2ea4ecdbb03c31ee8e0c79e..f618c43bc2c0dd31e52bf1c3f3615d0e6063dfca 100644 --- a/test/unittest/services/src/soft_bus_manager_test.cpp +++ b/test/unittest/services/src/soft_bus_manager_test.cpp @@ -57,18 +57,11 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnBind_001, TestSize.Level0) EXPECT_NO_THROW(SoftBusManager::GetInstance().OnBind(-2, info)); } -HWTEST_F(SoftBusManagerTest, SoftBusManagerTestSocketUnInit_001, TestSize.Level0) -{ - EXPECT_NO_THROW({ - SoftBusManager::GetInstance().ServiceSocketUnInit(); - }); -} - HWTEST_F(SoftBusManagerTest, SoftBusManagerTestRegistSoftBusListener, TestSize.Level0) { EXPECT_NO_THROW({ SoftBusManager::GetInstance().RegistSoftBusListener(); - SoftBusManager::GetInstance().RegistSoftBusListener(); + SoftBusManager::GetInstance().UnRegistSoftBusListener(); }); } @@ -76,24 +69,10 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestSocketInit, TestSize.Level0) { EXPECT_NO_THROW({ SoftBusManager::GetInstance().ServiceSocketInit(); - }); -} - -HWTEST_F(SoftBusManagerTest, SoftBusManagerTestSocketUnInit_002, TestSize.Level0) -{ - EXPECT_NO_THROW({ SoftBusManager::GetInstance().ServiceSocketUnInit(); }); } -HWTEST_F(SoftBusManagerTest, SoftBusManagerTestUnRegistSoftBusListener, TestSize.Level0) -{ - EXPECT_NO_THROW({ - SoftBusManager::GetInstance().UnRegistSoftBusListener(); - SoftBusManager::GetInstance().UnRegistSoftBusListener(); - }); -} - HWTEST_F(SoftBusManagerTest, SoftBusManagerTestDeleteSocket, TestSize.Level0) { EXPECT_NO_THROW({ @@ -104,25 +83,23 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestDeleteSocket, TestSize.Level0) HWTEST_F(SoftBusManagerTest, SoftBusManagerTestServiceSocketListen, TestSize.Level0) { int32_t socketId = 100; - SoftBusManager::GetInstance().RegistSoftBusListener(); + SoftBusManager::GetInstance().ServiceSocketUnInit(); SoftBusManager::GetInstance().ServiceSocketInit(); - std::shared_ptr serverSocket = SocketFactory::CreateServerSocket(socketId); int ret = SoftBusManager::GetInstance().ServiceSocketListen(socketId); - EXPECT_EQ(ret, GENERAL_ERROR); + EXPECT_EQ(ret, SUCCESS); PeerSocketInfo info; + char testNetworkId[] = "1122"; + info.networkId = testNetworkId; EXPECT_NO_THROW(SoftBusManager::GetInstance().OnBind(socketId, info)); - SoftBusManager::GetInstance().AddSocket(socketId, serverSocket); - SoftBusManager::GetInstance().SetServerSocket(serverSocket); - int32_t clientSocketId = 200; const std::string connectionName = "testConnection"; const uint32_t tokenId = 1234; const std::string networkId = "networkId"; SoftBusManager::GetInstance().DoOpenConnection(connectionName, tokenId, networkId); auto clientSocket = SocketFactory::CreateClientSocket(clientSocketId, connectionName, networkId); - EXPECT_EQ(SoftBusManager::GetInstance().ClientSocketBind(clientSocketId), GENERAL_ERROR); + EXPECT_EQ(SoftBusManager::GetInstance().ClientSocketBind(clientSocketId), SUCCESS); SoftBusManager::GetInstance().AddConnection(connectionName, clientSocket); SoftBusManager::GetInstance().AddSocket(clientSocketId, clientSocket); RemoteConnectListenerManager::GetInstance().OnConnectionUp(connectionName); @@ -133,11 +110,10 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestServiceSocketListen, TestSize.Lev ASSERT_NE(attributes, nullptr); MsgCallback callback; EXPECT_EQ(SoftBusManager::GetInstance().SendMessage(connectionName, srcEndPoint, destEndPoint, attributes, - callback), 2); + callback), SUCCESS); EXPECT_EQ(SoftBusManager::GetInstance().DoCloseConnection(connectionName), SUCCESS); SoftBusManager::GetInstance().ServiceSocketUnInit(); - SoftBusManager::GetInstance().UnRegistSoftBusListener(); SoftBusManager::GetInstance().DeleteSocket(socketId); SoftBusManager::GetInstance().DeleteSocket(clientSocketId); } @@ -148,7 +124,7 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOpenConnection, TestSize.Level0) const uint32_t tokenId = 1234; const std::string networkId = "networkId"; SoftBusManager::GetInstance().DoOpenConnection(connectionName, tokenId, networkId); - EXPECT_EQ(SoftBusManager::GetInstance().DoCloseConnection(connectionName), GENERAL_ERROR); + EXPECT_EQ(SoftBusManager::GetInstance().DoCloseConnection(connectionName), SUCCESS); } HWTEST_F(SoftBusManagerTest, SoftBusManagerTestServiceSocketUnInit_003, TestSize.Level0) @@ -157,16 +133,13 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestServiceSocketUnInit_003, TestSize int32_t clientSocketId = 200; const std::string connectionName = "testConnection"; const std::string networkId = "networkId"; - + SoftBusManager::GetInstance().ServiceSocketUnInit(); SoftBusManager::GetInstance().ServiceSocketInit(); - std::shared_ptr serverSocket = SocketFactory::CreateServerSocket(socketId); std::shared_ptr clientSocket = SocketFactory::CreateClientSocket(clientSocketId, connectionName, networkId); - SoftBusManager::GetInstance().AddSocket(socketId, serverSocket); SoftBusManager::GetInstance().AddSocket(clientSocketId, clientSocket); - SoftBusManager::GetInstance().SetServerSocket(serverSocket); auto findSocket = SoftBusManager::GetInstance().FindSocketBySocketId(socketId); - EXPECT_NE(findSocket, nullptr); + EXPECT_EQ(findSocket, nullptr); SoftBusManager::GetInstance().ServiceSocketUnInit(); SoftBusManager::GetInstance().DeleteSocket(socketId); SoftBusManager::GetInstance().DeleteSocket(clientSocketId); @@ -180,15 +153,13 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnServerBytes_001, TestSize.Level int32_t clientSocketId = 200; const std::string connectionName = "testConnection"; const std::string networkId = "networkId"; - std::shared_ptr serverSocket = SocketFactory::CreateServerSocket(socketId); + SoftBusManager::GetInstance().ServiceSocketUnInit(); + SoftBusManager::GetInstance().ServiceSocketInit(); std::shared_ptr clientSocket = SocketFactory::CreateClientSocket(clientSocketId, connectionName, networkId); - SoftBusManager::GetInstance().AddSocket(socketId, serverSocket); SoftBusManager::GetInstance().AddSocket(clientSocketId, clientSocket); - SoftBusManager::GetInstance().SetServerSocket(serverSocket); EXPECT_NO_THROW({ SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); - SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); }); SoftBusManager::GetInstance().ServiceSocketUnInit(); SoftBusManager::GetInstance().DeleteSocket(socketId); @@ -204,8 +175,8 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnShutdown_001, TestSize.Level0) SoftBusManager::GetInstance().AddSocket(socketId, serverSocket); SoftBusManager::GetInstance().SetServerSocket(serverSocket); EXPECT_NO_THROW(SoftBusManager::GetInstance().OnShutdown(600, reason)); - SoftBusManager::GetInstance().ServiceSocketUnInit(); SoftBusManager::GetInstance().DeleteSocket(socketId); + SoftBusManager::GetInstance().ServiceSocketUnInit(); } HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnServerBytes_002, TestSize.Level0) @@ -214,8 +185,6 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnServerBytes_002, TestSize.Level const void *data = new char[10]; uint32_t dataLen = 3; EXPECT_NO_THROW({ - SoftBusManager::GetInstance().ClearServerSocket(); - SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); }); } @@ -226,11 +195,8 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnServerBytes_003, TestSize.Level const void *data = new char[10]; uint32_t dataLen = 3; EXPECT_NO_THROW({ - SoftBusManager::GetInstance().ClearServerSocket(); - SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); SoftBusManager::GetInstance().OnClientBytes(socketId, nullptr, dataLen); - SoftBusManager::GetInstance().OnServerBytes(socketId, nullptr, dataLen); }); } @@ -243,7 +209,6 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnClientBytes_001, TestSize.Level std::string connectionName = "testConnection1"; const std::string networkId = "networkId1"; EXPECT_NO_THROW({ - SoftBusManager::GetInstance().ClearServerSocket(); SoftBusManager::GetInstance().OnClientBytes(socketId, data, dataLen); SoftBusManager::GetInstance().OnServerBytes(socketId, data, dataLen); }); @@ -266,11 +231,21 @@ HWTEST_F(SoftBusManagerTest, SoftBusManagerTestOnClientBytes_001, TestSize.Level connectionName += "testConnection1"; } SoftBusManager::GetInstance().ClientSocketInit(connectionName, networkId); - SoftBusManager::GetInstance().DoServiceSocketInit(); - SoftBusManager::GetInstance().RegistDeviceManagerListener(); - SoftBusManager::GetInstance().RegistDeviceManagerListener(); - SoftBusManager::GetInstance().DeviceInit(); - SoftBusManager::GetInstance().DeviceUnInit(); +} + +HWTEST_F(SoftBusManagerTest, SoftBusManagerTestStart, TestSize.Level0) +{ + EXPECT_NO_THROW({ + SoftBusManager::GetInstance().Start(); + SoftBusManager::GetInstance().Stop(); + }); +} + +HWTEST_F(SoftBusManagerTest, SoftBusManagerTestDeviceInit, TestSize.Level0) +{ + ResultCode ret = SoftBusManager::GetInstance().DeviceInit(); + EXPECT_EQ(ret, SUCCESS); + EXPECT_NO_THROW(SoftBusManager::GetInstance().DeviceUnInit()); } } // namespace UserAuth } // namespace UserIam