blob: 99782faffb2cb164884eb6c5ace2ea762f769ddc [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{
19 // The code returns compiled-in information, and therefore cannot really
20 // fail.
21 Handler h;
22 std::tuple<std::uint8_t, std::string> result = h.getEthDetails();
23 EXPECT_EQ(1, std::get<0>(result));
24 EXPECT_STREQ("eth0", std::get<1>(result).c_str());
25}
26
Patrick Ventured2037c62019-03-15 10:29:47 -070027TEST(HandlerTest, CableCheckIllegalPath)
28{
29 Handler h;
30 EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException);
31}
32
Patrick Ventureab650002019-03-16 09:08:47 -070033TEST(HandlerTest, readNameFromConfigInstanceVariety)
34{
35 // Make sure it handles the failures and successes as we expect.
36 struct testCase
37 {
38 std::string type;
39 std::uint8_t instance;
40 std::string expectedName;
41 };
42
43 std::vector<testCase> tests = {
44 {"cpu", 5, ""},
45 {"cpu", 3, "CPU2"},
46 };
47
48 auto j2 = R"(
49 {
50 "cpu": [
51 {"instance": 1, "name": "CPU0"},
52 {"instance": 2, "name": "CPU1"},
53 {"instance": 3, "name": "CPU2"},
54 {"instance": 4, "name": "CPU3"}
55 ]
56 }
57 )"_json;
58
59 for (const auto& test : tests)
60 {
61 EXPECT_STREQ(test.expectedName.c_str(),
62 readNameFromConfig(test.type, test.instance, j2).c_str());
63 }
64}
65
66// TODO: If we can test with phosphor-logging in the future, there are more
67// failure cases.
68
69TEST(HandlerTest, getEntityNameWithNameNotFoundExcepts)
70{
71 const char* testFilename = "test.json";
72 std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
73 std::ofstream outputJson(testFilename);
74 outputJson << contents;
75 outputJson.flush();
76 outputJson.close();
77
78 Handler h(testFilename);
79 EXPECT_THROW(h.getEntityName(0x03, 2), IpmiException);
80 (void)std::remove(testFilename);
81}
82
83TEST(HandlerTest, getEntityNameWithNameFoundReturnsIt)
84{
85 const char* testFilename = "test.json";
86 std::string contents = R"({"cpu": [{"instance": 1, "name": "CPU0"}]})";
87 std::ofstream outputJson(testFilename);
88 outputJson << contents;
89 outputJson.flush();
90 outputJson.close();
91
92 Handler h(testFilename);
93 EXPECT_STREQ("CPU0", h.getEntityName(0x03, 1).c_str());
94 (void)std::remove(testFilename);
95}
96
Patrick Venturef085d912019-03-15 08:50:00 -070097// TODO: Add checks for other functions of handler.
98
99} // namespace ipmi
100} // namespace google