1// Copyright 2014 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 <mach/mach.h> 6 7#include <vector> 8 9#include "base/memory/ref_counted.h" 10#include "base/strings/string_util.h" 11#include "base/strings/stringprintf.h" 12#include "chrome/browser/ui/browser.h" 13#include "chrome/browser/ui/browser_window.h" 14#include "chrome/browser/ui/tabs/tab_strip_model.h" 15#include "chrome/test/base/in_process_browser_test.h" 16#include "net/test/spawned_test_server/spawned_test_server.h" 17#include "testing/perf/perf_test.h" 18 19namespace { 20 21// This test spawns a new browser and counts the number of open Mach ports in 22// the browser process. It navigates tabs and closes them, repeatedly measuring 23// the number of open ports. This is used to protect against leaking Mach ports, 24// which was the source of <http://crbug.com/105513>. 25class MachPortsTest : public InProcessBrowserTest { 26 public: 27 MachPortsTest() 28 : server_(net::SpawnedTestServer::TYPE_HTTP, 29 net::SpawnedTestServer::kLocalhost, 30 base::FilePath(FILE_PATH_LITERAL("data/mach_ports/moz"))) { 31 } 32 33 virtual void SetUp() OVERRIDE { 34 InProcessBrowserTest::SetUp(); 35 36 ASSERT_TRUE(server_.Start()); 37 } 38 39 virtual void TearDown() OVERRIDE { 40 std::string ports; 41 for (std::vector<int>::iterator it = port_counts_.begin(); 42 it != port_counts_.end(); ++it) { 43 base::StringAppendF(&ports, "%d,", *it); 44 } 45 perf_test::PrintResultList("mach_ports", "", "", ports, "ports", true); 46 47 InProcessBrowserTest::TearDown(); 48 } 49 50 // Gets the browser's current number of Mach ports and records it. 51 void RecordPortCount() { 52 mach_port_name_array_t names; 53 mach_msg_type_number_t names_count = 0; 54 mach_port_type_array_t types; 55 mach_msg_type_number_t types_count = 0; 56 57 mach_port_t self = mach_task_self(); 58 59 // A friendlier interface would allow NULL buffers to only get the counts. 60 kern_return_t kr = mach_port_names(self, 61 &names, &names_count, 62 &types, &types_count); 63 ASSERT_EQ(KERN_SUCCESS, kr) << "Failed to get mach_port_names(): " 64 << mach_error_string(kr); 65 ASSERT_EQ(names_count, types_count); // Documented kernel invariant. 66 67 port_counts_.push_back(names_count); 68 69 vm_deallocate(self, reinterpret_cast<vm_address_t>(names), 70 names_count * sizeof(mach_port_name_array_t)); 71 vm_deallocate(self, reinterpret_cast<vm_address_t>(types), 72 types_count * sizeof(mach_port_type_array_t)); 73 } 74 75 // Adds a tab from the page cycler data at the specified domain. 76 void AddTab(const std::string& domain) { 77 GURL url = server_.GetURL("files/" + domain + "/").Resolve("?skip"); 78 AddTabAtIndex(0, url, ui::PAGE_TRANSITION_TYPED); 79 } 80 81 private: 82 net::SpawnedTestServer server_; 83 std::vector<int> port_counts_; 84}; 85 86IN_PROC_BROWSER_TEST_F(MachPortsTest, GetCounts) { 87 browser()->window()->Show(); 88 89 // Record startup number. 90 RecordPortCount(); 91 92 // Create a browser and open a few tabs. 93 AddTab("www.google.com"); 94 RecordPortCount(); 95 96 AddTab("www.cnn.com"); 97 RecordPortCount(); 98 99 AddTab("www.nytimes.com"); 100 RecordPortCount(); 101 102 TabStripModel* tab_strip_model = browser()->tab_strip_model(); 103 int tab_count = tab_strip_model->count(); 104 ASSERT_EQ(4, tab_count); // Also count about:blank. 105 106 // Close each tab, recording the number of ports after each. Do not close the 107 // last tab, which is about:blank. 108 for (int i = 0; i < tab_count - 1; ++i) { 109 EXPECT_TRUE( 110 tab_strip_model->CloseWebContentsAt(0, TabStripModel::CLOSE_NONE)); 111 RecordPortCount(); 112 } 113} 114 115} // namespace 116