1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <dirent.h>
17570af302Sopenharmony_ci#include <fcntl.h>
18570af302Sopenharmony_ci#include <stdio.h>
19570af302Sopenharmony_ci#include <string.h>
20570af302Sopenharmony_ci#include <unistd.h>
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#include "filepath_util.h"
23570af302Sopenharmony_ci
24570af302Sopenharmony_ciconst char buf[] = "hello";
25570af302Sopenharmony_ciconst char *filename = "./file.txt";
26570af302Sopenharmony_ciconst char *linkfilename = "./linkfile.txt";
27570af302Sopenharmony_ci
28570af302Sopenharmony_ciint create_file(char *path)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci    int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
31570af302Sopenharmony_ci    if (fd < 0) {
32570af302Sopenharmony_ci        return -1;
33570af302Sopenharmony_ci    }
34570af302Sopenharmony_ci
35570af302Sopenharmony_ci    ssize_t bytes = write(fd, buf, sizeof(buf));
36570af302Sopenharmony_ci    if (bytes <= 0) {
37570af302Sopenharmony_ci        return -1;
38570af302Sopenharmony_ci    }
39570af302Sopenharmony_ci
40570af302Sopenharmony_ci    int result = close(fd);
41570af302Sopenharmony_ci    if (result != 0) {
42570af302Sopenharmony_ci        return -1;
43570af302Sopenharmony_ci    }
44570af302Sopenharmony_ci
45570af302Sopenharmony_ci    return 0;
46570af302Sopenharmony_ci}
47570af302Sopenharmony_ci
48570af302Sopenharmony_ci/**
49570af302Sopenharmony_ci * @tc.name      : readlinkat_0100
50570af302Sopenharmony_ci * @tc.desc      : read value of a symbolic link
51570af302Sopenharmony_ci * @tc.level     : Level 0
52570af302Sopenharmony_ci */
53570af302Sopenharmony_civoid readlinkat_0100(void)
54570af302Sopenharmony_ci{
55570af302Sopenharmony_ci    char path[PATH_MAX] = {0};
56570af302Sopenharmony_ci    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
57570af302Sopenharmony_ci    int result = create_file(path);
58570af302Sopenharmony_ci    if (result != 0) {
59570af302Sopenharmony_ci        t_error("%s failed: result = %d\n", __func__, result);
60570af302Sopenharmony_ci        return;
61570af302Sopenharmony_ci    }
62570af302Sopenharmony_ci
63570af302Sopenharmony_ci    char linkpath[PATH_MAX] = {0};
64570af302Sopenharmony_ci    FILE_ABSOLUTE_PATH(STR_FILE_LINK_TXT, linkpath);
65570af302Sopenharmony_ci    remove(linkpath);
66570af302Sopenharmony_ci
67570af302Sopenharmony_ci    result = symlink(path, linkpath);
68570af302Sopenharmony_ci    if (result != 0) {
69570af302Sopenharmony_ci        t_error("%s failed: result = %d\n", __func__, result);
70570af302Sopenharmony_ci        return;
71570af302Sopenharmony_ci    }
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci    int fd = open(linkpath, O_RDONLY);
74570af302Sopenharmony_ci    if (fd < 0) {
75570af302Sopenharmony_ci        t_error("%s failed: fd = %d\n", __func__, fd);
76570af302Sopenharmony_ci        return;
77570af302Sopenharmony_ci    }
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci    char rbuf[BUFSIZ] = {0};
80570af302Sopenharmony_ci    ssize_t bytes = readlinkat(fd, linkpath, rbuf, sizeof(rbuf));
81570af302Sopenharmony_ci    if (bytes != strlen(rbuf)) {
82570af302Sopenharmony_ci        t_error("%s failed: bytes = %ld\n", __func__, bytes);
83570af302Sopenharmony_ci    }
84570af302Sopenharmony_ci
85570af302Sopenharmony_ci    if (strcmp(rbuf, path)) {
86570af302Sopenharmony_ci        t_error("%s failed: rbuf = %s\n", __func__, rbuf);
87570af302Sopenharmony_ci    }
88570af302Sopenharmony_ci
89570af302Sopenharmony_ci    remove(linkpath);
90570af302Sopenharmony_ci    remove(path);
91570af302Sopenharmony_ci}
92570af302Sopenharmony_ci
93570af302Sopenharmony_ci/**
94570af302Sopenharmony_ci * @tc.name      : readlinkat_0200
95570af302Sopenharmony_ci * @tc.desc      : read value of a symbolic link with a dir fd
96570af302Sopenharmony_ci * @tc.level     : Level 1
97570af302Sopenharmony_ci */
98570af302Sopenharmony_civoid readlinkat_0200(void)
99570af302Sopenharmony_ci{
100570af302Sopenharmony_ci    char path[PATH_MAX] = {0};
101570af302Sopenharmony_ci    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
102570af302Sopenharmony_ci    int result = create_file(path);
103570af302Sopenharmony_ci    if (result != 0) {
104570af302Sopenharmony_ci        t_error("%s failed: result = %d\n", __func__, result);
105570af302Sopenharmony_ci        return;
106570af302Sopenharmony_ci    }
107570af302Sopenharmony_ci
108570af302Sopenharmony_ci    char dirname[PATH_MAX] = {0};
109570af302Sopenharmony_ci    FILE_ABSOLUTE_DIR(dirname);
110570af302Sopenharmony_ci    DIR *dir = opendir(dirname);
111570af302Sopenharmony_ci    if (dir == NULL) {
112570af302Sopenharmony_ci        t_error("%s failed: dirname = %s\n", __func__, dirname);
113570af302Sopenharmony_ci        return;
114570af302Sopenharmony_ci    }
115570af302Sopenharmony_ci
116570af302Sopenharmony_ci    int fd = dirfd(dir);
117570af302Sopenharmony_ci    if (fd < 0) {
118570af302Sopenharmony_ci        t_error("%s failed: fd = %d\n", __func__, fd);
119570af302Sopenharmony_ci    }
120570af302Sopenharmony_ci
121570af302Sopenharmony_ci    char linkpath[PATH_MAX] = {0};
122570af302Sopenharmony_ci    FILE_ABSOLUTE_PATH(STR_FILE_LINK_TXT, linkpath);
123570af302Sopenharmony_ci    remove(linkpath);
124570af302Sopenharmony_ci
125570af302Sopenharmony_ci    result = symlinkat(filename, fd, linkfilename);
126570af302Sopenharmony_ci    if (result != 0) {
127570af302Sopenharmony_ci        t_error("%s failed: result = %d\n", __func__, result);
128570af302Sopenharmony_ci        return;
129570af302Sopenharmony_ci    }
130570af302Sopenharmony_ci
131570af302Sopenharmony_ci    char rbuf[BUFSIZ] = {0};
132570af302Sopenharmony_ci    ssize_t bytes = readlinkat(fd, linkfilename, rbuf, sizeof(rbuf));
133570af302Sopenharmony_ci    if (bytes != strlen(rbuf)) {
134570af302Sopenharmony_ci        t_error("%s failed: bytes = %ld\n", __func__, bytes);
135570af302Sopenharmony_ci    }
136570af302Sopenharmony_ci
137570af302Sopenharmony_ci    if (strcmp(rbuf, filename)) {
138570af302Sopenharmony_ci        t_error("%s failed: rbuf = %s\n", __func__, rbuf);
139570af302Sopenharmony_ci    }
140570af302Sopenharmony_ci
141570af302Sopenharmony_ci    remove(linkpath);
142570af302Sopenharmony_ci    remove(path);
143570af302Sopenharmony_ci}
144570af302Sopenharmony_ci
145570af302Sopenharmony_ci/**
146570af302Sopenharmony_ci * @tc.name      : readlinkat_0300
147570af302Sopenharmony_ci * @tc.desc      : read value of a symbolic link with invalid parameters
148570af302Sopenharmony_ci * @tc.level     : Level 2
149570af302Sopenharmony_ci */
150570af302Sopenharmony_civoid readlinkat_0300(void)
151570af302Sopenharmony_ci{
152570af302Sopenharmony_ci    ssize_t bytes = readlinkat(-1, NULL, NULL, 0);
153570af302Sopenharmony_ci    if (bytes >= 0) {
154570af302Sopenharmony_ci        t_error("%s failed: bytes = %ld\n", __func__, bytes);
155570af302Sopenharmony_ci    }
156570af302Sopenharmony_ci}
157570af302Sopenharmony_ci
158570af302Sopenharmony_ciint main(int argc, char *argv[])
159570af302Sopenharmony_ci{
160570af302Sopenharmony_ci    readlinkat_0100();
161570af302Sopenharmony_ci    readlinkat_0200();
162570af302Sopenharmony_ci    readlinkat_0300();
163570af302Sopenharmony_ci
164570af302Sopenharmony_ci    return t_status;
165570af302Sopenharmony_ci}
166