daemon.c 2.4 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 51 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
#include <fcntl.h>
#include <unistd.h>
#include "functionalext.h"

int successfully = 0;
int failed = -1;
typedef void (*TEST_FUN)();

/*
 * @tc.name      : daemon_0100
 * @tc.desc      : Verify that the working directory of the daemon can be changed (parameters is 0)
 * @tc.level     : Level 0
 */
void daemon_0100(void)
{
    pid_t pid;
    int result = daemon(0, 0);
    EXPECT_EQ("daemon_0100", result, successfully);
}

/*
 * @tc.name      : daemon_0200
 * @tc.desc      : Verify that the working directory of the daemon can be changed (parameters is 7)
 * @tc.level     : Level 0
 */
void daemon_0200(void)
{
    pid_t pid;
    int result = daemon(7, 0);
    EXPECT_EQ("daemon_0200", result, successfully);
    int fd = open("/dev/null", O_RDWR);
    EXPECT_EQ("daemon_0200", fd, successfully);
}

/*
 * @tc.name      : daemon_0300
 * @tc.desc      : Verify that the working directory of the daemon cannot be changed 
 *                 (the nochdir parameter has no effect)
 * @tc.level     : Level 2
 */
void daemon_0300(void)
{
    pid_t pid;
    int result = daemon(0, -1);
    EXPECT_EQ("daemon_0300", result, failed);
}

/*
 * @tc.name      : daemon_0400
 * @tc.desc      : Verify that the working directory of the daemon cannot be changed 
 *                 (the noclose parameter has no effect)
 * @tc.level     : Level 2
 */
void daemon_0400(void)
{
    pid_t pid;
    int result = daemon(-1, -1);
    EXPECT_EQ("daemon_0400", result, failed);
    int fd = open("/dev/null", O_RDWR);
    EXPECT_EQ("daemon_0400", fd, failed);
}

/*
 * @tc.name      : daemon_0500
 * @tc.desc      : Verify that the working directory of the daemon cannot be changed (parameter invalid)
 * @tc.level     : Level 2
 */
void daemon_0500(void)
{
    pid_t pid;
    int result = daemon(0, 0);
    EXPECT_EQ("daemon_0500", result, successfully);
    pid = fork();
    EXPECT_EQ("daemon_0500", pid, failed);
}

/*
 * @tc.name      : daemon_0600
 * @tc.desc      : Verify that the working directory of the daemon cannot be changed (parameter invalid)
 * @tc.level     : Level 2
 */
void daemon_0600(void)
{
    pid_t pid;
    int result = daemon(0, 0);
    EXPECT_EQ("daemon_0600", result, successfully);
    int mode = setsid();
    EXPECT_EQ("daemon_0600", mode, failed);
}

int main()
{
    daemon_0100();
    daemon_0200();
    daemon_0300();
    daemon_0400();
    daemon_0500();
    daemon_0600();

    return t_status;
}