| OLD | NEW |
| 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/native_messaging/native_messaging_reader.h" | 5 #include "remoting/host/native_messaging/native_messaging_reader.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include <stdint.h> |
| 8 |
| 8 #include "base/bind.h" | 9 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "remoting/host/setup/test_util.h" | 14 #include "remoting/host/setup/test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace remoting { | 17 namespace remoting { |
| 17 | 18 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), | 69 base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)), |
| 69 run_loop_.QuitClosure()); | 70 run_loop_.QuitClosure()); |
| 70 run_loop_.Run(); | 71 run_loop_.Run(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) { | 74 void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) { |
| 74 message_ = message.Pass(); | 75 message_ = message.Pass(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void NativeMessagingReaderTest::WriteMessage(const std::string& message) { | 78 void NativeMessagingReaderTest::WriteMessage(const std::string& message) { |
| 78 uint32 length = message.length(); | 79 uint32_t length = message.length(); |
| 79 WriteData(reinterpret_cast<char*>(&length), 4); | 80 WriteData(reinterpret_cast<char*>(&length), 4); |
| 80 WriteData(message.data(), length); | 81 WriteData(message.data(), length); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void NativeMessagingReaderTest::WriteData(const char* data, int length) { | 84 void NativeMessagingReaderTest::WriteData(const char* data, int length) { |
| 84 int written = write_file_.WriteAtCurrentPos(data, length); | 85 int written = write_file_.WriteAtCurrentPos(data, length); |
| 85 ASSERT_EQ(length, written); | 86 ASSERT_EQ(length, written); |
| 86 } | 87 } |
| 87 | 88 |
| 88 TEST_F(NativeMessagingReaderTest, GoodMessage) { | 89 TEST_F(NativeMessagingReaderTest, GoodMessage) { |
| 89 WriteMessage("{\"foo\": 42}"); | 90 WriteMessage("{\"foo\": 42}"); |
| 90 Run(); | 91 Run(); |
| 91 EXPECT_TRUE(message_); | 92 EXPECT_TRUE(message_); |
| 92 base::DictionaryValue* message_dict; | 93 base::DictionaryValue* message_dict; |
| 93 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); | 94 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); |
| 94 int result; | 95 int result; |
| 95 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | 96 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); |
| 96 EXPECT_EQ(42, result); | 97 EXPECT_EQ(42, result); |
| 97 } | 98 } |
| 98 | 99 |
| 99 TEST_F(NativeMessagingReaderTest, InvalidLength) { | 100 TEST_F(NativeMessagingReaderTest, InvalidLength) { |
| 100 uint32 length = 0xffffffff; | 101 uint32_t length = 0xffffffff; |
| 101 WriteData(reinterpret_cast<char*>(&length), 4); | 102 WriteData(reinterpret_cast<char*>(&length), 4); |
| 102 Run(); | 103 Run(); |
| 103 EXPECT_FALSE(message_); | 104 EXPECT_FALSE(message_); |
| 104 } | 105 } |
| 105 | 106 |
| 106 TEST_F(NativeMessagingReaderTest, EmptyFile) { | 107 TEST_F(NativeMessagingReaderTest, EmptyFile) { |
| 107 Run(); | 108 Run(); |
| 108 EXPECT_FALSE(message_); | 109 EXPECT_FALSE(message_); |
| 109 } | 110 } |
| 110 | 111 |
| 111 TEST_F(NativeMessagingReaderTest, ShortHeader) { | 112 TEST_F(NativeMessagingReaderTest, ShortHeader) { |
| 112 // Write only 3 bytes - the message length header is supposed to be 4 bytes. | 113 // Write only 3 bytes - the message length header is supposed to be 4 bytes. |
| 113 WriteData("xxx", 3); | 114 WriteData("xxx", 3); |
| 114 Run(); | 115 Run(); |
| 115 EXPECT_FALSE(message_); | 116 EXPECT_FALSE(message_); |
| 116 } | 117 } |
| 117 | 118 |
| 118 TEST_F(NativeMessagingReaderTest, EmptyBody) { | 119 TEST_F(NativeMessagingReaderTest, EmptyBody) { |
| 119 uint32 length = 1; | 120 uint32_t length = 1; |
| 120 WriteData(reinterpret_cast<char*>(&length), 4); | 121 WriteData(reinterpret_cast<char*>(&length), 4); |
| 121 Run(); | 122 Run(); |
| 122 EXPECT_FALSE(message_); | 123 EXPECT_FALSE(message_); |
| 123 } | 124 } |
| 124 | 125 |
| 125 TEST_F(NativeMessagingReaderTest, ShortBody) { | 126 TEST_F(NativeMessagingReaderTest, ShortBody) { |
| 126 uint32 length = 2; | 127 uint32_t length = 2; |
| 127 WriteData(reinterpret_cast<char*>(&length), 4); | 128 WriteData(reinterpret_cast<char*>(&length), 4); |
| 128 | 129 |
| 129 // Only write 1 byte, where the header indicates there should be 2 bytes. | 130 // Only write 1 byte, where the header indicates there should be 2 bytes. |
| 130 WriteData("x", 1); | 131 WriteData("x", 1); |
| 131 Run(); | 132 Run(); |
| 132 EXPECT_FALSE(message_); | 133 EXPECT_FALSE(message_); |
| 133 } | 134 } |
| 134 | 135 |
| 135 TEST_F(NativeMessagingReaderTest, InvalidJSON) { | 136 TEST_F(NativeMessagingReaderTest, InvalidJSON) { |
| 136 std::string text = "{"; | 137 std::string text = "{"; |
| 137 WriteMessage(text); | 138 WriteMessage(text); |
| 138 Run(); | 139 Run(); |
| 139 EXPECT_FALSE(message_); | 140 EXPECT_FALSE(message_); |
| 140 } | 141 } |
| 141 | 142 |
| 142 TEST_F(NativeMessagingReaderTest, SecondMessage) { | 143 TEST_F(NativeMessagingReaderTest, SecondMessage) { |
| 143 WriteMessage("{}"); | 144 WriteMessage("{}"); |
| 144 WriteMessage("{\"foo\": 42}"); | 145 WriteMessage("{\"foo\": 42}"); |
| 145 Run(); | 146 Run(); |
| 146 EXPECT_TRUE(message_); | 147 EXPECT_TRUE(message_); |
| 147 base::DictionaryValue* message_dict; | 148 base::DictionaryValue* message_dict; |
| 148 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); | 149 EXPECT_TRUE(message_->GetAsDictionary(&message_dict)); |
| 149 int result; | 150 int result; |
| 150 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); | 151 EXPECT_TRUE(message_dict->GetInteger("foo", &result)); |
| 151 EXPECT_EQ(42, result); | 152 EXPECT_EQ(42, result); |
| 152 } | 153 } |
| 153 | 154 |
| 154 } // namespace remoting | 155 } // namespace remoting |
| OLD | NEW |