poll.c 4.1 KB
Newer Older
G
ganlan 已提交
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 * 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 <errno.h>
#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "fortify_test.h"
#include "test.h"

#define PPOLL_TIMESPEC_NSEC (100)

/**
 * @tc.name     : poll_0010
 * @tc.desc     : test poll normal condition
 * @tc.level    : Level 0
 */
static void poll_0010(void)
{
    errno = 0;
    TEST(poll(NULL, 0, 1) == 0);
    TEST(errno == 0);
    return;
}

/**
 * @tc.name     : poll_0020
 * @tc.desc     : test poll suppress compiler optimizations
 * @tc.level    : Level 2
 */
static void poll_0020(void)
{
    struct sigaction sigabrt = {
        .sa_handler = SignalHandler,
    };
    sigaction(SIGABRT, &sigabrt, NULL);

G
ganlan 已提交
51
    nfds_t fd_count = atoi("2");
G
ganlan 已提交
52 53 54 55 56 57 58 59 60
    struct pollfd buf[1] = {{0, POLLIN, 0}};

    int status;
    int pid = fork();
    switch (pid) {
        case -1:
            t_error("fork failed: %s\n", strerror(errno));
            break;
        case 0:
G
ganlan 已提交
61
            // Set timeout to 0 to prevent waiting for polling if hardening tests fail.
G
ganlan 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75
            poll(buf, fd_count, 0);
            exit(0);
        default:
            waitpid(pid, &status, WUNTRACED);
            TEST(WIFEXITED(status) == 0);
            TEST(WIFSTOPPED(status) == 1);
            TEST(WSTOPSIG(status) == SIGSTOP);
            kill(pid, SIGCONT);
            break;
    }

    return;
}

D
dhy308 已提交
76 77
/**
 * @tc.name     : poll_0030
D
dhy308 已提交
78
 * @tc.desc     : test poll with buf's size greater than fd_count, return fd_count
D
dhy308 已提交
79 80 81 82 83 84 85 86 87 88 89
 * @tc.level    : Level 1
 */
static void poll_0030(void)
{
    nfds_t fd_count = atoi("1");
    struct pollfd buf[2] = {{0, POLLIN, 0}, {1, POLLIN, 0}};
    TEST(poll(buf, fd_count, 1) == 1);
    return;
}


G
ganlan 已提交
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
#ifdef _GNU_SOURCE
/**
 * @tc.name     : ppoll_0010
 * @tc.desc     : test ppoll normal condition
 * @tc.level    : Level 0
 */
static void ppoll_0010(void)
{
    errno = 0;
    struct timespec ts = { .tv_nsec = PPOLL_TIMESPEC_NSEC };
    TEST(ppoll(NULL, 0, &ts, NULL) == 0);
    TEST(errno == 0);
    return;
}

/**
 * @tc.name     : ppoll_0020
 * @tc.desc     : test ppoll suppress compiler optimizations
 * @tc.level    : Level 2
 */
static void ppoll_0020(void)
{
    struct sigaction sigabrt = {
        .sa_handler = SignalHandler,
    };
    sigaction(SIGABRT, &sigabrt, NULL);

G
ganlan 已提交
117
    nfds_t fd_count = atoi("2");
G
ganlan 已提交
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
    struct pollfd buf[1] = {{0, POLLIN, 0}};
    // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
    struct timespec timeout;
    timeout.tv_sec = timeout.tv_nsec = 0;

    int status;
    int pid = fork();
    switch (pid) {
        case -1:
            t_error("fork failed: %s\n", strerror(errno));
            break;
        case 0:
            ppoll(buf, fd_count, &timeout, NULL);
            exit(0);
        default:
            waitpid(pid, &status, WUNTRACED);
            TEST(WIFEXITED(status) == 0);
            TEST(WIFSTOPPED(status) == 1);
            TEST(WSTOPSIG(status) == SIGSTOP);
            kill(pid, SIGCONT);
            break;
    }

    return;
}
D
dhy308 已提交
143 144 145

/**
 * @tc.name     : ppoll_0030
D
dhy308 已提交
146
 * @tc.desc     : test poll with buf's size greater than fd_count, return fd_count
D
dhy308 已提交
147 148 149 150 151 152 153 154 155 156 157
 * @tc.level    : Level 1
 */
static void ppoll_0030(void)
{
    nfds_t fd_count = atoi("1");
    struct pollfd buf[2] = {{0, POLLIN, 0}, {1, POLLIN, 0}};
    struct timespec ts = { .tv_nsec = PPOLL_TIMESPEC_NSEC };
    TEST(ppoll(buf, fd_count, &ts, NULL) == 1);
    return;
}

G
ganlan 已提交
158 159 160 161 162
#endif

int main(int argc, char *argv[]) {
    poll_0010();
    poll_0020();
D
dhy308 已提交
163
    poll_0030();
G
ganlan 已提交
164 165 166
#ifdef _GNU_SOURCE
    ppoll_0010();
    ppoll_0020();
D
dhy308 已提交
167
    ppoll_0030();
G
ganlan 已提交
168 169 170 171
#endif

    return t_status;
}