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

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

Powered by Google App Engine
This is Rietveld 408576698