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 <signal.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "test.h"
22
23 const char *name = "/data/tests/libc-test/src";
24
handler(int sig)25 void handler(int sig)
26 {
27 exit(t_status);
28 }
29
30 /**
31 * @tc.name : rewinddir_0100
32 * @tc.desc : reset directory stream
33 * @tc.level : Level 0
34 */
rewinddir_0100(void)35 void rewinddir_0100(void)
36 {
37 DIR *dir = opendir(name);
38 if (dir == NULL) {
39 t_error("%s failed: opendir. name = %s\n", __func__, name);
40 return;
41 }
42
43 struct dirent *result = readdir(dir);
44 if (result == NULL) {
45 t_error("%s failed: readdir\n", __func__);
46 return;
47 }
48
49 char *d_name = result->d_name;
50
51 while ((result = readdir(dir)) != NULL) {
52 }
53
54 rewinddir(dir);
55 result = readdir(dir);
56 if (result == NULL) {
57 t_error("%s failed: readdir\n", __func__);
58 return;
59 }
60
61 if (strcmp(d_name, result->d_name)) {
62 t_error("%s failed: strcmp. d_name = %s, result->d_name = %s\n", __func__, d_name, result->d_name);
63 return;
64 }
65
66 closedir(dir);
67 }
68
69 /**
70 * @tc.name : rewinddir_0200
71 * @tc.desc : reset directory stream with a NULL dir
72 * @tc.level : Level 2
73 */
rewinddir_0200(void)74 void rewinddir_0200(void)
75 {
76 signal(SIGSEGV, handler);
77
78 DIR *dir = NULL;
79 rewinddir(dir);
80 }
81
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 rewinddir_0100();
85 rewinddir_0200();
86
87 return t_status;
88 }
89