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

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

Issue 11968028: Remove connect message from Native Messaging API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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 unified diff | Download patch | Annotate | Revision Log
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/threading/sequenced_worker_pool.h" 14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" 16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
17 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/chrome_version_info.h" 19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/extensions/features/feature.h" 20 #include "chrome/common/extensions/features/feature.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread.h" 22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 24
25 using content::BrowserThread; 25 using content::BrowserThread;
26 26
27 namespace { 27 namespace {
28 28
29 const char kTestMessage[] = "{\"text\": \"Hello.\"}";
30
29 FilePath GetTestDir() { 31 FilePath GetTestDir() {
30 FilePath test_dir; 32 FilePath test_dir;
31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); 33 PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
32 test_dir = test_dir.AppendASCII("native_messaging"); 34 test_dir = test_dir.AppendASCII("native_messaging");
33 return test_dir; 35 return test_dir;
34 } 36 }
35 37
36 } // namespace 38 } // namespace
37 39
38 namespace extensions { 40 namespace extensions {
(...skipping 24 matching lines...) Expand all
63 65
64 private: 66 private:
65 base::PlatformFile read_file_; 67 base::PlatformFile read_file_;
66 base::PlatformFile write_file_; 68 base::PlatformFile write_file_;
67 }; 69 };
68 70
69 class NativeMessagingTest : public ::testing::Test, 71 class NativeMessagingTest : public ::testing::Test,
70 public NativeMessageProcessHost::Client, 72 public NativeMessageProcessHost::Client,
71 public base::SupportsWeakPtr<NativeMessagingTest> { 73 public base::SupportsWeakPtr<NativeMessagingTest> {
72 public: 74 public:
73 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) { 75 NativeMessagingTest()
76 : current_channel_(chrome::VersionInfo::CHANNEL_DEV),
77 native_message_process_host_(NULL) {
74 } 78 }
75 79
76 virtual void SetUp() { 80 virtual void SetUp() OVERRIDE {
77 CommandLine::ForCurrentProcess()->AppendSwitch( 81 CommandLine::ForCurrentProcess()->AppendSwitch(
78 switches::kEnableNativeMessaging); 82 switches::kEnableNativeMessaging);
79 // Change the user data dir so native apps will be looked for in the test 83 // Change the user data dir so native apps will be looked for in the test
80 // directory. 84 // directory.
81 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); 85 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); 86 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir()));
83 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, 87 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
84 &message_loop_)); 88 &message_loop_));
85 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, 89 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
86 &message_loop_)); 90 &message_loop_));
87 } 91 }
88 92
89 virtual void TearDown() { 93 virtual void TearDown() OVERRIDE {
90 // Change the user data dir back for other tests. 94 // Change the user data dir back for other tests.
91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); 95 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_));
92 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, 96 if (native_message_process_host_) {
93 native_message_process_host_); 97 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
98 native_message_process_host_);
99 }
94 message_loop_.RunUntilIdle(); 100 message_loop_.RunUntilIdle();
95 } 101 }
96 102
97 void PostMessageFromNativeProcess(int port_id, const std::string& message) { 103 virtual void PostMessageFromNativeProcess(
104 int port_id,
105 const std::string& message) OVERRIDE {
98 last_posted_message_ = message; 106 last_posted_message_ = message;
99 } 107 }
100 108
101 void CloseChannel(int port_id, bool error) { 109 virtual void CloseChannel(int port_id, bool error) OVERRIDE {
102 } 110 }
103 111
104 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { 112 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) {
105 native_message_process_host_ = process.release(); 113 native_message_process_host_ = process.release();
106 } 114 }
107 115
108 protected: 116 protected:
117 std::string FormatMessage(const std::string& message) {
118 Pickle pickle;
119 pickle.WriteString(message);
120 return std::string(const_cast<const Pickle*>(&pickle)->payload(),
121 pickle.payload_size());
122 }
123
124 FilePath CreateTempFileWithMessage(const std::string& message) {
125 FilePath filename;
126 file_util::CreateTemporaryFile(&filename);
127 std::string message_with_header = FormatMessage(message);
128 EXPECT_TRUE(file_util::WriteFile(
129 filename, message_with_header.data(), message_with_header.size()));
130 return filename;
131 }
132
109 // Force the channel to be dev. 133 // Force the channel to be dev.
110 Feature::ScopedCurrentChannel current_channel_; 134 Feature::ScopedCurrentChannel current_channel_;
111 NativeMessageProcessHost* native_message_process_host_; 135 NativeMessageProcessHost* native_message_process_host_;
112 FilePath user_data_dir_; 136 FilePath user_data_dir_;
113 MessageLoopForIO message_loop_; 137 MessageLoopForIO message_loop_;
114 scoped_ptr<content::TestBrowserThread> ui_thread_; 138 scoped_ptr<content::TestBrowserThread> ui_thread_;
115 scoped_ptr<content::TestBrowserThread> file_thread_; 139 scoped_ptr<content::TestBrowserThread> file_thread_;
116 std::string last_posted_message_; 140 std::string last_posted_message_;
117 }; 141 };
118 142
119 // Read a single message from a local file (single_message_response.msg). 143 // Read a single message from a local file.
120 TEST_F(NativeMessagingTest, SingleSendMessageRead) { 144 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
121 FilePath temp_file; 145 FilePath temp_ouput_file;
122 file_util::CreateTemporaryFile(&temp_file); 146 file_util::CreateTemporaryFile(&temp_ouput_file);
123 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), 147 FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage);
124 temp_file); 148
149 FakeLauncher launcher(temp_input_file, temp_ouput_file);
125 NativeMessageProcessHost::CreateWithLauncher( 150 NativeMessageProcessHost::CreateWithLauncher(
126 AsWeakPtr(), "empty_app.py", "{}", 0, 151 AsWeakPtr(), "empty_app.py", 0,
127 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( 152 base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr()),
128 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
129 launcher); 153 launcher);
130 message_loop_.RunUntilIdle(); 154 message_loop_.RunUntilIdle();
131 ASSERT_TRUE(native_message_process_host_); 155 ASSERT_TRUE(native_message_process_host_);
132 native_message_process_host_->ReadNowForTesting(); 156 native_message_process_host_->ReadNowForTesting();
133 message_loop_.RunUntilIdle(); 157 message_loop_.RunUntilIdle();
134 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); 158 EXPECT_EQ(kTestMessage, last_posted_message_);
135 file_util::Delete(temp_file, false /* non-recursive */); 159
160 file_util::Delete(temp_ouput_file, false /* non-recursive */);
161 file_util::Delete(temp_input_file, false /* non-recursive */);
Matt Perry 2013/01/17 19:26:38 nit: might be better to use a ScopedTempDir and pu
Sergey Ulanov 2013/01/18 02:50:11 Done.
136 } 162 }
137 163
138 // Tests sending a single message. The message should get written to 164 // Tests sending a single message. The message should get written to
139 // |temp_file| and should match the contents of single_message_request.msg. 165 // |temp_file| and should match the contents of single_message_request.msg.
140 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { 166 TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
141 FilePath temp_file; 167 FilePath temp_output_file;
142 file_util::CreateTemporaryFile(&temp_file); 168 file_util::CreateTemporaryFile(&temp_output_file);
143 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), 169 FilePath temp_input_file = CreateTempFileWithMessage(std::string());
144 temp_file); 170
171 FakeLauncher launcher(temp_input_file, temp_output_file);
145 NativeMessageProcessHost::CreateWithLauncher( 172 NativeMessageProcessHost::CreateWithLauncher(
146 AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, 173 AsWeakPtr(), "empty_app.py", 0,
147 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( 174 base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr()),
148 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
149 launcher); 175 launcher);
150 message_loop_.RunUntilIdle(); 176 message_loop_.RunUntilIdle();
177 native_message_process_host_->Send(kTestMessage);
178 message_loop_.RunUntilIdle();
151 ASSERT_TRUE(native_message_process_host_); 179 ASSERT_TRUE(native_message_process_host_);
152 180
153 EXPECT_TRUE(file_util::ContentsEqual( 181 std::string output;
154 temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); 182 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output));
183 EXPECT_EQ(FormatMessage(kTestMessage), output);
155 184
156 file_util::Delete(temp_file, false /* non-recursive */); 185 file_util::Delete(temp_output_file, false /* non-recursive */);
186 file_util::Delete(temp_input_file, false /* non-recursive */);
157 } 187 }
158 188
159 // Disabled, see http://crbug.com/159754. 189 // Disabled, see http://crbug.com/159754.
160 // Test send message with a real client. The client just echo's back the text 190 // Test send message with a real client. The client just echo's back the text
161 // it recieved. 191 // it recieved.
162 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { 192 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) {
163 NativeMessageProcessHost::Create( 193 NativeMessageProcessHost::Create(
164 AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, 194 AsWeakPtr(), "echo.py", 0,
165 NativeMessageProcessHost::TYPE_CONNECT, base::Bind( 195 base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr()));
166 &NativeMessagingTest::AcquireProcess, AsWeakPtr())); 196 message_loop_.RunUntilIdle();
197 native_message_process_host_->Send("{\"text\": \"Hello.\"}");
167 message_loop_.RunUntilIdle(); 198 message_loop_.RunUntilIdle();
168 ASSERT_TRUE(native_message_process_host_); 199 ASSERT_TRUE(native_message_process_host_);
169 200
170 native_message_process_host_->ReadNowForTesting(); 201 native_message_process_host_->ReadNowForTesting();
171 message_loop_.RunUntilIdle(); 202 message_loop_.RunUntilIdle();
172 EXPECT_EQ(last_posted_message_, 203 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}",
173 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); 204 last_posted_message_);
174 205
175 native_message_process_host_->Send("{\"foo\": \"bar\"}"); 206 native_message_process_host_->Send("{\"foo\": \"bar\"}");
176 message_loop_.RunUntilIdle(); 207 message_loop_.RunUntilIdle();
177 native_message_process_host_->ReadNowForTesting(); 208 native_message_process_host_->ReadNowForTesting();
178 message_loop_.RunUntilIdle(); 209 message_loop_.RunUntilIdle();
179 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); 210 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_);
180 } 211 }
181 212
182 } // namespace extensions 213 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698