SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 1 | #include "editor_impl.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 2 | #include "ipz_parser.hpp" |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 3 | |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 4 | #include <nlohmann/json.hpp> |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 5 | |
| 6 | #include <algorithm> |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using namespace openpower::vpd; |
| 12 | using namespace openpower::vpd::manager::editor; |
| 13 | using namespace openpower::vpd::inventory; |
| 14 | using namespace openpower::vpd::constants; |
| 15 | |
| 16 | class vpdManagerEditorTest : public ::testing::Test |
| 17 | { |
| 18 | protected: |
| 19 | Binary vpd; |
| 20 | |
| 21 | nlohmann::json jsonFile; |
| 22 | |
| 23 | // map to hold the mapping of location code and inventory path |
| 24 | inventory::LocationCodeMap fruLocationCode; |
| 25 | |
| 26 | public: |
| 27 | // constructor |
| 28 | vpdManagerEditorTest() |
| 29 | { |
| 30 | processJson(); |
| 31 | } |
| 32 | |
| 33 | void processJson(); |
| 34 | void readFile(std::string pathToFile); |
| 35 | }; |
| 36 | |
| 37 | void vpdManagerEditorTest::readFile(std::string pathToFile) |
| 38 | { |
| 39 | // read the json file and parse it |
| 40 | std::ifstream vpdFile(pathToFile, std::ios::binary); |
| 41 | |
| 42 | if (!vpdFile) |
| 43 | { |
| 44 | throw std::runtime_error("json file not found"); |
| 45 | } |
| 46 | |
| 47 | vpd.assign((std::istreambuf_iterator<char>(vpdFile)), |
| 48 | std::istreambuf_iterator<char>()); |
| 49 | } |
| 50 | |
| 51 | void vpdManagerEditorTest::processJson() |
| 52 | { |
| 53 | // read the json file and parse it |
| 54 | std::ifstream json("vpd-manager-test/vpd_editor_test.json", |
| 55 | std::ios::binary); |
| 56 | |
| 57 | if (!json) |
| 58 | { |
| 59 | throw std::runtime_error("json file not found"); |
| 60 | } |
| 61 | |
| 62 | jsonFile = nlohmann::json::parse(json); |
| 63 | |
| 64 | const nlohmann::json& groupFRUS = |
| 65 | jsonFile["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 66 | for (const auto& itemFRUS : groupFRUS.items()) |
| 67 | { |
| 68 | const std::vector<nlohmann::json>& groupEEPROM = |
| 69 | itemFRUS.value().get_ref<const nlohmann::json::array_t&>(); |
| 70 | for (const auto& itemEEPROM : groupEEPROM) |
| 71 | { |
| 72 | fruLocationCode.emplace( |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 73 | itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF] |
| 74 | ["LocationCode"] |
| 75 | .get_ref<const nlohmann::json::string_t&>(), |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 76 | itemEEPROM["inventoryPath"] |
| 77 | .get_ref<const nlohmann::json::string_t&>()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | TEST_F(vpdManagerEditorTest, InvalidFile) |
| 83 | { |
| 84 | Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y', |
| 85 | 'D', 'A', 'T', 'A', 'O', 'K'}; |
| 86 | |
| 87 | Binary emptyVpdFile; |
| 88 | try |
| 89 | { |
| 90 | // Invalid kwd name |
| 91 | EditorImpl edit("VINI", "SN", std::move(emptyVpdFile)); |
Santosh Puranik | a0b2391 | 2022-02-10 13:37:09 +0530 | [diff] [blame] | 92 | edit.updateKeyword(dataToUodate, 0, true); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 93 | } |
| 94 | catch (const std::exception& e) |
| 95 | { |
| 96 | EXPECT_EQ(std::string(e.what()), std::string("Invalid File")); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | TEST_F(vpdManagerEditorTest, InvalidHeader) |
| 101 | { |
| 102 | Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y', |
| 103 | 'D', 'A', 'T', 'A', 'O', 'K'}; |
| 104 | |
| 105 | readFile("vpd-manager-test/invalidHeaderFile.dat"); |
| 106 | try |
| 107 | { |
| 108 | // the path is dummy |
| 109 | EditorImpl edit("VINI", "SN", std::move(vpd)); |
Santosh Puranik | a0b2391 | 2022-02-10 13:37:09 +0530 | [diff] [blame] | 110 | edit.updateKeyword(dataToUodate, 0, true); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 111 | } |
| 112 | catch (const std::exception& e) |
| 113 | { |
| 114 | EXPECT_EQ(std::string(e.what()), std::string("VHDR record not found")); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | TEST_F(vpdManagerEditorTest, InvalidRecordName) |
| 119 | { |
| 120 | Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y', |
| 121 | 'D', 'A', 'T', 'A', 'O', 'K'}; |
| 122 | |
| 123 | readFile("vpd-manager-test/vpdFile.dat"); |
| 124 | |
| 125 | try |
| 126 | { |
| 127 | // Invalid record name "VIN", path is dummy |
| 128 | EditorImpl edit("VIN", "SN", std::move(vpd)); |
Santosh Puranik | a0b2391 | 2022-02-10 13:37:09 +0530 | [diff] [blame] | 129 | edit.updateKeyword(dataToUodate, 0, true); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 130 | } |
| 131 | catch (const std::exception& e) |
| 132 | { |
| 133 | EXPECT_EQ(std::string(e.what()), std::string("Record not found")); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | TEST_F(vpdManagerEditorTest, InvalidKWdName) |
| 138 | { |
| 139 | Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y', |
| 140 | 'D', 'A', 'T', 'A', 'O', 'K'}; |
| 141 | |
| 142 | readFile("vpd-manager-test/vpdFile.dat"); |
| 143 | |
| 144 | try |
| 145 | { |
| 146 | // All valid data |
| 147 | EditorImpl edit("VINI", "Sn", std::move(vpd)); |
Santosh Puranik | a0b2391 | 2022-02-10 13:37:09 +0530 | [diff] [blame] | 148 | edit.updateKeyword(dataToUodate, 0, true); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 149 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 150 | catch (const std::runtime_error& e) |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 151 | { |
| 152 | EXPECT_EQ(std::string(e.what()), std::string("Keyword not found")); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | TEST_F(vpdManagerEditorTest, UpdateKwd_Success) |
| 157 | { |
| 158 | Binary dataToUodate{'M', 'O', 'D', 'I', 'F', 'Y', |
| 159 | 'D', 'A', 'T', 'A', 'O', 'K'}; |
| 160 | |
| 161 | readFile("vpd-manager-test/vpdFile.dat"); |
| 162 | |
| 163 | try |
| 164 | { |
Alpana Kumari | 0e34d35 | 2021-03-21 11:38:03 -0500 | [diff] [blame] | 165 | // All valid data, but can't update with dummy ECC code |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 166 | EditorImpl edit("VINI", "SN", std::move(vpd)); |
Santosh Puranik | a0b2391 | 2022-02-10 13:37:09 +0530 | [diff] [blame] | 167 | edit.updateKeyword(dataToUodate, 0, true); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 168 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 169 | catch (const std::runtime_error& e) |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 170 | { |
Alpana Kumari | 0e34d35 | 2021-03-21 11:38:03 -0500 | [diff] [blame] | 171 | EXPECT_EQ(std::string(e.what()), std::string("Ecc update failed")); |
SunnySrivastava1984 | a0d460e | 2020-06-03 07:49:26 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | int main(int argc, char** argv) |
| 176 | { |
| 177 | ::testing::InitGoogleTest(&argc, argv); |
| 178 | |
| 179 | return RUN_ALL_TESTS(); |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 180 | } |