Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / host / native_messaging / native_messaging_writer_unittest.cc
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/native_messaging/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
25  protected:
26   scoped_ptr<NativeMessagingWriter> writer_;
27   base::File read_file_;
28   base::File write_file_;
29 };
30
31 NativeMessagingWriterTest::NativeMessagingWriterTest() {}
32
33 NativeMessagingWriterTest::~NativeMessagingWriterTest() {}
34
35 void NativeMessagingWriterTest::SetUp() {
36   ASSERT_TRUE(MakePipe(&read_file_, &write_file_));
37   writer_.reset(new NativeMessagingWriter(write_file_.Pass()));
38 }
39
40 TEST_F(NativeMessagingWriterTest, GoodMessage) {
41   base::DictionaryValue message;
42   message.SetInteger("foo", 42);
43   EXPECT_TRUE(writer_->WriteMessage(message));
44
45   // Read from the pipe and verify the content.
46   uint32 length;
47   int read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
48   EXPECT_EQ(4, read);
49   std::string content(length, '\0');
50   read = read_file_.ReadAtCurrentPos(string_as_array(&content), length);
51   EXPECT_EQ(static_cast<int>(length), read);
52
53   // |content| should now contain serialized |message|.
54   scoped_ptr<base::Value> written_message(base::JSONReader::Read(content));
55   EXPECT_TRUE(message.Equals(written_message.get()));
56
57   // Nothing more should have been written. Close the write-end of the pipe,
58   // and verify the read end immediately hits EOF.
59   writer_.reset(NULL);
60   char unused;
61   read = read_file_.ReadAtCurrentPos(&unused, 1);
62   EXPECT_LE(read, 0);
63 }
64
65 TEST_F(NativeMessagingWriterTest, SecondMessage) {
66   base::DictionaryValue message1;
67   base::DictionaryValue message2;
68   message2.SetInteger("foo", 42);
69   EXPECT_TRUE(writer_->WriteMessage(message1));
70   EXPECT_TRUE(writer_->WriteMessage(message2));
71   writer_.reset(NULL);
72
73   // Read two messages.
74   uint32 length;
75   int read;
76   std::string content;
77   for (int i = 0; i < 2; i++) {
78     read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
79     EXPECT_EQ(4, read) << "i = " << i;
80     content.resize(length);
81     read = read_file_.ReadAtCurrentPos(string_as_array(&content), length);
82     EXPECT_EQ(static_cast<int>(length), read) << "i = " << i;
83   }
84
85   // |content| should now contain serialized |message2|.
86   scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content));
87   EXPECT_TRUE(message2.Equals(written_message2.get()));
88 }
89
90 TEST_F(NativeMessagingWriterTest, FailedWrite) {
91   // Close the read end so that writing fails immediately.
92   read_file_.Close();
93
94   base::DictionaryValue message;
95   EXPECT_FALSE(writer_->WriteMessage(message));
96 }
97
98 }  // namespace remoting