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

Unified Diff: tools/android/forwarder2/host_controllers_manager_unittest.cc

Issue 2736053003: [Android] Fix port leak in the forwarder. (Closed)
Patch Set: use callback for exit notifier fd Created 3 years, 9 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: tools/android/forwarder2/host_controllers_manager_unittest.cc
diff --git a/tools/android/forwarder2/host_controllers_manager_unittest.cc b/tools/android/forwarder2/host_controllers_manager_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e3b025debfd659702df41b4c2f0f87007b370bd0
--- /dev/null
+++ b/tools/android/forwarder2/host_controllers_manager_unittest.cc
@@ -0,0 +1,71 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "tools/android/forwarder2/host_controllers_manager.h"
+
+#include <cstdio>
+
+#include "base/files/file_util.h"
+#include "base/files/scoped_file.h"
+#include "base/memory/ref_counted.h"
+#include "base/strings/stringprintf.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace forwarder2 {
+
+namespace {
+
+int UnusedGetExitNotifierFD() {
+ return 0;
+}
+
+base::FilePath CreateScript(const std::string script_contents) {
+ base::FilePath script_file;
+ FILE* script_file_handle = base::CreateAndOpenTemporaryFile(&script_file);
+ base::WriteFile(script_file, script_contents.c_str(),
+ script_contents.length());
+ base::CloseFile(script_file_handle);
+ base::SetPosixFilePermissions(script_file,
+ base::FILE_PERMISSION_READ_BY_USER |
+ base::FILE_PERMISSION_EXECUTE_BY_USER);
+ return script_file;
+}
+
+} // anonymous namespace
+
+// Ensure that we don't start the adb binary with superfluous file descriptors
+// from the parent process.
+TEST(HostControllersManagerTest, AdbNoExtraFds) {
+ HostControllersManager h(base::Bind(&UnusedGetExitNotifierFD));
Ted C 2017/03/28 23:57:37 I would use something like manager over h (or even
jbudorick 2017/03/29 01:55:42 Switched to manager.
+ base::FilePath unrelated_file;
+ base::ScopedFILE open_unrelated_file(
+ CreateAndOpenTemporaryFile(&unrelated_file));
+ const int unrelated_fd = fileno(open_unrelated_file.get());
+ base::FilePath adb_script =
+ CreateScript(base::StringPrintf("#! /bin/sh\n"
+ "test ! -e /proc/$$/fd/%d\n",
+ unrelated_fd));
+ const std::string serial("0123456789abcdef");
+ const std::string map_call(
+ "forward tcp:12345 localabstract:chrome_device_forwarder");
+ std::string unused_output;
+ ASSERT_TRUE(h.Adb(adb_script.value(), serial, map_call, &unused_output))
+ << "File descriptor " << unrelated_fd << " leaked to child process";
+}
+
+// Ensure that we don't mangle the argument order.
+TEST(HostControllersManagerTest, AdbArgumentSequence) {
+ HostControllersManager h(base::Bind(&UnusedGetExitNotifierFD));
+ base::FilePath adb_script =
+ CreateScript(base::StringPrintf("#! /bin/sh\n"
+ "echo -n \"$@\"\n"));
+ const std::string serial("0123456789abcdef");
+ const std::string unmap_call("forward --remove tcp:12345");
+ std::string output;
+ ASSERT_TRUE(h.Adb(adb_script.value(), serial, unmap_call, &output));
+ ASSERT_STREQ("-s 0123456789abcdef forward --remove tcp:12345",
+ output.c_str());
+}
+
+} // namespace forwarder2

Powered by Google App Engine
This is Rietveld 408576698