blob: 6d3a30897102bace0a0637e49ef9149c66d03e4b [file] [log] [blame]
Calin Juravled4934a72014-02-24 16:13:50 +00001/*
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
24void 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
31void 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
40int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
41 sanity_check_ftw(fpath, sb, tflag);
42 return 0;
43}
44
45int 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
50int 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
56int 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
63TEST(ftw, ftw) {
64 TemporaryDir td;
65 TemporaryFile tf(td.dirname);
66 ftw(td.dirname, check_ftw, 1);
67}
68
69TEST(ftw, ftw64) {
70 TemporaryDir td;
71 GenericTemporaryFile<mkstemp64> tf(td.dirname);
72 ftw64(td.dirname, check_ftw64, 1);
73}
74
75TEST(ftw, nftw) {
76 TemporaryDir td;
77 TemporaryFile tf(td.dirname);
78 nftw(td.dirname, check_nftw, 1, 0);
79}
80
81TEST(ftw, nftw64) {
82 TemporaryDir td;
83 GenericTemporaryFile<mkstemp64> tf(td.dirname);
84 nftw64(td.dirname, check_nftw64, 1, 0);
85}