blob: a9cb0ec2fe8f37df08a4d3c140b32363d97d7984 [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 */
16#include <fstream>
17#include <gtest/gtest.h>
18#include <experimental/filesystem>
19#include "policy_table.hpp"
Matt Spinler62011e22018-03-27 15:27:39 -050020#include "policy_find.hpp"
Matt Spinlerb96fa322018-03-27 13:39:33 -050021
22using namespace ibm::logging;
23namespace fs = std::experimental::filesystem;
24
25static constexpr auto json = R"(
26[
27 {
28 "dtls":[
29 {
30 "CEID":"ABCD1234",
31 "mod":"",
32 "msg":"Error ABCD1234"
33 }
34 ],
35 "err":"xyz.openbmc_project.Error.Test1"
36 },
37
38 {
39 "dtls":[
40 {
41 "CEID":"XYZ222",
42 "mod":"",
43 "msg":"Error XYZ222"
44 }
45 ],
46 "err":"xyz.openbmc_project.Error.Test2"
47 },
48
49 {
50 "dtls":[
51 {
52 "CEID":"AAAAAA",
53 "mod":"mod1",
54 "msg":"Error AAAAAA"
55 },
56 {
57 "CEID":"BBBBBB",
58 "mod":"mod2",
59 "msg":"Error BBBBBB"
60 },
61 {
62 "CEID":"CCCCCC",
63 "mod":"mod3",
64 "msg":"Error CCCCCC"
65 }
66 ],
67 "err":"xyz.openbmc_project.Error.Test3"
68 },
69
70 {
71 "dtls":[
72 {
73 "CEID":"DDDDDDDD",
74 "mod":"I2C",
75 "msg":"Error DDDDDDDD"
76 },
77 {
78 "CEID":"EEEEEEEE",
79 "mod":"FSI",
80 "msg":"Error EEEEEEEE"
81 }
82 ],
83 "err":"xyz.openbmc_project.Error.Test4"
84 },
85
86 {
87 "dtls":[
88 {
89 "CEID":"FFFFFFFF",
90 "mod":"6D",
91 "msg":"Error FFFFFFFF"
92 }
93 ],
94
95 "err":"xyz.openbmc_project.Error.Test5"
96 },
97 {
98 "dtls":[
99 {
100 "CEID":"GGGGGGGG",
101 "mod":"RAIL_5",
102 "msg":"Error GGGGGGGG"
103 }
104 ],
105
106 "err":"xyz.openbmc_project.Error.Test6"
107 },
108 {
109 "dtls":[
110 {
111 "CEID":"HHHHHHHH",
112 "mod":"INPUT_42",
113 "msg":"Error HHHHHHHH"
114 }
115 ],
116 "err":"xyz.openbmc_project.Error.Test7"
117 }
118])";
119
Matt Spinlerb96fa322018-03-27 13:39:33 -0500120/**
121 * Helper class to write the above json to a file and then
122 * remove it when the tests are over.
123 */
124class PolicyTableTest : public ::testing::Test
125{
Matt Spinler259e7272018-03-29 10:57:17 -0500126 protected:
127 virtual void SetUp()
128 {
129 char dir[] = {"./jsonTestXXXXXX"};
Matt Spinlerb96fa322018-03-27 13:39:33 -0500130
Matt Spinler259e7272018-03-29 10:57:17 -0500131 jsonDir = mkdtemp(dir);
132 jsonFile = jsonDir / "policy.json";
Matt Spinlerb96fa322018-03-27 13:39:33 -0500133
Matt Spinler259e7272018-03-29 10:57:17 -0500134 std::ofstream f{jsonFile};
135 f << json;
136 }
Matt Spinlerb96fa322018-03-27 13:39:33 -0500137
Matt Spinler259e7272018-03-29 10:57:17 -0500138 virtual void TearDown()
139 {
140 fs::remove_all(jsonDir);
141 }
Matt Spinlerb96fa322018-03-27 13:39:33 -0500142
Matt Spinler259e7272018-03-29 10:57:17 -0500143 fs::path jsonDir;
144 fs::path jsonFile;
Matt Spinlerb96fa322018-03-27 13:39:33 -0500145};
146
Matt Spinlerb96fa322018-03-27 13:39:33 -0500147/**
148 * Test finding entries in the policy table
149 */
150TEST_F(PolicyTableTest, TestTable)
151{
152 policy::Table policy{jsonFile};
153 ASSERT_EQ(policy.isLoaded(), true);
154
155 ////////////////////////////////////
Matt Spinler259e7272018-03-29 10:57:17 -0500156 // Basic search, no modifier
Matt Spinlerb96fa322018-03-27 13:39:33 -0500157 std::string err{"xyz.openbmc_project.Error.Test2"};
158 std::string mod;
159
160 auto details = policy.find(err, mod);
161 ASSERT_EQ(static_cast<bool>(details), true);
162 if (details)
163 {
164 ASSERT_EQ((*details).get().ceid, "XYZ222");
165 ASSERT_EQ((*details).get().msg, "Error XYZ222");
166 }
167
168 /////////////////////////////////////
Matt Spinler259e7272018-03-29 10:57:17 -0500169 // Not found
Matt Spinlerb96fa322018-03-27 13:39:33 -0500170 err = "foo";
171 details = policy.find(err, mod);
172 ASSERT_EQ(static_cast<bool>(details), false);
173
174 /////////////////////////////////////
Matt Spinler259e7272018-03-29 10:57:17 -0500175 // Test with a modifier
Matt Spinlerb96fa322018-03-27 13:39:33 -0500176 err = "xyz.openbmc_project.Error.Test3";
177 mod = "mod3";
178
179 details = policy.find(err, mod);
180 ASSERT_EQ(static_cast<bool>(details), true);
181 if (details)
182 {
183 ASSERT_EQ((*details).get().ceid, "CCCCCC");
184 ASSERT_EQ((*details).get().msg, "Error CCCCCC");
185 }
186}
Matt Spinler62011e22018-03-27 15:27:39 -0500187
188/**
189 * Test policy::find() that uses the data from a property
190 * map to find entries in the policy table.
191 */
192TEST_F(PolicyTableTest, TestFinder)
193{
194 using namespace std::literals::string_literals;
195
196 policy::Table policy{jsonFile};
197 ASSERT_EQ(policy.isLoaded(), true);
198
Matt Spinler259e7272018-03-29 10:57:17 -0500199 // A basic search with no modifier
Matt Spinler62011e22018-03-27 15:27:39 -0500200 {
Matt Spinler259e7272018-03-29 10:57:17 -0500201 DbusPropertyMap testProperties{
202 {"Message"s, Value{"xyz.openbmc_project.Error.Test1"s}}};
Matt Spinler62011e22018-03-27 15:27:39 -0500203
204 auto values = policy::find(policy, testProperties);
205 ASSERT_EQ(std::get<policy::EIDField>(values), "ABCD1234");
206 ASSERT_EQ(std::get<policy::MsgField>(values), "Error ABCD1234");
207 }
208
Matt Spinler259e7272018-03-29 10:57:17 -0500209 // Use CALLOUT_INVENTORY_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500210 {
Matt Spinler259e7272018-03-29 10:57:17 -0500211 std::vector<std::string> ad{"FOO=BAR"s, "CALLOUT_INVENTORY_PATH=mod2"s};
212 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500213 {"Message"s, Value{"xyz.openbmc_project.Error.Test3"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500214 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500215
216 auto values = policy::find(policy, testProperties);
217 ASSERT_EQ(std::get<policy::EIDField>(values), "BBBBBB");
218 ASSERT_EQ(std::get<policy::MsgField>(values), "Error BBBBBB");
219 }
220
Matt Spinler259e7272018-03-29 10:57:17 -0500221 // Use an I2C DEVICE_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500222 {
Matt Spinler259e7272018-03-29 10:57:17 -0500223 std::vector<std::string> ad{"FOO=BAR"s,
224 "CALLOUT_DEVICE_PATH=/some/i2c/path"s};
225 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500226 {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500227 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500228
229 auto values = policy::find(policy, testProperties);
230 ASSERT_EQ(std::get<policy::EIDField>(values), "DDDDDDDD");
231 ASSERT_EQ(std::get<policy::MsgField>(values), "Error DDDDDDDD");
232 }
233
Matt Spinler259e7272018-03-29 10:57:17 -0500234 // Use an FSI DEVICE_PATH from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500235 {
Matt Spinler259e7272018-03-29 10:57:17 -0500236 std::vector<std::string> ad{"FOO=BAR"s,
237 "CALLOUT_DEVICE_PATH=/some/fsi/path"s};
238 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500239 {"Message"s, Value{"xyz.openbmc_project.Error.Test4"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500240 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500241
242 auto values = policy::find(policy, testProperties);
243 ASSERT_EQ(std::get<policy::EIDField>(values), "EEEEEEEE");
244 ASSERT_EQ(std::get<policy::MsgField>(values), "Error EEEEEEEE");
245 }
246
Matt Spinler259e7272018-03-29 10:57:17 -0500247 // Use PROCEDURE from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500248 {
Matt Spinler259e7272018-03-29 10:57:17 -0500249 std::vector<std::string> ad{"FOO=BAR"s, "PROCEDURE=109"s};
250 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500251 {"Message"s, Value{"xyz.openbmc_project.Error.Test5"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500252 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500253
254 auto values = policy::find(policy, testProperties);
255 ASSERT_EQ(std::get<policy::EIDField>(values), "FFFFFFFF");
256 ASSERT_EQ(std::get<policy::MsgField>(values), "Error FFFFFFFF");
257 }
258
Matt Spinler259e7272018-03-29 10:57:17 -0500259 // Use RAIL_NAME from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500260 {
Matt Spinler259e7272018-03-29 10:57:17 -0500261 std::vector<std::string> ad{"FOO=BAR"s, "RAIL_NAME=RAIL_5"s};
262 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500263 {"Message"s, Value{"xyz.openbmc_project.Error.Test6"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500264 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500265
266 auto values = policy::find(policy, testProperties);
267 ASSERT_EQ(std::get<policy::EIDField>(values), "GGGGGGGG");
268 ASSERT_EQ(std::get<policy::MsgField>(values), "Error GGGGGGGG");
269 }
270
Matt Spinler259e7272018-03-29 10:57:17 -0500271 // Use INPUT_NAME from the AdditionalData property
Matt Spinler62011e22018-03-27 15:27:39 -0500272 {
Matt Spinler259e7272018-03-29 10:57:17 -0500273 std::vector<std::string> ad{"FOO=BAR"s, "INPUT_NAME=INPUT_42"s};
274 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500275 {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500276 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500277
278 auto values = policy::find(policy, testProperties);
279 ASSERT_EQ(std::get<policy::EIDField>(values), "HHHHHHHH");
280 ASSERT_EQ(std::get<policy::MsgField>(values), "Error HHHHHHHH");
281 }
282
Matt Spinler259e7272018-03-29 10:57:17 -0500283 // Test not finding an entry.
Matt Spinler62011e22018-03-27 15:27:39 -0500284 {
Matt Spinler259e7272018-03-29 10:57:17 -0500285 DbusPropertyMap testProperties{{"Message"s, Value{"hello world"s}}};
Matt Spinler62011e22018-03-27 15:27:39 -0500286
287 auto values = policy::find(policy, testProperties);
288 ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID());
289 ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg());
290 }
291
292 // Test that strange AdditionalData values don't break anything
293 {
Matt Spinler259e7272018-03-29 10:57:17 -0500294 std::vector<std::string> ad{"FOO"s, "INPUT_NAME="s};
295 DbusPropertyMap testProperties{
Matt Spinler62011e22018-03-27 15:27:39 -0500296 {"Message"s, Value{"xyz.openbmc_project.Error.Test7"s}},
Matt Spinler259e7272018-03-29 10:57:17 -0500297 {"AdditionalData"s, ad}};
Matt Spinler62011e22018-03-27 15:27:39 -0500298
299 auto values = policy::find(policy, testProperties);
300 ASSERT_EQ(std::get<policy::EIDField>(values), policy.defaultEID());
301 ASSERT_EQ(std::get<policy::MsgField>(values), policy.defaultMsg());
302 }
303}