This source file includes following definitions.
- SetUp
- TearDown
- Run
- OnMessage
- WriteMessage
- WriteData
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "remoting/host/native_messaging/native_messaging_reader.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/platform_file.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "remoting/host/setup/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
class NativeMessagingReaderTest : public testing::Test {
public:
NativeMessagingReaderTest();
virtual ~NativeMessagingReaderTest();
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
void Run();
void OnMessage(scoped_ptr<base::Value> message);
void WriteMessage(std::string message);
void WriteData(const char* data, int length);
protected:
scoped_ptr<NativeMessagingReader> reader_;
base::PlatformFile read_handle_;
base::PlatformFile write_handle_;
scoped_ptr<base::Value> message_;
private:
base::MessageLoopForIO message_loop_;
base::RunLoop run_loop_;
};
NativeMessagingReaderTest::NativeMessagingReaderTest() {
}
NativeMessagingReaderTest::~NativeMessagingReaderTest() {}
void NativeMessagingReaderTest::SetUp() {
ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_));
reader_.reset(new NativeMessagingReader(read_handle_));
}
void NativeMessagingReaderTest::TearDown() {
}
void NativeMessagingReaderTest::Run() {
base::ClosePlatformFile(write_handle_);
reader_->Start(
base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)),
run_loop_.QuitClosure());
run_loop_.Run();
}
void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) {
message_ = message.Pass();
}
void NativeMessagingReaderTest::WriteMessage(std::string message) {
uint32 length = message.length();
WriteData(reinterpret_cast<char*>(&length), 4);
WriteData(message.data(), length);
}
void NativeMessagingReaderTest::WriteData(const char* data, int length) {
int written = base::WritePlatformFileAtCurrentPos(write_handle_, data,
length);
ASSERT_EQ(length, written);
}
TEST_F(NativeMessagingReaderTest, GoodMessage) {
WriteMessage("{\"foo\": 42}");
Run();
EXPECT_TRUE(message_);
base::DictionaryValue* message_dict;
EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
int result;
EXPECT_TRUE(message_dict->GetInteger("foo", &result));
EXPECT_EQ(42, result);
}
TEST_F(NativeMessagingReaderTest, InvalidLength) {
uint32 length = 0xffffffff;
WriteData(reinterpret_cast<char*>(&length), 4);
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, EmptyFile) {
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, ShortHeader) {
WriteData("xxx", 3);
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, EmptyBody) {
uint32 length = 1;
WriteData(reinterpret_cast<char*>(&length), 4);
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, ShortBody) {
uint32 length = 2;
WriteData(reinterpret_cast<char*>(&length), 4);
WriteData("x", 1);
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, InvalidJSON) {
std::string text = "{";
WriteMessage(text);
Run();
EXPECT_FALSE(message_);
}
TEST_F(NativeMessagingReaderTest, SecondMessage) {
WriteMessage("{}");
WriteMessage("{\"foo\": 42}");
Run();
EXPECT_TRUE(message_);
base::DictionaryValue* message_dict;
EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
int result;
EXPECT_TRUE(message_dict->GetInteger("foo", &result));
EXPECT_EQ(42, result);
}
}