1 #include "editor_impl.hpp"
2 #include "ipz_parser.hpp"
3
4 #include <nlohmann/json.hpp>
5
6 #include <algorithm>
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
vpdManagerEditorTest()28 vpdManagerEditorTest()
29 {
30 processJson();
31 }
32
33 void processJson();
34 void readFile(std::string pathToFile);
35 };
36
readFile(std::string pathToFile)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
processJson()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(
73 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
74 ["LocationCode"]
75 .get_ref<const nlohmann::json::string_t&>(),
76 itemEEPROM["inventoryPath"]
77 .get_ref<const nlohmann::json::string_t&>());
78 }
79 }
80 }
81
TEST_F(vpdManagerEditorTest,InvalidFile)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));
92 edit.updateKeyword(dataToUodate, 0, true);
93 }
94 catch (const std::exception& e)
95 {
96 EXPECT_EQ(std::string(e.what()), std::string("Invalid File"));
97 }
98 }
99
TEST_F(vpdManagerEditorTest,InvalidHeader)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));
110 edit.updateKeyword(dataToUodate, 0, true);
111 }
112 catch (const std::exception& e)
113 {
114 EXPECT_EQ(std::string(e.what()), std::string("VHDR record not found"));
115 }
116 }
117
TEST_F(vpdManagerEditorTest,InvalidRecordName)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));
129 edit.updateKeyword(dataToUodate, 0, true);
130 }
131 catch (const std::exception& e)
132 {
133 EXPECT_EQ(std::string(e.what()), std::string("Record not found"));
134 }
135 }
136
TEST_F(vpdManagerEditorTest,InvalidKWdName)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));
148 edit.updateKeyword(dataToUodate, 0, true);
149 }
150 catch (const std::runtime_error& e)
151 {
152 EXPECT_EQ(std::string(e.what()), std::string("Keyword not found"));
153 }
154 }
155
TEST_F(vpdManagerEditorTest,UpdateKwd_Success)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 {
165 // All valid data, but can't update with dummy ECC code
166 EditorImpl edit("VINI", "SN", std::move(vpd));
167 edit.updateKeyword(dataToUodate, 0, true);
168 }
169 catch (const std::runtime_error& e)
170 {
171 EXPECT_EQ(std::string(e.what()), std::string("Ecc update failed"));
172 }
173 }
174
main(int argc,char ** argv)175 int main(int argc, char** argv)
176 {
177 ::testing::InitGoogleTest(&argc, argv);
178
179 return RUN_ALL_TESTS();
180 }
181