ActsSoftBusTest.cpp 13.2 KB
Newer Older
W
wenjun 已提交
1
/**
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 * 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 "gtest/gtest.h"
#include "discovery_service.h"
#include "session.h"
#include "securec.h"
#include <unistd.h>
#include <stdlib.h>
#include <climits>

using namespace std;
using namespace testing::ext;

static const int PUB_SUCCESS = 1;
static const int PUB_FAIL = -1;
static const int TESTCASE_COUNT = 18;
static const int ERROR_SESSION_ID = -1;
static const int SESSION_NAME_LEN = 64;
static const int DEVID_MAX_LEN = 96;
static const int SOFTBUS_TEST_SUCCESS = 0;
static const int SOFTBUS_TEST_FAIL = -1;
static const int ONE_SECOND = 1;
static const int DEF_TIMEOUT = 6;
static const int DEF_PUB_ID = 33113322;
static const int DEF_PUB_CAPABILITY_DATA_LEN = 2;

static int g_pubFlag = 0;
static const char* g_devId = "sb_test_default_devid";
static const char* g_pubModuleName = "sb_pub_module_name";
static const char* g_pubCapability = "ddmpCapability";
static unsigned char* g_pubCapabilityData = (unsigned char*)"Hi";
static IPublishCallback g_pubCallback = {0};
M
mamingshuai 已提交
46
static struct ISessionListener* g_sessionListenerCallback = nullptr;
W
wenjun 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

static void ResetPubFlag(void)
{
    g_pubFlag = 0;
}

static void WaitPublishResult(void)
{
    int timeout = DEF_TIMEOUT;
    while (timeout > 0) {
        sleep(ONE_SECOND);
        timeout--;
        if (g_pubFlag == PUB_SUCCESS || g_pubFlag == PUB_FAIL) {
            printf("checkPublish:wait[%d].\n", DEF_TIMEOUT - timeout);
            break;
        }
    }
    if (timeout <= 0) {
        printf("checkPublish:timeout!\n");
    }
}

/**
 * callback of publish success
 */
void SbPublishSuccess(int pubId)
{
    printf("[PubSuccess]publish success id[%d].\n", pubId);
    g_pubFlag = PUB_SUCCESS;
}

/**
 * callback of publish fail
 */
void SbPublishFail(int pubId, PublishFailReason reason)
{
    printf("[PubFail]publish fail id[%d],reason[%d].\n", pubId, reason);
    g_pubFlag = PUB_FAIL;
}

/**
 * callback of session opened
 */
int SbSessionOpened(int sessionId)
{
M
mamingshuai 已提交
92 93
    (void)sessionId;
    printf("[Session] opened.\n");
W
wenjun 已提交
94 95 96 97 98 99 100 101
    return SOFTBUS_TEST_SUCCESS;
}

/**
 * callback of session closed
 */
void SbSessionClosed(int sessionId)
{
M
mamingshuai 已提交
102 103
    (void)sessionId;
    printf("[Session] closed.\n");
W
wenjun 已提交
104 105 106 107 108 109 110
}

/**
 * callback of received data
 */
void SbOnBytesReceived(int sessionId, const void *data, unsigned int len)
{
M
mamingshuai 已提交
111 112
    (void)sessionId;
    printf("[Session] receive bytes, data len[%u].\n", len);
W
wenjun 已提交
113 114 115 116 117 118 119
}

/**
 * init service during first publish
 */
static void DefaultPublishToInitService(void)
{
M
mamingshuai 已提交
120
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    if (pubInfo == NULL) {
        printf("[DefaultPublishToInitService]malloc fail!\n");
        return;
    }
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = MID;
    pubInfo->capability = g_pubCapability;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITY_DATA_LEN;

    ResetPubFlag();
    int ret = PublishService(g_pubModuleName, pubInfo, &g_pubCallback);
    if (ret != SOFTBUS_TEST_SUCCESS) {
        printf("[DefaultPublishToInitService]call PublishService fail!\n");
    } else {
        WaitPublishResult();
        if (g_pubFlag != PUB_SUCCESS) {
            printf("[DefaultPublishToInitService]call PublishService fail!\n");
            ret  = SOFTBUS_TEST_FAIL;
        }
    }
    free(pubInfo);
}

/**
 * undo the first publish
 */
