OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/dir_reader_posix.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 #include <unistd.h> |
| 12 |
| 13 #include "base/logging.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using base::DirReaderPosix; |
| 17 |
| 18 namespace { |
| 19 typedef testing::Test DirReaderPosixUnittest; |
| 20 |
| 21 TEST(DirReaderPosixUnittest, Read) { |
| 22 static const unsigned kNumFiles = 100; |
| 23 |
| 24 if (DirReaderPosix::IsFallback()) |
| 25 return; |
| 26 |
| 27 char kDirTemplate[] = "/tmp/org.chromium.dir-reader-posix-XXXXXX"; |
| 28 const char* dir = mkdtemp(kDirTemplate); |
| 29 CHECK(dir); |
| 30 |
| 31 const int prev_wd = open(".", O_RDONLY | O_DIRECTORY); |
| 32 CHECK_GE(prev_wd, 0); |
| 33 |
| 34 PCHECK(chdir(dir) == 0); |
| 35 |
| 36 for (unsigned i = 0; i < kNumFiles; i++) { |
| 37 char buf[16]; |
| 38 snprintf(buf, sizeof(buf), "%d", i); |
| 39 const int fd = open(buf, O_CREAT | O_RDONLY | O_EXCL, 0600); |
| 40 PCHECK(fd >= 0); |
| 41 PCHECK(close(fd) == 0); |
| 42 } |
| 43 |
| 44 std::set<unsigned> seen; |
| 45 |
| 46 DirReaderPosix reader(dir); |
| 47 EXPECT_TRUE(reader.IsValid()); |
| 48 |
| 49 if (!reader.IsValid()) |
| 50 return; |
| 51 |
| 52 bool seen_dot = false, seen_dotdot = false; |
| 53 |
| 54 for (; reader.Next(); ) { |
| 55 if (strcmp(reader.name(), ".") == 0) { |
| 56 seen_dot = true; |
| 57 continue; |
| 58 } |
| 59 if (strcmp(reader.name(), "..") == 0) { |
| 60 seen_dotdot = true; |
| 61 continue; |
| 62 } |
| 63 |
| 64 SCOPED_TRACE(testing::Message() << "reader.name(): " << reader.name()); |
| 65 |
| 66 char *endptr; |
| 67 const unsigned long value = strtoul(reader.name(), &endptr, 10); |
| 68 |
| 69 EXPECT_FALSE(*endptr); |
| 70 EXPECT_LT(value, kNumFiles); |
| 71 EXPECT_EQ(0u, seen.count(value)); |
| 72 seen.insert(value); |
| 73 } |
| 74 |
| 75 for (unsigned i = 0; i < kNumFiles; i++) { |
| 76 char buf[16]; |
| 77 snprintf(buf, sizeof(buf), "%d", i); |
| 78 PCHECK(unlink(buf) == 0); |
| 79 } |
| 80 |
| 81 PCHECK(rmdir(dir) == 0); |
| 82 |
| 83 PCHECK(fchdir(prev_wd) == 0); |
| 84 PCHECK(close(prev_wd) == 0); |
| 85 |
| 86 EXPECT_TRUE(seen_dot); |
| 87 EXPECT_TRUE(seen_dotdot); |
| 88 EXPECT_EQ(kNumFiles, seen.size()); |
| 89 } |
| 90 |
| 91 } // anonymous namespace |
OLD | NEW |