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

Side by Side Diff: chrome/browser/extensions/api/messaging/native_message_process_host_unittest_posix.cc

Issue 10918255: The Windows portion of Native Messagaing (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/platform_file.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/extensions/features/feature.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using content::BrowserThread;
26
27 namespace {
28
29 FilePath GetTestDir() {
30 FilePath test_dir;
31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
32 test_dir = test_dir.AppendASCII("native_messaging");
33 return test_dir;
34 }
35
36 } // namespace
37
38 namespace extensions {
39
40 class FakeLauncher : public NativeProcessLauncher {
41 public:
42 FakeLauncher(FilePath read_file, FilePath write_file) {
43 read_file_ = base::CreatePlatformFile(
44 read_file,
45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
46 NULL, NULL);
47 write_file_ = base::CreatePlatformFile(
48 write_file,
49 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
50 NULL, NULL);
51 }
52
53 virtual bool LaunchNativeProcess(
54 const FilePath& path,
55 base::ProcessHandle* native_process_handle,
56 NativeMessageProcessHost::FileHandle* read_file,
57 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE {
58 *native_process_handle = base::kNullProcessHandle;
59 *read_file = read_file_;
60 *write_file = write_file_;
61 return true;
62 }
63
64 private:
65 base::PlatformFile read_file_;
66 base::PlatformFile write_file_;
67 };
68
69 class NativeMessagingTest : public ::testing::Test,
70 public NativeMessageProcessHost::Client,
71 public base::SupportsWeakPtr<NativeMessagingTest> {
72 public:
73 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) {
74 }
75
76 virtual void SetUp() {
77 CommandLine::ForCurrentProcess()->AppendSwitch(
78 switches::kEnableNativeMessaging);
79 // Change the user data dir so native apps will be looked for in the test
80 // directory.
81 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir()));
83 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
84 &message_loop_));
85 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
86 &message_loop_));
87 }
88
89 virtual void TearDown() {
90 // Change the user data dir back for other tests.
91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_));
92 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
93 native_message_process_host_);
94 message_loop_.RunUntilIdle();
95 }
96
97 void PostMessageFromNativeProcess(int port_id, const std::string& message) {
98 last_posted_message_ = message;
99 }
100
101 void CloseChannel(int port_id, bool error) {
102 }
103
104 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) {
105 native_message_process_host_ = process.release();
106 }
107
108 protected:
109 // Force the channel to be dev.
110 Feature::ScopedCurrentChannel current_channel_;
111 NativeMessageProcessHost* native_message_process_host_;
112 FilePath user_data_dir_;
113 MessageLoopForIO message_loop_;
114 scoped_ptr<content::TestBrowserThread> ui_thread_;
115 scoped_ptr<content::TestBrowserThread> file_thread_;
116 std::string last_posted_message_;
117 };
118
119 // Read a single message from a local file (single_message_response.msg).
120 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
121 FilePath temp_file;
122 file_util::CreateTemporaryFile(&temp_file);
123 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
124 temp_file);
125 NativeMessageProcessHost::CreateWithLauncher(
126 AsWeakPtr(), "empty_app.py", "{}", 0,
127 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
128 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
129 launcher);
130 message_loop_.RunUntilIdle();
131 ASSERT_TRUE(native_message_process_host_);
132 native_message_process_host_->ReadNowForTesting();
133 message_loop_.RunUntilIdle();
134 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}");
135 file_util::Delete(temp_file, false /* non-recursive */);
136 }
137
138 // Tests sending a single message. The message should get written to
139 // |temp_file| and should match the contents of single_message_request.msg.
140 TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
141 FilePath temp_file;
142 file_util::CreateTemporaryFile(&temp_file);
143 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
144 temp_file);
145 NativeMessageProcessHost::CreateWithLauncher(
146 AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0,
147 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
148 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
149 launcher);
150 message_loop_.RunUntilIdle();
151 ASSERT_TRUE(native_message_process_host_);
152
153 EXPECT_TRUE(file_util::ContentsEqual(
154 temp_file, GetTestDir().AppendASCII("single_message_request.msg")));
155
156 file_util::Delete(temp_file, false /* non-recursive */);
157 }
158
159 // Disabled, see http://crbug.com/159754.
160 // Test send message with a real client. The client just echo's back the text
161 // it recieved.
162 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) {
163 NativeMessageProcessHost::Create(
164 AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0,
165 NativeMessageProcessHost::TYPE_CONNECT, base::Bind(
166 &NativeMessagingTest::AcquireProcess, AsWeakPtr()));
167 message_loop_.RunUntilIdle();
168 ASSERT_TRUE(native_message_process_host_);
169
170 native_message_process_host_->ReadNowForTesting();
171 message_loop_.RunUntilIdle();
172 EXPECT_EQ(last_posted_message_,
173 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}");
174
175 native_message_process_host_->Send("{\"foo\": \"bar\"}");
176 message_loop_.RunUntilIdle();
177 native_message_process_host_->ReadNowForTesting();
178 message_loop_.RunUntilIdle();
179 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}");
180 }
181
182 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698