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 <signal.h>
17#include <stdlib.h>
18#include <sys/wait.h>
19#include <unistd.h>
20#include "functionalext.h"
21
22/**
23 * @tc.name      : link_0100
24 * @tc.desc      : Determine whether the created connection is successful and whether the file exists
25 * @tc.level     : Level 0
26 */
27void link_0100(void)
28{
29    system("touch /etc/a.txt");
30    int result = link("/etc/a.txt", "/etc/a_link.txt");
31    EXPECT_EQ("link_0100", result, 0);
32    EXPECT_EQ("link_0100", access("/etc/a_link.txt", F_OK), 0);
33    system("rm -f /etc/a_link.txt");
34}
35
36/**
37 * @tc.name      : link_0200
38 * @tc.desc      : Determine whether the file is successfully created when creating a hard link file with the same name
39 * @tc.level     : Level 2
40 */
41void link_0200(void)
42{
43    if (access("/etc/a_link.txt", F_OK) != 0) {
44        int result = link("/etc/a.txt", "/etc/a_link.txt");
45        EXPECT_EQ("link_0200", result, 0);
46    }
47    EXPECT_NE("link_0200", link("/etc/a.txt", "/etc/a_link.txt"), 0);
48    EXPECT_EQ("link_0200", errno, EEXIST);
49    system("rm -f /etc/a_link.txt");
50}
51
52/**
53 * @tc.name      : link_0300
54 * @tc.desc      : Outlier judgment
55 * @tc.level     : Level 2
56 */
57void link_0300(void)
58{
59    int result = link(NULL, NULL);
60    EXPECT_EQ("link_0300", result, ERREXPECT);
61    EXPECT_EQ("link_0300", errno, EFAULT);
62
63    if (access("/etc/a.txt", F_OK) == 0) {
64        system("rm -f /etc/a.txt");
65    }
66
67    if (access("/etc/a_link.txt", F_OK) == 0) {
68        system("rm -f /etc/a_link.txt");
69    }
70}
71
72int main(void)
73{
74    // link_0100();
75    // link_0200();
76    // link_0300();
77
78    return t_status;
79}