 = 6666;

namespace OHOS {
class SoftbusCommonTest : public testing::Test {
public:
    SoftbusCommonTest()
    {}
    ~SoftbusCommonTest()
    {}
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    void SetUp();
    void TearDown();
};

int ThreadPoolTask(void* arg)
{
    pthread_mutex_lock(&g_isInitedLock);
    g_count++;
    pthread_mutex_unlock(&g_isInitedLock);
    return SOFTBUS_OK;
}

void SoftbusCommonTest::SetUpTestCase(void)
{
    pthread_mutex_init(&g_isInitedLock, nullptr);
    GTEST_LOG_(INFO) << "SoftbusCommonTestSetUp";
}

void SoftbusCommonTest::TearDownTestCase(void)
{
    g_count = 0;
    g_port++;
    GTEST_LOG_(INFO) << "+-------------------------------------------+";
}

void SoftbusCommonTest::SetUp(void)
{
    g_count = 0;
}

void SoftbusCommonTest::TearDown(void)
{
    g_count = 0;
}

int32_t ConnectEvent(int32_t events, int32_t cfd, const char *ip)
{
    return 0;
}

int32_t DataEvent(int32_t events, int32_t fd)
{
    return 0;
}

/*
* @tc.name: testBaseListener001
* @tc.desc: test GetSoftbusBaseListener invalid input param
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(SoftbusCommonTest, testBaseListener001, TestSize.Level1)
{
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, GetSoftbusBaseListener(PROXY, nullptr));
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, GetSoftbusBaseListener(AUTH, nullptr));
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, GetSoftbusBaseListener(DIRECT_CHANNEL_SERVER, nullptr));
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, GetSoftbusBaseListener(UNUSE_BUTT, nullptr));
};

/*
* @tc.name: testBaseListener002
* @tc.desc: test GetSoftbusBaseListener and set
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(SoftbusCommonTest, testBaseListener002, TestSize.Level1)
{
    int i;
    for (i = PROXY; i <= UNUSE_BUTT; i++) {
        EXPECT_EQ(SOFTBUS_INVALID_PARAM, SetSoftbusBaseListener(static_cast<ListenerModule>(i), nullptr));
    }
    SoftbusBaseListener *setListener = (SoftbusBaseListener *)malloc(sizeof(SoftbusBaseListener));
    ASSERT_TRUE(setListener!= nullptr);
    setListener->onConnectEvent = ConnectEvent;
    setListener->onDataEvent = DataEvent;
    for (i = PROXY; i < UNUSE_BUTT; i++) {
        SoftbusBaseListener *getListener = (SoftbusBaseListener *)malloc(sizeof(SoftbusBaseListener));
        if (getListener == nullptr) {
            free(setListener);
            return;
        }
        EXPECT_EQ(SOFTBUS_OK, SetSoftbusBaseListener(static_cast<ListenerModule>(i), setListener));
        EXPECT_EQ(SOFTBUS_OK, GetSoftbusBaseListener(static_cast<ListenerModule>(i), getListener));
        EXPECT_EQ(setListener->onConnectEvent, getListener->onConnectEvent);
        EXPECT_EQ(setListener->onDataEvent, getListener->onDataEvent);
        DestroyBaseListener(static_cast<ListenerModule>(i));
        if (getListener!= nullptr) {
            free(getListener);
        }
    }
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, SetSoftbusBaseListener(UNUSE_BUTT, setListener));
    free(setListener);
};

/*
* @tc.name: testBaseListener003
* @tc.desc: test start stop listener
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(SoftbusCommonTest, testBaseListener003, TestSize.Level1)
{
    ListenerModule module = PROXY;
    int port = 6666;
    EXPECT_EQ(SOFTBUS_ERR, StopBaseListener(module));
    SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
    ASSERT_TRUE(listener!= nullptr);
    listener->onConnectEvent = ConnectEvent;
    listener->onDataEvent = DataEvent;
    EXPECT_EQ(SOFTBUS_OK, SetSoftbusBaseListener(module, listener));
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, StartBaseListener(module, nullptr, port, SERVER_MODE));
    EXPECT_EQ(SOFTBUS_INVALID_PARAM, StartBaseListener(module, "127.0.0.1", -1, SERVER_MODE));
    EXPECT_EQ(port, StartBaseListener(module, "127.0.0.1", port, SERVER_MODE));
    EXPECT_EQ(SOFTBUS_ERR, StartBaseListener(module, "127.0.0.1", port, SERVER_MODE));
    EXPECT_EQ(SOFTBUS_OK, StopBaseListener(module));
    EXPECT_EQ(SOFTBUS_OK, StopBaseListener(module));
    DestroyBaseListener(module);
    free(listener);
};

/*
* @tc.name: testBaseListener004
* @tc.desc: test start client
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(SoftbusCommonTest, testBaseListener004, TestSize.Level1)
{
    ListenerModule module = DIRECT_CHANNEL_SERVER;
    EXPECT_EQ(SOFTBUS_ERR, StopBaseListener(module));
    SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
    ASSERT_TRUE(listener!= nullptr);
    listener->onConnectEvent = ConnectEvent;
    listener->onDataEvent = DataEvent;
    EXPECT_EQ(SOFTBUS_ERR, StartBaseClient(module));
    EXPECT_EQ(SOFTBUS_OK, SetSoftbusBaseListener(module, listener));
    EXPECT_EQ(SOFTBUS_OK, StartBaseClient(module));
    EXPECT_EQ(SOFTBUS_ERR, StartBaseClient(module));
    EXPECT_EQ(SOFTBUS_OK, StopBaseListener(module));
    DestroyBaseListener(module);
    free(listener);
};

/*
* @tc.name: testBaseListener005
* @tc.desc: test set start stop listener
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(SoftbusCommonTest, testBaseListener005, TestSize.Level1)
{
    int ret;
    int module;
    int port = 6666;
    for (module = PROXY; module < UNUSE_BUTT; module++) {
        SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
        if (listener == nullptr) {
            for (int i = 0; i < module; i++) {
                ret = StopBaseListener(static_cast<ListenerModule>(i));
                EXPECT_EQ(SOFTBUS_OK, ret);
                DestroyBaseListener(static_cast<ListenerModule>(i));
            }
            continue;
        }
        listener->onConnectEvent = ConnectEvent;
        listener->onDataEvent = DataEvent;
        ret = SetSoftbusBaseListener(static_cast<ListenerModule>(module), listener);
        EXPECT_EQ(SOFTBUS_OK, ret);
        ret = StartBaseListener(static_cast<ListenerModule>(module), "127.0.0.1",
            port + static_cast<ListenerModule>(module), SERVER_MODE);
        EXPECT_EQ(port + module, ret);
        free(listener);
    }
    for (module = PROXY; module < UNUSE_BUTT; module++) {
        ret = StopBaseListener