blob: 1dda3ce34687bbe7c46aa76df175329b64287fb1 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Patrick Ventured2037c62019-03-15 10:29:47 -070015#include "errors.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -070016#include "handler.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070017#include "handler_impl.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -070018
Patrick Ventureab650002019-03-16 09:08:47 -070019#include <fstream>
20#include <nlohmann/json.hpp>
Patrick Venturef085d912019-03-15 08:50:00 -070021#include <string>
22#include <tuple>
23
24#include <gtest/gtest.h>
25
26namespace google
27{
28namespace ipmi
29{
30
31TEST(HandlerTest, EthCheckValidHappy)
32{
Patrick Venturef085d912019-03-15 08:50:00 -070033 Handler h;
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070034 std::tuple<std::uint8_t, std::string> result = h.getEthDetails("et");
35 EXPECT_EQ(12, std::get<0>(result));
36 EXPECT_STREQ("et", std::get<1>(result).c_str());
Patrick Venturef085d912019-03-15 08:50:00 -070037}
38
Patrick Ventured2037c62019-03-15 10:29:47 -070039TEST(HandlerTest, CableCheckIllegalPath)
40{
41 Handler h;
42 EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException);
43}
44
Patrick Ventureab650002019-03-16 09:08:47 -070045TEST(HandlerTest, readNameFromConfigInstanceVariety)
46{
47 // Make sure it handles the failures and successes as we expect.
48 struct testCase
49 {
50 std::string type;
51 std::uint8_t instance;
52 std::string expectedName;
53 };
54
55 std::vector<testCase> tests = {
56 {"cpu", 5, ""},
57 {"cpu", 3, "CPU2"},
58 };
59
60 auto j2 = R"(
61 {
62 "cpu": [
63 {"instance": 1, "name": "CPU0"},
64 {"instance": 2, "name": "CPU1"},
65 {"instance": 3, "name": "CPU2"},
66 {"instance": 4, "name": "CPU3"}
67 ]
68 }
69 )"_json;
70
71 for (const auto& test : tests)
72 {
73 EXPECT_STREQ(test.expectedName.c_str(),
74 readNameFromConfig(test.type, test.instance, j2).c_str());
75 }
76}
77
78// TODO: If we can test with phosphor-logging in the future, there are more
79// failure cases.
80
81TEST(HandlerTest, getEntityNameWithNameNotFoundExcepts)
82{
83 const char* testFilename = "test.json";
84 std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
85 std::ofstream outputJson(testFilename);
86 outputJson << contents;
87 outputJson.flush();
88 outputJson.close();
89
90 Handler h(testFilename);
91 EXPECT_THROW(h.getEntityName(0x03, 2), IpmiException);
92 (void)std::remove(testFilename);
93}
94
95TEST(HandlerTest, getEntityNameWithNameFoundReturnsIt)
96{
97 const char* testFilename = "test.json";
98 std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
99 std::ofstream outputJson(testFilename);
100 outputJson << contents;
101 outputJson.flush();
102 outputJson.close();
103
104 Handler h(testFilename);
105 EXPECT_STREQ("CPU0", h.getEntityName(0x03, 1).c_str());
106 (void)std::remove(testFilename);
107}
108
Patrick Venturef085d912019-03-15 08:50:00 -0700109// TODO: Add checks for other functions of handler.
110
111} // namespace ipmi
112} // namespace google