fatal_message.c 7.1 KB
Newer Older
W
wangjiahui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (c) 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 <info/fatal_message.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <test.h>
#include <pthread.h>
#include <unistd.h>
25
#include <sys/wait.h>
W
wangjiahui 已提交
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

#define EXPECT_TRUE(c)                \
    do                                \
    {                                 \
        if (!(c))                     \
            t_error("[%s] failed\n"); \
    } while (0)

typedef void (*TEST_FUN)(void);
static const int WAIT_TIME = 1;

/**
 * @tc.name      : get_fatal_message
 * @tc.desc      : Test the function of get_fatal_message.
 * @tc.level     : Level 0
 */
static void fatal_message_0010(void)
{
    fatal_msg_t *fatal_message = get_fatal_message();
    EXPECT_TRUE(fatal_message == NULL);
}

/**
 * @tc.name      : set_fatal_message
 * @tc.desc      : Test the function of set_fatal_message.
 * @tc.level     : Level 0
 */
static void fatal_message_0020(void)
{
    const char msg[1024] = {"abcdefghijklmnopqrstuvwxyz1234567890"};
    fatal_msg_t *fatal_message = NULL;

58
    int childRet = 0;
W
wangjiahui 已提交
59 60 61 62 63 64 65 66 67 68 69 70
    int pidParent = 0;
    int pidChild = 0;

    pid_t fpid;
    fpid = fork();
    if (fpid < 0) {
        t_printf("error in fork!");
    } else if (fpid == 0) {
        pidChild = getpid();
        set_fatal_message(msg);
        fatal_message = get_fatal_message();
        EXPECT_TRUE(strcmp(fatal_message->msg, msg) == 0);
71
        exit(0);
W
wangjiahui 已提交
72
    }
73 74
    waitpid(fpid, &childRet, 0);
    EXPECT_TRUE(childRet == 0);
W
wangjiahui 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

/**
 * @tc.name      : set_fatal_message
 * @tc.desc      : Test the multiple processes of set_fatal_message.
 * @tc.level     : Level 0
 */
static void fatal_message_0030(void)
{
    fatal_msg_t *fatal_message = NULL;

    const char msgChild[1024] = {"msgChild"};
    const char msgParent[1024] = {"msgParent"};

89
    int childRet = 0;
W
wangjiahui 已提交
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
    int pidChild = 0;
    int pidParent = 0;
    int pidCParent = 0;
    int pidCChild = 0;

    pid_t fpid;

    // start process
    fpid = fork();
    if (fpid < 0) {
        t_printf("error in fork!");
    } else if (fpid == 0) {
        pidChild = getpid();
    } else {
        pidParent = getpid();
        pid_t fpidChild;

        // start process again
        fpidChild = fork();
        if (fpidChild < 0) {
            t_printf("error in fork!");
        } else if (fpidChild == 0) {
            pidCChild = getpid();
            set_fatal_message(msgParent);
            fatal_message = get_fatal_message();
            EXPECT_TRUE(strcmp(fatal_message->msg, msgParent) == 0);
116
            exit(0);
W
wangjiahui 已提交
117 118 119 120 121
        } else {
            pidCParent = getpid();
            set_fatal_message(msgChild);
            fatal_message = get_fatal_message();
            EXPECT_TRUE(strcmp(fatal_message->msg, msgChild) == 0);
122 123
            waitpid(fpidChild, &childRet, 0);
            EXPECT_TRUE(childRet == 0);
W
wangjiahui 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        }
    }
}

/**
 * @tc.name      : set_fatal_message
 * @tc.desc      : Test the multiple processes of set_fatal_message,
*                  One of the threads crashed.
 * @tc.level     : Level 0
 */
static void fatal_message_0040(void)
{
    fatal_msg_t *fatal_message = NULL;

    const char msgChild[1024] = {"msgChild004"};
    const char msgParent[1024] = {"msgParent004"};

141
    int childRet = 0;
W
wangjiahui 已提交
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
    int pidChild = 0;
    int pidParent = 0;
    int pidCParent = 0;
    int pidCChild = 0;

    pid_t fpid;

    // start process
    fpid = fork();
    if (fpid < 0) {
        t_printf("error in fork!");
    } else if (fpid == 0) {
        pidChild = getpid();
    } else {
        pidParent = getpid();
        pid_t fpidChild;

        // start process again
        fpidChild = fork();
        if (fpidChild < 0) {
            t_printf("error in fork!");
        } else if (fpidChild == 0) {
            pidCChild = getpid();
            char *str = NULL;
            str[0] = 0;
            // Process crash. Unable to continue calling the set_fatal_message interface
        } else {
            pidCParent = getpid();
            set_fatal_message(msgParent);
            fatal_message = get_fatal_message();
            EXPECT_TRUE(strcmp(fatal_message->msg, msgParent) == 0);
173 174
            waitpid(fpidChild, &childRet, 0);
            EXPECT_TRUE(childRet != 0);
W
wangjiahui 已提交
175 176 177 178 179 180 181 182 183 184 185
        }
    }
}

void *ThreadFun1(void *arg)
{
    if (arg == NULL) {
        t_printf("ThreadFun1 arg is NULL");
    }
    fatal_msg_t *fatal_message = get_fatal_message();
    EXPECT_TRUE(strcmp(fatal_message->msg, (char *)arg) == 0);
186
    pthread_exit("ThreadFun1 Exit");
W
wangjiahui 已提交
187 188 189 190 191 192 193 194
}

void *ThreadFun2(void *arg)
{
    if (arg == NULL) {
        t_printf("ThreadFun2 arg is NULL");
    }
    fatal_msg_t *fatal_message = get_fatal_message();
195
    EXPECT_TRUE(strcmp(fatal_message->msg, (char *)arg) == 0);
W
wangjiahui 已提交
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
    pthread_exit("ThreadFun2 Exit");
}

/**
 * @tc.name      : set_fatal_message
 * @tc.desc      : Test the multithreading of set_fatal_message.
 * @tc.level     : Level 0
 */
static void fatal_message_0050(void)
{
    const char msgThread[1024] = {"msgThread"};
    int res;
    pthread_t fatalMessageThread1, fatalMessageThread2;

    set_fatal_message(msgThread);
    res = pthread_create(&fatalMessageThread1, NULL, ThreadFun1, (void *)msgThread);
    if (res != 0) {
        t_printf("pthread_create1 error.");
    }
    sleep(WAIT_TIME);

    res = pthread_create(&fatalMessageThread2, NULL, ThreadFun2, (void *)msgThread);
    if (res != 0) {
        t_printf("pthread_create2 error.");
    }
    pthread_join(fatalMessageThread1, NULL);
    pthread_join(fatalMessageThread2, NULL);
}

D
dhy308 已提交
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
/**
 * @tc.name      : set_fatal_message
 * @tc.desc      : Test the function of null message.
 * @tc.level     : Level 0
 */
static void fatal_message_0060(void)
{
    const char* msg = NULL;
    fatal_msg_t *fatal_message = NULL;

    int pidChild = 0;

    pid_t fpid;
    fpid = fork();
    if (fpid < 0) {
        t_printf("error in fork!");
    } else if (fpid == 0) {
        pidChild = getpid();
        set_fatal_message(msg);
        fatal_message = get_fatal_message();
        EXPECT_TRUE(fatal_message->msg == msg);
        exit(pidChild);
    }
}

W
wangjiahui 已提交
250 251 252 253 254 255
TEST_FUN G_Fun_Array[] = {
    fatal_message_0010,
    fatal_message_0020,
    fatal_message_0030,
    fatal_message_0040,
    fatal_message_0050,
D
dhy308 已提交
256
    fatal_message_0060,
W
wangjiahui 已提交
257 258 259 260
};

int main(void)
{
261
    int childPid, childRet;
W
wangjiahui 已提交
262 263
    int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
    for (int pos = 0; pos < num; ++pos) {
264 265 266 267 268 269 270 271
        // Run each function in a new process to
        // keep the initial state of fatal_message.
        if ((childPid = fork()) == 0) {
            G_Fun_Array[pos]();
            exit(0);
        }
        waitpid(childPid, &childRet, 0);
        EXPECT_TRUE(childRet == 0);
W
wangjiahui 已提交
272 273 274 275
    }

    return t_status;
}