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

Unified Diff: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc

Issue 24409004: Disconnect native messaging port on read pipe EOF. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
index b8b6df156f55e9c454622d65ab29074d258a1172..77f81c4f741b6b9189e2406e47051a2f3d99dd2a 100644
--- a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
+++ b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
@@ -13,6 +13,7 @@
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/platform_file.h"
+#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/test_timeouts.h"
@@ -31,6 +32,13 @@
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(OS_WIN)
+#include <windows.h>
+#include "base/win/scoped_handle.h"
+#else
+#include <unistd.h>
+#endif
+
using content::BrowserThread;
namespace {
@@ -50,17 +58,30 @@ namespace extensions {
class FakeLauncher : public NativeProcessLauncher {
public:
- FakeLauncher(base::FilePath read_file, base::FilePath write_file) {
- read_file_ = base::CreatePlatformFile(
- read_file,
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC,
- NULL, NULL);
- write_file_ = base::CreatePlatformFile(
- write_file,
- base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC,
- NULL, NULL);
+ FakeLauncher(base::PlatformFile read_file, base::PlatformFile write_file)
+ : read_file_(read_file),
+ write_file_(write_file) {
+ }
+
+ static scoped_ptr<NativeProcessLauncher> Create(base::FilePath read_file,
+ base::FilePath write_file) {
+ int read_flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
+ base::PLATFORM_FILE_ASYNC;
+ int write_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE |
+ base::PLATFORM_FILE_ASYNC;
+ return scoped_ptr<NativeProcessLauncher>(new FakeLauncher(
+ base::CreatePlatformFile(read_file, read_flags, NULL, NULL),
+ base::CreatePlatformFile(write_file, write_flags, NULL, NULL)));
+ }
+
+ static scoped_ptr<NativeProcessLauncher> CreateWithPipeInput(
+ base::PlatformFile read_pipe,
+ base::FilePath write_file) {
+ int write_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE |
+ base::PLATFORM_FILE_ASYNC;
+ return scoped_ptr<NativeProcessLauncher>(new FakeLauncher(
+ read_pipe,
+ base::CreatePlatformFile(write_file, write_flags, NULL, NULL)));
}
virtual void Launch(const GURL& origin,
@@ -158,8 +179,8 @@ TEST_F(NativeMessagingTest, SingleSendMessageRead) {
base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage);
- scoped_ptr<NativeProcessLauncher> launcher(
- new FakeLauncher(temp_input_file, temp_output_file));
+ scoped_ptr<NativeProcessLauncher> launcher =
+ FakeLauncher::Create(temp_input_file, temp_output_file).Pass();
native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
0, launcher.Pass());
@@ -179,10 +200,34 @@ TEST_F(NativeMessagingTest, SingleSendMessageRead) {
// |temp_file| and should match the contents of single_message_request.msg.
TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
- base::FilePath temp_input_file = CreateTempFileWithMessage(std::string());
- scoped_ptr<NativeProcessLauncher> launcher(
- new FakeLauncher(temp_input_file, temp_output_file));
+ base::PlatformFile read_file;
+#if defined(OS_WIN)
+ string16 pipe_name = base::StringPrintf(
+ L"\\\\.\\pipe\\chrome.nativeMessaging.out.%llx", base::RandUint64());
+ base::win::ScopedHandle write_handle(
+ CreateNamedPipeW(pipe_name.c_str(),
+ PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED |
+ FILE_FLAG_FIRST_PIPE_INSTANCE,
+ PIPE_TYPE_BYTE, 1, 0, 0, 5000, NULL));
+ ASSERT_TRUE(write_handle);
+ base::win::ScopedHandle read_handle(
+ CreateFileW(pipe_name.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL));
+ ASSERT_TRUE(read_handle);
+
+ read_file = read_handle.Get();
+#else // defined(OS_WIN)
+ base::PlatformFile pipe_handles[2];
+ ASSERT_EQ(0, pipe(pipe_handles));
+ file_util::ScopedFD read_fd(pipe_handles);
+ file_util::ScopedFD write_fd(pipe_handles + 1);
+
+ read_file = pipe_handles[0];
+#endif // !defined(OS_WIN)
+
+ scoped_ptr<NativeProcessLauncher> launcher =
+ FakeLauncher::CreateWithPipeInput(read_file, temp_output_file).Pass();
native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
0, launcher.Pass());

Powered by Google App Engine
This is Rietveld 408576698