softbus_func_test.c 38.6 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 46 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
 * 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 "hctest.h"
#include "discovery_service.h"
#include "session.h"
#include "securec.h"
#include <unistd.h>
#include <stdlib.h>

#define PUB_SUCCESS 1
#define PUB_FAIL (-1)
#define TESTCASE_COUNT 23
#define SESSION_NAME_LEN 64
#define SOFTBUS_TEST_SUCCESS 0
#define SOFTBUS_TEST_FAIL (-1)
#define ONE_SECOND 1
#define DEF_TIMEOUT 6
#define DEF_DEVTYPE "L0"
#define DEF_DEVID "sb_test_default_devid"
#define DEF_PUB_ID 33113322
#define DEF_PUB_MODULE_NAME "sb_pub_module_name"
#define DEF_PUB_CAPABILITY "ddmpCapability"
#define DEF_PUB_CAPABILITYDATA_LEN 2
#define DEVID_MAX_LEN 96

static int g_pubFlag = 0;
static int g_setUpFlag = 0;
static int g_caseExeCount = 0;
static unsigned char* g_pubCapabilityData = (unsigned char*)"Hi";
static IPublishCallback g_pubCallback = {0};
static struct ISessionListener *g_sessionListenerCallback = NULL;

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
 */
static void SbPublishSuccess(int pubId)
{
    printf("[PubSuccess]publish success id[%d].\n", pubId);
    g_pubFlag = PUB_SUCCESS;
}

/**
 * callback of publish fail
 */
static 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
 */
static int SbSessionOpened(int sessionId)
{
    if (sessionId < 0) {
        printf("[Session opened]sessionId is invalid.\n");
    }
    return SOFTBUS_TEST_SUCCESS;
}

/**
 * callback of session closed
 */
static void SbSessionClosed(int sessionId)
{
    if (sessionId < 0) {
        printf("[Session closed]sessionId is invalid.\n");
    }
    printf("[Session closed]session closed.\n");
}

/**
 * callback of received data
 */
static void SbOnBytesReceived(int sessionId, const void *data, unsigned int len)
{
    if (sessionId < 0 || data == NULL) {
M
mamingshuai 已提交
113
        printf("[Session receive]id or data invalid, .\n");
W
wenjun 已提交
114
    }
M
mamingshuai 已提交
115
    printf("[Session receive]receive data, length[%u].\n", len);
W
wenjun 已提交
116 117 118 119 120 121 122
}

/**
 * init service during first publish
 */
static void DefaultPublishToInitService(void)
{
M
mamingshuai 已提交
123
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
    if (ret != SOFTBUS_TEST_SUCCESS) {
M
mamingshuai 已提交
141
        printf("[DefaultPublishToInitService]call PublishService fail, ret=%d\n", ret);
W
wenjun 已提交
142 143 144
    } else {
        WaitPublishResult();
        if (g_pubFlag != PUB_SUCCESS) {
M
mamingshuai 已提交
145
            printf("[DefaultPublishToInitService]wait publish result fail!\n");
W
wenjun 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
        }
    }
    free(pubInfo);
}

/**
 * undo the first publish
 */
static void UnDefaultPublish(void)
{
    int ret;
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
    if (ret != SOFTBUS_TEST_SUCCESS) {
M
mamingshuai 已提交
159
        printf("[UnDefaultPublish]unpublish fail, ret=%d\n", ret);
W
wenjun 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }
}

/**
 * @tc.desc      : register a test suite, this suite is used to test function
 * @param        : subsystem name is communication
 * @param        : module name is softbus
 * @param        : test suit name is SoftBusFuncTestSuite
 */
LITE_TEST_SUIT(communication, softbus, SoftBusFuncTestSuite);

/**
 * @tc.setup     : setup for all testcases
 * @return       : setup result, TRUE is success, FALSE is fail
 */
