• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "filepath_util.h"
23 
24 const char *path = "/proc/version";
25 
26 /**
27  * @tc.name      : splice_0100
28  * @tc.desc      : splice 0 data to/from a pipe
29  * @tc.level     : Level 0
30  */
splice_0100(void)31 void splice_0100(void)
32 {
33     char fromfile[PATH_MAX] = {0};
34     char tofile[PATH_MAX] = {0};
35     FILE_ABSOLUTE_PATH(STR_FILE_FROM_TXT, fromfile);
36     FILE_ABSOLUTE_PATH(STR_FILE_TO_TXT, tofile);
37 
38     int fromfd = open(fromfile, O_RDWR | O_CREAT, TEST_MODE);
39     if (fromfd < 0) {
40         t_error("%s failed: fromfd = %d\n", __func__, fromfd);
41     }
42 
43     int tofd = open(tofile, O_RDWR | O_CREAT, TEST_MODE);
44     if (tofd < 0) {
45         t_error("%s failed: tofd = %d\n", __func__, tofd);
46     }
47 
48     off_t off = 0;
49     size_t len = 0;
50     int flags = 0;
51 
52     errno = 0;
53     ssize_t result = splice(fromfd, &off, tofd, &off, len, flags);
54     if (result != 0) {
55         t_error("%s failed: result = %ld\n", __func__, result);
56     }
57 
58     if (errno != 0) {
59         t_error("%s failed: errno = %d\n", __func__, errno);
60     }
61 
62     remove(fromfile);
63     remove(tofile);
64 }
65 
66 /**
67  * @tc.name      : splice_0200
68  * @tc.desc      : splice data to/from a pipe
69  * @tc.level     : Level 1
70  */
splice_0200(void)71 void splice_0200(void)
72 {
73     char buf[BUFSIZ] = {0};
74     FILE *f = fopen(path, "r");
75     if (f == NULL) {
76         t_error("%s failed: fopen\n", __func__);
77         return;
78     }
79 
80     fgets(buf, sizeof(buf), f);
81     fclose(f);
82 
83     int fromfd = open(path, O_RDONLY);
84     if (fromfd < 0) {
85         t_error("%s failed: fromfd = %d\n", __func__, fromfd);
86     }
87 
88     int pipe1[1 + 1];
89     int result = pipe(pipe1);
90     if (result != 0) {
91         t_error("%s failed: result = %d\n", __func__, result);
92         return;
93     }
94 
95     size_t len = BUFSIZ;
96     int flags = SPLICE_F_MORE | SPLICE_F_MOVE;
97 
98     ssize_t bytes = splice(fromfd, NULL, pipe1[1], NULL, len, flags);
99     if (bytes <= 0) {
100         t_error("%s failed: bytes = %ld\n", __func__, bytes);
101         return;
102     }
103 
104     close(pipe1[1]);
105 
106     char buf2[BUFSIZ] = {0};
107     f = fdopen(pipe1[0], "r");
108     if (f == NULL) {
109         t_error("%s failed: fdopen\n", __func__);
110         return;
111     }
112 
113     fgets(buf2, sizeof(buf2), f);
114     fclose(f);
115 
116     if (strcmp(buf2, buf)) {
117         t_error("%s failed: buf2 = %s\n", __func__, buf2);
118         return;
119     }
120 }
121 
122 /**
123  * @tc.name      : splice_0300
124  * @tc.desc      : splice data to/from a pipe with invalid parameters
125  * @tc.level     : Level 2
126  */
splice_0300(void)127 void splice_0300(void)
128 {
129     errno = 0;
130     ssize_t bytes = splice(-1, NULL, -1, NULL, -1, -1);
131     if (bytes >= 0) {
132         t_error("%s failed: bytes = %ld\n", __func__, bytes);
133     }
134 
135     if (errno == 0) {
136         t_error("%s failed: errno = %ld\n", __func__, errno);
137     }
138 }
139 
main(int argc,char * argv[])140 int main(int argc, char *argv[])
141 {
142     splice_0100();
143     splice_0200();
144     splice_0300();
145 
146     return t_status;
147 }
148