readlinkat.c 3.9 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
/*
 * 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 <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

22
#include "filepath_util.h"
G
ganlan 已提交
23 24 25 26 27

const char buf[] = "hello";
const char *filename = "./file.txt";
const char *linkfilename = "./linkfile.txt";

D
dhy308 已提交
28
int create_file(char *path)
G
ganlan 已提交
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
{
    int fd = open(path, O_RDWR | O_CREAT);
    if (fd < 0) {
        return -1;
    }

    ssize_t bytes = write(fd, buf, sizeof(buf));
    if (bytes <= 0) {
        return -1;
    }

    int result = close(fd);
    if (result != 0) {
        return -1;
    }

    return 0;
}

/**
 * @tc.name      : readlinkat_0100
 * @tc.desc      : read value of a symbolic link
 * @tc.level     : Level 0
 */
void readlinkat_0100(void)
{
55 56
    char path[PATH_MAX] = {0};
    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
D
dhy308 已提交
57
    int result = create_file(path);
G
ganlan 已提交
58 59 60 61 62
    if (result != 0) {
        t_error("%s failed: result = %d\n", __func__, result);
        return;
    }

63 64
    char linkpath[PATH_MAX] = {0};
    FILE_ABSOLUTE_PATH(STR_FILE_LINK_TXT, linkpath);
G
ganlan 已提交
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
    remove(linkpath);

    result = symlink(path, linkpath);
    if (result != 0) {
        t_error("%s failed: result = %d\n", __func__, result);
        return;
    }

    int fd = open(linkpath, O_RDONLY);
    if (fd < 0) {
        t_error("%s failed: fd = %d\n", __func__, fd);
        return;
    }

    char rbuf[BUFSIZ] = {0};
    ssize_t bytes = readlinkat(fd, linkpath, rbuf, sizeof(rbuf));
    if (bytes != strlen(rbuf)) {
        t_error("%s failed: bytes = %ld\n", __func__, bytes);
    }

    if (strcmp(rbuf, path)) {
        t_error("%s failed: rbuf = %s\n", __func__, rbuf);
    }

    remove(linkpath);
    remove(path);
}

/**
 * @tc.name      : readlinkat_0200
 * @tc.desc      : read value of a symbolic link with a dir fd
 * @tc.level     : Level 1
 */
void readlinkat_0200(void)
{
100 101
    char path[PATH_MAX] = {0};
    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
D
dhy308 已提交
102
    int result = create_file(path);
G
ganlan 已提交
103 104 105 106 107
    if (result != 0) {
        t_error("%s failed: result = %d\n", __func__, result);
        return;
    }

108 109
    char dirname[PATH_MAX] = {0};
    FILE_ABSOLUTE_DIR(dirname);
G
ganlan 已提交
110 111 112 113 114 115 116 117 118 119 120
    DIR *dir = opendir(dirname);
    if (dir == NULL) {
        t_error("%s failed: dirname = %s\n", __func__, dirname);
        return;
    }

    int fd = dirfd(dir);
    if (fd < 0) {
        t_error("%s failed: fd = %d\n", __func__, fd);
    }

121 122
    char linkpath[PATH_MAX] = {0};
    FILE_ABSOLUTE_PATH(STR_FILE_LINK_TXT, linkpath);
G
ganlan 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    remove(linkpath);

    result = symlinkat(filename, fd, linkfilename);
    if (result != 0) {
        t_error("%s failed: result = %d\n", __func__, result);
        return;
    }

    char rbuf[BUFSIZ] = {0};
    ssize_t bytes = readlinkat(fd, linkfilename, rbuf, sizeof(rbuf));
    if (bytes != strlen(rbuf)) {
        t_error("%s failed: bytes = %ld\n", __func__, bytes);
    }

    if (strcmp(rbuf, filename)) {
        t_error("%s failed: rbuf = %s\n", __func__, rbuf);
    }

    remove(linkpath);
    remove(path);
}

/**
 * @tc.name      : readlinkat_0300
 * @tc.desc      : read value of a symbolic link with invalid parameters
 * @tc.level     : Level 2
 */
void readlinkat_0300(void)
{
    ssize_t bytes = readlinkat(-1, NULL, NULL, 0);
    if (bytes >= 0) {
        t_error("%s failed: bytes = %ld\n", __func__, bytes);
    }
}

int main(int argc, char *argv[])
{
    readlinkat_0100();
    readlinkat_0200();
    readlinkat_0300();

    return t_status;
}