blob: b93e8772c00a12c45949ce0890cd8e02ec7f1f82 [file] [log] [blame]
Patrick Ventured2037c62019-03-15 10:29:47 -07001#include "errors.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -07002#include "handler.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -07003#include "handler_impl.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -07004
Patrick Ventureab650002019-03-16 09:08:47 -07005#include <fstream>
6#include <nlohmann/json.hpp>
Patrick Venturef085d912019-03-15 08:50:00 -07007#include <string>
8#include <tuple>
9
10#include <gtest/gtest.h>
11
12namespace google
13{
14namespace ipmi
15{
16
17TEST(HandlerTest, EthCheckValidHappy)
18{
Patrick Venturef085d912019-03-15 08:50:00 -070019 Handler h;
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070020 std::tuple<std::uint8_t, std::string> result = h.getEthDetails("et");
21 EXPECT_EQ(12, std::get<0>(result));
22 EXPECT_STREQ("et", std::get<1>(result).c_str());
Patrick Venturef085d912019-03-15 08:50:00 -070023}
24
Patrick Ventured2037c62019-03-15 10:29:47 -070025TEST(HandlerTest, CableCheckIllegalPath)
26{
27 Handler h;
28 EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException);
29}
30
Patrick Ventureab650002019-03-16 09:08:47 -070031TEST(HandlerTest, readNameFromConfigInstanceVariety)
32{
33 // Make sure it handles the failures and successes as we expect.
34 struct testCase
35 {
36 std::string type;
37 std::uint8_t instance;
38 std::string expectedName;
39 };
40
41 std::vector<testCase> tests = {
42 {"cpu", 5, ""},
43 {"cpu", 3, "CPU2"},
44 };
45
46 auto j2 = R"(
47 {
48 "cpu": [
49 {"instance": 1, "name": "CPU0"},
50 {"instance": 2, "name": "CPU1"},
51 {"instance": 3, "name": "CPU2"},
52 {"instance": 4, "name": "CPU3"}
53 ]
54 }
55 )"_json;
56
57 for (const auto& test : tests)
58 {
59 EXPECT_STREQ(test.expectedName.c_str(),
60 readNameFromConfig(test.type, test.instance, j2).c_str());
61 }
62}
63
64// TODO: If we can test with phosphor-logging in the future, there are more
65// failure cases.
66
67TEST(HandlerTest, getEntityNameWithNameNotFoundExcepts)
68{
69 const char* testFilename = "test.json";
70 std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
71 std::ofstream outputJson(testFilename);
72 outputJson << contents;
73 outputJson.flush();
74 outputJson.close();
75
76 Handler h(testFilename);
77 EXPECT_THROW(h.getEntityName(0x03, 2), IpmiException);
78 (void)std::remove(testFilename);
79}
80
81TEST(HandlerTest, getEntityNameWithNameFoundReturnsIt)
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_STREQ("CPU0", h.getEntityName(0x03, 1).c_str());
92 (void)std::remove(testFilename);
93}
94
Patrick Venturef085d912019-03-15 08:50:00 -070095// TODO: Add checks for other functions of handler.
96
97} // namespace ipmi
98} // namespace google