blob: 6741d003ee3efa9a78b20116f6db408e2a04c25a [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
Calin Juravled4934a72014-02-24 16:13:50 +000017#include <ftw.h>
Elliott Hughes63bd43b2014-11-18 15:57:23 -080018
Elliott Hughes7f925092015-02-10 14:15:33 -080019#include <stdio.h>
Calin Juravled4934a72014-02-24 16:13:50 +000020#include <stdlib.h>
21#include <sys/stat.h>
Elliott Hughes7f925092015-02-10 14:15:33 -080022#include <sys/types.h>
23#include <unistd.h>
24
25#include "TemporaryFile.h"
Calin Juravled4934a72014-02-24 16:13:50 +000026
Elliott Hughes63bd43b2014-11-18 15:57:23 -080027#include <gtest/gtest.h>
28
Elliott Hughes7f925092015-02-10 14:15:33 -080029static void MakeTree(const char* root) {
30 char path[PATH_MAX];
31
32 snprintf(path, sizeof(path), "%s/dir", root);
33 ASSERT_EQ(0, mkdir(path, 0555));
34 snprintf(path, sizeof(path), "%s/dir/sub", root);
35 ASSERT_EQ(0, mkdir(path, 0555));
36 snprintf(path, sizeof(path), "%s/unreadable-dir", root);
37 ASSERT_EQ(0, mkdir(path, 0000));
38
39 snprintf(path, sizeof(path), "%s/dangler", root);
40 ASSERT_EQ(0, symlink("/does-not-exist", path));
41 snprintf(path, sizeof(path), "%s/symlink", root);
42 ASSERT_EQ(0, symlink("sub2", path));
43
44 int fd;
45 snprintf(path, sizeof(path), "%s/regular", root);
46 ASSERT_NE(-1, fd = open(path, O_CREAT|O_TRUNC, 0666));
47 ASSERT_EQ(0, close(fd));
48}
49
Calin Juravled4934a72014-02-24 16:13:50 +000050void sanity_check_ftw(const char* fpath, const struct stat* sb, int tflag) {
51 ASSERT_TRUE(fpath != NULL);
52 ASSERT_TRUE(sb != NULL);
Elliott Hughes7f925092015-02-10 14:15:33 -080053
Elliott Hughes63bd43b2014-11-18 15:57:23 -080054 if (S_ISDIR(sb->st_mode)) {
Elliott Hughes7f925092015-02-10 14:15:33 -080055 EXPECT_TRUE(tflag == FTW_D || tflag == FTW_DNR || tflag == FTW_DP) << fpath;
Elliott Hughes63bd43b2014-11-18 15:57:23 -080056 } else if (S_ISLNK(sb->st_mode)) {
Elliott Hughes7f925092015-02-10 14:15:33 -080057 EXPECT_EQ(FTW_SL, tflag) << fpath;
Elliott Hughes63bd43b2014-11-18 15:57:23 -080058 } else {
Elliott Hughes7f925092015-02-10 14:15:33 -080059 EXPECT_EQ(FTW_F, tflag) << fpath;
Elliott Hughes63bd43b2014-11-18 15:57:23 -080060 }
Calin Juravled4934a72014-02-24 16:13:50 +000061}
62
Elliott Hughes63bd43b2014-11-18 15:57:23 -080063void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
Calin Juravled4934a72014-02-24 16:13:50 +000064 sanity_check_ftw(fpath, sb, tflag);
Elliott Hughes63bd43b2014-11-18 15:57:23 -080065 ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath;
Calin Juravled4934a72014-02-24 16:13:50 +000066}
67
68int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
69 sanity_check_ftw(fpath, sb, tflag);
70 return 0;
71}
72
73int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) {
74 sanity_check_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag);
75 return 0;
76}
77
Elliott Hughes63bd43b2014-11-18 15:57:23 -080078int check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
Calin Juravled4934a72014-02-24 16:13:50 +000079 sanity_check_nftw(fpath, sb, tflag, ftwbuf);
80 return 0;
81}
82
Elliott Hughes63bd43b2014-11-18 15:57:23 -080083int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
84 sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf);
Calin Juravled4934a72014-02-24 16:13:50 +000085 return 0;
86}
87
88TEST(ftw, ftw) {
Elliott Hughes7f925092015-02-10 14:15:33 -080089 TemporaryDir root;
90 MakeTree(root.dirname);
91 ASSERT_EQ(0, ftw(root.dirname, check_ftw, 128));
Calin Juravled4934a72014-02-24 16:13:50 +000092}
93
94TEST(ftw, ftw64) {
Elliott Hughes7f925092015-02-10 14:15:33 -080095 TemporaryDir root;
96 MakeTree(root.dirname);
97 ASSERT_EQ(0, ftw64(root.dirname, check_ftw64, 128));
Calin Juravled4934a72014-02-24 16:13:50 +000098}
99
100TEST(ftw, nftw) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800101 TemporaryDir root;
102 MakeTree(root.dirname);
103 ASSERT_EQ(0, nftw(root.dirname, check_nftw, 128, 0));
Calin Juravled4934a72014-02-24 16:13:50 +0000104}
105
106TEST(ftw, nftw64) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800107 TemporaryDir root;
108 MakeTree(root.dirname);
109 ASSERT_EQ(0, nftw64(root.dirname, check_nftw64, 128, 0));
Calin Juravled4934a72014-02-24 16:13:50 +0000110}