Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 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 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 17 | #include <ftw.h> |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 18 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <sys/stat.h> |
| 21 | |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 24 | void sanity_check_ftw(const char* fpath, const struct stat* sb, int tflag) { |
| 25 | ASSERT_TRUE(fpath != NULL); |
| 26 | ASSERT_TRUE(sb != NULL); |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 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 | } |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 36 | void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) { |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 37 | sanity_check_ftw(fpath, sb, tflag); |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 38 | |
| 39 | size_t slash_count = 0; |
| 40 | const char* p = fpath; |
| 41 | while ((p = strchr(p + 1, '/')) != NULL) { |
| 42 | ++slash_count; |
| 43 | } |
| 44 | |
| 45 | ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath; |
| 46 | ASSERT_EQ(slash_count, static_cast<size_t>(ftwbuf->level)) << fpath; |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int check_ftw(const char* fpath, const struct stat* sb, int tflag) { |
| 50 | sanity_check_ftw(fpath, sb, tflag); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 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 | } |
| 58 | |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 59 | int check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) { |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 60 | sanity_check_nftw(fpath, sb, tflag, ftwbuf); |
| 61 | return 0; |
| 62 | } |
| 63 | |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 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); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | TEST(ftw, ftw) { |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 70 | ASSERT_EQ(0, ftw("/sys", check_ftw, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ftw, ftw64) { |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 74 | ASSERT_EQ(0, ftw64("/sys", check_ftw64, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(ftw, nftw) { |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 78 | ASSERT_EQ(0, nftw("/sys", check_nftw, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(ftw, nftw64) { |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 82 | ASSERT_EQ(0, nftw64("/sys", check_nftw64, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 83 | } |