Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 16 | #include "policy_find.hpp" |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame^] | 17 | #include "policy_table.hpp" |
| 18 | |
| 19 | #include <experimental/filesystem> |
| 20 | #include <fstream> |
| 21 | |
| 22 | #include <gtest/gtest.h> |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 23 | |
| 24 | using namespace ibm::logging; |
| 25 | namespace fs = std::experimental::filesystem; |
| 26 | |
| 27 | static constexpr auto json = R"( |
| 28 | [ |
| 29 | { |
| 30 | "dtls":[ |
| 31 | { |
| 32 | "CEID":"ABCD1234", |
| 33 | "mod":"", |
| 34 | "msg":"Error ABCD1234" |
| 35 | } |
| 36 | ], |
| 37 | "err":"xyz.openbmc_project.Error.Test1" |
| 38 | }, |
| 39 | |
| 40 | { |
| 41 | "dtls":[ |
| 42 | { |
| 43 | "CEID":"XYZ222", |
| 44 | "mod":"", |
| 45 | "msg":"Error XYZ222" |
| 46 | } |
| 47 | ], |
| 48 | "err":"xyz.openbmc_project.Error.Test2" |
| 49 | }, |
| 50 | |
| 51 | { |
| 52 | "dtls":[ |
| 53 | { |
| 54 | "CEID":"AAAAAA", |
| 55 | "mod":"mod1", |
| 56 | "msg":"Error AAAAAA" |
| 57 | }, |
| 58 | { |
| 59 | "CEID":"BBBBBB", |
| 60 | "mod":"mod2", |
| 61 | "msg":"Error BBBBBB" |
| 62 | }, |
| 63 | { |
| 64 | "CEID":"CCCCCC", |
| 65 | "mod":"mod3", |
| 66 | "msg":"Error CCCCCC" |
| 67 | } |
| 68 | ], |
| 69 | "err":"xyz.openbmc_project.Error.Test3" |
| 70 | }, |
| 71 | |
| 72 | { |
| 73 | "dtls":[ |
| 74 | { |
| 75 | "CEID":"DDDDDDDD", |
| 76 | "mod":"I2C", |
| 77 | "msg":"Error DDDDDDDD" |
| 78 | }, |
| 79 | { |
| 80 | "CEID":"EEEEEEEE", |
| 81 | "mod":"FSI", |
| 82 | "msg":"Error EEEEEEEE" |
| 83 | } |
| 84 | ], |
| 85 | "err":"xyz.openbmc_project.Error.Test4" |
| 86 | }, |
| 87 | |
| 88 | { |
| 89 | "dtls":[ |
| 90 | { |
| 91 | "CEID":"FFFFFFFF", |
| 92 | "mod":"6D", |
| 93 | "msg":"Error FFFFFFFF" |
| 94 | } |
| 95 | ], |
| 96 | |
| 97 | "err":"xyz.openbmc_project.Error.Test5" |
| 98 | }, |
| 99 | { |
| 100 | "dtls":[ |
| 101 | { |
| 102 | "CEID":"GGGGGGGG", |
| 103 | "mod":"RAIL_5", |
| 104 | "msg":"Error GGGGGGGG" |
| 105 | } |
| 106 | ], |
| 107 | |
| 108 | "err":"xyz.openbmc_project.Error.Test6" |
| 109 | }, |
| 110 | { |
| 111 | "dtls":[ |
| 112 | { |
| 113 | "CEID":"HHHHHHHH", |
| 114 | "mod":"INPUT_42", |
| 115 | "msg":"Error HHHHHHHH" |
| 116 | } |
| 117 | ], |
| 118 | "err":"xyz.openbmc_project.Error.Test7" |
| 119 | } |
| 120 | ])"; |
| 121 | |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 122 | /** |
| 123 | * Helper class to write the above json to a file and then |
| 124 | * remove it when the tests are over. |
| 125 | */ |
| 126 | class PolicyTableTest : public ::testing::Test |
| 127 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 128 | protected: |
| 129 | virtual void SetUp() |
| 130 | { |
| 131 | char dir[] = {"./jsonTestXXXXXX"}; |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 132 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 133 | jsonDir = mkdtemp(dir); |
| 134 | jsonFile = jsonDir / "policy.json"; |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 135 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 136 | std::ofstream f{jsonFile}; |
| 137 | f << json; |
| 138 | } |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 139 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 140 | virtual void TearDown() |
| 141 | { |
| 142 | fs::remove_all(jsonDir); |
| 143 | } |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 144 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 145 | fs::path jsonDir; |
| 146 | fs::path jsonFile; |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 147 | }; |
| 148 | |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 149 | /** |
| 150 | * Test finding entries in the policy table |
| 151 | */ |
| 152 | TEST_F(PolicyTableTest, TestTable) |
| 153 | { |
| 154 | policy::Table policy{jsonFile}; |
| 155 | ASSERT_EQ(policy.isLoaded(), true); |
| 156 | |
| 157 | //////////////////////////////////// |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 158 | // Basic search, no modifier |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 159 | std::string err{"xyz.openbmc_project.Error.Test2"}; |
| 160 | std::string mod; |
| 161 | |
| 162 | auto details = policy.find(err, mod); |
| 163 | ASSERT_EQ(static_cast<bool>(details), true); |
| 164 | if (details) |
| 165 | { |
| 166 | ASSERT_EQ((*details).get().ceid, "XYZ222"); |
| 167 | ASSERT_EQ((*details).get().msg, "Error XYZ222"); |
| 168 | } |
| 169 | |
| 170 | ///////////////////////////////////// |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 171 | // Not found |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 172 | err = "foo"; |
| 173 | details = policy.find(err, mod); |
| 174 | ASSERT_EQ(static_cast<bool>(details), false); |
| 175 | |
| 176 | ///////////////////////////////////// |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 177 | // Test with a modifier |
Matt Spinler | b96fa32 | 2018-03-27 13:39:33 -0500 | [diff] [blame] | 178 | err = "xyz.openbmc_project.Error.Test3"; |
| 179 | mod = "mod3"; |
| 180 | |
| 181 | details = policy.find(err, mod); |
| 182 | ASSERT_EQ(static_cast<bool>(details), true); |
| 183 | if (details) |
| 184 | { |
| 185 | ASSERT_EQ((*details).get().ceid, "CCCCCC"); |
| 186 | ASSERT_EQ((*details).get().msg, "Error CCCCCC"); |
| 187 | } |
| 188 | } |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * Test policy::find() that uses the data from a property |
| 192 | * map to find entries in the policy table. |
| 193 | */ |
| 194 | TEST_F(PolicyTableTest, TestFinder) |
| 195 | { |
| 196 | using namespace std::literals::string_literals; |
| 197 | |
| 198 | policy::Table policy{jsonFile}; |
| 199 | ASSERT_EQ(policy.isLoaded(), true); |
| 200 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 201 | // A basic search with no modifier |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 202 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 203 | DbusPropertyMap testProperties{ |
| 204 | {"Message"s, Value{"xyz.openbmc_project.Error.Test1"s}}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 205 | |
| 206 | auto values = policy::find(policy, testProperties); |
| 207 | ASSERT_EQ(std::get<policy::EIDField>(values), "ABCD1234"); |
| 208 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error ABCD1234"); |
| 209 | } |
| 210 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 211 | // Use CALLOUT_INVENTORY_PATH from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 212 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 213 | std::vector<std::string> ad{"FOO=BAR"s, "CALLOUT_INVENTORY_PATH=mod2"s}; |
| 214 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 215 | {"Message"s, Value{"xyz.openbmc_project.Error.Test3"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 216 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 217 | |
| 218 | auto values = policy::find(policy, testProperties); |
| 219 | ASSERT_EQ(std::get<policy::EIDField>(values), "BBBBBB"); |
| 220 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error BBBBBB"); |
| 221 | } |
| 222 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 223 | // Use an I2C DEVICE_PATH from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 224 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 225 | std::vector<std::string> ad{"FOO=BAR"s, |
| 226 | "CALLOUT_DEVICE_PATH=/some/i2c/path"s}; |
| 227 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 228 | {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 229 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 230 | |
| 231 | auto values = policy::find(policy, testProperties); |
| 232 | ASSERT_EQ(std::get<policy::EIDField>(values), "DDDDDDDD"); |
| 233 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error DDDDDDDD"); |
| 234 | } |
| 235 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 236 | // Use an FSI DEVICE_PATH from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 237 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 238 | std::vector<std::string> ad{"FOO=BAR"s, |
| 239 | "CALLOUT_DEVICE_PATH=/some/fsi/path"s}; |
| 240 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 241 | {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 242 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 243 | |
| 244 | auto values = policy::find(policy, testProperties); |
| 245 | ASSERT_EQ(std::get<policy::EIDField>(values), "EEEEEEEE"); |
| 246 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error EEEEEEEE"); |
| 247 | } |
| 248 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 249 | // Use PROCEDURE from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 250 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 251 | std::vector<std::string> ad{"FOO=BAR"s, "PROCEDURE=109"s}; |
| 252 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 253 | {"Message"s, Value{"xyz.openbmc_project.Error.Test5"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 254 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 255 | |
| 256 | auto values = policy::find(policy, testProperties); |
| 257 | ASSERT_EQ(std::get<policy::EIDField>(values), "FFFFFFFF"); |
| 258 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error FFFFFFFF"); |
| 259 | } |
| 260 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 261 | // Use RAIL_NAME from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 262 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 263 | std::vector<std::string> ad{"FOO=BAR"s, "RAIL_NAME=RAIL_5"s}; |
| 264 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 265 | {"Message"s, Value{"xyz.openbmc_project.Error.Test6"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 266 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 267 | |
| 268 | auto values = policy::find(policy, testProperties); |
| 269 | ASSERT_EQ(std::get<policy::EIDField>(values), "GGGGGGGG"); |
| 270 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error GGGGGGGG"); |
| 271 | } |
| 272 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 273 | // Use INPUT_NAME from the AdditionalData property |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 274 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 275 | std::vector<std::string> ad{"FOO=BAR"s, "INPUT_NAME=INPUT_42"s}; |
| 276 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 277 | {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 278 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 279 | |
| 280 | auto values = policy::find(policy, testProperties); |
| 281 | ASSERT_EQ(std::get<policy::EIDField>(values), "HHHHHHHH"); |
| 282 | ASSERT_EQ(std::get<policy::MsgField>(values), "Error HHHHHHHH"); |
| 283 | } |
| 284 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 285 | // Test not finding an entry. |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 286 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 287 | DbusPropertyMap testProperties{{"Message"s, Value{"hello world"s}}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 288 | |
| 289 | auto values = policy::find(policy, testProperties); |
| 290 | ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID()); |
| 291 | ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg()); |
| 292 | } |
| 293 | |
| 294 | // Test that strange AdditionalData values don't break anything |
| 295 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 296 | std::vector<std::string> ad{"FOO"s, "INPUT_NAME="s}; |
| 297 | DbusPropertyMap testProperties{ |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 298 | {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}}, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 299 | {"AdditionalData"s, ad}}; |
Matt Spinler | 62011e2 | 2018-03-27 15:27:39 -0500 | [diff] [blame] | 300 | |
| 301 | auto values = policy::find(policy, testProperties); |
| 302 | ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID()); |
| 303 | ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg()); |
| 304 | } |
| 305 | } |