blob: 4828c63ddc052a7b9e0dec3ec77abad49e005ece [file] [log] [blame]
Matt Spinlerb96fa322018-03-27 13:39:33 -05001/**
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 Spinler62011e22018-03-27 15:27:39 -050016#include "policy_find.hpp"
Matt Spinler66e07072018-09-12 10:36:14 -050017#include "policy_table.hpp"
18
19#include <experimental/filesystem>
20#include <fstream>
21
22#include <gtest/gtest.h>
Matt Spinlerb96fa322018-03-27 13:39:33 -050023
24using namespace ibm::logging;
25namespace fs = std::experimental::filesystem;
26
27static 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 Spinlerb96fa322018-03-27 13:39:33 -0500122/**
123 * Helper class to write the above json to a file and then
124 * remove it when the tests are over.
125 */
126class PolicyTableTest : public ::testing::Test
127{
Matt Spinler259e7272018-03-29 10:57:17 -0500128 protected:
129 virtual void SetUp()
130 {
131 char dir[] = {"./jsonTestXXXXXX"};
Matt Spinlerb96fa322018-03-27 13:39:33 -0500132
Matt Spinler259e7272018-03-29 10:57:17 -0500133 jsonDir = mkdtemp(dir);
134 jsonFile = jsonDir / "policy.json";
Matt Spinlerb96fa322018-03-27 13:39:33 -0500135
Matt Spinler259e7272018-03-29 10:57:17 -0500136 std::ofstream f{jsonFile};
137 f << json;
138 }
Matt Spinlerb96fa322018-03-27 13:39:33 -0500139
Matt Spinler259e7272018-03-29 10:57:17 -0500140 virtual void TearDown()
141 {
142 fs::remove_all(jsonDir);
143 }
Matt Spinlerb96fa322018-03-27 13:39:33 -0500144
Matt Spinler259e7272018-03-29 10:57:17 -0500145 fs::path jsonDir;
146 fs::path jsonFile;
Matt Spinlerb96fa322018-03-27 13:39:33 -0500147};
148
Matt Spinlerb96fa322018-03-27 13:39:33 -0500149/**
150 * Test finding entries in the policy table
151 */
152TEST_F(PolicyTableTest, TestTable)
153{
154 policy::Table policy{jsonFile};
155 ASSERT_EQ(policy.isLoaded(), true);
156
157 ////////////////////////////////////
Matt Spinler259e7272018-03-29 10:57:17 -0500158 // Basic search, no modifier
Matt Spinlerb96fa322018-03-27 13:39:33 -0500159 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 Spinler259e7272018-03-29 10:57:17 -0500171 // Not found
Matt Spinlerb96fa322018-03-27 13:39:33 -0500172 err = "foo";
173 details = policy.find(err, mod);
174 ASSERT_EQ(static_cast<bool>(details), false);
175
176 /////////////////////////////////////
Matt Spinler259e7272018-03-29 10:57:17 -0500177 // Test with a modifier
Matt Spinlerb96fa322018-03-27 13:39:33 -0500178 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 Spinler62011e22018-03-27 15:27:39 -0500189
190/**
191 * Test policy::find() that uses the data from a property
192 * map to find entries in the policy table.
193 */
194TEST_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 Spinler259e7272018-03-29 10:57:17 -0500201 // A basic search with no modifier
Matt Spinler62011e22018-03-27 15:27:39 -0500202 {
Matt Spinler259e7272018-03-29 10:57:17 -0500203 DbusPropertyMap testProperties{
204 {"Message"s, Value{"xyz.openbmc_project.Error.Test1"s}}};
Matt Spinler62011e22018-03-27 15:27:39 -0500205
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 Spinler259e7272018-03-29 10:57:17 -0500211 // Use CALLOUT_INVENTORY_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500212 {
Matt Spinler259e7272018-03-29 10:57:17 -0500213 std::vector<std::string> ad{"FOO=BAR"s, "CALLOUT_INVENTORY_PATH=mod2"s};
214 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500215 {"Message"s, Value{"xyz.openbmc_project.Error.Test3"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500216 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500217
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 Spinler259e7272018-03-29 10:57:17 -0500223 // Use an I2C DEVICE_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500224 {
Matt Spinler259e7272018-03-29 10:57:17 -0500225 std::vector<std::string> ad{"FOO=BAR"s,
226 "CALLOUT_DEVICE_PATH=/some/i2c/path"s};
227 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500228 {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500229 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500230
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 Spinler259e7272018-03-29 10:57:17 -0500236 // Use an FSI DEVICE_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500237 {
Matt Spinler259e7272018-03-29 10:57:17 -0500238 std::vector<std::string> ad{"FOO=BAR"s,
239 "CALLOUT_DEVICE_PATH=/some/fsi/path"s};
240 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500241 {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500242 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500243
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 Spinler259e7272018-03-29 10:57:17 -0500249 // Use PROCEDURE from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500250 {
Matt Spinler259e7272018-03-29 10:57:17 -0500251 std::vector<std::string> ad{"FOO=BAR"s, "PROCEDURE=109"s};
252 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500253 {"Message"s, Value{"xyz.openbmc_project.Error.Test5"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500254 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500255
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 Spinler259e7272018-03-29 10:57:17 -0500261 // Use RAIL_NAME from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500262 {
Matt Spinler259e7272018-03-29 10:57:17 -0500263 std::vector<std::string> ad{"FOO=BAR"s, "RAIL_NAME=RAIL_5"s};
264 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500265 {"Message"s, Value{"xyz.openbmc_project.Error.Test6"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500266 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500267
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 Spinler259e7272018-03-29 10:57:17 -0500273 // Use INPUT_NAME from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500274 {
Matt Spinler259e7272018-03-29 10:57:17 -0500275 std::vector<std::string> ad{"FOO=BAR"s, "INPUT_NAME=INPUT_42"s};
276 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500277 {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500278 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500279
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 Spinler259e7272018-03-29 10:57:17 -0500285 // Test not finding an entry.
Matt Spinler62011e22018-03-27 15:27:39 -0500286 {
Matt Spinler259e7272018-03-29 10:57:17 -0500287 DbusPropertyMap testProperties{{"Message"s, Value{"hello world"s}}};
Matt Spinler62011e22018-03-27 15:27:39 -0500288
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 Spinler259e7272018-03-29 10:57:17 -0500296 std::vector<std::string> ad{"FOO"s, "INPUT_NAME="s};
297 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500298 {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500299 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500300
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}