1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *   http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <fcntl.h>
17#include <unistd.h>
18#include "functionalext.h"
19
20int successfully = 0;
21int failed = -1;
22
23/*
24 * @tc.name      : daemon_0100
25 * @tc.desc      : Verify that the working directory of the daemon can be changed (parameters is 0)
26 * @tc.level     : Level 0
27 */
28void daemon_0100(void)
29{
30    pid_t pid;
31    int result = daemon(0, 0);
32    EXPECT_EQ("daemon_0100", result, successfully);
33}
34
35/*
36 * @tc.name      : daemon_0200
37 * @tc.desc      : Verify that the working directory of the daemon can be changed (parameters is 7)
38 * @tc.level     : Level 0
39 */
40void daemon_0200(void)
41{
42    pid_t pid;
43    int result = daemon(7, 0);
44    EXPECT_EQ("daemon_0200", result, successfully);
45    int fd = open("/dev/null", O_RDWR);
46    EXPECT_EQ("daemon_0200", fd, successfully);
47}
48
49/*
50 * @tc.name      : daemon_0300
51 * @tc.desc      : Verify that the working directory of the daemon cannot be changed
52 *                 (the nochdir parameter has no effect)
53 * @tc.level     : Level 2
54 */
55void daemon_0300(void)
56{
57    pid_t pid;
58    int result = daemon(0, -1);
59    EXPECT_EQ("daemon_0300", result, failed);
60}
61
62/*
63 * @tc.name      : daemon_0400
64 * @tc.desc      : Verify that the working directory of the daemon cannot be changed
65 *                 (the noclose parameter has no effect)
66 * @tc.level     : Level 2
67 */
68void daemon_0400(void)
69{
70    pid_t pid;
71    int result = daemon(-1, -1);
72    EXPECT_EQ("daemon_0400", result, failed);
73    int fd = open("/dev/null", O_RDWR);
74    EXPECT_EQ("daemon_0400", fd, failed);
75}
76
77/*
78 * @tc.name      : daemon_0500
79 * @tc.desc      : Verify that the working directory of the daemon cannot be changed (parameter invalid)
80 * @tc.level     : Level 2
81 */
82void daemon_0500(void)
83{
84    pid_t pid;
85    int result = daemon(0, 0);
86    EXPECT_EQ("daemon_0500", result, successfully);
87    pid = fork();
88    EXPECT_EQ("daemon_0500", pid, failed);
89}
90
91/*
92 * @tc.name      : daemon_0600
93 * @tc.desc      : Verify that the working directory of the daemon cannot be changed (parameter invalid)
94 * @tc.level     : Level 2
95 */
96void daemon_0600(void)
97{
98    pid_t pid;
99    int result = daemon(0, 0);
100    EXPECT_EQ("daemon_0600", result, successfully);
101    int mode = setsid();
102    EXPECT_EQ("daemon_0600", mode, failed);
103}
104
105int main(int argc, char *argv[])
106{
107    daemon_0100();
108    daemon_0200();
109    daemon_0300();
110    daemon_0400();
111    daemon_0500();
112    daemon_0600();
113
114    return t_status;
115}