Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / files / dir_reader_posix_unittest.cc
blob2e181b3d8517d9309f1e24e090fef2719901cf08
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/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"
19 #endif
21 namespace base {
23 TEST(DirReaderPosixUnittest, Read) {
24 static const unsigned kNumFiles = 100;
26 if (DirReaderPosix::IsFallback())
27 return;
29 base::ScopedTempDir temp_dir;
30 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
31 const char* dir = temp_dir.path().value().c_str();
32 ASSERT_TRUE(dir);
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++) {
40 char buf[16];
41 snprintf(buf, sizeof(buf), "%d", i);
42 const int fd = open(buf, O_CREAT | O_RDONLY | O_EXCL, 0600);
43 PCHECK(fd >= 0);
44 PCHECK(close(fd) == 0);
47 std::set<unsigned> seen;
49 DirReaderPosix reader(dir);
50 EXPECT_TRUE(reader.IsValid());
52 if (!reader.IsValid())
53 return;
55 bool seen_dot = false, seen_dotdot = false;
57 for (; reader.Next(); ) {
58 if (strcmp(reader.name(), ".") == 0) {
59 seen_dot = true;
60 continue;
62 if (strcmp(reader.name(), "..") == 0) {
63 seen_dotdot = true;
64 continue;
67 SCOPED_TRACE(testing::Message() << "reader.name(): " << reader.name());
69 char *endptr;
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));
75 seen.insert(value);
78 for (unsigned i = 0; i < kNumFiles; i++) {
79 char buf[16];
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());
94 } // namespace base