ActsLibuvTest.cpp 6.2 KB
Newer Older
W
wangshi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 <gtest/gtest.h>
#include <stdlib.h>
L
lushi1202 已提交
18
#include "ActsLibuvTest.h"
W
wangshi 已提交
19 20 21
extern "C"{
#include "runner.h"
}
W
wanglichao 已提交
22 23 24
#include "uv.h"

typedef void (*init_plugin_function)();
W
wangshi 已提交
25 26 27 28

namespace OHOS {
    using namespace std;
    using namespace testing::ext;
W
wanglichao 已提交
29 30 31 32 33
    static const char TESTFILE[] = "foo.gz";

    static uv_once_t once_only = UV_ONCE_INIT;

    int i = 0;
W
wangshi 已提交
34

W
wanglichao 已提交
35 36 37 38
    void Increment()
    {
        i++;
    }
W
wangshi 已提交
39
    // Preset action of the test suite, which is executed before the first test case
L
lushi1202 已提交
40
    void ActsLibuvTest::SetUpTestCase(void)
W
wangshi 已提交
41 42 43
    {
    }
    // Test suite cleanup action, which is executed after the last test case
L
lushi1202 已提交
44
    void ActsLibuvTest::TearDownTestCase(void)
W
wangshi 已提交
45 46 47
    {
    }
    // Preset action of the test case
L
lushi1202 已提交
48
    void ActsLibuvTest::SetUp()
W
wangshi 已提交
49 50 51
    {
    }
    // Cleanup action of the test case
L
lushi1202 已提交
52
    void ActsLibuvTest::TearDown()
W
wangshi 已提交
53 54 55
    {
    }

W
wanglichao 已提交
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
/**
 * @tc.number    : ActsZlibTest_0100
 * @tc.name      : Test uv_version and uv_version_string test
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestUVVersion, Function | MediumTest | Level2)
{
    int version = uv_version();
    ASSERT_EQ(version, UV_VERSION_HEX);
    fprintf(stderr, "uv_version error: %d\n", version);

    auto versionString = uv_version_string();
    fprintf(stderr, "uv_version_string error: %s\n", versionString);

    int replaceAllocator = uv_replace_allocator(nullptr, nullptr, nullptr, nullptr);
    ASSERT_EQ(replaceAllocator, UV_EINVAL);
    fprintf(stderr, "uv_replace_allocator error: %d\n", replaceAllocator);

    uv_loop_t* loop = uv_loop_new();
    ASSERT_TRUE(loop != nullptr);
}

/**
 * @tc.number    : ActsZlibTest_0200
 * @tc.name      : Test uv_loop_new
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestLoop, Function | MediumTest | Level2)
{
    uv_loop_t* newLoop;
    newLoop = uv_loop_new();
    ASSERT_TRUE(newLoop != NULL);
    uv_loop_delete(newLoop);
    ASSERT_TRUE(true);
}

/**
 * @tc.number    : ActsZlibTest_0300
 * @tc.name      : Test uv_err_name_r
 * @tc.desc      : [C- SOFTWARE -0200]
*/

HWTEST_F(ActsLibuvTest, TestLibuvTestErrName, Function | MediumTest | Level2)
{
    int err = 0;
    char buf[40];
    size_t buflen = 40;
    char* newBuf = uv_err_name_r(err, buf, buflen);
    fprintf(stderr, "uv_err_name_r error: %s\n", newBuf);
    ASSERT_STREQ(newBuf, "Unknown system error 0");
}

/**
 * @tc.number    : ActsZlibTest_0400
 * @tc.name      : Test uv_req_get_data and  uv_req_set_data and uv_req_get_type
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestReqGetData, Function | MediumTest | Level2)
{
    float f = 5.5f;
    float* pf = &f;
    uv_req_t* req = (uv_req_t *)malloc(sizeof(uv_req_t));
    uv_req_t* setReq = (uv_req_t *)malloc(sizeof(uv_req_t));
    req->data = pf;
    req->type = UV_UNKNOWN_REQ;
    fprintf(stderr, "uv_req_get_data: %f\n", *(float*)uv_req_get_data(req));
    fprintf(stderr, "uv_req_get_type: %d\n", uv_req_get_type(req));
    ASSERT_EQ(uv_req_get_data(req), pf);
    ASSERT_EQ(uv_req_get_type(req), UV_UNKNOWN_REQ);

    uv_req_set_data(setReq, pf);
    ASSERT_EQ(uv_req_get_data(setReq), pf);
}

/**
 * @tc.number    : ActsZlibTest_0500
 * @tc.name      : Test uv_print_all_handles and uv_print_active_handles
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestPrintHandles, Function | MediumTest | Level2)
{
    uv_loop_t* loop = nullptr;
    FILE *fp = fopen(TESTFILE, "w");
    uv_print_all_handles(loop, fp);
    uv_print_active_handles(loop, fp);
    ASSERT_TRUE(true);
}

/**
 * @tc.number    : ActsZlibTest_0600
 * @tc.name      : Test uv_udp_get_send_queue_size and uv_udp_get_send_queue_count
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestUpdSendQueue, Function | MediumTest | Level2)
{
    size_t size, sizeC;
    uv_udp_t* handle = (uv_udp_t *)malloc(sizeof(uv_udp_t));
    handle->send_queue_size = 5;
    handle->send_queue_count = 5;
    size = uv_udp_get_send_queue_size(handle);
    ASSERT_TRUE(size == 5);

    sizeC = uv_udp_get_send_queue_count(handle);
    ASSERT_TRUE(sizeC == 5);
}

/**
 * @tc.number    : ActsZlibTest_0700
 * @tc.name      : Test uv_fs_lchown
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestFsLchown, Function | MediumTest | Level2)
{
    int retC;
    uv_loop_t loop;
    uv_fs_t req;
    const char *path = "/date/";
    uv_uid_t uid = 10000;
    uv_gid_t gid = 2020;
    retC = uv_fs_lchown(&loop, &req, path, uid, gid, nullptr);
    fprintf(stderr, "uv_err_name_r error: %d\n", retC);
}

/**
 * @tc.number    : ActsZlibTest_0800
 * @tc.name      : Test uv_disable_stdio_inheritance
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestDisableStdioInheritance, Function | MediumTest | Level2)
{
    uv_disable_stdio_inheritance();
    ASSERT_TRUE(true);
}

/**
 * @tc.number    : ActsZlibTest_0900
 * @tc.name      : Test uv_dlsym
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLibuvTest, TestLibuvTestDlsym, Function | MediumTest | Level2) {
    int argc = 3;
    const char *tmp[] = {"aaaa", "bbbb", "cccc"};
    const char **argv = tmp;
    uv_lib_t *lib = (uv_lib_t *)malloc(sizeof(uv_lib_t));
    while (--argc) {
        uv_dlopen(argv[argc], lib);
        ASSERT_TRUE(true);

        init_plugin_function init_plugin;
        uv_dlsym(lib, "initialize", (void **)&init_plugin);
        ASSERT_TRUE(true);
W
wangshi 已提交
207 208
    }
}
W
wanglichao 已提交
209 210 211 212 213 214 215 216 217 218 219 220

/**
 * @tc.number    : ActsZlibTest_1000
 * @tc.name      : Test uv_once
 * @tc.desc      : [C- SOFTWARE -0200]
*/
HWTEST_F(ActsLibuvTest, TestLibuvTestUvOnce, Function | MediumTest | Level2)
{
    uv_once(&once_only, Increment);
    ASSERT_TRUE(true);
}
}