1 // Copyright (c) 2012 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.
5 #include "base/files/dir_reader_posix.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/logging.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #if defined(OS_ANDROID)
18 #include "base/os_compat_android.h"
23 TEST(DirReaderPosixUnittest
, Read
) {
24 static const unsigned kNumFiles
= 100;
26 if (DirReaderPosix::IsFallback())
29 base::ScopedTempDir temp_dir
;
30 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
31 const char* dir
= temp_dir
.path().value().c_str();
34 const int prev_wd
= open(".", O_RDONLY
| O_DIRECTORY
);
35 DCHECK_GE(prev_wd
, 0);
37 PCHECK(chdir(dir
) == 0);
39 for (unsigned i
= 0; i
< kNumFiles
; i
++) {
41 snprintf(buf
, sizeof(buf
), "%d", i
);
42 const int fd
= open(buf
, O_CREAT
| O_RDONLY
| O_EXCL
, 0600);
44 PCHECK(close(fd
) == 0);
47 std::set
<unsigned> seen
;
49 DirReaderPosix
reader(dir
);
50 EXPECT_TRUE(reader
.IsValid());
52 if (!reader
.IsValid())
55 bool seen_dot
= false, seen_dotdot
= false;
57 for (; reader
.Next(); ) {
58 if (strcmp(reader
.name(), ".") == 0) {
62 if (strcmp(reader
.name(), "..") == 0) {
67 SCOPED_TRACE(testing::Message() << "reader.name(): " << reader
.name());
70 const unsigned long value
= strtoul(reader
.name(), &endptr
, 10);
72 EXPECT_FALSE(*endptr
);
73 EXPECT_LT(value
, kNumFiles
);
74 EXPECT_EQ(0u, seen
.count(value
));
78 for (unsigned i
= 0; i
< kNumFiles
; i
++) {
80 snprintf(buf
, sizeof(buf
), "%d", i
);
81 PCHECK(unlink(buf
) == 0);
84 PCHECK(rmdir(dir
) == 0);
86 PCHECK(fchdir(prev_wd
) == 0);
87 PCHECK(close(prev_wd
) == 0);
89 EXPECT_TRUE(seen_dot
);
90 EXPECT_TRUE(seen_dotdot
);
91 EXPECT_EQ(kNumFiles
, seen
.size());