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