session_test.c 11.7 KB
Newer Older
1 2 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
/**
 * Copyright (c) 2021-2022 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 <string.h>
#include <unistd.h>

#include "hctest.h"
#include "securec.h"
#include "session.h"
#include "softbus_def.h"
#include "softbus_config_type.h"
#include "softbus_bus_center.h"
#include "softbus_errcode.h"

#define TEST_PKG_NAME "com.softbus.test"
#define DEFAULT_NODE_STATE_CB_NUM 9
#define LOG2_DBG(fmt, ...) printf("DEBUG:%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG2_INFO(fmt, ...) printf("INFO:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG2_WARN(fmt, ...) printf("WARN:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG2_ERR(fmt, ...)  printf("ERROR:%s:%d " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)

enum StatusNum {
W
wwx1101856 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
    TRANS_STATE_NONE = 0,
    TRANS_STATE_INIT,
    TRANS_STATE_CREATE_SESSION_SERVER,
    TRANS_SET_FILE_SEND_LISTENER,
    TRANS_STATE_OPEN,
    TRANS_STATE_SEND_BYTE,
    TRANS_STATE_SEND_MESSAGE,
    TRANS_STATE_SEND_FILE,
    TRANS_STATE_CLOSE,
    TRANS_STATE_REMOVE_SESSION_SERVER,
    TRANS_STATE_CREATE_PHONE = 10,
    TRANS_STATE_GET_SESSION_NAME,
    TRANS_STATE_GET_DEVICE_ID,
47 48 49
    TRANS_CLEAR_LOG,
    TRANS_TEST_FIN,

W
wwx1101856 已提交
50 51
    LNN_STATE_JOINLNN = 20,
    LNN_STATE_LEAVELNN,
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 113 114 115 116 117 118 119 120 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 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
};

const char *g_pkgName = "dms";
const char *g_sessionName = "ohos.distributedschedule.dms.test";
const char *g_networkid = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
const char *g_groupid = "TEST_GROUP_ID";
const char *g_testModuleName   = "com.plrdtest.dsoftbus";
const char *g_testSessionName = "com.plrdtest.dsoftbus.JtOnOpenFileSession";
const char *g_testSessionNamE2 = "com.plrdtest.dsoftbus.JtOpenFileSession";
static SessionAttribute g_sessionAttr = {
    .dataType = TYPE_BYTES,
};
const int FILE_NUM = 4;
int32_t g_sessionId = -1;
int32_t g_stateDebug = LNN_STATE_JOINLNN;

static void TestChangeDebugState(int32_t state)
{
    g_stateDebug = state;
    LOG2_INFO("change to debug state: %d", state);
}

static int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
{
    LOG2_INFO("OnSendFileProcess sessionId = %d, bytesUpload = %llu, total = %llu\n",
        sessionId, bytesUpload, bytesTotal);
    return 0;
}

static int OnSendFileFinished(int sessionId, const char *firstFile)
{
    LOG2_INFO("OnSendFileFinished sessionId = %d, first file = %s\n", sessionId, firstFile);
    TestChangeDebugState(TRANS_STATE_CLOSE);
    return 0;
}

static void OnFileTransError(int sessionId)
{
    LOG2_INFO("OnFileTransError sessionId = %d\n", sessionId);
}

static IFileSendListener g_fileSendListener = {
    .OnSendFileProcess = OnSendFileProcess,
    .OnSendFileFinished = OnSendFileFinished,
    .OnFileTransError = OnFileTransError,
};

static ISessionListener g_sessionlistener = {
    .OnSessionOpened = 1,
};

static int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
{
    LOG2_INFO("File receive start sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
    return 0;
}

static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
{
    LOG2_INFO("File receive finished sessionId = %d, first file = %s, fileCnt = %d\n", sessionId, files, fileCnt);
}

static IFileReceiveListener g_fileRecvListener = {
    .OnReceiveFileStarted = OnReceiveFileStarted,
    .OnReceiveFileFinished = OnReceiveFileFinished,
    .OnFileTransError = OnFileTransError,
};

LITE_TEST_SUIT(dsoftbus, session, SessionTestSuite);

static BOOL SessionTestSuiteSetUp(void)
{
    printf("----------test case with SessionTestSuite start-------------\n");
    return TRUE;
}

static BOOL SessionTestSuiteTearDown(void)
{
    printf("----------test case with SessionTestSuite end-------------\n");
    return TRUE;
}

/**
 * @tc.name: TestCreateSessionServer001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC99
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestCreateSessionServer001, Function | MediumTest | Level0)
{
    int ret;
    ret = CreateSessionServer(NULL, g_sessionName, &g_sessionlistener);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = CreateSessionServer(g_pkgName, NULL, &g_sessionlistener);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = CreateSessionServer(g_pkgName, g_sessionName, NULL);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
}

/**
 * @tc.name: TestRemoveSessionServer001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestRemoveSessionServer001, Function | MediumTest | Level0)
{
    int ret;
    ret = RemoveSessionServer(NULL, g_sessionName);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = RemoveSessionServer(g_pkgName, NULL);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
}

/**
 * @tc.name: TestOpenSession001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestOpenSession001, Function | MediumTest | Level0)
{
    int ret;
    g_sessionAttr.dataType = TYPE_BYTES;

    ret = OpenSession(NULL, g_sessionName, g_networkid, g_groupid, &g_sessionAttr);
    TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);

    ret = OpenSession(g_sessionName, NULL, g_networkid, g_groupid, &g_sessionAttr);
    TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);

    ret = OpenSession(g_sessionName, g_sessionName, NULL, g_groupid, &g_sessionAttr);
    TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);

    ret = OpenSession(g_sessionName, g_sessionName, g_networkid, NULL, &g_sessionAttr);
    TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);

    ret = OpenSession(g_sessionName, g_sessionName, g_networkid, g_groupid, NULL);
    TEST_ASSERT_EQUAL_INT(INVALID_SESSION_ID, ret);
}

/**
 * @tc.name: TestSendBytes001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSendBytes001, Function | MediumTest | Level0)
{
    int ret;
    int sessionId = 1;
    const char *data = "testdata";
    uint32_t len = strlen(data);
    uint32_t maxLen;

    ret = SendBytes(-1, data, len);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);

    ret = SendBytes(sessionId, NULL, len);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = SendBytes(sessionId, data, 0);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
}

/**
 * @tc.name: TestSendMessage001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSendMessage001, Function | MediumTest | Level0)
{
    int ret;
    int sessionId = 1;
    const char *data = "testdata";
    uint32_t len = strlen(data);
    uint32_t maxLen;

    ret = SendMessage(-1, data, len);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);

    ret = SendMessage(sessionId, NULL, len);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = SendMessage(sessionId, data, 0);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
}


/**
 * @tc.name: TestSendStream001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSendStream001, Function | MediumTest | Level0)
{
    int ret;
    int sessionId = 1;
    const StreamData streamData = {0};
    const StreamData ext = {0};
    const StreamFrameInfo param = {0};

    ret = SendStream(-1, &streamData, &ext, &param);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_TRANS_INVALID_SESSION_ID, ret);

    ret = SendStream(sessionId, NULL, &ext, &param);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = SendStream(sessionId, &streamData, NULL, &param);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);

    ret = SendStream(sessionId, &streamData, &ext, NULL);
    TEST_ASSERT_EQUAL_INT(SOFTBUS_INVALID_PARAM, ret);
}

/**
 * @tc.name: TestQosReport00
 * @tc.desc: test the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestQosReport001, Function | MediumTest | Level0)
{
    int ret;
    ret = QosReport(1, 1, QOS_IMPROVE);
    TEST_ASSERT_TRUE(ret != SOFTBUS_OK);

    ret = QosReport(1, 1, QOS_RECOVER);
    TEST_ASSERT_TRUE(ret != SOFTBUS_OK);

    ret = QosReport(1, 1, QOS_IMPROVE);
    TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
}

/**
 * @tc.name: TestSetFileSendListener001
 * @tc.desc: test the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSetFileSendListener001, Function | MediumTest | Level0)
{
    int ret = SetFileSendListener(g_testModuleName, g_testSessionName, NULL);
    TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
}

/**
 * @tc.name: TestSendFile001
 * @tc.desc: test the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSendFile001, Function | MediumTest | Level0)
{
    const char *sfileList[] = {
        "/data/big.tar",
        "/data/richu.jpg",
        "/data/richu-002.jpg",
        "/data/richu-003.jpg",
    };
    int ret = SendFile(g_sessionId, NULL, NULL, FILE_NUM);
    TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);

    ret = SendFile(g_sessionId, sfileList, NULL, 0);
    TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);

    ret = SendFile(g_sessionId, sfileList, NULL, FILE_NUM);
    TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
}

/**
 * @tc.name: TestSetFileReceiveListener001
 * @tc.desc: test the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestSetFileReceiveListener001, Function | MediumTest | Level0)
{
    int ret = SetFileReceiveListener(g_testModuleName, g_testSessionNamE2, NULL, "/data/");
    TEST_ASSERT_TRUE(ret == SOFTBUS_INVALID_PARAM);
}

/**
 * @tc.name: TestGetMySessionName
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestGetMySessionName001, Function | MediumTest | Level0)
{
    int ret;
W
wwx1101856 已提交
348
    int sessionId = -1;
349 350 351 352 353 354 355 356 357 358 359 360 361 362
    int len = 5;
    ret = GetMySessionName(sessionId, NULL, len);
    TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
}

/**
 * @tc.name: TestGetPeerSessionName001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestGetPeerSessionName001, Function | MediumTest | Level0)
{
    int ret;
W
wwx1101856 已提交
363
    int sessionId = -1;
364 365 366 367 368 369 370 371 372 373 374 375 376 377
    int len = 5;
    ret = GetPeerSessionName(sessionId, NULL, len);
    TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
}

/**
 * @tc.name: TestGetPeerDeviceId001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestGetPeerDeviceId001, Function | MediumTest | Level0)
{
    int ret;
W
wwx1101856 已提交
378
    int sessionId = -1;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    int len = 5;
    ret = GetPeerDeviceId(sessionId, NULL, len);
    TEST_ASSERT_EQUAL_INT(ret, SOFTBUS_INVALID_PARAM);
}

/**
 * @tc.name: TestGetSessionSide001
 * @tc.desc: extern module active publish, use the wrong parameter.
 * @tc.type: FUNC
 * @tc.require:
 */
LITE_TEST_CASE(SessionTestSuite, TestGetSessionSide001, Function | MediumTest | Level0)
{
    int ret = GetSessionSide(g_sessionId);
    TEST_ASSERT_TRUE(ret != SOFTBUS_OK);
}

RUN_TEST_SUITE(SessionTestSuite);