Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame^] | 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 | |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 15 | #include "errors.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 16 | #include "handler.hpp" |
Patrick Venture | c87de55 | 2020-05-20 20:25:39 -0700 | [diff] [blame] | 17 | #include "handler_impl.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 18 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 19 | #include <fstream> |
| 20 | #include <nlohmann/json.hpp> |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 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 | { |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 33 | Handler h; |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 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()); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 39 | TEST(HandlerTest, CableCheckIllegalPath) |
| 40 | { |
| 41 | Handler h; |
| 42 | EXPECT_THROW(h.getRxPackets("eth0/../../"), IpmiException); |
| 43 | } |
| 44 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 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 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 109 | // TODO: Add checks for other functions of handler. |
| 110 | |
| 111 | } // namespace ipmi |
| 112 | } // namespace google |