1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
50f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "remoting/host/native_messaging/native_messaging_writer.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/json/json_reader.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/stl_util.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/values.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "remoting/host/setup/test_util.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace remoting {
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class NativeMessagingWriterTest : public testing::Test {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  NativeMessagingWriterTest();
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~NativeMessagingWriterTest();
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) protected:
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<NativeMessagingWriter> writer_;
26010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  base::File read_file_;
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  base::File write_file_;
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NativeMessagingWriterTest::NativeMessagingWriterTest() {}
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NativeMessagingWriterTest::~NativeMessagingWriterTest() {}
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NativeMessagingWriterTest::SetUp() {
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ASSERT_TRUE(MakePipe(&read_file_, &write_file_));
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  writer_.reset(new NativeMessagingWriter(write_file_.Pass()));
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(NativeMessagingWriterTest, GoodMessage) {
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::DictionaryValue message;
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message.SetInteger("foo", 42);
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(writer_->WriteMessage(message));
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Read from the pipe and verify the content.
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  uint32 length;
46010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(4, read);
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string content(length, '\0');
49010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  read = read_file_.ReadAtCurrentPos(string_as_array(&content), length);
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_EQ(static_cast<int>(length), read);
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |content| should now contain serialized |message|.
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::Value> written_message(base::JSONReader::Read(content));
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(message.Equals(written_message.get()));
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Nothing more should have been written. Close the write-end of the pipe,
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // and verify the read end immediately hits EOF.
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  writer_.reset(NULL);
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  char unused;
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  read = read_file_.ReadAtCurrentPos(&unused, 1);
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_LE(read, 0);
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(NativeMessagingWriterTest, SecondMessage) {
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::DictionaryValue message1;
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::DictionaryValue message2;
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message2.SetInteger("foo", 42);
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(writer_->WriteMessage(message1));
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(writer_->WriteMessage(message2));
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  writer_.reset(NULL);
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Read two messages.
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  uint32 length;
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int read;
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::string content;
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (int i = 0; i < 2; i++) {
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    read = read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4);
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(4, read) << "i = " << i;
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content.resize(length);
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    read = read_file_.ReadAtCurrentPos(string_as_array(&content), length);
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    EXPECT_EQ(static_cast<int>(length), read) << "i = " << i;
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |content| should now contain serialized |message2|.
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content));
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_TRUE(message2.Equals(written_message2.get()));
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TEST_F(NativeMessagingWriterTest, FailedWrite) {
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Close the read end so that writing fails immediately.
91010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  read_file_.Close();
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::DictionaryValue message;
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EXPECT_FALSE(writer_->WriteMessage(message));
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace remoting
98