static BOOL SoftBusFuncTestSuiteSetUp(void)
{
    if (g_setUpFlag == 0) {
        g_pubCallback.onPublishSuccess = SbPublishSuccess;
        g_pubCallback.onPublishFail = SbPublishFail;
        g_sessionListenerCallback = (struct ISessionListener*)malloc(sizeof(struct ISessionListener));
        if (g_sessionListenerCallback == NULL) {
            printf("SetUp:malloc(g_sessionListenerCallback) fail!\n");
            return FALSE;
        }
        g_sessionListenerCallback->onSessionOpened = SbSessionOpened;
        g_sessionListenerCallback->onSessionClosed = SbSessionClosed;
        g_sessionListenerCallback->onBytesReceived = SbOnBytesReceived;
        g_setUpFlag = 1;

        DefaultPublishToInitService();
        UnDefaultPublish();
    }
    g_caseExeCount++;
    return TRUE;
}

/**
 * @tc.teardown  : teardown for all testcases
 * @return       : teardown result, TRUE is success, FALSE is fail
 */
static BOOL SoftBusFuncTestSuiteTearDown(void)
{
    if (g_caseExeCount == TESTCASE_COUNT) {
        if (g_sessionListenerCallback != NULL) {
            free(g_sessionListenerCallback);
            g_sessionListenerCallback = NULL;
        }
    }
    printf("+----------------------------------------------------------+\n");
    return TRUE;
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0100
 * @tc.name      : abnormal parameter test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
218
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetNumGreaterThanMax, Function | MediumTest | Level3)
W
wenjun 已提交
219
{
M
mamingshuai 已提交
220
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
221 222 223 224 225 226 227
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
    devInfo->value = DEF_DEVID;
    
    int ret = SetCommonDeviceInfo(devInfo, COMM_DEVICE_KEY_MAX + 1);
M
mamingshuai 已提交
228
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
229 230
    int num = 0;
    ret  = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
231
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
232 233
    num = 1;
    ret  = SetCommonDeviceInfo(NULL, num);
M
mamingshuai 已提交
234
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
235 236 237 238 239 240 241 242
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0200
 * @tc.name      : set ID value less than maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
243
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevIdLessThanMax, Function | MediumTest | Level2)
W
wenjun 已提交
244
{
M
mamingshuai 已提交
245 246 247
    const char* info =
        "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890abcde";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
248 249 250 251
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
M
mamingshuai 已提交
252 253 254
    devInfo->value = info;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
255 256 257 258 259 260 261 262
    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 已提交
263
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevIdEqualMax, Function | MediumTest | Level3)
W
wenjun 已提交
264
{
M
mamingshuai 已提交
265 266 267 268 269
    const char* info1 =
        "abcdef123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    const char* info2 =
        "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890abcdefg";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
270 271 272 273
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
M
mamingshuai 已提交
274 275 276 277 278 279
    devInfo->value = info1;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
    devInfo->value = info2;
    ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
280 281 282 283 284 285 286 287
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0400
 * @tc.name      : set ID value with special characters
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
288
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevIdSpecialChars, Function | MediumTest | Level2)
W
wenjun 已提交
289
{
M
mamingshuai 已提交
290 291
    const char* info = "!@#$%^&*()_+:><?\n\0\r/.,[123]";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
292 293 294 295
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVID;
M
mamingshuai 已提交
296 297 298
    devInfo->value = info;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
299 300 301 302 303 304 305 306
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0500
 * @tc.name      : set name value less than maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
307
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevNameLessThanMax, Function | MediumTest | Level2)
W
wenjun 已提交
308
{
M
mamingshuai 已提交
309 310
    const char* info = "123456789012345678901234567890123456789012345678901234567890abc";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
311 312 313 314
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVNAME;
M
mamingshuai 已提交
315 316 317
    devInfo->value = info;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
318 319 320 321 322 323 324 325
    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 已提交
326
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevNameEqualMax, Function | MediumTest | Level3)
W
wenjun 已提交
327
{
M
mamingshuai 已提交
328 329 330
    const char* info1 = "abcd123456789012345678901234567890123456789012345678901234567890";
    const char* info2 = "123456789012345678901234567890123456789012345678901234567890abcde";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
331 332 333 334
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVNAME;
M
mamingshuai 已提交
335
    devInfo->value = info1;
W
wenjun 已提交
336 337
    int num = 1;
    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
338
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
339

M
mamingshuai 已提交
340
    devInfo->value = info2;
W
wenjun 已提交
341
    ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
342
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
343 344 345 346 347 348 349 350
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0700
 * @tc.name      : set name value with special characters
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
351
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevNameSpecialChars, Function | MediumTest | Level2)
W
wenjun 已提交
352
{
M
mamingshuai 已提交
353 354
    const char* info = "!@#$%^&*()_+:><?\n\0\r/.,[123]";
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
355 356 357 358
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVNAME;
M
mamingshuai 已提交
359 360 361
    devInfo->value = info;
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
362 363 364 365 366 367 368 369
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0800
 * @tc.name      : set type value not in enum
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
370
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevTypeError, Function | MediumTest | Level3)
W
wenjun 已提交
371
{
M
mamingshuai 已提交
372
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
373 374 375 376 377
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVTYPE;
    devInfo->value = "error";
M
mamingshuai 已提交
378 379
    int ret = SetCommonDeviceInfo(devInfo, 1);
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
380 381 382 383 384 385 386 387
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_0900
 * @tc.name      : set type value not match actual
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
388
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevTypeNotMacthActual, Function | MediumTest | Level2)
W
wenjun 已提交
389 390 391 392 393 394 395 396 397
{
    CommonDeviceInfo *devInfo = NULL;
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    devInfo->key = COMM_DEVICE_KEY_DEVTYPE;
    devInfo->value = "PAD";
    unsigned int num = 1;
    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
398
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
399 400 401 402 403 404 405 406
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1000
 * @tc.name      : set key is error
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
407
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetDevKeyIsError, Function | MediumTest | Level2)
W
wenjun 已提交
408 409 410 411 412 413 414 415 416 417
{
    CommonDeviceInfo *devInfo = NULL;
    devInfo = (CommonDeviceInfo *)malloc(sizeof(CommonDeviceInfo));
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, sizeof(CommonDeviceInfo), 0, sizeof(CommonDeviceInfo));
    int errorKey = 10;
    devInfo->key = errorKey;
    devInfo->value = DEF_DEVID;
    unsigned int num = 1;
    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
