-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversions_command_test.cc
88 lines (71 loc) · 3.04 KB
/
versions_command_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2020 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include "biod/versions_command.h"
namespace biod {
namespace {
using ::testing::Return;
TEST(VersionsCommand, VersionsCommand) {
VersionsCommand cmd(EC_CMD_FP_CONTEXT);
EXPECT_EQ(cmd.Version(), 1);
EXPECT_EQ(cmd.Command(), EC_CMD_GET_CMD_VERSIONS);
EXPECT_EQ(cmd.CommandCode(), EC_CMD_FP_CONTEXT);
}
// Mock the underlying EcCommand to test
class VersionsCommandTest : public testing::Test {
public:
class MockVersionsCommand : public VersionsCommand {
public:
using VersionsCommand::VersionsCommand;
MOCK_METHOD(bool, Run, (int fd), (override));
MOCK_METHOD(struct ec_response_get_cmd_versions*, Resp, (), (override));
MOCK_METHOD(uint32_t, Result, (), (const, override));
};
};
TEST_F(VersionsCommandTest, Unknown) {
VersionsCommand cmd(EC_CMD_FP_CONTEXT);
// The command has not been run, so we don't know the status.
EXPECT_EQ(cmd.IsVersionSupported(1), EcCmdVersionSupportStatus::UNKNOWN);
}
TEST_F(VersionsCommandTest, CommandNotFound) {
MockVersionsCommand mock_command(EC_CMD_FP_CONTEXT);
EXPECT_CALL(mock_command, Result)
.WillRepeatedly(Return(EC_RES_INVALID_COMMAND));
EXPECT_EQ(mock_command.IsVersionSupported(1),
EcCmdVersionSupportStatus::UNSUPPORTED);
}
TEST_F(VersionsCommandTest, CommandNotSupported) {
MockVersionsCommand mock_command(EC_CMD_FP_CONTEXT);
EXPECT_CALL(mock_command, Result).WillRepeatedly(Return(EC_RES_SUCCESS));
constexpr int kVersionZero = 0;
constexpr int kVersionOne = 1;
struct ec_response_get_cmd_versions response = {.version_mask =
1 << kVersionZero};
EXPECT_CALL(mock_command, Resp).WillOnce(Return(&response));
EXPECT_EQ(mock_command.IsVersionSupported(kVersionOne),
EcCmdVersionSupportStatus::UNSUPPORTED);
}
TEST_F(VersionsCommandTest, CommandSupported) {
MockVersionsCommand mock_command(EC_CMD_FP_CONTEXT);
EXPECT_CALL(mock_command, Result).WillRepeatedly(Return(EC_RES_SUCCESS));
constexpr int kVersionOne = 1;
struct ec_response_get_cmd_versions response = {.version_mask =
1 << kVersionOne};
EXPECT_CALL(mock_command, Resp).WillOnce(Return(&response));
EXPECT_EQ(mock_command.IsVersionSupported(kVersionOne),
EcCmdVersionSupportStatus::SUPPORTED);
}
TEST_F(VersionsCommandTest, InvalidParamShouldNotBeRetried) {
MockVersionsCommand mock_command(EC_CMD_FP_CONTEXT);
// We should only run the command once even though we specify retries,
// since this type of failure cannot be retried.
EXPECT_CALL(mock_command, Run).WillOnce(Return(false));
EXPECT_CALL(mock_command, Result).WillOnce(Return(EC_RES_INVALID_PARAM));
constexpr int kTestFd = -1;
constexpr int kNumAttempts = 2;
EXPECT_FALSE(mock_command.RunWithMultipleAttempts(kTestFd, kNumAttempts));
}
} // namespace
} // namespace biod