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

Side by Side Diff: remoting/host/it2me/it2me_native_messaging_host_unittest.cc

Issue 1272833002: Pass error messages from native messaging to web-app. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests. Created 5 years, 4 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "remoting/host/it2me/it2me_native_messaging_host.h" 5 #include "remoting/host/it2me/it2me_native_messaging_host.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/stringize_macros.h" 14 #include "base/strings/stringize_macros.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "net/base/file_stream.h" 16 #include "net/base/file_stream.h"
17 #include "net/base/net_util.h" 17 #include "net/base/net_util.h"
18 #include "remoting/base/auto_thread_task_runner.h" 18 #include "remoting/base/auto_thread_task_runner.h"
19 #include "remoting/host/chromoting_host_context.h" 19 #include "remoting/host/chromoting_host_context.h"
20 #include "remoting/host/native_messaging/log_message_handler.h"
20 #include "remoting/host/native_messaging/native_messaging_pipe.h" 21 #include "remoting/host/native_messaging/native_messaging_pipe.h"
21 #include "remoting/host/native_messaging/pipe_messaging_channel.h" 22 #include "remoting/host/native_messaging/pipe_messaging_channel.h"
22 #include "remoting/host/policy_watcher.h" 23 #include "remoting/host/policy_watcher.h"
23 #include "remoting/host/setup/test_util.h" 24 #include "remoting/host/setup/test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 namespace remoting { 27 namespace remoting {
27 28
28 namespace { 29 namespace {
29 30
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); 255 scoped_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
255 EXPECT_FALSE(response); 256 EXPECT_FALSE(response);
256 257
257 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. 258 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it.
258 // So the only handle left to close is |output_read_file_|. 259 // So the only handle left to close is |output_read_file_|.
259 output_read_file_.Close(); 260 output_read_file_.Close();
260 } 261 }
261 262
262 scoped_ptr<base::DictionaryValue> 263 scoped_ptr<base::DictionaryValue>
263 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { 264 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() {
264 uint32 length; 265 while (true) {
265 int read_result = output_read_file_.ReadAtCurrentPos( 266 uint32 length;
266 reinterpret_cast<char*>(&length), sizeof(length)); 267 int read_result = output_read_file_.ReadAtCurrentPos(
267 if (read_result != sizeof(length)) { 268 reinterpret_cast<char*>(&length), sizeof(length));
268 // The output pipe has been closed, return an empty message. 269 if (read_result != sizeof(length)) {
269 return nullptr; 270 // The output pipe has been closed, return an empty message.
271 return nullptr;
272 }
273
274 std::string message_json(length, '\0');
275 read_result = output_read_file_.ReadAtCurrentPos(
276 string_as_array(&message_json), length);
277 if (read_result != static_cast<int>(length)) {
278 LOG(ERROR) << "Message size (" << read_result
279 << ") doesn't match the header (" << length << ").";
280 return nullptr;
281 }
282
283 scoped_ptr<base::Value> message = base::JSONReader::Read(message_json);
284 if (!message || !message->IsType(base::Value::TYPE_DICTIONARY)) {
285 LOG(ERROR) << "Malformed message:" << message_json;
286 return nullptr;
287 }
288
289 scoped_ptr<base::DictionaryValue> result = make_scoped_ptr(
290 static_cast<base::DictionaryValue*>(message.release()));
291 std::string type;
292 if (!result->GetString("type", &type) ||
Sergey Ulanov 2015/08/20 21:50:20 Add a comment that we are skipping log messages he
Jamie 2015/08/20 22:29:16 Done.
293 type != LogMessageHandler::debug_message_type_name) {
294 return result;
295 }
270 } 296 }
271
272 std::string message_json(length, '\0');
273 read_result = output_read_file_.ReadAtCurrentPos(
274 string_as_array(&message_json), length);
275 if (read_result != static_cast<int>(length)) {
276 LOG(ERROR) << "Message size (" << read_result
277 << ") doesn't match the header (" << length << ").";
278 return nullptr;
279 }
280
281 scoped_ptr<base::Value> message = base::JSONReader::Read(message_json);
282 if (!message || !message->IsType(base::Value::TYPE_DICTIONARY)) {
283 LOG(ERROR) << "Malformed message:" << message_json;
284 return nullptr;
285 }
286
287 return make_scoped_ptr(
288 static_cast<base::DictionaryValue*>(message.release()));
289 } 297 }
290 298
291 void It2MeNativeMessagingHostTest::WriteMessageToInputPipe( 299 void It2MeNativeMessagingHostTest::WriteMessageToInputPipe(
292 const base::Value& message) { 300 const base::Value& message) {
293 std::string message_json; 301 std::string message_json;
294 base::JSONWriter::Write(message, &message_json); 302 base::JSONWriter::Write(message, &message_json);
295 303
296 uint32 length = message_json.length(); 304 uint32 length = message_json.length();
297 input_write_file_.WriteAtCurrentPos(reinterpret_cast<char*>(&length), 305 input_write_file_.WriteAtCurrentPos(reinterpret_cast<char*>(&length),
298 sizeof(length)); 306 sizeof(length));
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 } 548 }
541 549
542 // Verify rejection if type is unrecognized. 550 // Verify rejection if type is unrecognized.
543 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { 551 TEST_F(It2MeNativeMessagingHostTest, InvalidType) {
544 base::DictionaryValue message; 552 base::DictionaryValue message;
545 message.SetString("type", "xxx"); 553 message.SetString("type", "xxx");
546 TestBadRequest(message, true); 554 TestBadRequest(message, true);
547 } 555 }
548 556
549 } // namespace remoting 557 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698