-
Notifications
You must be signed in to change notification settings - Fork 1
/
goldfish_library_test.cc
47 lines (38 loc) · 1.28 KB
/
goldfish_library_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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()));
close(fds[1]);
std::string got;
EXPECT_EQ(expected_result, ReadOneMessage(fds[0], &got));
close(fds[0]);
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