static void UnDefaultPublish(void)
{
    int ret = UnPublishService(g_pubModuleName, DEF_PUB_ID);
    if (ret != SOFTBUS_TEST_SUCCESS) {
        printf("[UnDefaultPublish]unpublish fail!\n");
    }
}

class ActsSoftBusTest : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void)
    {
        g_pubCallback.onPublishSuccess = SbPublishSuccess;
        g_pubCallback.onPublishFail = SbPublishFail;
        g_sessionListenerCallback = (struct ISessionListener*)malloc(sizeof(struct ISessionListener));
M
mamingshuai 已提交
168 169
        ASSERT_EQ(true, g_sessionListenerCallback != nullptr);

W
wenjun 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        g_sessionListenerCallback->onSessionOpened = SbSessionOpened;
        g_sessionListenerCallback->onSessionClosed = SbSessionClosed;
        g_sessionListenerCallback->onBytesReceived = SbOnBytesReceived;
        DefaultPublishToInitService();
        UnDefaultPublish();
    }
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void)
    {
        if (g_sessionListenerCallback != nullptr) {
            free(g_sessionListenerCallback);
            g_sessionListenerCallback = nullptr;
        }
    }
    // Testcase setup
    virtual void SetUp() {}
    // Testcase teardown
    virtual void TearDown() {}
};
/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0100
 * @tc.name      : abnormal parameter test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
