| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "tools/android/forwarder2/host_controllers_manager.h" |
| 6 |
| 7 #include <cstdio> |
| 8 |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace forwarder2 { |
| 16 |
| 17 namespace { |
| 18 |
| 19 int UnusedGetExitNotifierFD() { |
| 20 return 0; |
| 21 } |
| 22 |
| 23 base::FilePath CreateScript(const std::string script_contents) { |
| 24 base::FilePath script_file; |
| 25 FILE* script_file_handle = base::CreateAndOpenTemporaryFile(&script_file); |
| 26 base::WriteFile(script_file, script_contents.c_str(), |
| 27 script_contents.length()); |
| 28 base::CloseFile(script_file_handle); |
| 29 base::SetPosixFilePermissions(script_file, |
| 30 base::FILE_PERMISSION_READ_BY_USER | |
| 31 base::FILE_PERMISSION_EXECUTE_BY_USER); |
| 32 return script_file; |
| 33 } |
| 34 |
| 35 } // anonymous namespace |
| 36 |
| 37 // Ensure that we don't start the adb binary with superfluous file descriptors |
| 38 // from the parent process. |
| 39 TEST(HostControllersManagerTest, AdbNoExtraFds) { |
| 40 HostControllersManager manager(base::Bind(&UnusedGetExitNotifierFD)); |
| 41 base::FilePath unrelated_file; |
| 42 base::ScopedFILE open_unrelated_file( |
| 43 CreateAndOpenTemporaryFile(&unrelated_file)); |
| 44 const int unrelated_fd = fileno(open_unrelated_file.get()); |
| 45 base::FilePath adb_script = |
| 46 CreateScript(base::StringPrintf("#! /bin/sh\n" |
| 47 "test ! -e /proc/$$/fd/%d\n", |
| 48 unrelated_fd)); |
| 49 const std::string serial("0123456789abcdef"); |
| 50 const std::string map_call( |
| 51 "forward tcp:12345 localabstract:chrome_device_forwarder"); |
| 52 std::string unused_output; |
| 53 ASSERT_TRUE(manager.Adb(adb_script.value(), serial, map_call, &unused_output)) |
| 54 << "File descriptor " << unrelated_fd << " leaked to child process"; |
| 55 } |
| 56 |
| 57 // Ensure that we don't mangle the argument order. |
| 58 TEST(HostControllersManagerTest, AdbArgumentSequence) { |
| 59 HostControllersManager manager(base::Bind(&UnusedGetExitNotifierFD)); |
| 60 base::FilePath adb_script = |
| 61 CreateScript(base::StringPrintf("#! /bin/sh\n" |
| 62 "echo -n \"$@\"\n")); |
| 63 const std::string serial("0123456789abcdef"); |
| 64 const std::string unmap_call("forward --remove tcp:12345"); |
| 65 std::string output; |
| 66 ASSERT_TRUE(manager.Adb(adb_script.value(), serial, unmap_call, &output)); |
| 67 ASSERT_STREQ("-s 0123456789abcdef forward --remove tcp:12345", |
| 68 output.c_str()); |
| 69 } |
| 70 |
| 71 } // namespace forwarder2 |
| OLD | NEW |