1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
17 #include <ftw.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
22 #include <gtest/gtest.h>
24 void sanity_check_ftw(const char* fpath, const struct stat* sb, int tflag) {
25 ASSERT_TRUE(fpath != NULL);
26 ASSERT_TRUE(sb != NULL);
27 if (S_ISDIR(sb->st_mode)) {
28 ASSERT_TRUE(tflag == FTW_D || tflag == FTW_DNR || tflag == FTW_DP) << fpath;
29 } else if (S_ISLNK(sb->st_mode)) {
30 ASSERT_EQ(FTW_SL, tflag) << fpath;
31 } else {
32 ASSERT_EQ(FTW_F, tflag) << fpath;
33 }
34 }
36 void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
37 sanity_check_ftw(fpath, sb, tflag);
39 size_t slash_count = 0;
40 const char* p = fpath;
41 while ((p = strchr(p + 1, '/')) != NULL) {
42 ++slash_count;
43 }
45 ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath;
46 ASSERT_EQ(slash_count, static_cast<size_t>(ftwbuf->level)) << fpath;
47 }
49 int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
50 sanity_check_ftw(fpath, sb, tflag);
51 return 0;
52 }
54 int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) {
55 sanity_check_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag);
56 return 0;
57 }
59 int check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
60 sanity_check_nftw(fpath, sb, tflag, ftwbuf);
61 return 0;
62 }
64 int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
65 sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf);
66 return 0;
67 }
69 TEST(ftw, ftw) {
70 ASSERT_EQ(0, ftw("/sys", check_ftw, 128));
71 }
73 TEST(ftw, ftw64) {
74 ASSERT_EQ(0, ftw64("/sys", check_ftw64, 128));
75 }
77 TEST(ftw, nftw) {
78 ASSERT_EQ(0, nftw("/sys", check_nftw, 128, 0));
79 }
81 TEST(ftw, nftw64) {
82 ASSERT_EQ(0, nftw64("/sys", check_nftw64, 128, 0));
83 }