Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2279)

Unified Diff: base/trace_event/process_memory_maps_dump_provider_unittest.cc

Issue 1332943002: [tracing] Smaps provider uses fscanf instead of istream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/trace_event/process_memory_maps_dump_provider_unittest.cc
diff --git a/base/trace_event/process_memory_maps_dump_provider_unittest.cc b/base/trace_event/process_memory_maps_dump_provider_unittest.cc
index f951a09fb8a2aaf84a290a96ece8bf0db26a3518..d9b0832a6e065f335fc289fa0e425cc9db9bc43f 100644
--- a/base/trace_event/process_memory_maps_dump_provider_unittest.cc
+++ b/base/trace_event/process_memory_maps_dump_provider_unittest.cc
@@ -4,9 +4,9 @@
#include "base/trace_event/process_memory_maps_dump_provider.h"
-#include <fstream>
-#include <sstream>
+#include <fcntl.h>
+#include "base/files/file_util.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/process_memory_maps.h"
#include "base/trace_event/trace_event_argument.h"
@@ -104,6 +104,17 @@ const char kTestSmaps2[] =
"MMUPageSize: 4 kB\n"
"Locked: 0 kB\n"
"VmFlags: rd wr mr mw me ac sd\n";
+
+void CreateAndSetSmapsFileForTesting(const char* smaps_string, ScopedFD& fd) {
+ FilePath temp_path;
+ FILE* temp_file = CreateAndOpenTemporaryFile(&temp_path);
+ fd.reset(fileno(temp_file));
+ ASSERT_TRUE(fd.is_valid());
+
+ ASSERT_TRUE(
+ base::WriteFileDescriptor(fd.get(), smaps_string, strlen(smaps_string)));
+}
+
} // namespace
TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
@@ -116,23 +127,24 @@ TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
// Emulate a non-existent /proc/self/smaps.
ProcessMemoryDump pmd_invalid(nullptr /* session_state */);
- std::ifstream non_existent_file("/tmp/does-not-exist");
- ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file;
- CHECK_EQ(false, non_existent_file.good());
+ ScopedFD non_existent_fd(open("/tmp/does-not-exist", O_RDONLY));
+ ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_fd;
+ CHECK_EQ(false, non_existent_fd.is_valid());
pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
ASSERT_FALSE(pmd_invalid.has_process_mmaps());
// Emulate an empty /proc/self/smaps.
- std::ifstream empty_file("/dev/null");
+ ScopedFD empty_file(open("/dev/null", O_RDONLY));
+ ASSERT_TRUE(empty_file.is_valid());
ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file;
- CHECK_EQ(true, empty_file.good());
pmmdp->OnMemoryDump(dump_args, &pmd_invalid);
ASSERT_FALSE(pmd_invalid.has_process_mmaps());
// Parse the 1st smaps file.
ProcessMemoryDump pmd_1(nullptr /* session_state */);
- std::istringstream test_smaps_1(kTestSmaps1);
- ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1;
+ ScopedFD temp_fd1;
+ CreateAndSetSmapsFileForTesting(kTestSmaps1, temp_fd1);
+ ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &temp_fd1;
pmmdp->OnMemoryDump(dump_args, &pmd_1);
ASSERT_TRUE(pmd_1.has_process_mmaps());
const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
@@ -162,8 +174,9 @@ TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
// Parse the 2nd smaps file.
ProcessMemoryDump pmd_2(nullptr /* session_state */);
- std::istringstream test_smaps_2(kTestSmaps2);
- ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2;
+ ScopedFD temp_fd2;
+ CreateAndSetSmapsFileForTesting(kTestSmaps2, temp_fd2);
+ ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &temp_fd2;
pmmdp->OnMemoryDump(dump_args, &pmd_2);
ASSERT_TRUE(pmd_2.has_process_mmaps());
const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();

Powered by Google App Engine
This is Rietveld 408576698