splice.c 3.3 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/*
 * 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 <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "test.h"

const char *fromfile = "/data/tests/libc-test/src/fromfile.txt";
const char *tofile = "/data/tests/libc-test/src/tofile.txt";
const char *path = "/proc/version";

/**
 * @tc.name      : splice_0100
 * @tc.desc      : splice 0 data to/from a pipe
 * @tc.level     : Level 0
 */
void splice_0100(void)
{
    int fromfd = open(fromfile, O_RDWR | O_CREAT);
    if (fromfd < 0) {
        t_error("%s failed: fromfd = %d\n", __func__, fromfd);
    }

    int tofd = open(tofile, O_RDWR | O_CREAT);
    if (tofd < 0) {
        t_error("%s failed: tofd = %d\n", __func__, tofd);
    }

    off_t off = 0;
    size_t len = 0;
    int flags = 0;

    errno = 0;
    ssize_t result = splice(fromfd, &off, tofd, &off, len, flags);
    if (result != 0) {
        t_error("%s failed: result = %ld\n", __func__, result);
    }

    if (errno != 0) {
        t_error("%s failed: errno = %d\n", __func__, errno);
    }

    remove(fromfile);
    remove(tofile);
}

/**
 * @tc.name      : splice_0200
 * @tc.desc      : splice data to/from a pipe
 * @tc.level     : Level 1
 */
void splice_0200(void)
{
    char buf[BUFSIZ] = {0};
    FILE *f = fopen(path, "r");
    if (f == NULL) {
        t_error("%s failed: fopen\n", __func__);
        return;
    }

    fgets(buf, sizeof(buf), f);
    fclose(f);

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

    int pipe1[1 + 1];
    int result = pipe(pipe1);
    if (result != 0) {
        t_error("%s failed: result = %d\n", __func__, result);
        return;
    }

    size_t len = BUFSIZ;
    int flags = SPLICE_F_MORE | SPLICE_F_MOVE;

    ssize_t bytes = splice(fromfd, NULL, pipe1[1], NULL, len, flags);
    if (bytes <= 0) {
        t_error("%s failed: bytes = %ld\n", __func__, bytes);
        return;
    }

    close(pipe1[1]);

    char buf2[BUFSIZ] = {0};
    f = fdopen(pipe1[0], "r");
    if (f == NULL) {
        t_error("%s failed: fdopen\n", __func__);
        return;
    }

    fgets(buf2, sizeof(buf2), f);
    fclose(f);

    if (strcmp(buf2, buf)) {
        t_error("%s failed: buf2 = %s\n", __func__, buf2);
        return;
    }
}

/**
 * @tc.name      : splice_0300
 * @tc.desc      : splice data to/from a pipe with invalid parameters
 * @tc.level     : Level 2
 */
void splice_0300(void)
{
    errno = 0;
    ssize_t bytes = splice(-1, NULL, -1, NULL, -1, -1);
    if (bytes >= 0) {
        t_error("%s failed: bytes = %ld\n", __func__, bytes);
    }

    if (errno == 0) {
        t_error("%s failed: errno = %ld\n", __func__, errno);
    }
}

int main(int argc, char *argv[])
{
    splice_0100();
    splice_0200();
    splice_0300();

    return t_status;
}