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 */
16
17 #include <gtest/gtest.h>
18 #include "TemporaryFile.h"
19
20 #include <ftw.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23
sanity_check_ftw(const char * fpath,const struct stat * sb,int tflag)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 bool is_dir = S_ISDIR(sb->st_mode);
28 ASSERT_TRUE((is_dir && tflag == FTW_D) || (!is_dir && tflag == FTW_F));
29 }
30
sanity_check_nftw(const char * fpath,const struct stat * sb,int tflag,struct FTW * ftwbuf)31 void sanity_check_nftw(
32 const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
33 sanity_check_ftw(fpath, sb, tflag);
34 // either the parent dir or the file
35 bool is_dir = S_ISDIR(sb->st_mode);
36 ASSERT_TRUE(
37 (is_dir && ftwbuf->level == 0) || (!is_dir && ftwbuf->level == 1));
38 }
39
check_ftw(const char * fpath,const struct stat * sb,int tflag)40 int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
41 sanity_check_ftw(fpath, sb, tflag);
42 return 0;
43 }
44
check_ftw64(const char * fpath,const struct stat64 * sb,int tflag)45 int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) {
46 sanity_check_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag);
47 return 0;
48 }
49
check_nftw(const char * fpath,const struct stat * sb,int tflag,struct FTW * ftwbuf)50 int check_nftw(
51 const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
52 sanity_check_nftw(fpath, sb, tflag, ftwbuf);
53 return 0;
54 }
55
check_nftw64(const char * fpath,const struct stat64 * sb,int tflag,struct FTW * ftwbuf)56 int check_nftw64(
57 const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
58 sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb),
59 tflag, ftwbuf);
60 return 0;
61 }
62
TEST(ftw,ftw)63 TEST(ftw, ftw) {
64 TemporaryDir td;
65 TemporaryFile tf(td.dirname);
66 ftw(td.dirname, check_ftw, 1);
67 }
68
TEST(ftw,ftw64)69 TEST(ftw, ftw64) {
70 TemporaryDir td;
71 GenericTemporaryFile<mkstemp64> tf(td.dirname);
72 ftw64(td.dirname, check_ftw64, 1);
73 }
74
TEST(ftw,nftw)75 TEST(ftw, nftw) {
76 TemporaryDir td;
77 TemporaryFile tf(td.dirname);
78 nftw(td.dirname, check_nftw, 1, 0);
79 }
80
TEST(ftw,nftw64)81 TEST(ftw, nftw64) {
82 TemporaryDir td;
83 GenericTemporaryFile<mkstemp64> tf(td.dirname);
84 nftw64(td.dirname, check_nftw64, 1, 0);
85 }
86