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