418
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
419 420 421 422 423 424 425 426
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1100
 * @tc.name      : set all three
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
427
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetAllDevInfo, Function | MediumTest | Level2)
W
wenjun 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
    int num = 3;
    CommonDeviceInfo *devInfo = NULL;
    int size = sizeof(CommonDeviceInfo)*num;
    devInfo = (CommonDeviceInfo *)malloc(size);
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, size, 0, size);
    devInfo[2].key = COMM_DEVICE_KEY_DEVID;
    devInfo[2].value = DEF_DEVID;
    devInfo[0].key = COMM_DEVICE_KEY_DEVTYPE;
    devInfo[0].value = DEF_DEVTYPE;
    devInfo[1].key = COMM_DEVICE_KEY_DEVNAME;
    devInfo[1].value = "sb_test_default_devname";

    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
443
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
444 445 446 447 448 449 450 451
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1200
 * @tc.name      : first set id and type ,but type is error, will set fail, then set name, will success
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
452
LITE_TEST_CASE(SoftBusFuncTestSuite, testSetTwoInfoOneIsError, Function | MediumTest | Level2)
W
wenjun 已提交
453 454
{
    int num = 2;
M
mamingshuai 已提交
455
    CommonDeviceInfo* devInfo = NULL;
W
wenjun 已提交
456 457 458 459 460 461 462 463 464 465
    int size = sizeof(CommonDeviceInfo)*num;
    devInfo = (CommonDeviceInfo *)malloc(size);
    TEST_ASSERT_NOT_NULL(devInfo);
    (void)memset_s(devInfo, size, 0, size);
    devInfo[0].key = COMM_DEVICE_KEY_DEVID;
    devInfo[0].value = DEF_DEVTYPE;
    devInfo[1].key = COMM_DEVICE_KEY_DEVTYPE;
    devInfo[1].value = "error";

    int ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
466
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
467 468 469 470
    num = 1;
    devInfo[0].key = COMM_DEVICE_KEY_DEVNAME;
    devInfo[0].value = "testSetTwoOneIsError";
    ret = SetCommonDeviceInfo(devInfo, num);
M
mamingshuai 已提交
471
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
472 473 474 475 476 477 478 479
    free(devInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1300
 * @tc.name      : Test publish with invalid parameter
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
480
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishParameterIsNull, Function | MediumTest | Level2)
W
wenjun 已提交
481
{
M
mamingshuai 已提交
482
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(NULL, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
496
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
497
    WaitPublishResult();
M
mamingshuai 已提交
498
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
499
    ret = PublishService(DEF_PUB_MODULE_NAME, NULL, &g_pubCallback);
M
mamingshuai 已提交
500
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
501
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, NULL);
M
mamingshuai 已提交
502
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
503
    ret = PublishService(NULL, NULL, NULL);
M
mamingshuai 已提交
504
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
505 506 507 508 509 510 511 512
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1400
 * @tc.name      : set name value empty and less than maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
513
LITE_TEST_CASE(SoftBusFuncTestSuite, testPubPkgNameNormal, Function | MediumTest | Level2)
W
wenjun 已提交
514
{
M
mamingshuai 已提交
515
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
516 517 518 519 520 521 522 523 524 525 526 527
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
M
mamingshuai 已提交
528
    char* pkgNameEmpty = "";
W
wenjun 已提交
529
    int ret = PublishService(pkgNameEmpty, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
530
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
531
    WaitPublishResult();
M
mamingshuai 已提交
532
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
533 534

    ResetPubFlag();
M
mamingshuai 已提交
535
    char* pkgNameLessMax = "123456789012345678901234567890123456789012345678901234567890abc";
W
wenjun 已提交
536
    ret = PublishService(pkgNameLessMax, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
537
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
538
    WaitPublishResult();
M
mamingshuai 已提交
539
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
540 541

    ret = UnPublishService(pkgNameEmpty, DEF_PUB_ID);
M
mamingshuai 已提交
542
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
543
    ret = UnPublishService(pkgNameLessMax, DEF_PUB_ID);
M
mamingshuai 已提交
544
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
545 546 547 548 549 550 551 552
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1500
 * @tc.name      : set package name value equal and greater than maximum value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
553
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishPkgNameAbnormal, Function | MediumTest | Level2)
W
wenjun 已提交
554
{
M
mamingshuai 已提交
555
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(NULL, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
569
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
570
    WaitPublishResult();
M
mamingshuai 已提交
571
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
572 573

    ResetPubFlag();
M
mamingshuai 已提交
574
    char* pkgNameMax = "123456789012345678901234567890123456789012345678901234567890abcd";
W
wenjun 已提交
575
    ret = PublishService(pkgNameMax, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
576
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
577
    WaitPublishResult();
M
mamingshuai 已提交
578
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
579 580

    ResetPubFlag();
M
mamingshuai 已提交
581
    char* pkgNameMoreMax = "abcde123456789012345678901234567890123456789012345678901234567890";
W
wenjun 已提交
582
    ret = PublishService(pkgNameMoreMax, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
583
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
584
    WaitPublishResult();
M
mamingshuai 已提交
585
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
586 587

    ret = UnPublishService(pkgNameMax, DEF_PUB_ID);
M
mamingshuai 已提交
588
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
589
    ret = UnPublishService(pkgNameMoreMax, DEF_PUB_ID);
M
mamingshuai 已提交
590
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
591 592 593 594 595 596 597 598
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1600
 * @tc.name      : test publish limit
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
599
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishCountLimit, Function | MediumTest | Level2)
W
wenjun 已提交
600
{
M
mamingshuai 已提交
601
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
602 603 604 605 606 607 608 609 610 611 612 613
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
M
mamingshuai 已提交
614
    char* pkgNameOne = "one";
W
wenjun 已提交
615
    int ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
616
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
617
    WaitPublishResult();
M
mamingshuai 已提交
618
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
619 620

    ResetPubFlag();
M
mamingshuai 已提交
621
    char* pkgNameTwo = "two";
W
wenjun 已提交
622
    ret = PublishService(pkgNameTwo, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
623
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
624
    WaitPublishResult();
M
mamingshuai 已提交
625
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
626 627

    ResetPubFlag();
M
mamingshuai 已提交
628
    char* pkgNameThree = "three";
W
wenjun 已提交
629
    ret = PublishService(pkgNameThree, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
630
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
631
    WaitPublishResult();
M
mamingshuai 已提交
632
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
633 634

    ResetPubFlag();
M
mamingshuai 已提交
635
    char* pkgNameFour = "four";
W
wenjun 已提交
636
    ret = PublishService(pkgNameFour, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
637
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
638
    WaitPublishResult();
M
mamingshuai 已提交
639
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
640 641 642
    ResetPubFlag();
    pubInfo->publishId = DEF_PUB_ID + 1;
    ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
643
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
644
    WaitPublishResult();
M
mamingshuai 已提交
645
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
646 647
    ResetPubFlag();
    ret = PublishService(pkgNameFour, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
648
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
649
    WaitPublishResult();
M
mamingshuai 已提交
650
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
651 652 653 654
    pubInfo->publishId = DEF_PUB_ID;

    ResetPubFlag();
    ret = PublishService(pkgNameTwo, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
655
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
656
    WaitPublishResult();
M
mamingshuai 已提交
657
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
658 659

    ret = UnPublishService(pkgNameOne, DEF_PUB_ID);
M
mamingshuai 已提交
660
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
661 662
    ResetPubFlag();
    ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
663
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
664
    WaitPublishResult();
M
mamingshuai 已提交
665
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
666 667
    ResetPubFlag();
    ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
668
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
669
    WaitPublishResult();
M
mamingshuai 已提交
670
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
671 672

    ret = UnPublishService(pkgNameThree, DEF_PUB_ID);
M
mamingshuai 已提交
673
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
674 675
    ResetPubFlag();
    ret = PublishService(pkgNameFour, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
676
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
677
    WaitPublishResult();
M
mamingshuai 已提交
678
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
679 680

    ret = UnPublishService(pkgNameTwo, DEF_PUB_ID);
M
mamingshuai 已提交
681
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
682
    ret = UnPublishService(pkgNameOne, DEF_PUB_ID);
M
mamingshuai 已提交
683
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
684
    ret = UnPublishService(pkgNameThree, DEF_PUB_ID);
M
mamingshuai 已提交
685
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
686
    ret = UnPublishService(pkgNameFour, DEF_PUB_ID);
M
mamingshuai 已提交
687
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
688

M
mamingshuai 已提交
689
    char* pkgNameComm = "common";
W
wenjun 已提交
690 691 692 693
    int pubIdOne = 110;
    pubInfo->publishId = pubIdOne;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
694
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
695
    WaitPublishResult();
M
mamingshuai 已提交
696
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
697 698 699 700
    int pubIdTwo = 220;
    pubInfo->publishId = pubIdTwo;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
701
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
702
    WaitPublishResult();
M
mamingshuai 已提交
703
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
704 705 706 707
    int pubIdThree = 330;
    pubInfo->publishId = pubIdThree;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
708
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
709
    WaitPublishResult();
M
mamingshuai 已提交
710
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
711 712 713 714 715

    int pubIdFour = 440;
    pubInfo->publishId = pubIdFour;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
716
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
717
    WaitPublishResult();
M
mamingshuai 已提交
718
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
719 720 721
    pubInfo->publishId = pubIdThree;
    ResetPubFlag();
    ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
722
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
723
    WaitPublishResult();
M
mamingshuai 已提交
724
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
725 726 727
    pubInfo->publishId = pubIdFour;
    ResetPubFlag();
    ret = PublishService(pkgNameOne, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
728
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
729
    WaitPublishResult();
M
mamingshuai 已提交
730
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
731 732 733 734

    ResetPubFlag();
    pubInfo->publishId = pubIdOne;
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
735
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
736
    WaitPublishResult();
M
mamingshuai 已提交
737
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
738 739

    ret = UnPublishService(pkgNameComm, pubIdOne);
M
mamingshuai 已提交
740
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
741 742 743
    pubInfo->publishId = pubIdOne;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
744
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
745
    WaitPublishResult();
M
mamingshuai 已提交
746
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
747 748
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
749
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
750
    WaitPublishResult();
M
mamingshuai 已提交
751
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
752 753

    ret = UnPublishService(pkgNameComm, pubIdOne);
M
mamingshuai 已提交
754
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
755 756 757 758
    int pubIdFive = 555;
    pubInfo->publishId = pubIdFive;
    ResetPubFlag();
    ret = PublishService(pkgNameComm, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
759
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
760
    WaitPublishResult();
M
mamingshuai 已提交
761
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
762 763

    ret = UnPublishService(pkgNameComm, pubIdFive);
M
mamingshuai 已提交
764
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
765
    ret = UnPublishService(pkgNameComm, pubIdThree);
M
mamingshuai 已提交
766
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
767
    ret = UnPublishService(pkgNameComm, pubIdTwo);
M
mamingshuai 已提交
768
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
769
    ret = UnPublishService(pkgNameComm, pubIdOne);
M
mamingshuai 已提交
770
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
771 772 773 774 775 776 777 778 779

    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1700
 * @tc.name      : set capability value not in list
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
780
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishCapabilityError, Function | MediumTest | Level3)
W
wenjun 已提交
781
{
M
mamingshuai 已提交
782
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = "error capability";
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
796
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
797
    WaitPublishResult();
M
mamingshuai 已提交
798
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
799 800 801 802 803 804 805 806
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1800
 * @tc.name      : this case used to cover interface only
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
807
LITE_TEST_CASE(SoftBusFuncTestSuite, testSessionSample, Function | MediumTest | Level3)
W
wenjun 已提交
808 809
{
    int randomId = 5;
M
mamingshuai 已提交
810
    char* moduleName = "default_test_module_name";
W
wenjun 已提交
811
    int ret = CreateSessionServer(moduleName, "ohos", g_sessionListenerCallback);
M
mamingshuai 已提交
812
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
813 814 815

    char mySessionName[SESSION_NAME_LEN] = {0};
    ret = GetMySessionName(randomId, (char *)mySessionName, SESSION_NAME_LEN);
M
mamingshuai 已提交
816
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
817 818 819

    char peerSeesionName[SESSION_NAME_LEN] = {0};
    ret = GetPeerSessionName(randomId, (char *)peerSeesionName, SESSION_NAME_LEN);
M
mamingshuai 已提交
820
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
821 822 823

    char peerDeviceId[DEVID_MAX_LEN] = {0};
    ret = GetPeerDeviceId(randomId, (char *)peerDeviceId, DEVID_MAX_LEN);
M
mamingshuai 已提交
824
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
825 826

    int size = 10;
M
mamingshuai 已提交
827
    char* sampleCharData = "L0->phone";
W
wenjun 已提交
828
    ret = SendBytes(randomId, sampleCharData, size);
M
mamingshuai 已提交
829
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
830 831 832 833

    CloseSession(randomId);

    ret = RemoveSessionServer(moduleName, (char *)mySessionName);
M
mamingshuai 已提交
834
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
835 836 837 838 839 840 841
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_1900
 * @tc.name      : test pubInfo-publishId mode medium invalid value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
842
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishInfoInvalidValue, Function | MediumTest | Level3)
W
wenjun 已提交
843 844
{
    int pubId = -1;
M
mamingshuai 已提交
845
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = pubId;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = MID;
    pubInfo->capability = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
859
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
860
    WaitPublishResult();
M
mamingshuai 已提交
861
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
862 863 864 865 866

    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_ACTIVE;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
867
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
868
    WaitPublishResult();
M
mamingshuai 已提交
869
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
870
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
871
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
872 873 874 875 876

    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = BLE;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
877
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
878
    WaitPublishResult();
M
mamingshuai 已提交
879
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
880 881 882 883

    pubInfo->medium = AUTO;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
884
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
885
    WaitPublishResult();
M
mamingshuai 已提交
886
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
887 888 889 890

    pubInfo->medium = 10;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
891
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
892
    WaitPublishResult();
M
mamingshuai 已提交
893
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
894 895

    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
896
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
897 898 899 900 901 902 903 904
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_2000
 * @tc.name      : test pubInfo-freq value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
905
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishInfoFreq, Function | MediumTest | Level3)
W
wenjun 已提交
906
{
M
mamingshuai 已提交
907
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (void)memset_s(pubInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
    pubInfo->publishId = DEF_PUB_ID;
    pubInfo->mode = DISCOVER_MODE_PASSIVE;
    pubInfo->medium = COAP;
    pubInfo->freq = LOW;
    pubInfo->capability = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
921
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
922
    WaitPublishResult();
M
mamingshuai 已提交
923
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
924
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
925
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
926 927 928 929

    pubInfo->freq = HIGH;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
930
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
931
    WaitPublishResult();
M
mamingshuai 已提交
932
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
933
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
934
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
935 936 937 938

    pubInfo->freq = SUPER_HIGH;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
939
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
940
    WaitPublishResult();
M
mamingshuai 已提交
941
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
942
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
943
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
944 945 946 947

    pubInfo->freq = -1;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
948
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
949
    WaitPublishResult();
M
mamingshuai 已提交
950
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
951
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
952
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
953 954 955 956 957 958 959 960 961

    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_2100
 * @tc.name      : test pubInfo-capability invalid value
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
962
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishInfoCapability, Function | MediumTest | Level3)
W
wenjun 已提交
963
{
M
mamingshuai 已提交
964
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = "hicall";
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
978
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
979
    WaitPublishResult();
M
mamingshuai 已提交
980
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
981 982 983 984

    pubInfo->capability = "error capability";
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
985
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
986
    WaitPublishResult();
M
mamingshuai 已提交
987
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
988 989 990 991

    pubInfo->capability = NULL;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
992
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
993
    WaitPublishResult();
M
mamingshuai 已提交
994
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
995 996

    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
997
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
998 999 1000 1001 1002 1003 1004 1005
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_2200
 * @tc.name      : set capabilitydata value invalid
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
1006
LITE_TEST_CASE(SoftBusFuncTestSuite, testPublishCapabilityDataError, Function | MediumTest | Level3)
W
wenjun 已提交
1007
{
M
mamingshuai 已提交
1008
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = NULL;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
1022
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1023
    WaitPublishResult();
M
mamingshuai 已提交
1024
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
1025 1026 1027 1028 1029

    pubInfo->capabilityData = (unsigned char *)"1";
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN - 1;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
1030
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1031
    WaitPublishResult();
M
mamingshuai 已提交
1032
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
1033 1034 1035 1036 1037

    pubInfo->capabilityData = (unsigned char *)"123";
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN + 1;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
1038
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1039
    WaitPublishResult();
M
mamingshuai 已提交
1040
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
1041 1042 1043 1044 1045

    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN - 1;
    ResetPubFlag();
    ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
1046
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1047
    WaitPublishResult();
M
mamingshuai 已提交
1048
    TEST_ASSERT_EQUAL_INT(PUB_FAIL, g_pubFlag);
W
wenjun 已提交
1049 1050

    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
1051
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1052 1053 1054 1055 1056 1057 1058 1059
    free(pubInfo);
}

/**
 * @tc.number    : SUB_COMMUNICATION_SOFTBUS_SDK_2300
 * @tc.name      : test unpublish invalid parameters
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
1060
LITE_TEST_CASE(SoftBusFuncTestSuite, testUnPublishInvalidParam, Function | MediumTest | Level2)
W
wenjun 已提交
1061
{
M
mamingshuai 已提交
1062
    PublishInfo* pubInfo = NULL;
W
wenjun 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    pubInfo = (PublishInfo *)malloc(sizeof(PublishInfo));
    TEST_ASSERT_NOT_NULL(pubInfo);
    (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 = DEF_PUB_CAPABILITY;
    pubInfo->capabilityData = g_pubCapabilityData;
    pubInfo->dataLen = DEF_PUB_CAPABILITYDATA_LEN;

    ResetPubFlag();
    int ret = PublishService(DEF_PUB_MODULE_NAME, pubInfo, &g_pubCallback);
M
mamingshuai 已提交
1076
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1077
    WaitPublishResult();
M
mamingshuai 已提交
1078
    TEST_ASSERT_EQUAL_INT(PUB_SUCCESS, g_pubFlag);
W
wenjun 已提交
1079 1080

    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
1081
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1082
    ret = UnPublishService(DEF_PUB_MODULE_NAME, DEF_PUB_ID);
M
mamingshuai 已提交
1083
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1084 1085

    ret = UnPublishService(NULL, DEF_PUB_ID);
M
mamingshuai 已提交
1086
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1087
    ret = UnPublishService(DEF_PUB_MODULE_NAME, -1);
M
mamingshuai 已提交
1088
    TEST_ASSERT_NOT_EQUAL(SOFTBUS_TEST_SUCCESS, ret);
W
wenjun 已提交
1089 1090 1091 1092
    free(pubInfo);
}

RUN_TEST_SUITE(SoftBusFuncTestSuite);