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 <dirent.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "test.h"
24
25 const char *path = "/data";
26
handler(int sig)27 void handler(int sig)
28 {
29 exit(t_status);
30 }
31
32 /**
33 * @tc.name : seekdir_0100
34 * @tc.desc : set the position of the next readdir() call in the directory stream
35 * @tc.level : Level 0
36 */
seekdir_0100(void)37 void seekdir_0100(void)
38 {
39 DIR *dir = opendir(path);
40 if (dir == NULL) {
41 t_error("%s failed: opendir. path = %s\n", __func__, path);
42 }
43
44 int counter = 0;
45 struct dirent *result;
46 long offset = 0;
47 while ((result = readdir(dir)) != NULL) {
48 counter++;
49
50 if (counter > 1) {
51 offset = telldir(dir);
52 break;
53 }
54 }
55
56 result = readdir(dir);
57 if (result == NULL) {
58 t_error("%s failed: readdir\n", __func__);
59 }
60
61 char d_name[BUFSIZ] = {0};
62 strcpy(d_name, result->d_name);
63
64 errno = 0;
65 seekdir(dir, offset);
66 if (errno != 0) {
67 t_error("%s failed: seekdir. errno = %d\n", __func__, errno);
68 }
69
70 result = readdir(dir);
71 if (result == NULL) {
72 t_error("%s failed: readdir\n", __func__);
73 }
74
75 if (strcmp(result->d_name, d_name)) {
76 t_error("%s failed: strcmp. result->d_name = %s, d_name = %s\n", __func__, result->d_name, d_name);
77 }
78
79 closedir(dir);
80 }
81
82 /**
83 * @tc.name : seekdir_0200
84 * @tc.desc : set the position of the next readdir() call in the directory stream to zero
85 * @tc.level : Level 1
86 */
seekdir_0200(void)87 void seekdir_0200(void)
88 {
89 DIR *dir = opendir(path);
90 if (dir == NULL) {
91 t_error("%s failed: opendir. path = %s\n", __func__, path);
92 }
93
94 struct dirent *result = readdir(dir);
95 if (result == NULL) {
96 t_error("%s failed: readdir\n", __func__);
97 }
98
99 char d_name[BUFSIZ] = {0};
100 strcpy(d_name, result->d_name);
101
102 errno = 0;
103 seekdir(dir, 0);
104 if (errno != 0) {
105 t_error("%s failed: seekdir. errno = %d\n", __func__, errno);
106 }
107
108 result = readdir(dir);
109 if (result == NULL) {
110 t_error("%s failed: readdir\n", __func__);
111 }
112
113 if (strcmp(result->d_name, d_name)) {
114 t_error("%s failed: strcmp. result->d_name = %s, d_name = %s\n", __func__, result->d_name, d_name);
115 }
116
117 closedir(dir);
118 }
119
120 /**
121 * @tc.name : seekdir_0300
122 * @tc.desc : set the position of the next readdir() call in the directory stream with NULL
123 * @tc.level : Level 2
124 */
seekdir_0300(void)125 void seekdir_0300(void)
126 {
127 signal(SIGSEGV, handler);
128
129 DIR *dir = NULL;
130 seekdir(dir, -1);
131 }
132
main(int argc,char * argv[])133 int main(int argc, char *argv[])
134 {
135 seekdir_0100();
136 seekdir_0200();
137 seekdir_0300();
138
139 return t_status;
140 }
141