blob: a2e38a101695de1c95322199f5d4a9e6e63bf3b8 [file] [log] [blame]
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "goldfishd/goldfish_library.h"
#include <string>
#include <unistd.h>
#include <base/files/file_util.h>
#include <testing/gtest/include/gtest/gtest.h>
namespace goldfishd {
namespace {
// Test if ReadOneMessage can parse |input|.
// |expected_result| is expected result.
// |expected_msg| is expected parsed message.
void ExpectReadMessage(const std::string& input,
bool expected_result,
const std::string& expected_msg) {
int fds[2];
int ret = pipe(fds);
ASSERT_EQ(0, ret);
ASSERT_TRUE(base::WriteFileDescriptor(fds[1], input.c_str(), input.size()));
std::string got;
EXPECT_EQ(expected_result, ReadOneMessage(fds[0], &got));
if (expected_result) {
EXPECT_EQ(expected_msg, got);
TEST(GoldfishLibraryTest, ReadOneMessage) {
ExpectReadMessage("0002OK", true, "OK");
ExpectReadMessage("O", false, "");
ExpectReadMessage("INVALID", false, "");
ExpectReadMessage("-001OK", false, "");
ExpectReadMessage("0400OK", false, "");
ExpectReadMessage("0003OK", false, "");
} // namespace
} // namespace goldfishd