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 */ 16 #include <fstream> 17 #include <gtest/gtest.h> 18 #include <experimental/filesystem> 19 #include "policy_table.hpp" 20 21 using namespace ibm::logging; 22 namespace fs = std::experimental::filesystem; 23 24 static constexpr auto json = R"( 25 [ 26 { 27 "dtls":[ 28 { 29 "CEID":"ABCD1234", 30 "mod":"", 31 "msg":"Error ABCD1234" 32 } 33 ], 34 "err":"xyz.openbmc_project.Error.Test1" 35 }, 36 37 { 38 "dtls":[ 39 { 40 "CEID":"XYZ222", 41 "mod":"", 42 "msg":"Error XYZ222" 43 } 44 ], 45 "err":"xyz.openbmc_project.Error.Test2" 46 }, 47 48 { 49 "dtls":[ 50 { 51 "CEID":"AAAAAA", 52 "mod":"mod1", 53 "msg":"Error AAAAAA" 54 }, 55 { 56 "CEID":"BBBBBB", 57 "mod":"mod2", 58 "msg":"Error BBBBBB" 59 }, 60 { 61 "CEID":"CCCCCC", 62 "mod":"mod3", 63 "msg":"Error CCCCCC" 64 } 65 ], 66 "err":"xyz.openbmc_project.Error.Test3" 67 }, 68 69 { 70 "dtls":[ 71 { 72 "CEID":"DDDDDDDD", 73 "mod":"I2C", 74 "msg":"Error DDDDDDDD" 75 }, 76 { 77 "CEID":"EEEEEEEE", 78 "mod":"FSI", 79 "msg":"Error EEEEEEEE" 80 } 81 ], 82 "err":"xyz.openbmc_project.Error.Test4" 83 }, 84 85 { 86 "dtls":[ 87 { 88 "CEID":"FFFFFFFF", 89 "mod":"6D", 90 "msg":"Error FFFFFFFF" 91 } 92 ], 93 94 "err":"xyz.openbmc_project.Error.Test5" 95 }, 96 { 97 "dtls":[ 98 { 99 "CEID":"GGGGGGGG", 100 "mod":"RAIL_5", 101 "msg":"Error GGGGGGGG" 102 } 103 ], 104 105 "err":"xyz.openbmc_project.Error.Test6" 106 }, 107 { 108 "dtls":[ 109 { 110 "CEID":"HHHHHHHH", 111 "mod":"INPUT_42", 112 "msg":"Error HHHHHHHH" 113 } 114 ], 115 "err":"xyz.openbmc_project.Error.Test7" 116 } 117 ])"; 118 119 120 /** 121 * Helper class to write the above json to a file and then 122 * remove it when the tests are over. 123 */ 124 class PolicyTableTest : public ::testing::Test 125 { 126 protected: 127 128 virtual void SetUp() 129 { 130 char dir[] = {"./jsonTestXXXXXX"}; 131 132 jsonDir = mkdtemp(dir); 133 jsonFile = jsonDir / "policy.json"; 134 135 std::ofstream f{jsonFile}; 136 f << json; 137 } 138 139 virtual void TearDown() 140 { 141 fs::remove_all(jsonDir); 142 } 143 144 fs::path jsonDir; 145 fs::path jsonFile; 146 }; 147 148 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 //////////////////////////////////// 158 //Basic search, no modifier 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 ///////////////////////////////////// 171 //Not found 172 err = "foo"; 173 details = policy.find(err, mod); 174 ASSERT_EQ(static_cast<bool>(details), false); 175 176 ///////////////////////////////////// 177 //Test with a modifier 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 } 189