1 // 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 
15 #include "errors.hpp"
16 #include "handler.hpp"
17 #include "handler_impl.hpp"
18 
19 #include <fstream>
20 #include <nlohmann/json.hpp>
21 #include <string>
22 #include <tuple>
23 
24 #include <gtest/gtest.h>
25 
26 namespace google
27 {
28 namespace ipmi
29 {
30 
31 TEST(HandlerTest, EthCheckValidHappy)
32 {
33     Handler h;
34     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());
37 }
38 
39 TEST(HandlerTest, CableCheckIllegalPath)
40 {
41     Handler h;
42     EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException);
43 }
44 
45 TEST(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 
81 TEST(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 
95 TEST(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 
109 // TODO: Add checks for other functions of handler.
110 
111 } // namespace ipmi
112 } // namespace google
113