194
HWTEST_F(ActsSoftBusTest, testSetNumGreaterThanMax, Function | MediumTest | Level3)
W
wenjun 已提交
195
{
M
mamingshuai 已提交
196
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
197
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
M
mamingshuai 已提交
198
    ASSERT_EQ(true, devInfo != NULL);
W
wenjun 已提交
199 200 201 202 203
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
    devInfo->value = g_devId;
    
    int ret = SetCommonDeviceInfo(devInfo, COMM_DEVICE_KEY_MAX + 1);
M
mamingshuai 已提交
204 205 206 207 208
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
    ret  = SetCommonDeviceInfo(devInfo, 0);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
    ret  = SetCommonDeviceInfo(NULL, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
209 210 211 212 213 214 215 216
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0300
 * @tc.name      : set ID value equal and greater maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
217
HWTEST_F(ActsSoftBusTest, testSetDevIdEqualMax, Function | MediumTest | Level3)
W
wenjun 已提交
218
{
M
mamingshuai 已提交
219 220 221 222 223
    const char* info1 =
        "abcdef123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    const char* info2 =
        "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890abcdefg";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
224
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
M
mamingshuai 已提交
225
    ASSERT_EQ(true, devInfo != NULL);
W
wenjun 已提交
226 227
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
M
mamingshuai 已提交
228 229 230 231 232 233 234
    devInfo->value = info1;

    int ret = SetCommonDeviceInfo(devInfo, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
    devInfo->value = info2;
    ret = SetCommonDeviceInfo(devInfo, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
235 236 237 238 239 240 241 242
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0600
 * @tc.name      : set name value equal and greater maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
243
HWTEST_F(ActsSoftBusTest, testSetDevNameEqualMax, Function | MediumTest | Level3)
W
wenjun 已提交
244
{
M
mamingshuai 已提交
245 246 247
    const char* info1 = "abcd123456789012345678901234567890123456789012345678901234567890";
    const char* info2 = "123456789012345678901234567890123456789012345678901234567890abcde";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
248
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
M
mamingshuai 已提交
249
    ASSERT_EQ(true, devInfo != NULL);
W
wenjun 已提交
250 251
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVNAME;
M
mamingshuai 已提交
252 253 254
    devInfo->value = info1;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
255

M
mamingshuai 已提交
256 257 258
    devInfo->value = info2;
    ret = SetCommonDeviceInfo(devInfo, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
259 260 261 262 263 264 265 266
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0800
 * @tc.name      : set type value not in enum
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
267
HWTEST_F(ActsSoftBusTest, testSetDevTypeError, Function | MediumTest | Level3)
W
wenjun 已提交
268
{
M
mamingshuai 已提交
269 270
    const char* info = "error";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
271
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
M
mamingshuai 已提交
272
    ASSERT_EQ(true, devInfo != NULL);
W
wenjun 已提交
273 274
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVTYPE;
M
mamingshuai 已提交
275 276 277
    devInfo->value = info;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
278 279 280 281 282 283 284 285
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1000
 * @tc.name      : set key is error
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
286
HWTEST_F(ActsSoftBusTest, testSetDevKeyIsError, Function | MediumTest | Level2)
W
wenjun 已提交
287
{
M
mamingshuai 已提交
288
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
289
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
M
mamingshuai 已提交
290
    ASSERT_EQ(true, devInfo != NULL);
W
wenjun 已提交
291 292 293 294 295
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_MAX;
    devInfo->value = g_devId;
    unsigned int num = 1;
    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
296
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
297 298 299 300 301 302 303 304
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1300
 * @tc.name      : Test publish with invalid parameter
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
305
HWTEST_F(ActsSoftBusTest, testPublishParameterIsNull, Function | MediumTest | Level2)
W
wenjun 已提交
306
{
M
mamingshuai 已提交
307
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
308
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
M
mamingshuai 已提交
309
    ASSERT_EQ(true, pubInfo!= NULL);
W
wenjun 已提交
310 311 312 313 314 315 316 317 318 319 320
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = MID;
    pubInfo->capability = g_pubCapability;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITY_DATA_LEN;

    ResetPubFlag();
    int ret = PublishService(NULL, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
321
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
322
    WaitPublishResult();
M
mamingshuai 已提交
323
    EXPECT_EQ(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
324
    ret = PublishService(g_pubModuleName, NULL, &g_pubCallback);
M
mamingshuai 已提交
325
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
326
    ret = PublishService(g_pubModuleName, pubInfo, NULL);
M
mamingshuai 已提交
327
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
328
    ret = PublishService(NULL, NULL, NULL);
M
mamingshuai 已提交
329
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
330 331 332 333 334
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1500
M
mamingshuai 已提交
335
 * @tc.name      : set package name value equal and greater than maximum value
W
wenjun 已提交
336 337
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
338
HWTEST_F(ActsSoftBusTest, testPublishPkgNameAbnormal, Function | MediumTest | Level2)
W
wenjun 已提交
339
{
M
mamingshuai 已提交
340
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
341
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
M
mamingshuai 已提交
342
    ASSERT_EQ(true, pubInfo!= NULL);
W
wenjun 已提交
343 344 345 346 347 348 349 350 351 352 353
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = MID;
    pubInfo->capability = g_pubCapability;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITY_DATA_LEN;

    ResetPubFlag();
    int ret = PublishService(NULL, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
354
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
355
    WaitPublishResult();
M
mamingshuai 已提交
356
    EXPECT_EQ(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
357 358

    ResetPubFlag();
M
mamingshuai 已提交
359
    const char* pkgNameMax = "123456789012345678901234567890123456789012345678901234567890abcd";
W
wenjun 已提交
360
    ret = PublishService(pkgNameMax, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
361
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
362
    WaitPublishResult();
M
mamingshuai 已提交
363
    EXPECT_EQ(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
364 365

    ResetPubFlag();
M
mamingshuai 已提交
366
    const char* pkgNameMoreMax = "abcde123456789012345678901234567890123456789012345678901234567890";
W
wenjun 已提交
367
    ret = PublishService(pkgNameMoreMax, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
368
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
369
    WaitPublishResult();
M
mamingshuai 已提交
370
    EXPECT_EQ(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
371 372

    ret = UnPublishService(pkgNameMax, DEF_PUB_ID);
M
mamingshuai 已提交
373
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
374
    ret = UnPublishService(pkgNameMoreMax, DEF_PUB_ID);
M
mamingshuai 已提交
375
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
376 377 378 379 380 381 382 383
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1700
 * @tc.name      : set capability value not in list
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
384
HWTEST_F(ActsSoftBusTest, testPublishCapabilityError, Function | MediumTest | Level3)
W
wenjun 已提交
385
{
M
mamingshuai 已提交
386
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
387
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
M
mamingshuai 已提交
388
    EXPECT_EQ(true, pubInfo!= NULL);
W
wenjun 已提交
389 390 391 392 393 394 395 396 397 398 399
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = MID;
    pubInfo->capability = (char *)"error capability";
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITY_DATA_LEN;

    ResetPubFlag();
    int ret = PublishService(g_pubModuleName, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
400
    EXPECT_NE(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
401
    WaitPublishResult();
M
mamingshuai 已提交
402
    EXPECT_EQ(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
403 404
    free(pubInfo);
}