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

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

Issue 2736053003: [Android] Fix port leak in the forwarder. (Closed)
Patch Set: tedchoc comments 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..3188780ec443393ba044b4f7c43018322d2f57a1
--- /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 manager(base::Bind(&UnusedGetExitNotifierFD));
+ 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(manager.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 manager(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(manager.Adb(adb_script.value(), serial, unmap_call, &output));
+ ASSERT_STREQ("-s 0123456789abcdef forward --remove tcp:12345",
+ output.c_str());
+}
+
+} // namespace forwarder2
« no previous file with comments | « tools/android/forwarder2/host_controllers_manager.cc ('k') | tools/android/forwarder2/host_forwarder_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698