OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/setup/native_messaging_writer.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/platform_file.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/values.h" | |
13 #include "remoting/host/setup/test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace remoting { | |
17 | |
18 class NativeMessagingWriterTest : public testing::Test { | |
19 public: | |
20 NativeMessagingWriterTest(); | |
21 virtual ~NativeMessagingWriterTest(); | |
22 | |
23 virtual void SetUp() OVERRIDE; | |
24 virtual void TearDown() OVERRIDE; | |
25 | |
26 protected: | |
27 scoped_ptr<NativeMessagingWriter> writer_; | |
28 base::PlatformFile read_handle_; | |
29 base::PlatformFile write_handle_; | |
30 bool read_handle_open_; | |
31 }; | |
32 | |
33 NativeMessagingWriterTest::NativeMessagingWriterTest() {} | |
34 | |
35 NativeMessagingWriterTest::~NativeMessagingWriterTest() {} | |
36 | |
37 void NativeMessagingWriterTest::SetUp() { | |
38 ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_)); | |
39 writer_.reset(new NativeMessagingWriter(write_handle_)); | |
40 read_handle_open_ = true; | |
41 } | |
42 | |
43 void NativeMessagingWriterTest::TearDown() { | |
44 // |write_handle_| is owned by NativeMessagingWriter's FileStream, so don't | |
45 // try to close it here. And close |read_handle_| only if it hasn't | |
46 // already been closed. | |
47 if (read_handle_open_) | |
48 base::ClosePlatformFile(read_handle_); | |
49 } | |
50 | |
51 TEST_F(NativeMessagingWriterTest, GoodMessage) { | |
52 base::DictionaryValue message; | |
53 message.SetInteger("foo", 42); | |
54 EXPECT_TRUE(writer_->WriteMessage(message)); | |
55 | |
56 // Read from the pipe and verify the content. | |
57 uint32 length; | |
58 int read = base::ReadPlatformFileAtCurrentPos( | |
59 read_handle_, reinterpret_cast<char*>(&length), 4); | |
60 EXPECT_EQ(4, read); | |
61 std::string content(length, '\0'); | |
62 read = base::ReadPlatformFileAtCurrentPos(read_handle_, | |
63 string_as_array(&content), length); | |
64 EXPECT_EQ(static_cast<int>(length), read); | |
65 | |
66 // |content| should now contain serialized |message|. | |
67 scoped_ptr<base::Value> written_message(base::JSONReader::Read(content)); | |
68 EXPECT_TRUE(message.Equals(written_message.get())); | |
69 | |
70 // Nothing more should have been written. Close the write-end of the pipe, | |
71 // and verify the read end immediately hits EOF. | |
72 writer_.reset(NULL); | |
73 char unused; | |
74 read = base::ReadPlatformFileAtCurrentPos(read_handle_, &unused, 1); | |
75 EXPECT_LE(read, 0); | |
76 } | |
77 | |
78 TEST_F(NativeMessagingWriterTest, SecondMessage) { | |
79 base::DictionaryValue message1; | |
80 base::DictionaryValue message2; | |
81 message2.SetInteger("foo", 42); | |
82 EXPECT_TRUE(writer_->WriteMessage(message1)); | |
83 EXPECT_TRUE(writer_->WriteMessage(message2)); | |
84 writer_.reset(NULL); | |
85 | |
86 // Read two messages. | |
87 uint32 length; | |
88 int read; | |
89 std::string content; | |
90 for (int i = 0; i < 2; i++) { | |
91 read = base::ReadPlatformFileAtCurrentPos( | |
92 read_handle_, reinterpret_cast<char*>(&length), 4); | |
93 EXPECT_EQ(4, read) << "i = " << i; | |
94 content.resize(length); | |
95 read = base::ReadPlatformFileAtCurrentPos(read_handle_, | |
96 string_as_array(&content), | |
97 length); | |
98 EXPECT_EQ(static_cast<int>(length), read) << "i = " << i; | |
99 } | |
100 | |
101 // |content| should now contain serialized |message2|. | |
102 scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content)); | |
103 EXPECT_TRUE(message2.Equals(written_message2.get())); | |
104 } | |
105 | |
106 TEST_F(NativeMessagingWriterTest, FailedWrite) { | |
107 // Close the read end so that writing fails immediately. | |
108 base::ClosePlatformFile(read_handle_); | |
109 read_handle_open_ = false; | |
110 | |
111 base::DictionaryValue message; | |
112 EXPECT_FALSE(writer_->WriteMessage(message)); | |
113 } | |
114 | |
115 } // namespace remoting | |
OLD | NEW |