De-duplicate BASE_IMPLEMENTATION define in the GN build.
[chromium-blink-merge.git] / base / files / dir_reader_posix_unittest.cc
blob0685031a982c22772c8e187c5fc301578b9d0ea0
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"
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "base/logging.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_ANDROID)
17 #include "base/os_compat_android.h"
18 #endif
20 namespace base {
22 TEST(DirReaderPosixUnittest, Read) {
23 static const unsigned kNumFiles = 100;
25 if (DirReaderPosix::IsFallback())
26 return;
28 char kDirTemplate[] = "/tmp/org.chromium.dir-reader-posix-XXXXXX";
29 const char* dir = mkdtemp(kDirTemplate);
30 ASSERT_TRUE(dir);
32 const int prev_wd = open(".", O_RDONLY | O_DIRECTORY);
33 DCHECK_GE(prev_wd, 0);
35 PCHECK(chdir(dir) == 0);
37 for (unsigned i = 0; i < kNumFiles; i++) {
38 char buf[16];
39 snprintf(buf, sizeof(buf), "%d", i);
40 const int fd = open(buf, O_CREAT | O_RDONLY | O_EXCL, 0600);
41 PCHECK(fd >= 0);
42 PCHECK(close(fd) == 0);
45 std::set<unsigned> seen;
47 DirReaderPosix reader(dir);
48 EXPECT_TRUE(reader.IsValid());
50 if (!reader.IsValid())
51 return;
53 bool seen_dot = false, seen_dotdot = false;
55 for (; reader.Next(); ) {
56 if (strcmp(reader.name(), ".") == 0) {
57 seen_dot = true;
58 continue;
60 if (strcmp(reader.name(), "..") == 0) {
61 seen_dotdot = true;
62 continue;
65 SCOPED_TRACE(testing::Message() << "reader.name(): " << reader.name());
67 char *endptr;
68 const unsigned long value = strtoul(reader.name(), &endptr, 10);
70 EXPECT_FALSE(*endptr);
71 EXPECT_LT(value, kNumFiles);
72 EXPECT_EQ(0u, seen.count(value));
73 seen.insert(value);
76 for (unsigned i = 0; i < kNumFiles; i++) {
77 char buf[16];
78 snprintf(buf, sizeof(buf), "%d", i);
79 PCHECK(unlink(buf) == 0);
82 PCHECK(rmdir(dir) == 0);
84 PCHECK(fchdir(prev_wd) == 0);
85 PCHECK(close(prev_wd) == 0);
87 EXPECT_TRUE(seen_dot);
88 EXPECT_TRUE(seen_dotdot);
89 EXPECT_EQ(kNumFiles, seen.size());
92 } // namespace base