1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <securec.h>
16 #include "disc_coap_mock.h"
17 #include "softbus_error_code.h"
18
19 const int32_t MOCK_PHONE_DEVICE_TYPE = 14;
20 const int32_t MOCK_ACCOUNT_ID = 1234567890;
21 const int32_t MOCK_AUTH_PORT = 1234;
22 const char *MOCK_DEVICE_ID = "ABCDEF11223344556677889900";
23 const char *MOCK_DEVICE_NAME = "deviceName";
24 const char *MOCK_WLAN_IP = "192.168.0.1";
25
DiscCoapMock()26 DiscCoapMock::DiscCoapMock()
27 {
28 mock.store(this);
29 }
30
~DiscCoapMock()31 DiscCoapMock::~DiscCoapMock()
32 {
33 mock.store(nullptr);
34 }
35
NSTACKX_RegisterServiceDataV2(const struct NSTACKX_ServiceData * param,uint32_t cnt)36 int32_t NSTACKX_RegisterServiceDataV2(const struct NSTACKX_ServiceData *param, uint32_t cnt)
37 {
38 return DiscCoapMock::ActionOfRegisterServiceDataV2(param, cnt);
39 }
40
LnnGetLocalNumInfo(InfoKey key,int32_t * info)41 int32_t LnnGetLocalNumInfo(InfoKey key, int32_t *info)
42 {
43 return DiscCoapMock::ActionOfLnnGetLocalNumInfo(key, info);
44 }
45
LnnGetLocalNum64Info(InfoKey key,int64_t * info)46 int32_t LnnGetLocalNum64Info(InfoKey key, int64_t *info)
47 {
48 return DiscCoapMock::ActionOfLnnGetLocalNum64Info(key, info);
49 }
50
LnnGetLocalStrInfo(InfoKey key,char * info,uint32_t len)51 int32_t LnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len)
52 {
53 return DiscCoapMock::ActionOfLnnGetLocalStrInfo(key, info, len);
54 }
55
LnnGetLocalNumInfoByIfnameIdx(InfoKey key,int32_t * info,int32_t ifIdx)56 int32_t LnnGetLocalNumInfoByIfnameIdx(InfoKey key, int32_t *info, int32_t ifIdx)
57 {
58 return DiscCoapMock::ActionOfLnnGetLocalNumInfoByIfnameIdx(key, info, ifIdx);
59 }
60
LnnGetLocalStrInfoByIfnameIdx(InfoKey key,char * info,uint32_t len,int32_t ifIdx)61 int32_t LnnGetLocalStrInfoByIfnameIdx(InfoKey key, char *info, uint32_t len, int32_t ifIdx)
62 {
63 return DiscCoapMock::ActionOfLnnGetLocalStrInfoByIfnameIdx(key, info, len, ifIdx);
64 }
65
ActionOfRegisterServiceDataV2(const struct NSTACKX_ServiceData * param,uint32_t cnt)66 int32_t DiscCoapMock::ActionOfRegisterServiceDataV2(const struct NSTACKX_ServiceData *param, uint32_t cnt)
67 {
68 if (cnt == 0) {
69 return SOFTBUS_INVALID_PARAM;
70 }
71 return SOFTBUS_OK;
72 }
73
ActionOfLnnGetLocalNumInfo(InfoKey key,int32_t * info)74 int32_t DiscCoapMock::ActionOfLnnGetLocalNumInfo(InfoKey key, int32_t *info)
75 {
76 if (info == NULL) {
77 return SOFTBUS_INVALID_PARAM;
78 }
79
80 if (key == NUM_KEY_DEV_TYPE_ID) {
81 *info = MOCK_PHONE_DEVICE_TYPE;
82 }
83 return SOFTBUS_OK;
84 }
85
ActionOfLnnGetLocalNum64Info(InfoKey key,int64_t * info)86 int32_t DiscCoapMock::ActionOfLnnGetLocalNum64Info(InfoKey key, int64_t *info)
87 {
88 if (info == NULL) {
89 return SOFTBUS_INVALID_PARAM;
90 }
91
92 if (key == NUM_KEY_ACCOUNT_LONG) {
93 *info = MOCK_ACCOUNT_ID;
94 }
95 return SOFTBUS_OK;
96 }
97
ActionOfLnnGetLocalStrInfo(InfoKey key,char * info,uint32_t len)98 int32_t DiscCoapMock::ActionOfLnnGetLocalStrInfo(InfoKey key, char *info, uint32_t len)
99 {
100 if (info == NULL) {
101 return SOFTBUS_INVALID_PARAM;
102 }
103
104 if (key == STRING_KEY_DEV_UDID) {
105 (void)strncpy_s(info, len, MOCK_DEVICE_ID, strlen(MOCK_DEVICE_ID));
106 }
107 if (key == STRING_KEY_DEV_NAME) {
108 (void)strncpy_s(info, len, MOCK_DEVICE_NAME, strlen(MOCK_DEVICE_NAME));
109 }
110 return SOFTBUS_OK;
111 }
112
ActionOfLnnGetLocalNumInfoByIfnameIdx(InfoKey key,int32_t * info,int32_t ifIdx)113 int32_t DiscCoapMock::ActionOfLnnGetLocalNumInfoByIfnameIdx(InfoKey key, int32_t *info, int32_t ifIdx)
114 {
115 if (info == NULL) {
116 return SOFTBUS_INVALID_PARAM;
117 }
118
119 if (key == NUM_KEY_AUTH_PORT) {
120 *info = MOCK_AUTH_PORT;
121 }
122 return SOFTBUS_OK;
123 }
124
ActionOfLnnGetLocalStrInfoByIfnameIdx(InfoKey key,char * info,uint32_t len,int32_t ifIdx)125 int32_t DiscCoapMock::ActionOfLnnGetLocalStrInfoByIfnameIdx(InfoKey key, char *info, uint32_t len, int32_t ifIdx)
126 {
127 if (info == NULL) {
128 return SOFTBUS_INVALID_PARAM;
129 }
130
131 if (key == STRING_KEY_IP) {
132 (void)strncpy_s(info, len, MOCK_WLAN_IP, strlen(MOCK_WLAN_IP));
133 }
134 if (key == STRING_KEY_NET_IF_NAME) {
135 (void)strncpy_s(info, len, "wlan0", strlen("wlan0"));
136 }
137 return SOFTBUS_OK;
138 }
139
SetupSuccessStub()140 void DiscCoapMock::SetupSuccessStub()
141 {
142 EXPECT_CALL(*this, NSTACKX_RegisterServiceDataV2).WillRepeatedly(DiscCoapMock::ActionOfRegisterServiceDataV2);
143 EXPECT_CALL(*this, LnnGetLocalNumInfo).WillRepeatedly(DiscCoapMock::ActionOfLnnGetLocalNumInfo);
144 EXPECT_CALL(*this, LnnGetLocalNum64Info).WillRepeatedly(DiscCoapMock::ActionOfLnnGetLocalNum64Info);
145 EXPECT_CALL(*this, LnnGetLocalStrInfo).WillRepeatedly(DiscCoapMock::ActionOfLnnGetLocalStrInfo);
146 EXPECT_CALL(*this, LnnGetLocalNumInfoByIfnameIdx).
147 WillRepeatedly(DiscCoapMock::ActionOfLnnGetLocalNumInfoByIfnameIdx);
148 EXPECT_CALL(*this, LnnGetLocalStrInfoByIfnameIdx).
149 WillRepeatedly(DiscCoapMock::ActionOfLnnGetLocalStrInfoByIfnameIdx);
150 }